macos: use normal swiftui parameters for resizable publisher/inc

This commit is contained in:
Mitchell Hashimoto
2023-11-06 09:06:20 -08:00
parent 582df33083
commit 1ff0573518
2 changed files with 33 additions and 41 deletions

View File

@ -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<SplitNode.Neighbors, SplitNode?> = 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<SplitNode?> {

View File

@ -10,6 +10,13 @@ struct SplitView<L: View, R: View>: 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<Double, Never>
/// The left and right views to render.
let left: L
let right: R
@ -17,13 +24,6 @@ struct SplitView<L: View, R: View>: 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<L: View, R: View>: 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<Double, Never>,
@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<L: View, R: View>: 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<Double, Never> = .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<Double, Never> {
get { self[ResizePublisherKey.self] }
set { self[ResizePublisherKey.self] = newValue }
}
}
extension View {
func resizeIncrements(_ increments: CGSize) -> some View {
environment(\.resizeIncrements, increments)
}
func resizePublisher(_ publisher: PassthroughSubject<Double, Never>) -> some View {
environment(\.resizePublisher, publisher)
}
}