From 837b551a92cfd3e94236564bac96a98b552a367f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 10 Jan 2024 21:24:47 -0800 Subject: [PATCH 1/2] macos: invalid restorable state whenever surface tree changes Related to #1177 This detects whenever the surface tree (splits) change in any way and requests that the restorable state be invalided by macOS. --- .../Sources/Features/Terminal/TerminalController.swift | 6 ++++++ macos/Sources/Features/Terminal/TerminalView.swift | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index 8e4ddb270..b49e502e8 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -470,6 +470,12 @@ class TerminalController: NSWindowController, NSWindowDelegate, self.window?.close() } + func surfaceTreeDidChange() { + // Whenever our surface tree changes in any way (new split, close split, etc.) + // we want to invalidate our state. + invalidateRestorableState() + } + //MARK: - Clipboard Confirmation func clipboardConfirmationComplete(_ action: ClipboardConfirmationView.Action, _ request: Ghostty.ClipboardRequest) { diff --git a/macos/Sources/Features/Terminal/TerminalView.swift b/macos/Sources/Features/Terminal/TerminalView.swift index 7596c343a..ba3f86db6 100644 --- a/macos/Sources/Features/Terminal/TerminalView.swift +++ b/macos/Sources/Features/Terminal/TerminalView.swift @@ -13,6 +13,10 @@ protocol TerminalViewDelegate: AnyObject { /// The cell size changed. func cellSizeDidChange(to: NSSize) + + /// The surface tree did change in some way, i.e. a split was added, removed, etc. This is + /// not called initially. + func surfaceTreeDidChange() } // Default all the functions so they're optional @@ -97,6 +101,12 @@ struct TerminalView: View { guard let size = newValue else { return } self.delegate?.cellSizeDidChange(to: size) } + .onChange(of: viewModel.surfaceTree?.hashValue) { _ in + // This is funky, but its the best way I could think of to detect + // ANY CHANGE within the deeply nested surface tree -- detecting a change + // in the hash value. + self.delegate?.surfaceTreeDidChange() + } } } } From 8b0d4312ee7b6183c829b991f2482c4b52aaf9ac Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 10 Jan 2024 21:28:49 -0800 Subject: [PATCH 2/2] macos: invalidate restorable state whenever a terminal window is closed --- macos/Sources/Features/Terminal/TerminalManager.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/macos/Sources/Features/Terminal/TerminalManager.swift b/macos/Sources/Features/Terminal/TerminalManager.swift index 6f00bed18..af85341c6 100644 --- a/macos/Sources/Features/Terminal/TerminalManager.swift +++ b/macos/Sources/Features/Terminal/TerminalManager.swift @@ -190,6 +190,11 @@ class TerminalManager { let frame = focusedWindow.frame Self.lastCascadePoint = NSPoint(x: frame.minX, y: frame.maxY) } + + // I don't think we strictly have to do this but if a window is + // closed I want to make sure that the app state is invalided so + // we don't reopen closed windows. + NSApplication.shared.invalidateRestorableState() } /// Close all windows, asking for confirmation if necessary.