-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspaceManager+SurfaceOperations.swift
More file actions
682 lines (593 loc) · 26.2 KB
/
Copy pathWorkspaceManager+SurfaceOperations.swift
File metadata and controls
682 lines (593 loc) · 26.2 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
import AppKit
import Foundation
/// Pane, surface, and Ghostty command flows for the shared manager.
extension WorkspaceManager {
/// Handles a terminal child-process exit by closing the surface unless app shutdown is in progress.
func handleSurfaceChildExit(workspaceId: UUID, surfaceId: UUID) {
guard !isTerminating else { return }
guard let workspace = workspace(id: workspaceId),
let paneId = workspace.rootPane.paneId(containing: surfaceId) else {
return
}
closeSurface(workspaceId: workspaceId, paneId: paneId, surfaceId: surfaceId)
}
/// Adds a surface to the target pane leaf.
func addSurface(workspaceId: UUID, paneId: UUID, surface: SurfaceModel) {
let appended = surfaceManager.addSurface(
workspaceId: workspaceId,
paneId: paneId,
surface: surface,
workspaces: &workspaces,
persistence: persistence
)
if appended {
GhosttyRuntime.shared.focusSurfaceHost(surfaceId: surface.id)
}
}
/// Closes a surface from a target pane leaf.
func closeSurface(workspaceId: UUID, paneId: UUID, surfaceId: UUID) {
surfaceManager.closeSurface(
workspaceId: workspaceId,
paneId: paneId,
surfaceId: surfaceId,
workspaces: &workspaces,
persistence: persistence
)
completionNotifications.removeNotifications(for: surfaceId)
updateDockBadge()
GhosttyRuntime.shared.endSearch(surfaceId: surfaceId)
GhosttyRuntime.shared.releaseSurface(surfaceId: surfaceId)
clearBusySurface(surfaceId)
clearGitBranch(surfaceId: surfaceId)
clearProgressReport(surfaceId: surfaceId)
if let workspace = workspace(id: workspaceId),
let focusedSurfaceId = workspace.focusedSurfaceId ?? workspace.rootPane.firstActiveSurfaceId(),
let focusedSurface = surface(in: workspace.rootPane, surfaceId: focusedSurfaceId) {
refreshGitBranch(
workspaceId: workspaceId,
surfaceId: focusedSurfaceId,
workingDirectory: focusedSurface.terminalConfig.workingDirectory
)
}
}
/// Marks a surface as active in both pane and workspace focus state.
func activateSurface(workspaceId: UUID, paneId: UUID, surfaceId: UUID) {
surfaceManager.activateSurface(
workspaceId: workspaceId,
paneId: paneId,
surfaceId: surfaceId,
workspaces: &workspaces,
persistence: persistence
)
let didHandleCompletion = surfaceManager.clearPendingCompletion(
workspaceId: workspaceId,
surfaceId: surfaceId,
workspaces: &workspaces,
persistence: persistence
)
if didHandleCompletion {
markRecentlyHandled(surfaceId: surfaceId)
}
completionNotifications.removeNotifications(for: surfaceId)
updateDockBadge()
GhosttyRuntime.shared.focusSurfaceHost(surfaceId: surfaceId)
if let workspace = workspace(id: workspaceId),
let surface = surface(in: workspace.rootPane, surfaceId: surfaceId) {
refreshGitBranch(
workspaceId: workspaceId,
surfaceId: surfaceId,
workingDirectory: surface.terminalConfig.workingDirectory
)
}
}
/// Splits a leaf pane and creates a fresh terminal in the new pane.
func splitPane(
workspaceId: UUID,
paneId: UUID,
orientation: SplitOrientation,
position: SplitChildPosition = .second,
sourceSurfaceId: UUID? = nil
) {
if let surfaceId = surfaceManager.splitPane(
workspaceId: workspaceId,
paneId: paneId,
orientation: orientation,
position: position,
newSurface: configuredSplitSurface(
workspaceId: workspaceId,
paneId: paneId,
sourceSurfaceId: sourceSurfaceId
),
workspaces: &workspaces,
persistence: persistence
) {
GhosttyRuntime.shared.focusSurfaceHost(surfaceId: surfaceId)
}
}
/// Updates persisted split ratio. Use `persist: false` during drag updates.
func updateSplitRatio(workspaceId: UUID, paneId: UUID, ratio: Double, persist: Bool = true) {
surfaceManager.updateSplitRatio(
workspaceId: workspaceId,
paneId: paneId,
ratio: ratio,
persist: persist,
workspaces: &workspaces,
persistence: persistence
)
}
/// Records terminal activity for idle-state tracking.
func handleSurfaceInput(workspaceId: UUID, surfaceId: UUID, input: SurfaceInputEvent) {
surfaceManager.setIdleState(
workspaceId: workspaceId,
surfaceId: surfaceId,
isIdle: false,
workspaces: &workspaces,
persistence: persistence
)
}
/// Interval after which a progress report is automatically cleared if no update arrives.
private static let progressAutoClearInterval: TimeInterval = 15
/// Stores or removes an OSC 9;4 progress report for a surface, resetting the auto-clear timer.
func setProgressReport(workspaceId: UUID, surfaceId: UUID, report: SurfaceProgressReport?) {
guard let workspace = workspace(id: workspaceId),
self.surface(in: workspace.rootPane, surfaceId: surfaceId) != nil else { return }
if let report {
progressBySurfaceId[surfaceId] = report
progressClearTimers[surfaceId]?.invalidate()
let generation = (progressTimerGeneration[surfaceId] ?? 0) + 1
progressTimerGeneration[surfaceId] = generation
let timer = Timer(timeInterval: Self.progressAutoClearInterval, repeats: false) { [weak self] _ in
Task { @MainActor [weak self] in
guard self?.progressTimerGeneration[surfaceId] == generation else { return }
self?.progressBySurfaceId.removeValue(forKey: surfaceId)
self?.progressClearTimers.removeValue(forKey: surfaceId)
self?.progressTimerGeneration.removeValue(forKey: surfaceId)
}
}
RunLoop.main.add(timer, forMode: .common)
progressClearTimers[surfaceId] = timer
} else {
clearProgressReport(surfaceId: surfaceId)
}
}
/// Returns the progress report for the focused surface of a workspace, if any.
func focusedSurfaceProgress(workspaceId: UUID) -> SurfaceProgressReport? {
guard let workspace = workspace(id: workspaceId) else { return nil }
let surfaceId = workspace.focusedSurfaceId ?? workspace.rootPane.firstActiveSurfaceId()
guard let surfaceId else { return nil }
return progressBySurfaceId[surfaceId]
}
/// Removes any stored progress state and cancels the auto-clear timer for a surface.
func clearProgressReport(surfaceId: UUID) {
progressBySurfaceId.removeValue(forKey: surfaceId)
progressClearTimers[surfaceId]?.invalidate()
progressClearTimers.removeValue(forKey: surfaceId)
progressTimerGeneration.removeValue(forKey: surfaceId)
}
/// Updates tab title using the current terminal title.
func setSurfaceTitle(workspaceId: UUID, surfaceId: UUID, title: String) {
surfaceManager.setSurfaceTitle(
workspaceId: workspaceId,
surfaceId: surfaceId,
title: title,
workspaces: &workspaces,
persistence: persistence
)
}
/// Updates the tracked working directory for a surface and refreshes its Git branch state.
@discardableResult
func setSurfaceWorkingDirectory(workspaceId: UUID, surfaceId: UUID, workingDirectory: String) -> Task<Void, Never>? {
let normalizedWorkingDirectory = workingDirectory.trimmingCharacters(in: .whitespacesAndNewlines)
surfaceManager.setSurfaceWorkingDirectory(
workspaceId: workspaceId,
surfaceId: surfaceId,
workingDirectory: normalizedWorkingDirectory,
workspaces: &workspaces,
persistence: persistence
)
guard !normalizedWorkingDirectory.isEmpty else { return nil }
return refreshGitBranch(
workspaceId: workspaceId,
surfaceId: surfaceId,
workingDirectory: normalizedWorkingDirectory
)
}
/// Creates a surface in the currently focused pane of the selected workspace.
func createSurfaceInFocusedPane() {
guard let context = focusedPaneContext() else { return }
addSurface(
workspaceId: context.workspaceId,
paneId: context.paneId,
surface: configuredDefaultSurface()
)
}
/// Returns whether an app-owned focused-pane command is currently supported.
func canPerformFocusedPaneCommand(_ command: FocusedPaneCommand) -> Bool {
guard let context = focusedPaneContext() else { return false }
return canPerformPaneCommand(command, context: context)
}
/// Executes an app-owned focused-pane command when supported.
@discardableResult
func performFocusedPaneCommand(_ command: FocusedPaneCommand) -> Bool {
guard let context = focusedPaneContext() else { return false }
return performPaneCommand(command, context: context)
}
/// Returns whether a pane command is supported for a specific target pane.
func canPerformPaneCommand(
_ command: FocusedPaneCommand,
workspaceId: UUID,
paneId: UUID,
surfaceId: UUID? = nil
) -> Bool {
guard let context = paneCommandContext(
workspaceId: workspaceId,
paneId: paneId,
surfaceId: surfaceId
) else {
return false
}
return canPerformPaneCommand(command, context: context)
}
/// Executes a pane command for a specific target pane or tab.
@discardableResult
func performPaneCommand(
_ command: FocusedPaneCommand,
workspaceId: UUID,
paneId: UUID,
surfaceId: UUID? = nil
) -> Bool {
guard let context = paneCommandContext(
workspaceId: workspaceId,
paneId: paneId,
surfaceId: surfaceId
) else {
return false
}
return performPaneCommand(command, context: context)
}
/// Splits the currently focused pane in the selected workspace.
func splitFocusedPane(orientation: SplitOrientation) {
guard let context = focusedPaneContext() else { return }
splitPane(
workspaceId: context.workspaceId,
paneId: context.paneId,
orientation: orientation,
sourceSurfaceId: context.surfaceId
)
}
/// Closes the currently focused surface in the selected workspace.
func closeFocusedSurface() {
guard let workspaceId = window.selectedWorkspaceId else { return }
guard let workspace = self.workspace(id: workspaceId) else { return }
let surfaceId = focusedSurfaceId() ?? workspace.focusedSurfaceId ?? workspace.rootPane.firstActiveSurfaceId()
guard let surfaceId else { return }
guard let paneId = workspace.rootPane.paneId(containing: surfaceId) else { return }
closeSurface(workspaceId: workspaceId, paneId: paneId, surfaceId: surfaceId)
}
/// Moves tab focus to the next surface in the focused pane.
func focusNextSurfaceInPane() {
focusAdjacentSurfaceInPane(step: 1)
}
/// Moves tab focus to the previous surface in the focused pane.
func focusPreviousSurfaceInPane() {
focusAdjacentSurfaceInPane(step: -1)
}
/// Moves pane focus in the provided direction relative to the focused pane.
func focusAdjacentPane(direction: PaneNodeModel.PaneFocusDirection) {
if let sourceSurfaceId = currentResponderSurfaceId() {
focusAdjacentPane(from: sourceSurfaceId, direction: direction)
return
}
guard let workspaceId = window.selectedWorkspaceId else { return }
guard let workspace = self.workspace(id: workspaceId) else { return }
let sourcePaneId = focusedPaneId(in: workspace) ?? workspace.rootPane.firstLeafId()
guard let sourcePaneId else { return }
guard let targetPaneId = workspace.rootPane.adjacentPaneId(from: sourcePaneId, direction: direction) else {
return
}
guard let targetSurfaceId = workspace.rootPane.activeSurfaceId(in: targetPaneId) else {
return
}
activateSurface(workspaceId: workspaceId, paneId: targetPaneId, surfaceId: targetSurfaceId)
}
/// Moves pane focus in the provided direction relative to a specific surface.
func focusAdjacentPane(from sourceSurfaceId: UUID, direction: PaneNodeModel.PaneFocusDirection) {
guard let workspaceId = window.selectedWorkspaceId else { return }
guard let workspace = self.workspace(id: workspaceId) else { return }
let sourcePaneId = workspace.rootPane.paneId(containing: sourceSurfaceId) ?? focusedPaneId(in: workspace)
guard let sourcePaneId else { return }
guard let targetPaneId = workspace.rootPane.adjacentPaneId(from: sourcePaneId, direction: direction) else {
return
}
guard let targetSurfaceId = workspace.rootPane.activeSurfaceId(in: targetPaneId) else {
return
}
activateSurface(workspaceId: workspaceId, paneId: targetPaneId, surfaceId: targetSurfaceId)
}
/// Returns whether a workspace is currently selected.
var hasSelectedWorkspace: Bool {
window.selectedWorkspaceId != nil
}
/// Returns whether a terminal surface is available for focused binding actions.
var hasFocusedSurface: Bool {
focusedSurfaceId() != nil
}
/// Returns the current menu title for the focused close action.
var closeFocusedItemTitle: String {
guard let context = focusedPaneContext() else {
return "Close Active Pane/Tab"
}
return closeItemTitle(for: context)
}
/// Returns the current menu title for a specific pane or tab close action.
func closeItemTitle(workspaceId: UUID, paneId: UUID, surfaceId: UUID? = nil) -> String {
guard let context = paneCommandContext(
workspaceId: workspaceId,
paneId: paneId,
surfaceId: surfaceId
) else {
return "Close Active Pane/Tab"
}
return closeItemTitle(for: context)
}
/// Runs a named Ghostty binding action against the focused surface.
@discardableResult
func performFocusedSurfaceBindingAction(_ action: String) -> Bool {
guard let surfaceId = focusedSurfaceId() else { return false }
return GhosttyRuntime.shared.performBindingAction(surfaceId: surfaceId, action: action)
}
/// Returns whether the focused pane is currently zoomed.
var isFocusedPaneZoomed: Bool {
guard let workspaceId = window.selectedWorkspaceId,
let workspace = self.workspace(id: workspaceId),
let paneId = focusedPaneId(in: workspace) else {
return false
}
return workspace.zoomedPaneId == paneId
}
/// Toggles app-level zoom for the currently focused pane.
func toggleFocusedPaneZoom() {
guard let context = focusedPaneContext() else { return }
surfaceManager.togglePaneZoom(
workspaceId: context.workspaceId,
paneId: context.paneId,
workspaces: &workspaces,
persistence: persistence
)
}
/// Advances pane tab focus by a signed step with wraparound.
private func focusAdjacentSurfaceInPane(step: Int) {
guard step != 0 else { return }
guard let workspaceId = window.selectedWorkspaceId else { return }
guard let workspace = self.workspace(id: workspaceId) else { return }
guard let currentSurfaceId = workspace.focusedSurfaceId else { return }
guard let paneId = workspace.rootPane.paneId(containing: currentSurfaceId) else { return }
guard let surfaceIds = workspace.rootPane.surfaceIds(in: paneId), !surfaceIds.isEmpty else { return }
guard let currentIndex = surfaceIds.firstIndex(of: currentSurfaceId) else { return }
let rawIndex = currentIndex + step
let normalizedIndex = (rawIndex % surfaceIds.count + surfaceIds.count) % surfaceIds.count
let nextSurfaceId = surfaceIds[normalizedIndex]
activateSurface(workspaceId: workspaceId, paneId: paneId, surfaceId: nextSurfaceId)
}
/// Returns the currently focused pane identifier for a workspace.
private func focusedPaneId(in workspace: WorkspaceModel) -> UUID? {
guard let focusedSurfaceId = workspace.focusedSurfaceId else { return nil }
return workspace.rootPane.paneId(containing: focusedSurfaceId)
}
/// Returns the best available focused surface identifier for command routing.
private func focusedSurfaceId() -> UUID? {
if let surfaceId = currentResponderSurfaceId() {
return surfaceId
}
guard let workspaceId = window.selectedWorkspaceId,
let workspace = self.workspace(id: workspaceId) else {
return nil
}
return workspace.focusedSurfaceId ?? workspace.rootPane.firstActiveSurfaceId()
}
/// Returns whether a specific pane command can run in a resolved context.
private func canPerformPaneCommand(_ command: FocusedPaneCommand, context: PaneCommandContext) -> Bool {
switch command {
case .newSurface, .split, .toggleZoom:
return context.workspace.rootPane.containsPane(context.paneId)
case .closeActiveItem:
return closeSurfaceId(for: context) != nil
case .focus(let direction):
return context.workspace.rootPane.adjacentPaneId(
from: context.paneId,
direction: direction
) != nil
case .nextSurface, .previousSurface:
guard let surfaceIds = context.workspace.rootPane.surfaceIds(in: context.paneId) else {
return false
}
return surfaceIds.count > 1
}
}
/// Executes a specific pane command in a resolved context.
@discardableResult
private func performPaneCommand(_ command: FocusedPaneCommand, context: PaneCommandContext) -> Bool {
guard canPerformPaneCommand(command, context: context) else { return false }
switch command {
case .newSurface:
addSurface(
workspaceId: context.workspaceId,
paneId: context.paneId,
surface: configuredDefaultSurface()
)
case .split(let orientation):
splitPane(
workspaceId: context.workspaceId,
paneId: context.paneId,
orientation: orientation,
sourceSurfaceId: context.surfaceId
)
case .closeActiveItem:
guard let surfaceId = closeSurfaceId(for: context) else { return false }
closeSurface(workspaceId: context.workspaceId, paneId: context.paneId, surfaceId: surfaceId)
case .focus(let direction):
guard let targetPaneId = context.workspace.rootPane.adjacentPaneId(
from: context.paneId,
direction: direction
),
let targetSurfaceId = context.workspace.rootPane.activeSurfaceId(in: targetPaneId) else {
return false
}
activateSurface(workspaceId: context.workspaceId, paneId: targetPaneId, surfaceId: targetSurfaceId)
case .nextSurface:
cycleSurface(in: context, step: 1)
case .previousSurface:
cycleSurface(in: context, step: -1)
case .toggleZoom:
surfaceManager.togglePaneZoom(
workspaceId: context.workspaceId,
paneId: context.paneId,
workspaces: &workspaces,
persistence: persistence
)
}
return true
}
/// Returns the close target surface for the resolved pane context.
private func closeSurfaceId(for context: PaneCommandContext) -> UUID? {
if let surfaceId = context.surfaceId,
context.workspace.rootPane.paneId(containing: surfaceId) == context.paneId {
return surfaceId
}
return context.workspace.rootPane.activeSurfaceId(in: context.paneId)
}
/// Returns the close command title for the resolved pane context.
private func closeItemTitle(for context: PaneCommandContext) -> String {
guard let surfaceIds = context.workspace.rootPane.surfaceIds(in: context.paneId) else {
return "Close Active Pane/Tab"
}
if context.surfaceId != nil || surfaceIds.count > 1 {
return "Close Active Tab"
}
return "Close Active Pane"
}
/// Advances tab focus within a specific pane context.
private func cycleSurface(in context: PaneCommandContext, step: Int) {
guard step != 0 else { return }
guard let surfaceIds = context.workspace.rootPane.surfaceIds(in: context.paneId), !surfaceIds.isEmpty else {
return
}
let currentSurfaceId = context.surfaceId
?? context.workspace.rootPane.activeSurfaceId(in: context.paneId)
?? surfaceIds.first
guard let currentSurfaceId,
let currentIndex = surfaceIds.firstIndex(of: currentSurfaceId) else {
return
}
let rawIndex = currentIndex + step
let normalizedIndex = (rawIndex % surfaceIds.count + surfaceIds.count) % surfaceIds.count
let nextSurfaceId = surfaceIds[normalizedIndex]
activateSurface(workspaceId: context.workspaceId, paneId: context.paneId, surfaceId: nextSurfaceId)
}
/// Returns a resolved pane command context for an explicit pane or tab target.
private func paneCommandContext(
workspaceId: UUID,
paneId: UUID,
surfaceId: UUID? = nil
) -> PaneCommandContext? {
guard let workspace = self.workspace(id: workspaceId),
workspace.rootPane.containsPane(paneId) else {
return nil
}
if let surfaceId,
workspace.rootPane.paneId(containing: surfaceId) != paneId {
return nil
}
return PaneCommandContext(
workspaceId: workspaceId,
workspace: workspace,
paneId: paneId,
surfaceId: surfaceId
)
}
/// Builds the standard surface model used by app-driven surface creation flows.
private func configuredDefaultSurface() -> SurfaceModel {
var surface = SurfaceModel.makeDefault()
surface.terminalConfig.workingDirectory = NSHomeDirectory()
return surface
}
/// Builds the standard surface model used by split flows, inheriting cwd from the source surface when available.
func configuredSplitSurface(
workspaceId: UUID,
paneId: UUID,
sourceSurfaceId: UUID? = nil
) -> SurfaceModel {
var surface = SurfaceModel.makeDefault()
surface.terminalConfig.workingDirectory = resolvedSplitWorkingDirectory(
workspaceId: workspaceId,
paneId: paneId,
sourceSurfaceId: sourceSurfaceId
) ?? NSHomeDirectory()
return surface
}
/// Resolves the best available cwd to seed a newly split terminal surface.
func resolvedSplitWorkingDirectory(
workspaceId: UUID,
paneId: UUID,
sourceSurfaceId: UUID? = nil
) -> String? {
guard let workspace = workspace(id: workspaceId) else { return nil }
if let sourceSurfaceId {
guard let surface = surface(in: workspace.rootPane, surfaceId: sourceSurfaceId) else {
return nil
}
return normalizedWorkingDirectory(surface.terminalConfig.workingDirectory)
}
guard let activeSurfaceId = workspace.rootPane.activeSurfaceId(in: paneId),
let surface = surface(in: workspace.rootPane, surfaceId: activeSurfaceId) else {
return nil
}
return normalizedWorkingDirectory(surface.terminalConfig.workingDirectory)
}
/// Trims a cwd string and returns `nil` when it carries no usable path.
func normalizedWorkingDirectory(_ workingDirectory: String?) -> String? {
let trimmedWorkingDirectory = workingDirectory?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
return trimmedWorkingDirectory.isEmpty ? nil : trimmedWorkingDirectory
}
/// Returns focused pane context resolved from responder, workspace, and pane state.
private func focusedPaneContext() -> PaneCommandContext? {
guard let workspaceId = window.selectedWorkspaceId,
let workspace = self.workspace(id: workspaceId) else {
return nil
}
if let sourceSurfaceId = currentResponderSurfaceId(),
let paneId = workspace.rootPane.paneId(containing: sourceSurfaceId) {
return PaneCommandContext(
workspaceId: workspaceId,
workspace: workspace,
paneId: paneId,
surfaceId: sourceSurfaceId
)
}
guard let paneId = focusedPaneId(in: workspace) ?? workspace.rootPane.firstLeafId() else {
return nil
}
return PaneCommandContext(
workspaceId: workspaceId,
workspace: workspace,
paneId: paneId,
surfaceId: workspace.rootPane.activeSurfaceId(in: paneId)
)
}
/// Returns the surface id of the current first-responder terminal view, if any.
func currentResponderSurfaceId() -> UUID? {
guard let responder = NSApplication.shared.keyWindow?.firstResponder else { return nil }
if let host = responder as? LibghosttySurfaceView {
return host.surfaceId
}
guard let viewResponder = responder as? NSView else { return nil }
var view: NSView? = viewResponder
while let current = view {
if let host = current as? LibghosttySurfaceView {
return host.surfaceId
}
view = current.superview
}
return nil
}
}