Repository navigation
Expand file tree
/
Copy pathRightSidebarPanelView.swift
More file actions
558 lines (519 loc) · 21.3 KB
/
Copy pathRightSidebarPanelView.swift
File metadata and controls
558 lines (519 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import AppKit
import Bonsplit
import CMUXWorkstream
import Observation
import SwiftUI
private func rightSidebarDebugResponder(_ responder: NSResponder?) -> String {
guard let responder else { return "nil" }
return String(describing: type(of: responder))
}
/// Mode shown in the right sidebar (the panel toggled by ⌘⌥B).
nonisolated enum RightSidebarMode: String, CaseIterable, Sendable {
case files
case find
case sessions
case feed
case dock
var label: String {
switch self {
case .files: return String(localized: "rightSidebar.mode.files", defaultValue: "Files")
case .find: return String(localized: "rightSidebar.mode.find", defaultValue: "Find")
case .sessions: return String(localized: "rightSidebar.mode.sessions", defaultValue: "Vault")
case .feed: return String(localized: "rightSidebar.mode.feed", defaultValue: "Feed")
case .dock: return String(localized: "rightSidebar.mode.dock", defaultValue: "Dock")
}
}
var symbolName: String {
switch self {
case .files: return "folder"
case .find: return "magnifyingglass"
case .sessions: return "books.vertical"
case .feed: return "dot.radiowaves.left.and.right"
case .dock: return "dock.rectangle"
}
}
var shortcutAction: KeyboardShortcutSettings.Action {
switch self {
case .files: return .switchRightSidebarToFiles
case .find: return .switchRightSidebarToFind
case .sessions: return .switchRightSidebarToSessions
case .feed: return .switchRightSidebarToFeed
case .dock: return .switchRightSidebarToDock
}
}
}
extension RightSidebarMode {
static func modeShortcut(for event: NSEvent) -> RightSidebarMode? {
guard event.type == .keyDown else { return nil }
if KeyboardShortcutSettings.shortcut(for: .switchRightSidebarToFiles).matches(event: event) {
return .files
}
if KeyboardShortcutSettings.shortcut(for: .switchRightSidebarToFind).matches(event: event) {
return .find
}
if KeyboardShortcutSettings.shortcut(for: .switchRightSidebarToSessions).matches(event: event) {
return .sessions
}
if KeyboardShortcutSettings.shortcut(for: .switchRightSidebarToFeed).matches(event: event),
RightSidebarMode.feed.isAvailable() {
return .feed
}
if KeyboardShortcutSettings.shortcut(for: .switchRightSidebarToDock).matches(event: event),
RightSidebarMode.dock.isAvailable() {
return .dock
}
return nil
}
}
enum RightSidebarKeyboardNavigation {
enum DisclosureAction {
case collapse
case expand
}
static func moveDelta(for event: NSEvent) -> Int? {
guard event.type == .keyDown else { return nil }
let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
let hasCommandOrOption = !flags.intersection([.command, .option]).isEmpty
if flags.contains(.control), !hasCommandOrOption {
switch event.keyCode {
case 45: return 1 // Ctrl+N
case 35: return -1 // Ctrl+P
default: break
}
}
guard flags.intersection([.command, .control, .option]).isEmpty else {
return nil
}
switch event.keyCode {
case 38, 125: return 1 // J or Down
case 40, 126: return -1 // K or Up
default: return nil
}
}
static func disclosureAction(for event: NSEvent) -> DisclosureAction? {
guard event.type == .keyDown else { return nil }
let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
guard flags.intersection([.command, .control, .option]).isEmpty else {
return nil
}
switch event.keyCode {
case 4: return .collapse // H
case 37: return .expand // L
case 123: return .collapse // Left
case 124: return .expand // Right
default: return nil
}
}
static func isPlainSlash(_ event: NSEvent) -> Bool {
guard event.type == .keyDown else { return false }
let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
guard flags.intersection([.command, .control, .option]).isEmpty else {
return false
}
return event.keyCode == 44
}
static func isPlainPrintableText(_ event: NSEvent) -> Bool {
guard event.type == .keyDown else { return false }
let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
guard flags.intersection([.command, .control, .option]).isEmpty else {
return false
}
guard let text = event.charactersIgnoringModifiers, !text.isEmpty else {
return false
}
return text.unicodeScalars.allSatisfy {
!CharacterSet.controlCharacters.contains($0)
}
}
}
/// Right sidebar root view. Hosts a segmented mode picker plus the active panel.
struct RightSidebarPanelView: View {
@ObservedObject var fileExplorerStore: FileExplorerStore
@ObservedObject var fileExplorerState: FileExplorerState
@ObservedObject var sessionIndexStore: SessionIndexStore
let titlebarHeight: CGFloat
let workspaceId: UUID?
let onResumeSession: ((SessionEntry) -> Void)?
let onOpenFilePreview: (String) -> Void
let onClose: () -> Void
@State private var modeShortcutHintMonitor = WindowScopedShortcutHintModifierMonitor(activation: .commandOrControl) { window in
guard let responder = window.firstResponder else { return false }
return AppDelegate.shared?.isRightSidebarFocusResponder(responder, in: window) == true
}
@State private var focusShortcutHintMonitor = WindowScopedShortcutHintModifierMonitor(activation: .commandOnly)
@State private var closeShortcutHintMonitor = WindowScopedShortcutHintModifierMonitor(activation: .commandOnly)
@StateObject private var dockStore = DockControlsStore()
@ObservedObject private var keyboardShortcutSettingsObserver = KeyboardShortcutSettingsObserver.shared
private let alwaysShowShortcutHints = ShortcutHintDebugSettings.alwaysShowHints()
private let closeShortcutHintXOffset = ShortcutHintDebugSettings.defaultRightSidebarCloseHintX
private let closeShortcutHintYOffset = ShortcutHintDebugSettings.defaultRightSidebarCloseHintY
private let focusShortcutHintXOffset = ShortcutHintDebugSettings.defaultRightSidebarFocusHintX
private let focusShortcutHintYOffset = ShortcutHintDebugSettings.defaultRightSidebarFocusHintY
@AppStorage(RightSidebarBetaFeatureSettings.dockEnabledKey)
private var dockEnabled = RightSidebarBetaFeatureSettings.defaultDockEnabled
// Re-reading the observable store inside modeBar causes SwiftUI to
// track the pending count so the badge updates live when hooks push
// new items.
private var feedPendingCount: Int {
FeedCoordinator.shared.store?.pending.count ?? 0
}
private var availableModes: [RightSidebarMode] {
RightSidebarMode.availableModes(dockEnabled: dockEnabled)
}
var body: some View {
VStack(spacing: 0) {
modeBar
.rightSidebarChromeBottomBorder()
contentForMode
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.shortcutHintVisibilityAnimation(value: focusShortcutHintMonitor.isModifierPressed)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
RightSidebarKeyboardFocusBridge()
.frame(width: 1, height: 1)
)
.background(
WindowAccessor { window in
modeShortcutHintMonitor.setHostWindow(window)
focusShortcutHintMonitor.setHostWindow(window)
closeShortcutHintMonitor.setHostWindow(window)
}
.frame(width: 0, height: 0)
)
.accessibilityIdentifier("RightSidebar")
.onAppear {
modeShortcutHintMonitor.start()
focusShortcutHintMonitor.start()
closeShortcutHintMonitor.start()
fileExplorerState.refreshModeAvailability()
}
.onDisappear {
modeShortcutHintMonitor.stop()
focusShortcutHintMonitor.stop()
closeShortcutHintMonitor.stop()
}
.onChange(of: fileExplorerState.mode) { _, mode in
if mode != .dock { dockStore.deactivate() }
}
.onChange(of: fileExplorerState.isVisible) { _, visible in if !visible { dockStore.deactivate() } }
.onChange(of: dockEnabled) { _, _ in refreshModeAvailabilityAndFocusIfNeeded() }
}
private var modeBar: some View {
let _ = keyboardShortcutSettingsObserver.revision
let showsModeShortcutHints = alwaysShowShortcutHints || modeShortcutHintMonitor.isModifierPressed
return ZStack {
WindowDragHandleView()
HStack(spacing: 4) {
ForEach(availableModes, id: \.rawValue) { mode in
ModeBarButton(
mode: mode,
isSelected: fileExplorerState.mode == mode,
badgeCount: mode == .feed ? feedPendingCount : 0,
shortcutHint: KeyboardShortcutSettings.shortcut(for: mode.shortcutAction),
showsShortcutHint: showsModeShortcutHints
) {
if AppDelegate.shared?.focusRightSidebarInActiveMainWindow(
mode: mode,
focusFirstItem: true,
preferredWindow: NSApp.keyWindow ?? NSApp.mainWindow
) != true {
selectMode(mode)
}
}
}
Spacer(minLength: 0)
closeButton
}
}
.rightSidebarChromeBar(leadingPadding: 4, trailingPadding: 6, height: titlebarHeight)
.overlay(alignment: .topLeading) {
focusShortcutHintOverlay
}
.background(TitlebarDoubleClickMonitorView())
.background(MinimalModeTitlebarControlHitRegionView())
.accessibilityElement(children: .contain)
.accessibilityIdentifier("RightSidebarModeBar")
.reportRightSidebarChromeGeometryForBonsplitUITest(
isVisible: true,
titlebarHeight: titlebarHeight
)
}
private var closeButton: some View {
let _ = keyboardShortcutSettingsObserver.revision
let shortcut = KeyboardShortcutSettings.shortcut(for: .toggleRightSidebar)
let showsShortcutHint = titlebarShortcutHintShouldShow(
shortcut: shortcut,
alwaysShowShortcutHints: alwaysShowShortcutHints,
modifierPressed: closeShortcutHintMonitor.isModifierPressed
)
return ZStack {
Button(action: onClose) {
Image(systemName: "xmark")
.font(.system(size: 11, weight: .semibold))
.frame(width: RightSidebarChromeMetrics.controlHeight, height: RightSidebarChromeMetrics.controlHeight)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.foregroundColor(.secondary)
.safeHelp(
KeyboardShortcutSettings.Action.toggleRightSidebar.tooltip(
String(localized: "rightSidebar.toggle.tooltip", defaultValue: "Toggle right sidebar")
)
)
.accessibilityLabel(String(localized: "rightSidebar.close.accessibilityLabel", defaultValue: "Close Right Sidebar"))
.accessibilityIdentifier("RightSidebar.closeButton")
}
.frame(width: RightSidebarChromeMetrics.controlHeight, height: RightSidebarChromeMetrics.controlHeight)
.overlay(alignment: .top) {
if showsShortcutHint {
ShortcutHintPill(shortcut: shortcut, fontSize: 9, emphasis: 1.05)
.fixedSize(horizontal: true, vertical: false)
.offset(
x: CGFloat(ShortcutHintDebugSettings.clamped(closeShortcutHintXOffset)),
y: CGFloat(ShortcutHintDebugSettings.clamped(closeShortcutHintYOffset))
)
.shortcutHintTransition()
.accessibilityIdentifier("rightSidebarCloseShortcutHint")
.allowsHitTesting(false)
.zIndex(10)
}
}
.shortcutHintVisibilityAnimation(value: showsShortcutHint)
}
@ViewBuilder
private var focusShortcutHintOverlay: some View {
let _ = keyboardShortcutSettingsObserver.revision
let shortcut = KeyboardShortcutSettings.shortcut(for: .focusRightSidebar)
let showsFocusShortcutHint = titlebarShortcutHintShouldShow(
shortcut: shortcut,
alwaysShowShortcutHints: alwaysShowShortcutHints,
modifierPressed: focusShortcutHintMonitor.isModifierPressed
)
if showsFocusShortcutHint {
ShortcutHintPill(
shortcut: shortcut,
fontSize: 9,
emphasis: 1.05
)
.padding(.leading, 6)
.padding(.top, 5)
.offset(
x: CGFloat(ShortcutHintDebugSettings.clamped(focusShortcutHintXOffset)),
y: CGFloat(ShortcutHintDebugSettings.clamped(focusShortcutHintYOffset))
)
.shortcutHintTransition()
.accessibilityIdentifier("rightSidebarFocusShortcutHint")
.allowsHitTesting(false)
.zIndex(10)
}
}
@ViewBuilder
private var contentForMode: some View {
switch fileExplorerState.mode {
case .files:
FileExplorerPanelView(
store: fileExplorerStore,
state: fileExplorerState,
onOpenFilePreview: onOpenFilePreview,
presentation: .files
)
case .find:
FileExplorerPanelView(
store: fileExplorerStore,
state: fileExplorerState,
onOpenFilePreview: onOpenFilePreview,
presentation: .find
)
case .sessions:
SessionIndexView(store: sessionIndexStore, onResume: onResumeSession)
.onAppear {
sessionIndexStore.setCurrentDirectoryIfChanged(sessionIndexDirectory)
}
case .feed:
FeedPanelView()
case .dock:
DockPanelView(rootDirectory: sessionIndexDirectory, workspaceId: workspaceId, store: dockStore)
}
}
private var sessionIndexDirectory: String? {
fileExplorerStore.rootPath.isEmpty ? nil : fileExplorerStore.rootPath
}
private func selectMode(_ mode: RightSidebarMode) {
fileExplorerState.mode = mode
if fileExplorerState.mode == .sessions {
sessionIndexStore.setCurrentDirectoryIfChanged(sessionIndexDirectory)
if sessionIndexStore.entries.isEmpty {
sessionIndexStore.reload()
}
}
}
private func refreshModeAvailabilityAndFocusIfNeeded() {
let previousMode = fileExplorerState.mode
fileExplorerState.refreshModeAvailability()
guard previousMode != fileExplorerState.mode,
fileExplorerState.isVisible,
let window = NSApp.keyWindow ?? NSApp.mainWindow
else { return }
_ = AppDelegate.shared?.focusRightSidebarInActiveMainWindow(
mode: fileExplorerState.mode,
focusFirstItem: false,
preferredWindow: window
)
}
}
private struct RightSidebarKeyboardFocusBridge: NSViewRepresentable {
func makeNSView(context: Context) -> RightSidebarKeyboardFocusView {
let view = RightSidebarKeyboardFocusView(frame: NSRect(x: 0, y: 0, width: 1, height: 1))
return view
}
func updateNSView(_ nsView: RightSidebarKeyboardFocusView, context: Context) {
nsView.registerWithKeyboardFocusCoordinatorIfNeeded()
}
}
final class RightSidebarKeyboardFocusView: NSView {
override var acceptsFirstResponder: Bool { true }
override var canBecomeKeyView: Bool { true }
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
guard let window else { return }
AppDelegate.shared?.keyboardFocusCoordinator(for: window)?.registerRightSidebarHost(self)
#if DEBUG
dlog(
"rs.focus.host.attach win=\(window.windowNumber) canAccept=\(cmuxCanAcceptRightSidebarKeyboardFocus ? 1 : 0) " +
"fr=\(rightSidebarDebugResponder(window.firstResponder))"
)
#endif
}
func registerWithKeyboardFocusCoordinatorIfNeeded() {
guard let window else { return }
AppDelegate.shared?.keyboardFocusCoordinator(for: window)?.registerRightSidebarHost(self)
}
override func layout() {
super.layout()
registerWithKeyboardFocusCoordinatorIfNeeded()
}
override func keyDown(with event: NSEvent) {
if let mode = RightSidebarMode.modeShortcut(for: event) {
_ = AppDelegate.shared?.focusRightSidebarInActiveMainWindow(
mode: mode,
focusFirstItem: true,
preferredWindow: window
)
return
}
if event.keyCode == 53 {
if let window,
AppDelegate.shared?.keyboardFocusCoordinator(for: window)?.focusTerminal() == true {
return
}
window?.makeFirstResponder(nil)
return
}
if let characters = event.charactersIgnoringModifiers, !characters.isEmpty {
return
}
super.keyDown(with: event)
}
func focusHostFromCoordinator() -> Bool {
guard let window else {
#if DEBUG
dlog("rs.focus.host.focus result=0 reason=noWindow")
#endif
return false
}
let result = window.makeFirstResponder(self)
#if DEBUG
dlog(
"rs.focus.host.focus result=\(result ? 1 : 0) win=\(window.windowNumber) " +
"fr=\(rightSidebarDebugResponder(window.firstResponder))"
)
#endif
return result
}
}
extension NSView {
var cmuxCanAcceptRightSidebarKeyboardFocus: Bool {
guard window != nil, !isHiddenOrHasHiddenAncestor else { return false }
var view: NSView? = self
while let current = view {
if current.bounds.width <= 0.5 || current.bounds.height <= 0.5 {
return false
}
view = current.superview
}
return true
}
}
private struct ModeBarButton: View {
let mode: RightSidebarMode
let isSelected: Bool
var badgeCount: Int = 0
let shortcutHint: StoredShortcut
let showsShortcutHint: Bool
let action: () -> Void
@State private var isHovered: Bool = false
var body: some View {
Button(action: action) {
HStack(spacing: 4) {
Image(systemName: mode.symbolName)
.font(.system(size: 11, weight: .medium))
Text(mode.label)
.font(.system(size: 11, weight: .medium))
.lineLimit(1)
.truncationMode(.tail)
if badgeCount > 0 {
pendingChip
}
}
.rightSidebarChromePill(
isSelected: isSelected,
isHovered: isHovered,
geometryKeyPrefix: "rightSidebarModeControl_\(mode.rawValue)"
)
.overlay(alignment: .trailing) {
if showsShortcutHint {
ShortcutHintPill(shortcut: shortcutHint, fontSize: 9, emphasis: isSelected ? 1.15 : 0.95)
.offset(x: 5)
.shortcutHintTransition()
.accessibilityIdentifier("rightSidebarModeShortcutHint.\(mode.rawValue)")
}
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.onHover { isHovered = $0 }
.help(helpText)
.accessibilityIdentifier("RightSidebarModeButton.\(mode.rawValue)")
.shortcutHintVisibilityAnimation(value: showsShortcutHint)
}
private var helpText: String {
if badgeCount > 0 {
return String(
localized: "rightSidebar.mode.pendingHelp",
defaultValue: "\(mode.label) · \(badgeCount) pending"
)
}
return mode.label
}
/// Subtle inline count chip that sits after the label instead of
/// floating a red capsule over the icon. Tinted orange (the "needs
/// attention" color used elsewhere in the Feed) and sized to match
/// the label's typography.
private var pendingChip: some View {
let countText = badgeCount > 9 ? "9+" : String(badgeCount)
return Text(countText)
.font(.system(size: 10, weight: .bold).monospacedDigit())
.lineLimit(1)
.fixedSize(horizontal: true, vertical: true)
.foregroundColor(.orange)
.padding(.horizontal, 5)
.padding(.vertical, 1)
.background(
Capsule(style: .continuous)
.fill(Color.orange.opacity(0.20))
)
.fixedSize(horizontal: true, vertical: true)
.layoutPriority(2)
}
}