Merge pull request #1304 from gpanders/resize-bounds

macos: respect minimum split size when using resize keys
This commit is contained in:
Mitchell Hashimoto
2024-01-15 10:48:40 -08:00
committed by GitHub

View File

@ -20,7 +20,10 @@ struct SplitView<L: View, R: View>: View {
/// The left and right views to render. /// The left and right views to render.
let left: L let left: L
let right: R let right: R
/// The minimum size (in points) of a split
let minSize: CGFloat = 10
/// The current fractional width of the split view. 0.5 means L/R are equally sized, for example. /// The current fractional width of the split view. 0.5 means L/R are equally sized, for example.
@Binding var split: CGFloat @Binding var split: CGFloat
@ -83,22 +86,22 @@ struct SplitView<L: View, R: View>: View {
} }
private func resize(for size: CGSize, amount: Double) { private func resize(for size: CGSize, amount: Double) {
let dim: CGFloat
switch (direction) { switch (direction) {
case .horizontal: case .horizontal:
split += amount / size.width dim = size.width
case .vertical: case .vertical:
split += amount / size.height dim = size.height
} }
// Ensure split is clamped between 0 and 1 let pos = split * dim
split = max(0.0, min(split, 1.0)) let new = min(max(minSize, pos + amount), dim - minSize)
split = new / dim
} }
private func dragGesture(_ size: CGSize, splitterPoint: CGPoint) -> some Gesture { private func dragGesture(_ size: CGSize, splitterPoint: CGPoint) -> some Gesture {
return DragGesture() return DragGesture()
.onChanged { gesture in .onChanged { gesture in
let minSize: CGFloat = 10
switch (direction) { switch (direction) {
case .horizontal: case .horizontal:
let new = min(max(minSize, gesture.location.x), size.width - minSize) let new = min(max(minSize, gesture.location.x), size.width - minSize)