macos: implement newWindowForTab for "+" button

This commit is contained in:
Mitchell Hashimoto
2023-08-04 17:35:57 -07:00
parent 994d456223
commit bb7c67b967
3 changed files with 27 additions and 5 deletions

View File

@ -23,12 +23,18 @@ class PrimaryWindow: NSWindow {
backing: .buffered, backing: .buffered,
defer: false) defer: false)
window.center() window.center()
window.contentView = NSHostingView(rootView: PrimaryView( window.contentView = NSHostingView(rootView: PrimaryView(
ghostty: ghostty, ghostty: ghostty,
appDelegate: appDelegate, appDelegate: appDelegate,
focusedSurfaceWrapper: window.focusedSurfaceWrapper)) focusedSurfaceWrapper: window.focusedSurfaceWrapper))
// We do want to cascade when new windows are created
window.windowController?.shouldCascadeWindows = true window.windowController?.shouldCascadeWindows = true
// A default title. This should be overwritten quickly by the Ghostty core.
window.title = "Ghostty 👻" window.title = "Ghostty 👻"
return window return window
} }

View File

@ -6,9 +6,16 @@ class PrimaryWindowController: NSWindowController {
// of each other. // of each other.
static var lastCascadePoint = NSPoint(x: 0, y: 0) static var lastCascadePoint = NSPoint(x: 0, y: 0)
static func create(ghosttyApp: Ghostty.AppState, appDelegate: AppDelegate) -> PrimaryWindowController { // This is used to programmatically control tabs.
let window = PrimaryWindow.create(ghostty: ghosttyApp, appDelegate: appDelegate) weak var windowManager: PrimaryWindowManager?
lastCascadePoint = window.cascadeTopLeft(from: lastCascadePoint)
return PrimaryWindowController(window: window) // This is required for the "+" button to show up in the tab bar to add a
// new tab.
override func newWindowForTab(_ sender: Any?) {
// TODO: specify our window so the tab is created in the proper window
// guard let window = self.window else { preconditionFailure("Expected window to be loaded") }
guard let manager = self.windowManager else { return }
manager.addNewTab()
} }
} }

View File

@ -17,6 +17,11 @@ class PrimaryWindowManager {
let window: NSWindow let window: NSWindow
let closePublisher: AnyCancellable let closePublisher: AnyCancellable
} }
// Keep track of the last point that our window was launched at so that new
// windows "cascade" over each other and don't just launch directly on top
// of each other.
static var lastCascadePoint = NSPoint(x: 0, y: 0)
private var ghostty: Ghostty.AppState private var ghostty: Ghostty.AppState
private var managedWindows: [ManagedWindow] = [] private var managedWindows: [ManagedWindow] = []
@ -58,7 +63,11 @@ class PrimaryWindowManager {
private func createWindowController() -> PrimaryWindowController? { private func createWindowController() -> PrimaryWindowController? {
guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return nil } guard let appDelegate = NSApplication.shared.delegate as? AppDelegate else { return nil }
return PrimaryWindowController.create(ghosttyApp: self.ghostty, appDelegate: appDelegate) let window = PrimaryWindow.create(ghostty: ghostty, appDelegate: appDelegate)
Self.lastCascadePoint = window.cascadeTopLeft(from: Self.lastCascadePoint)
let controller = PrimaryWindowController(window: window)
controller.windowManager = self
return controller
} }
private func addManagedWindow(windowController: PrimaryWindowController) -> ManagedWindow? { private func addManagedWindow(windowController: PrimaryWindowController) -> ManagedWindow? {