mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
macos: redo all the split views
This commit is contained in:
@ -11,90 +11,143 @@ extension Ghostty {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if let app = app {
|
if let app = app {
|
||||||
TerminalSplitChild(app)
|
TerminalSplitContainer(app: app)
|
||||||
.navigationTitle(surfaceTitle ?? "Ghostty")
|
.navigationTitle(surfaceTitle ?? "Ghostty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct TerminalSplitChild: View {
|
private struct TerminalSplitPane: View {
|
||||||
enum Direction {
|
@ObservedObject var surfaceView: SurfaceView
|
||||||
case none
|
@Binding var requestSplit: SplitViewDirection?
|
||||||
case vertical
|
@Binding var requestClose: Bool
|
||||||
case horizontal
|
|
||||||
|
var body: some View {
|
||||||
|
let pub = NotificationCenter.default.publisher(for: Notification.ghosttyNewSplit, object: surfaceView)
|
||||||
|
let pubClose = NotificationCenter.default.publisher(for: Notification.ghosttyCloseSurface, object: surfaceView)
|
||||||
|
SurfaceWrapper(surfaceView: surfaceView)
|
||||||
|
.onReceive(pub) { onNewSplit(notification: $0) }
|
||||||
|
.onReceive(pubClose) { _ in requestClose = true }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The stored state between invocations.
|
private func onNewSplit(notification: SwiftUI.Notification) {
|
||||||
class ViewState: ObservableObject {
|
guard let directionAny = notification.userInfo?["direction"] else { return }
|
||||||
/// The direction of the split currently
|
guard let direction = directionAny as? ghostty_split_direction_e else { return }
|
||||||
@Published var direction: Direction = .none
|
switch (direction) {
|
||||||
|
case GHOSTTY_SPLIT_RIGHT:
|
||||||
/// The top or left view. This is always set.
|
requestSplit = .horizontal
|
||||||
@Published var topLeft: Ghostty.SurfaceView
|
|
||||||
|
case GHOSTTY_SPLIT_DOWN:
|
||||||
/// The bottom or right view. This can be nil if the direction == .none.
|
requestSplit = .vertical
|
||||||
@Published var bottomRight: Ghostty.SurfaceView? = nil
|
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct TerminalSplitContainer: View {
|
||||||
|
let app: ghostty_app_t
|
||||||
|
var parentClose: Binding<Bool>? = nil
|
||||||
|
@State private var direction: SplitViewDirection? = nil
|
||||||
|
@State private var proposedDirection: SplitViewDirection? = nil
|
||||||
|
@State private var closeTopLeft: Bool = false
|
||||||
|
@State private var closeBottomRight: Bool = false
|
||||||
|
@StateObject private var panes: PaneState
|
||||||
|
|
||||||
|
class PaneState: ObservableObject {
|
||||||
|
@Published var topLeft: SurfaceView
|
||||||
|
@Published var bottomRight: SurfaceView? = nil
|
||||||
|
|
||||||
/// Initialize the view state for the first time. This will create our topLeft view from new.
|
/// Initialize the view state for the first time. This will create our topLeft view from new.
|
||||||
init(_ app: ghostty_app_t) {
|
init(_ app: ghostty_app_t) {
|
||||||
self.topLeft = Ghostty.SurfaceView(app)
|
self.topLeft = SurfaceView(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize the view state using an existing top left. This is usually used when a split happens and
|
/// Initialize the view state using an existing top left. This is usually used when a split happens and
|
||||||
/// the child view inherits the top left.
|
/// the child view inherits the top left.
|
||||||
init(topLeft: Ghostty.SurfaceView) {
|
init(topLeft: SurfaceView) {
|
||||||
self.topLeft = topLeft
|
self.topLeft = topLeft
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let app: ghostty_app_t
|
init(app: ghostty_app_t) {
|
||||||
@StateObject private var state: ViewState
|
|
||||||
|
|
||||||
init(_ app: ghostty_app_t) {
|
|
||||||
self.app = app
|
self.app = app
|
||||||
_state = StateObject(wrappedValue: ViewState(app))
|
_panes = StateObject(wrappedValue: PaneState(app))
|
||||||
}
|
}
|
||||||
|
|
||||||
init(_ app: ghostty_app_t, topLeft: Ghostty.SurfaceView) {
|
init(app: ghostty_app_t, parentClose: Binding<Bool>, topLeft: SurfaceView) {
|
||||||
self.app = app
|
self.app = app
|
||||||
_state = StateObject(wrappedValue: ViewState(topLeft: topLeft))
|
self.parentClose = parentClose
|
||||||
|
_panes = StateObject(wrappedValue: PaneState(topLeft: topLeft))
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
if let direction = self.direction {
|
||||||
|
SplitView(direction, left: {
|
||||||
|
TerminalSplitContainer(
|
||||||
|
app: app,
|
||||||
|
parentClose: $closeTopLeft,
|
||||||
|
topLeft: panes.topLeft
|
||||||
|
)
|
||||||
|
.onChange(of: closeTopLeft) { value in
|
||||||
|
guard value else { return }
|
||||||
|
|
||||||
|
// Move our bottom to our top and reset all of our state
|
||||||
|
panes.topLeft = panes.bottomRight!
|
||||||
|
panes.bottomRight = nil
|
||||||
|
self.direction = nil
|
||||||
|
closeTopLeft = false
|
||||||
|
closeBottomRight = false
|
||||||
|
|
||||||
|
// See fixFocus comment, we have to run this whenever split changes.
|
||||||
|
fixFocus()
|
||||||
|
}
|
||||||
|
}, right: {
|
||||||
|
TerminalSplitContainer(
|
||||||
|
app: app,
|
||||||
|
parentClose: $closeBottomRight,
|
||||||
|
topLeft: panes.bottomRight!
|
||||||
|
)
|
||||||
|
.onChange(of: closeBottomRight) { value in
|
||||||
|
guard value else { return }
|
||||||
|
|
||||||
|
// Move our bottom to our top and reset all of our state
|
||||||
|
panes.bottomRight = nil
|
||||||
|
self.direction = nil
|
||||||
|
closeTopLeft = false
|
||||||
|
closeBottomRight = false
|
||||||
|
|
||||||
|
// See fixFocus comment, we have to run this whenever split changes.
|
||||||
|
fixFocus()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
TerminalSplitPane(surfaceView: panes.topLeft, requestSplit: $proposedDirection, requestClose: $closeTopLeft)
|
||||||
|
.onChange(of: proposedDirection) { value in
|
||||||
|
guard let newDirection = value else { return }
|
||||||
|
split(to: newDirection)
|
||||||
|
}
|
||||||
|
.onChange(of: closeTopLeft) { value in
|
||||||
|
guard value else { return }
|
||||||
|
self.parentClose?.wrappedValue = value
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func split(to: Direction) {
|
private func split(to: SplitViewDirection) {
|
||||||
assert(to != .none)
|
assert(direction == nil)
|
||||||
assert(state.direction == .none)
|
|
||||||
|
|
||||||
// Make the split the desired value
|
// Make the split the desired value
|
||||||
state.direction = to
|
direction = to
|
||||||
|
|
||||||
// Create the new split which always goes to the bottom right.
|
// Create the new split which always goes to the bottom right.
|
||||||
state.bottomRight = Ghostty.SurfaceView(app)
|
panes.bottomRight = SurfaceView(app)
|
||||||
|
|
||||||
// See fixFocus comment, we have to run this whenever split changes.
|
// See fixFocus comment, we have to run this whenever split changes.
|
||||||
fixFocus()
|
fixFocus()
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeTopLeft() {
|
|
||||||
assert(state.direction != .none)
|
|
||||||
assert(state.bottomRight != nil)
|
|
||||||
state.topLeft = state.bottomRight!
|
|
||||||
state.bottomRight = nil
|
|
||||||
state.direction = .none
|
|
||||||
|
|
||||||
// See fixFocus comment, we have to run this whenever split changes.
|
|
||||||
fixFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
func closeBottomRight() {
|
|
||||||
assert(state.direction != .none)
|
|
||||||
assert(state.bottomRight != nil)
|
|
||||||
state.bottomRight = nil
|
|
||||||
state.direction = .none
|
|
||||||
|
|
||||||
// See fixFocus comment, we have to run this whenever split changes.
|
|
||||||
fixFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// There is a bug I can't figure out where when changing the split state, the terminal view
|
/// There is a bug I can't figure out where when changing the split state, the terminal view
|
||||||
/// will lose focus. There has to be some nice SwiftUI-native way to fix this but I can't
|
/// will lose focus. There has to be some nice SwiftUI-native way to fix this but I can't
|
||||||
/// figure it out so we're going to do this hacky thing to bring focus back to the terminal
|
/// figure it out so we're going to do this hacky thing to bring focus back to the terminal
|
||||||
@ -102,8 +155,8 @@ extension Ghostty {
|
|||||||
private func fixFocus() {
|
private func fixFocus() {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
// The view we want to focus
|
// The view we want to focus
|
||||||
var view = state.topLeft
|
var view = panes.topLeft
|
||||||
if let right = state.bottomRight { view = right }
|
if let right = panes.bottomRight { view = right }
|
||||||
|
|
||||||
// If the callback runs before the surface is attached to a view
|
// If the callback runs before the surface is attached to a view
|
||||||
// then the window will be nil. We just reschedule in that case.
|
// then the window will be nil. We just reschedule in that case.
|
||||||
@ -112,55 +165,10 @@ extension Ghostty {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = state.topLeft.resignFirstResponder()
|
_ = panes.topLeft.resignFirstResponder()
|
||||||
_ = state.bottomRight?.resignFirstResponder()
|
_ = panes.bottomRight?.resignFirstResponder()
|
||||||
window.makeFirstResponder(view)
|
window.makeFirstResponder(view)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func onNewSplit(notification: SwiftUI.Notification) {
|
|
||||||
guard let directionAny = notification.userInfo?["direction"] else { return }
|
|
||||||
guard let direction = directionAny as? ghostty_split_direction_e else { return }
|
|
||||||
switch (direction) {
|
|
||||||
case GHOSTTY_SPLIT_RIGHT:
|
|
||||||
split(to: .horizontal)
|
|
||||||
|
|
||||||
case GHOSTTY_SPLIT_DOWN:
|
|
||||||
split(to: .vertical)
|
|
||||||
|
|
||||||
default:
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
switch (state.direction) {
|
|
||||||
case .none:
|
|
||||||
let pub = NotificationCenter.default.publisher(for: Ghostty.Notification.ghosttyNewSplit, object: state.topLeft)
|
|
||||||
SurfaceWrapper(surfaceView: state.topLeft)
|
|
||||||
.onReceive(pub) { onNewSplit(notification: $0) }
|
|
||||||
case .horizontal:
|
|
||||||
SplitView(.horizontal, left: {
|
|
||||||
let pub = NotificationCenter.default.publisher(for: Ghostty.Notification.ghosttyCloseSurface, object: state.topLeft)
|
|
||||||
TerminalSplitChild(app, topLeft: state.topLeft)
|
|
||||||
.onReceive(pub) { _ in closeTopLeft() }
|
|
||||||
}, right: {
|
|
||||||
let pub = NotificationCenter.default.publisher(for: Ghostty.Notification.ghosttyCloseSurface, object: state.bottomRight!)
|
|
||||||
TerminalSplitChild(app, topLeft: state.bottomRight!)
|
|
||||||
.onReceive(pub) { _ in closeBottomRight() }
|
|
||||||
})
|
|
||||||
case .vertical:
|
|
||||||
SplitView(.vertical, left: {
|
|
||||||
let pub = NotificationCenter.default.publisher(for: Ghostty.Notification.ghosttyCloseSurface, object: state.topLeft)
|
|
||||||
TerminalSplitChild(app, topLeft: state.topLeft)
|
|
||||||
.onReceive(pub) { _ in closeTopLeft() }
|
|
||||||
}, right: {
|
|
||||||
let pub = NotificationCenter.default.publisher(for: Ghostty.Notification.ghosttyCloseSurface, object: state.bottomRight!)
|
|
||||||
TerminalSplitChild(app, topLeft: state.bottomRight!)
|
|
||||||
.onReceive(pub) { _ in closeBottomRight() }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,21 +9,27 @@ extension Ghostty {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if let app = self.app {
|
if let app = self.app {
|
||||||
TerminalForApp(app)
|
SurfaceForApp(app) { surfaceView in
|
||||||
.navigationTitle(surfaceTitle ?? "Ghostty")
|
SurfaceWrapper(surfaceView: surfaceView)
|
||||||
|
}
|
||||||
|
.navigationTitle(surfaceTitle ?? "Ghostty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct TerminalForApp: View {
|
/// Yields a SurfaceView for a ghostty app that can then be used however you want.
|
||||||
|
struct SurfaceForApp<Content: View>: View {
|
||||||
|
let content: ((SurfaceView) -> Content)
|
||||||
|
|
||||||
@StateObject private var surfaceView: SurfaceView
|
@StateObject private var surfaceView: SurfaceView
|
||||||
|
|
||||||
init(_ app: ghostty_app_t) {
|
init(_ app: ghostty_app_t, @ViewBuilder content: @escaping ((SurfaceView) -> Content)) {
|
||||||
_surfaceView = StateObject(wrappedValue: SurfaceView(app))
|
_surfaceView = StateObject(wrappedValue: SurfaceView(app))
|
||||||
|
self.content = content
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
SurfaceWrapper(surfaceView: surfaceView)
|
content(surfaceView)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import SwiftUI
|
|||||||
extension SplitView {
|
extension SplitView {
|
||||||
/// The split divider that is rendered and can be used to resize a split view.
|
/// The split divider that is rendered and can be used to resize a split view.
|
||||||
struct Divider: View {
|
struct Divider: View {
|
||||||
let direction: Direction
|
let direction: SplitViewDirection
|
||||||
let visibleSize: CGFloat
|
let visibleSize: CGFloat
|
||||||
let invisibleSize: CGFloat
|
let invisibleSize: CGFloat
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import SwiftUI
|
|||||||
/// as time goes on. For example, the splitter divider size and styling is all hardcoded.
|
/// as time goes on. For example, the splitter divider size and styling is all hardcoded.
|
||||||
struct SplitView<L: View, R: View>: View {
|
struct SplitView<L: View, R: View>: View {
|
||||||
/// Direction of the split
|
/// Direction of the split
|
||||||
let direction: Direction
|
let direction: SplitViewDirection
|
||||||
|
|
||||||
/// The left and right views to render.
|
/// The left and right views to render.
|
||||||
let left: L
|
let left: L
|
||||||
@ -41,7 +41,7 @@ struct SplitView<L: View, R: View>: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init(_ direction: Direction, @ViewBuilder left: (() -> L), @ViewBuilder right: (() -> R)) {
|
init(_ direction: SplitViewDirection, @ViewBuilder left: (() -> L), @ViewBuilder right: (() -> R)) {
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
self.left = left()
|
self.left = left()
|
||||||
self.right = right()
|
self.right = right()
|
||||||
@ -114,8 +114,6 @@ struct SplitView<L: View, R: View>: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension SplitView {
|
enum SplitViewDirection {
|
||||||
enum Direction {
|
case horizontal, vertical
|
||||||
case horizontal, vertical
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user