From f0c2d3d75a59004457e7c18e0075bd4df7b19d6a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Jan 2025 07:01:44 -0800 Subject: [PATCH] macos: fix retain cycle preventing window from freeing --- macos/Sources/App/macOS/AppDelegate.swift | 16 ++++++++-------- .../Features/Terminal/TerminalWindow.swift | 6 +++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index 70873236a..1a23eca90 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -424,35 +424,35 @@ class AppDelegate: NSObject, // If we have a main window then we don't process any of the keys // because we let it capture and propagate. guard NSApp.mainWindow == nil else { return event } - + // If this event as-is would result in a key binding then we send it. if let app = ghostty.app, ghostty_app_key_is_binding( - app, - event.ghosttyKeyEvent(GHOSTTY_ACTION_PRESS)) { + app, + event.ghosttyKeyEvent(GHOSTTY_ACTION_PRESS)) { ghostty_app_key(app, event.ghosttyKeyEvent(GHOSTTY_ACTION_PRESS)) return nil } - + // If this event would be handled by our menu then we do nothing. if let mainMenu = NSApp.mainMenu, mainMenu.performKeyEquivalent(with: event) { return nil } - + // If we reach this point then we try to process the key event // through the Ghostty key mechanism. - + // Ghostty must be loaded guard let ghostty = self.ghostty.app else { return event } - + // Build our event input and call ghostty if (ghostty_app_key(ghostty, event.ghosttyKeyEvent(GHOSTTY_ACTION_PRESS))) { // The key was used so we want to stop it from going to our Mac app Ghostty.logger.debug("local key event handled event=\(event)") return nil } - + return event } diff --git a/macos/Sources/Features/Terminal/TerminalWindow.swift b/macos/Sources/Features/Terminal/TerminalWindow.swift index 35f629bfd..0eb8daeeb 100644 --- a/macos/Sources/Features/Terminal/TerminalWindow.swift +++ b/macos/Sources/Features/Terminal/TerminalWindow.swift @@ -667,12 +667,16 @@ fileprivate class WindowDragView: NSView { // A view that matches the color of selected and unselected tabs in the adjacent tab bar. fileprivate class WindowButtonsBackdropView: NSView { - private let terminalWindow: TerminalWindow + // This must be weak because the window has this view. Otherwise + // a retain cycle occurs. + private weak var terminalWindow: TerminalWindow? private let isLightTheme: Bool private let overlayLayer = VibrantLayer() var isHighlighted: Bool = true { didSet { + guard let terminalWindow else { return } + if isLightTheme { overlayLayer.isHidden = isHighlighted layer?.backgroundColor = .clear