Repository Public
1import Foundation2import CoreGraphics34/// A divider between two children of a split, located in the same coordinate space as `frames(in:)`.5public struct DividerGeometry: Sendable, Hashable {6 public let splitID: SplitID7 public let dividerIndex: Int8 public let axis: SplitAxis9 /// The line of the divider; `frame` has zero thickness along the axis direction.10 public let frame: CGRect11 /// Total extent of the parent split along its axis, used to convert pixel deltas to fractions.12 public let splitExtent: CGFloat13}1415extension LayoutNode {16 /// Lays out the tree in `bounds` (top-left origin, y grows downward) and returns the frame of each pane.17 public func frames(in bounds: CGRect, dividerThickness: CGFloat = 0) -> [PaneID: CGRect] {18 var result: [PaneID: CGRect] = [:]19 var dividers: [DividerGeometry] = []20 layout(in: bounds, dividerThickness: dividerThickness, frames: &result, dividers: ÷rs)21 return result22 }2324 /// Lays out the tree and returns every divider, for drag-to-resize hit testing.25 public func dividers(in bounds: CGRect, dividerThickness: CGFloat = 0) -> [DividerGeometry] {26 var result: [PaneID: CGRect] = [:]27 var dividers: [DividerGeometry] = []28 layout(in: bounds, dividerThickness: dividerThickness, frames: &result, dividers: ÷rs)29 return dividers30 }3132 private func layout(in bounds: CGRect, dividerThickness: CGFloat, frames: inout [PaneID: CGRect], dividers: inout [DividerGeometry]) {33 switch self {34 case .leaf(let id):35 frames[id] = bounds36 case .split(let sid, let axis, let children, let fractions):37 let count = CGFloat(children.count)38 let extent = axis == .horizontal ? bounds.width : bounds.height39 let usable = max(0, extent - dividerThickness * (count - 1))40 var cursor: CGFloat = axis == .horizontal ? bounds.minX : bounds.minY41 for (index, (child, fraction)) in zip(children, fractions).enumerated() {42 let size = usable * CGFloat(fraction)43 let childBounds: CGRect = axis == .horizontal44 ? CGRect(x: cursor, y: bounds.minY, width: size, height: bounds.height)45 : CGRect(x: bounds.minX, y: cursor, width: bounds.width, height: size)46 child.layout(in: childBounds, dividerThickness: dividerThickness, frames: &frames, dividers: ÷rs)47 cursor += size48 if index < children.count - 1 {49 let dividerFrame: CGRect = axis == .horizontal50 ? CGRect(x: cursor, y: bounds.minY, width: dividerThickness, height: bounds.height)51 : CGRect(x: bounds.minX, y: cursor, width: bounds.width, height: dividerThickness)52 dividers.append(DividerGeometry(splitID: sid, dividerIndex: index, axis: axis, frame: dividerFrame, splitExtent: usable))53 cursor += dividerThickness54 }55 }56 }57 }5859 /// Finds the pane geometrically adjacent to `pane` in `direction`, preferring the candidate whose60 /// centre is closest to the origin pane's centre on the perpendicular axis.61 public func neighbor(of pane: PaneID, direction: NavigationDirection) -> PaneID? {62 let frames = self.frames(in: CGRect(x: 0, y: 0, width: 1000, height: 1000))63 guard let origin = frames[pane] else { return nil }64 let tolerance: CGFloat = 0.565 var candidates: [(id: PaneID, distance: CGFloat, order: CGFloat)] = []66 for (id, frame) in frames where id != pane {67 let adjacent: Bool68 let perpendicularDistance: CGFloat69 let order: CGFloat70 switch direction {71 case .left:72 adjacent = abs(frame.maxX - origin.minX) < tolerance && frame.maxY > origin.minY && frame.minY < origin.maxY73 perpendicularDistance = abs(frame.midY - origin.midY); order = frame.minY74 case .right:75 adjacent = abs(frame.minX - origin.maxX) < tolerance && frame.maxY > origin.minY && frame.minY < origin.maxY76 perpendicularDistance = abs(frame.midY - origin.midY); order = frame.minY77 case .up:78 adjacent = abs(frame.maxY - origin.minY) < tolerance && frame.maxX > origin.minX && frame.minX < origin.maxX79 perpendicularDistance = abs(frame.midX - origin.midX); order = frame.minX80 case .down:81 adjacent = abs(frame.minY - origin.maxY) < tolerance && frame.maxX > origin.minX && frame.minX < origin.maxX82 perpendicularDistance = abs(frame.midX - origin.midX); order = frame.minX83 }84 if adjacent { candidates.append((id, perpendicularDistance, order)) }85 }86 // Deterministic: closest centre first, then top-most / left-most.87 return candidates.min { lhs, rhs in88 if abs(lhs.distance - rhs.distance) > 0.5 { return lhs.distance < rhs.distance }89 return lhs.order < rhs.order90 }?.id91 }92}93