macos: split traversal uses direction to determine proper focus target

Fixes #415
This commit is contained in:
Mitchell Hashimoto
2023-09-10 08:59:05 -07:00
parent 946826c384
commit 303c5ca189

View File

@ -54,17 +54,30 @@ extension Ghostty {
/// Returns the view that would prefer receiving focus in this tree. This is always the /// Returns the view that would prefer receiving focus in this tree. This is always the
/// top-left-most view. This is used when creating a split or closing a split to find the /// top-left-most view. This is used when creating a split or closing a split to find the
/// next view to send focus to. /// next view to send focus to.
func preferredFocus() -> SurfaceView { func preferredFocus(_ direction: SplitFocusDirection = .top) -> SurfaceView {
let container: Container
switch (self) { switch (self) {
case .noSplit(let leaf): case .noSplit(let leaf):
// noSplit is easy because there is only one thing to focus
return leaf.surface return leaf.surface
case .horizontal(let container): case .horizontal(let c):
return container.topLeft.preferredFocus() container = c
case .vertical(let container): case .vertical(let c):
return container.topLeft.preferredFocus() container = c
} }
let node: SplitNode
switch (direction) {
case .previous, .bottom, .left:
node = container.bottomRight
case .next, .top, .right:
node = container.topLeft
}
return node.preferredFocus(direction)
} }
/// Close the surface associated with this node. This will likely deinitialize the /// Close the surface associated with this node. This will likely deinitialize the
@ -405,7 +418,10 @@ extension Ghostty {
guard let directionAny = notification.userInfo?[Notification.SplitDirectionKey] else { return } guard let directionAny = notification.userInfo?[Notification.SplitDirectionKey] else { return }
guard let direction = directionAny as? SplitFocusDirection else { return } guard let direction = directionAny as? SplitFocusDirection else { return }
guard let next = neighbors.get(direction: direction) else { return } guard let next = neighbors.get(direction: direction) else { return }
Ghostty.moveFocus(to: next.preferredFocus(), from: node.preferredFocus()) Ghostty.moveFocus(
to: next.preferredFocus(direction),
from: node.preferredFocus()
)
} }
} }