From 1ff0573518ed0cf239e22507c29c527ec420bca8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Nov 2023 09:06:20 -0800 Subject: [PATCH] macos: use normal swiftui parameters for resizable publisher/inc --- .../Ghostty/Ghostty.TerminalSplit.swift | 8 ++- .../Sources/Helpers/SplitView/SplitView.swift | 66 ++++++++----------- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift b/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift index a4869d9b1..4e4b3d39c 100644 --- a/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift +++ b/macos/Sources/Ghostty/Ghostty.TerminalSplit.swift @@ -277,7 +277,11 @@ extension Ghostty { @State private var resizeIncrements: NSSize = .init(width: 1.0, height: 1.0) var body: some View { - SplitView(container.direction, left: { + SplitView( + container.direction, + resizeIncrements: resizeIncrements, + resizePublisher: container.resizeEvent, + left: { let neighborKey: WritableKeyPath = container.direction == .horizontal ? \.right : \.bottom TerminalSplitNested( @@ -303,8 +307,6 @@ extension Ghostty { guard ghostty.windowStepResize else { return } self.resizeIncrements = increments } - .resizeIncrements(resizeIncrements) - .resizePublisher(container.resizeEvent) } private func closeableTopLeft() -> Binding { diff --git a/macos/Sources/Helpers/SplitView/SplitView.swift b/macos/Sources/Helpers/SplitView/SplitView.swift index 79c8ed91c..3dd0f8129 100644 --- a/macos/Sources/Helpers/SplitView/SplitView.swift +++ b/macos/Sources/Helpers/SplitView/SplitView.swift @@ -10,6 +10,13 @@ struct SplitView: View { /// Direction of the split let direction: SplitViewDirection + /// If set, the split view supports programmatic resizing via events sent via the publisher. + /// Minimum increment (in points) that this split can be resized by, in + /// each direction. Both `height` and `width` should be whole numbers + /// greater than or equal to 1.0 + let resizeIncrements: NSSize + let resizePublisher: PassthroughSubject + /// The left and right views to render. let left: L let right: R @@ -17,13 +24,6 @@ struct SplitView: View { /// The current fractional width of the split view. 0.5 means L/R are equally sized, for example. @State var split: CGFloat = 0.5 - /// Minimum increment (in points) that this split can be resized by, in - /// each direction. Both `height` and `width` should be whole numbers - /// greater than or equal to 1.0 - @Environment(\.resizeIncrements) var resizeIncrements - - @Environment(\.resizePublisher) var resizePublisher - /// The visible size of the splitter, in points. The invisible size is a transparent hitbox that can still /// be used for getting a resize handle. The total width/height of the splitter is the sum of both. private let splitterVisibleSize: CGFloat = 1 @@ -52,8 +52,29 @@ struct SplitView: View { } } + /// Initialize a split view. This view isn't programmatically resizable; it can only be resized + /// by manually dragging the divider. init(_ direction: SplitViewDirection, @ViewBuilder left: (() -> L), @ViewBuilder right: (() -> R)) { + self.init( + direction, + resizeIncrements: .init(width: 1, height: 1), + resizePublisher: .init(), + left: left, + right: right + ) + } + + /// Initialize a split view that supports programmatic resizing. + init( + _ direction: SplitViewDirection, + resizeIncrements: NSSize, + resizePublisher: PassthroughSubject, + @ViewBuilder left: (() -> L), + @ViewBuilder right: (() -> R) + ) { self.direction = direction + self.resizeIncrements = resizeIncrements + self.resizePublisher = resizePublisher self.left = left() self.right = right() } @@ -142,34 +163,3 @@ struct SplitView: View { enum SplitViewDirection { case horizontal, vertical } - -private struct ResizeIncrementsKey: EnvironmentKey { - static let defaultValue: CGSize = .init(width: 1.0, height: 1.0) -} - -private struct ResizePublisherKey: EnvironmentKey { - static let defaultValue: PassthroughSubject = .init() -} - -extension EnvironmentValues { - /// An environment value that specifies the resize increments of a resizable view - var resizeIncrements: CGSize { - get { self[ResizeIncrementsKey.self] } - set { self[ResizeIncrementsKey.self] = newValue } - } - - var resizePublisher: PassthroughSubject { - get { self[ResizePublisherKey.self] } - set { self[ResizePublisherKey.self] = newValue } - } -} - -extension View { - func resizeIncrements(_ increments: CGSize) -> some View { - environment(\.resizeIncrements, increments) - } - - func resizePublisher(_ publisher: PassthroughSubject) -> some View { - environment(\.resizePublisher, publisher) - } -}