From 3e7c4475a1fe81b38e9457e262a162b41c9f1271 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Thu, 17 Aug 2023 20:48:16 +0200 Subject: [PATCH] macOS: move newTab code to PrimaryWindowManager --- macos/Sources/AppDelegate.swift | 40 +-------------- .../Features/Primary Window/PrimaryView.swift | 2 +- .../PrimaryWindowController.swift | 5 +- .../Primary Window/PrimaryWindowManager.swift | 49 ++++++++++++++++++- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index efe494906..fa468221d 100644 --- a/macos/Sources/AppDelegate.swift +++ b/macos/Sources/AppDelegate.swift @@ -1,7 +1,6 @@ import AppKit import OSLog import GhosttyKit -import SwiftUI @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { @@ -25,22 +24,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { super.init() windowManager = PrimaryWindowManager(ghostty: self.ghostty) - - // Register self as observer for the NewTab notification that - // is triggered via callback from Zig code. - NotificationCenter.default.addObserver( - self, - selector: #selector(onNewTab), - name: Ghostty.Notification.ghosttyNewTab, - object: nil) - } - - deinit { - // Clean up the observer. - NotificationCenter.default.removeObserver( - self, - name: Ghostty.Notification.ghosttyNewTab, - object: nil) } func applicationDidFinishLaunching(_ notification: Notification) { @@ -98,28 +81,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject { } @IBAction func newTab(_ sender: Any?) { - if windowManager.mainWindow != nil { - guard let surface = focusedSurface() else { return } - ghostty.newTab(surface: surface) - } else { - windowManager.addNewWindow() - } - } - - @objc private func onNewTab(notification: SwiftUI.Notification) { - guard let surfaceView = notification.object as? Ghostty.SurfaceView else { return } - guard let window = surfaceView.window else { return } - - let fontSizeAny = notification.userInfo?[Ghostty.Notification.NewTabKey] - let fontSize = fontSizeAny as? UInt8 - - if fontSize != nil { - // Add the new tab to the window with the given font size. - windowManager.addNewTab(to: window, withFontSize: fontSize) - } else { - // No font size specified, just add new tab. - windowManager.addNewTab(to: window) - } + windowManager.newTab() } @IBAction func closeWindow(_ sender: Any) { diff --git a/macos/Sources/Features/Primary Window/PrimaryView.swift b/macos/Sources/Features/Primary Window/PrimaryView.swift index 40b0f464c..07bbec947 100644 --- a/macos/Sources/Features/Primary Window/PrimaryView.swift +++ b/macos/Sources/Features/Primary Window/PrimaryView.swift @@ -11,7 +11,7 @@ struct PrimaryView: View { // We need this to report back up the app controller which surface in this view is focused. let focusedSurfaceWrapper: FocusedSurfaceWrapper - // TODO: Document this + // If this is set, we inherit the fontSize from the parent tab or window. let fontSize: UInt8? // We need access to our window to know if we're the key window to determine diff --git a/macos/Sources/Features/Primary Window/PrimaryWindowController.swift b/macos/Sources/Features/Primary Window/PrimaryWindowController.swift index 6d860e641..257a0bb6f 100644 --- a/macos/Sources/Features/Primary Window/PrimaryWindowController.swift +++ b/macos/Sources/Features/Primary Window/PrimaryWindowController.swift @@ -12,9 +12,8 @@ class PrimaryWindowController: NSWindowController { // This is required for the "+" button to show up in the tab bar to add a // new tab. override func newWindowForTab(_ sender: Any?) { - guard let window = self.window else { preconditionFailure("Expected window to be loaded") } + guard let window = self.window as? PrimaryWindow else { preconditionFailure("Expected window to be loaded") } guard let manager = self.windowManager else { return } - // TODO: We need to call to Zig code here so we can get the surface - manager.addNewTab(to: window) + manager.newTabForWindow(window: window) } } diff --git a/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift b/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift index 14f93f4e9..8b8fb94d2 100644 --- a/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift +++ b/macos/Sources/Features/Primary Window/PrimaryWindowManager.swift @@ -1,5 +1,6 @@ import Cocoa import Combine +import SwiftUI // PrimaryWindowManager manages the windows and tabs in the primary window // of the application. It keeps references to windows and cleans them up when @@ -43,6 +44,22 @@ class PrimaryWindowManager { init(ghostty: Ghostty.AppState) { self.ghostty = ghostty + + // Register self as observer for the NewTab notification that + // is triggered via callback from Zig code. + NotificationCenter.default.addObserver( + self, + selector: #selector(onNewTab), + name: Ghostty.Notification.ghosttyNewTab, + object: nil) + } + + deinit { + // Clean up the observer. + NotificationCenter.default.removeObserver( + self, + name: Ghostty.Notification.ghosttyNewTab, + object: nil) } /// Add the initial window for the application. This should only be called once from the AppDelegate. @@ -61,7 +78,37 @@ class PrimaryWindowManager { newWindow.makeKeyAndOrderFront(nil) } - func addNewTab(to window: NSWindow, withFontSize fontSize: UInt8? = nil) { + func newTabForWindow(window: PrimaryWindow) { + guard let surface = window.focusedSurfaceWrapper.surface else { return } + ghostty.newTab(surface: surface) + } + + func newTab() { + if mainWindow != nil { + guard let window = mainWindow as? PrimaryWindow else { return } + self.newTabForWindow(window: window) + } else { + self.addNewWindow() + } + } + + @objc private func onNewTab(notification: SwiftUI.Notification) { + guard let surfaceView = notification.object as? Ghostty.SurfaceView else { return } + guard let window = surfaceView.window else { return } + + let fontSizeAny = notification.userInfo?[Ghostty.Notification.NewTabKey] + let fontSize = fontSizeAny as? UInt8 + + if fontSize != nil { + // Add the new tab to the window with the given font size. + self.addNewTab(to: window, withFontSize: fontSize) + } else { + // No font size specified, just add new tab. + self.addNewTab(to: window) + } + } + + private func addNewTab(to window: NSWindow, withFontSize fontSize: UInt8? = nil) { guard let controller = createWindowController(withFontSize: fontSize) else { return } guard let newWindow = addManagedWindow(windowController: controller)?.window else { return } window.addTabbedWindow(newWindow, ordered: .above)