mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
macos: working on splits
This commit is contained in:
@ -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 {
|
||||||
|
@ -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,43 +51,87 @@ 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:
|
||||||
VSplitView {
|
VStack {
|
||||||
TerminalSplittableView(app: app)
|
HStack {
|
||||||
.focused($focusedSide, equals: .TopLeft)
|
Button("Close Top") { closeTopLeft() }
|
||||||
TerminalSplittableView(app: app)
|
Button("Close Bottom") { closeBottomRight() }
|
||||||
.focused($focusedSide, equals: .BottomRight)
|
}
|
||||||
|
|
||||||
|
VSplitView {
|
||||||
|
TerminalSplittableView(app, topLeft: topLeft)
|
||||||
|
.focused($focusedSide, equals: .TopLeft)
|
||||||
|
TerminalSplittableView(app, topLeft: bottomRight!)
|
||||||
|
.focused($focusedSide, equals: .BottomRight)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user