mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
macOS: move newTab code to PrimaryWindowManager
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
import OSLog
|
import OSLog
|
||||||
import GhosttyKit
|
import GhosttyKit
|
||||||
import SwiftUI
|
|
||||||
|
|
||||||
@NSApplicationMain
|
@NSApplicationMain
|
||||||
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
|
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
|
||||||
@ -25,22 +24,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
|
|||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
windowManager = PrimaryWindowManager(ghostty: self.ghostty)
|
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) {
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||||
@ -98,28 +81,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func newTab(_ sender: Any?) {
|
@IBAction func newTab(_ sender: Any?) {
|
||||||
if windowManager.mainWindow != nil {
|
windowManager.newTab()
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func closeWindow(_ sender: Any) {
|
@IBAction func closeWindow(_ sender: Any) {
|
||||||
|
@ -11,7 +11,7 @@ struct PrimaryView: View {
|
|||||||
// We need this to report back up the app controller which surface in this view is focused.
|
// We need this to report back up the app controller which surface in this view is focused.
|
||||||
let focusedSurfaceWrapper: FocusedSurfaceWrapper
|
let focusedSurfaceWrapper: FocusedSurfaceWrapper
|
||||||
|
|
||||||
// TODO: Document this
|
// If this is set, we inherit the fontSize from the parent tab or window.
|
||||||
let fontSize: UInt8?
|
let fontSize: UInt8?
|
||||||
|
|
||||||
// We need access to our window to know if we're the key window to determine
|
// We need access to our window to know if we're the key window to determine
|
||||||
|
@ -12,9 +12,8 @@ class PrimaryWindowController: NSWindowController {
|
|||||||
// This is required for the "+" button to show up in the tab bar to add a
|
// This is required for the "+" button to show up in the tab bar to add a
|
||||||
// new tab.
|
// new tab.
|
||||||
override func newWindowForTab(_ sender: Any?) {
|
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 }
|
guard let manager = self.windowManager else { return }
|
||||||
// TODO: We need to call to Zig code here so we can get the surface
|
manager.newTabForWindow(window: window)
|
||||||
manager.addNewTab(to: window)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import Cocoa
|
import Cocoa
|
||||||
import Combine
|
import Combine
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
// PrimaryWindowManager manages the windows and tabs in the primary window
|
// PrimaryWindowManager manages the windows and tabs in the primary window
|
||||||
// of the application. It keeps references to windows and cleans them up when
|
// of the application. It keeps references to windows and cleans them up when
|
||||||
@ -43,6 +44,22 @@ class PrimaryWindowManager {
|
|||||||
|
|
||||||
init(ghostty: Ghostty.AppState) {
|
init(ghostty: Ghostty.AppState) {
|
||||||
self.ghostty = ghostty
|
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.
|
/// 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)
|
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 controller = createWindowController(withFontSize: fontSize) else { return }
|
||||||
guard let newWindow = addManagedWindow(windowController: controller)?.window else { return }
|
guard let newWindow = addManagedWindow(windowController: controller)?.window else { return }
|
||||||
window.addTabbedWindow(newWindow, ordered: .above)
|
window.addTabbedWindow(newWindow, ordered: .above)
|
||||||
|
Reference in New Issue
Block a user