macos: working on splits

This commit is contained in:
Mitchell Hashimoto
2023-03-05 14:23:25 -08:00
parent 6854d09a8d
commit 00cf9edc94
2 changed files with 65 additions and 16 deletions

View File

@ -21,7 +21,7 @@ struct GhosttyApp: App {
case .error: case .error:
ErrorView() ErrorView()
case .ready: case .ready:
TerminalSplittableView(app: ghostty.app!) TerminalSplittableView(ghostty.app!)
.modifier(WindowObservationModifier()) .modifier(WindowObservationModifier())
} }
}.commands { }.commands {

View File

@ -2,7 +2,8 @@ import SwiftUI
import GhosttyKit import GhosttyKit
struct TerminalView: View { struct TerminalView: View {
// The surface to create a view for // The surface to create a view for. This must be created upstream. As long as this
// remains the same, the surface that is being rendered remains the same.
@ObservedObject var surfaceView: TerminalSurfaceView @ObservedObject var surfaceView: TerminalSurfaceView
@FocusState private var surfaceFocus: Bool @FocusState private var surfaceFocus: Bool
@ -17,6 +18,10 @@ struct TerminalView: View {
self.surfaceView = TerminalSurfaceView(app) self.surfaceView = TerminalSurfaceView(app)
} }
init(surface: TerminalSurfaceView) {
self.surfaceView = surface
}
var body: some View { var body: some View {
// We use a GeometryReader to get the frame bounds so that our metal surface // We use a GeometryReader to get the frame bounds so that our metal surface
// is up to date. See TerminalSurfaceView for why we don't use the NSView // is up to date. See TerminalSurfaceView for why we don't use the NSView
@ -46,44 +51,88 @@ struct TerminalSplittableView: View {
@FocusState private var focusedSide: Side? @FocusState private var focusedSide: Side?
let app: ghostty_app_t let app: ghostty_app_t;
@State private var topLeft: TerminalSurfaceView
/// The bottom or right surface. This is private because in a splittable view it is only possible that we set
/// this, because it is triggered from a split event.
@State private var bottomRight: TerminalSurfaceView? = nil
/// Direction of the current split. If this is "nil" then the terminal is not currently split at all. /// Direction of the current split. If this is "nil" then the terminal is not currently split at all.
@State var splitDirection: Direction = .none @State private var splitDirection: Direction = .none
init(_ app: ghostty_app_t) {
self.app = app
_topLeft = State(wrappedValue: TerminalSurfaceView(app))
}
init(_ app: ghostty_app_t, topLeft: TerminalSurfaceView) {
self.app = app
_topLeft = State(wrappedValue: topLeft)
}
func split(to: Direction) {
assert(to != .none)
assert(splitDirection == .none)
splitDirection = to
bottomRight = TerminalSurfaceView(app)
}
func closeTopLeft() {
assert(splitDirection != .none)
assert(bottomRight != nil)
topLeft = bottomRight!
splitDirection = .none
}
func closeBottomRight() {
assert(splitDirection != .none)
assert(bottomRight != nil)
bottomRight = nil
splitDirection = .none
}
var body: some View { var body: some View {
switch (splitDirection) { switch (splitDirection) {
case .none: case .none:
VStack { VStack {
HStack { HStack {
Button("Split Horizontal") { splitDirection = .horizontal } Button("Split Horizontal") { split(to: .horizontal) }
Button("Split Vertical") { splitDirection = .vertical } Button("Split Vertical") { split(to: .vertical) }
} }
TerminalView(app) TerminalView(surface: topLeft)
.focused($focusedSide, equals: .TopLeft) .focused($focusedSide, equals: .TopLeft)
} }
case .horizontal: case .horizontal:
VStack { VStack {
HStack { HStack {
Button("Close Left") { splitDirection = .none } Button("Close Left") { closeTopLeft() }
Button("Close Right") { splitDirection = .none } Button("Close Right") { closeBottomRight() }
} }
HSplitView { HSplitView {
TerminalSplittableView(app: app) TerminalSplittableView(app, topLeft: topLeft)
.focused($focusedSide, equals: .TopLeft) .focused($focusedSide, equals: .TopLeft)
TerminalSplittableView(app: app) TerminalSplittableView(app, topLeft: bottomRight!)
.focused($focusedSide, equals: .BottomRight) .focused($focusedSide, equals: .BottomRight)
} }
} }
case .vertical: case .vertical:
VStack {
HStack {
Button("Close Top") { closeTopLeft() }
Button("Close Bottom") { closeBottomRight() }
}
VSplitView { VSplitView {
TerminalSplittableView(app: app) TerminalSplittableView(app, topLeft: topLeft)
.focused($focusedSide, equals: .TopLeft) .focused($focusedSide, equals: .TopLeft)
TerminalSplittableView(app: app) TerminalSplittableView(app, topLeft: bottomRight!)
.focused($focusedSide, equals: .BottomRight) .focused($focusedSide, equals: .BottomRight)
} }
} }
} }
}
} }