Repository Public License
1import AppKit2import SwiftUI34/// Two panes with a draggable divider, implemented purely in SwiftUI.5///6/// SwiftUI's `HSplitView`/`VSplitView` are backed by `NSSplitView` and derive Auto Layout limits from their children.7/// With content whose size limits change during layout (a fixed-width card next to a growing table was enough), AppKit8/// aborts the process with "more Update Constraints in Window passes than there are views" — the PRISM 0.1.0 crash of9/// 2026-11-22. This view takes all the space it is offered and never reports size limits upward, so it cannot do that.10public enum SplitInitial: Sendable, Equatable {11 case fraction(CGFloat)12 /// A fixed starting length for the first pane, in points.13 case first(CGFloat)14 /// A fixed starting length for the second pane, in points.15 case second(CGFloat)16}1718public struct ResizableSplit<First: View, Second: View>: View {19 public typealias Initial = SplitInitial2021 let vertical: Bool22 let initial: Initial23 let minFirst: CGFloat24 let minSecond: CGFloat25 let first: First26 let second: Second27 /// The first pane's length once the user has dragged; nil until then, so the layout follows `initial` on resize.28 @State private var dragged: CGFloat?29 @State private var dragStart: CGFloat?30 @State private var hovering = false3132 public init(vertical: Bool = false, initial: Initial = .fraction(0.5), minFirst: CGFloat = 120, minSecond: CGFloat = 120,33 @ViewBuilder first: () -> First, @ViewBuilder second: () -> Second) {34 self.vertical = vertical; self.initial = initial; self.minFirst = minFirst; self.minSecond = minSecond35 self.first = first(); self.second = second()36 }3738 nonisolated static var handle: CGFloat { SplitMetrics.handle }3940 /// The first pane's length for a given total, clamped so both panes keep their minimum when there is room.41 public nonisolated static func firstLength(total: CGFloat, initial: SplitInitial, dragged: CGFloat?, minFirst: CGFloat, minSecond: CGFloat) -> CGFloat {42 let available = max(total - handle, 0)43 var length: CGFloat44 if let dragged { length = dragged } else {45 switch initial {46 case .fraction(let f): length = available * min(max(f, 0.05), 0.95)47 case .first(let l): length = l48 case .second(let l): length = available - l49 }50 }51 let upper = max(available - minSecond, 0)52 let lower = min(minFirst, upper)53 return min(max(length, lower), upper)54 }5556 public var body: some View {57 GeometryReader { geo in58 let total = vertical ? geo.size.height : geo.size.width59 let a = Self.firstLength(total: total, initial: initial, dragged: dragged, minFirst: minFirst, minSecond: minSecond)60 let b = max(total - Self.handle - a, 0)61 let layout = vertical ? AnyLayout(VStackLayout(spacing: 0)) : AnyLayout(HStackLayout(spacing: 0))62 layout {63 first64 .frame(width: vertical ? geo.size.width : a, height: vertical ? a : geo.size.height, alignment: .topLeading)65 .clipped()66 divider(total: total, current: a)67 second68 .frame(width: vertical ? geo.size.width : b, height: vertical ? b : geo.size.height, alignment: .topLeading)69 .clipped()70 }71 }72 }7374 private func divider(total: CGFloat, current: CGFloat) -> some View {75 ZStack {76 Color.clear77 Rectangle().fill(hovering || dragStart != nil ? Color.accentColor.opacity(0.6) : Color(nsColor: .separatorColor))78 .frame(width: vertical ? nil : 1, height: vertical ? 1 : nil)79 }80 .frame(width: vertical ? nil : Self.handle, height: vertical ? Self.handle : nil)81 .contentShape(Rectangle())82 .onHover { inside in83 hovering = inside84 if inside { (vertical ? NSCursor.resizeUpDown : NSCursor.resizeLeftRight).push() } else { NSCursor.pop() }85 }86 .gesture(DragGesture(minimumDistance: 1, coordinateSpace: .global)87 .onChanged { value in88 let start = dragStart ?? current89 dragStart = start90 let delta = vertical ? value.translation.height : value.translation.width91 dragged = Self.firstLength(total: total, initial: initial, dragged: start + delta, minFirst: minFirst, minSecond: minSecond)92 }93 .onEnded { _ in dragStart = nil })94 .accessibilityElement()95 .accessibilityLabel("Pane divider")96 .accessibilityValue("\(Int((total > 0 ? current / total : 0) * 100)) percent")97 .accessibilityAdjustableAction { direction in98 let step = total * 0.05 * (direction == .increment ? 1 : -1)99 dragged = Self.firstLength(total: total, initial: initial, dragged: current + step, minFirst: minFirst, minSecond: minSecond)100 }101 }102}103104public enum SplitMetrics {105 public static let handle: CGFloat = 9106}107108/// Hosts content at exactly the offered size and reports no size limits of its own to the window.109/// Used around every running app so that no generated layout can drive the enclosing window's constraints.110public struct SizeIsolated<Content: View>: View {111 let content: Content112 public init(@ViewBuilder _ content: () -> Content) { self.content = content() }113114 public var body: some View {115 GeometryReader { geo in116 content.frame(width: geo.size.width, height: geo.size.height, alignment: .topLeading).clipped()117 }118 }119}120