macos: handle surfaceTreeDidChange

This commit is contained in:
Mitchell Hashimoto
2025-06-04 11:28:22 -07:00
parent ea1ff438f8
commit 8b979d6dce
6 changed files with 21 additions and 37 deletions

View File

@@ -185,11 +185,11 @@ class QuickTerminalController: BaseTerminalController {
// MARK: Base Controller Overrides
override func surfaceTreeDidChange(from: Ghostty.SplitNode?, to: Ghostty.SplitNode?) {
override func surfaceTreeDidChange(from: SplitTree<Ghostty.SurfaceView>, to: SplitTree<Ghostty.SurfaceView>) {
super.surfaceTreeDidChange(from: from, to: to)
// If our surface tree is nil then we animate the window out.
if (to == nil) {
if (to.isEmpty) {
animateOut()
}
}

View File

@@ -42,11 +42,11 @@ class BaseTerminalController: NSWindowController,
}
/// The surface tree for this window.
@Published var surfaceTree: Ghostty.SplitNode? = nil {
didSet { surfaceTreeDidChange(from: oldValue, to: surfaceTree) }
}
@Published var surfaceTree: Ghostty.SplitNode? = nil
@Published var surfaceTree2: SplitTree<Ghostty.SurfaceView> = .init()
@Published var surfaceTree2: SplitTree<Ghostty.SurfaceView> = .init() {
didSet { surfaceTreeDidChange(from: oldValue, to: surfaceTree2) }
}
/// This can be set to show/hide the command palette.
@Published var commandPaletteIsShowing: Bool = false
@@ -174,9 +174,9 @@ class BaseTerminalController: NSWindowController,
/// Called when the surfaceTree variable changed.
///
/// Subclasses should call super first.
func surfaceTreeDidChange(from: Ghostty.SplitNode?, to: Ghostty.SplitNode?) {
func surfaceTreeDidChange(from: SplitTree<Ghostty.SurfaceView>, to: SplitTree<Ghostty.SurfaceView>) {
// If our surface tree becomes nil then we have no focused surface.
if (to == nil) {
if (to.isEmpty) {
focusedSurface = nil
}
}
@@ -442,8 +442,11 @@ class BaseTerminalController: NSWindowController,
surfaceTree2 = SplitTree(root: surfaceTree2.root, zoomed: targetNode)
}
// Ensure focus stays on the target surface
Ghostty.moveFocus(to: target)
// Ensure focus stays on the target surface. We lose focus when we do
// this so we need to grab it again.
DispatchQueue.main.async {
Ghostty.moveFocus(to: target)
}
}
// MARK: Local Events
@@ -525,8 +528,6 @@ class BaseTerminalController: NSWindowController,
self.window?.contentResizeIncrements = to
}
func zoomStateDidChange(to: Bool) {}
func splitDidResize(node: SplitTree<Ghostty.SurfaceView>.Node, to newRatio: Double) {
let resizedNode = node.resize(to: newRatio)
do {

View File

@@ -106,15 +106,20 @@ class TerminalController: BaseTerminalController {
// MARK: Base Controller Overrides
override func surfaceTreeDidChange(from: Ghostty.SplitNode?, to: Ghostty.SplitNode?) {
override func surfaceTreeDidChange(from: SplitTree<Ghostty.SurfaceView>, to: SplitTree<Ghostty.SurfaceView>) {
super.surfaceTreeDidChange(from: from, to: to)
// Whenever our surface tree changes in any way (new split, close split, etc.)
// we want to invalidate our state.
invalidateRestorableState()
// Update our zoom state
if let window = window as? TerminalWindow {
window.surfaceIsZoomed = to.zoomed != nil
}
// If our surface tree is now nil then we close our window.
if (to == nil) {
if (to.isEmpty) {
self.window?.close()
}
}
@@ -677,12 +682,7 @@ class TerminalController: BaseTerminalController {
toolbar.titleText = to
}
}
override func zoomStateDidChange(to: Bool) {
guard let window = window as? TerminalWindow else { return }
window.surfaceIsZoomed = to
}
override func focusedSurfaceDidChange(to: Ghostty.SurfaceView?) {
super.focusedSurfaceDidChange(to: to)

View File

@@ -14,9 +14,6 @@ protocol TerminalViewDelegate: AnyObject {
/// The cell size changed.
func cellSizeDidChange(to: NSSize)
/// This is called when a split is zoomed.
func zoomStateDidChange(to: Bool)
/// Perform an action. At the time of writing this is only triggered by the command palette.
func performAction(_ action: String, on: Ghostty.SurfaceView)
@@ -56,7 +53,6 @@ struct TerminalView<ViewModel: TerminalViewModel>: View {
// Various state values sent back up from the currently focused terminals.
@FocusedValue(\.ghosttySurfaceView) private var focusedSurface
@FocusedValue(\.ghosttySurfacePwd) private var surfacePwd
@FocusedValue(\.ghosttySurfaceZoomed) private var zoomedSplit
@FocusedValue(\.ghosttySurfaceCellSize) private var cellSize
// The pwd of the focused surface as a URL
@@ -101,9 +97,6 @@ struct TerminalView<ViewModel: TerminalViewModel>: View {
guard let size = newValue else { return }
self.delegate?.cellSizeDidChange(to: size)
}
.onChange(of: zoomedSplit) { newValue in
self.delegate?.zoomStateDidChange(to: newValue ?? false)
}
}
// Ignore safe area to extend up in to the titlebar region if we have the "hidden" titlebar style
.ignoresSafeArea(.container, edges: ghostty.config.macosTitlebarStyle == "hidden" ? .top : [])

View File

@@ -30,7 +30,6 @@ extension Ghostty {
InspectableSurface(surfaceView: surfaceView)
}
}
.focusedValue(\.ghosttySurfaceZoomed, zoomedSurface != nil)
}
}

View File

@@ -502,15 +502,6 @@ extension FocusedValues {
typealias Value = String
}
var ghosttySurfaceZoomed: Bool? {
get { self[FocusedGhosttySurfaceZoomed.self] }
set { self[FocusedGhosttySurfaceZoomed.self] = newValue }
}
struct FocusedGhosttySurfaceZoomed: FocusedValueKey {
typealias Value = Bool
}
var ghosttySurfaceCellSize: OSSize? {
get { self[FocusedGhosttySurfaceCellSize.self] }
set { self[FocusedGhosttySurfaceCellSize.self] = newValue }