|
| 1 | +// |
| 2 | +// HorizontalSplitView.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import AppKit |
| 7 | +import SwiftUI |
| 8 | + |
| 9 | +struct HorizontalSplitView<Leading: View, Trailing: View>: NSViewRepresentable { |
| 10 | + var isTrailingCollapsed: Bool |
| 11 | + @Binding var trailingWidth: CGFloat |
| 12 | + var minTrailingWidth: CGFloat |
| 13 | + var maxTrailingWidth: CGFloat |
| 14 | + var autosaveName: String |
| 15 | + @ViewBuilder var leading: Leading |
| 16 | + @ViewBuilder var trailing: Trailing |
| 17 | + |
| 18 | + func makeCoordinator() -> Coordinator { |
| 19 | + Coordinator(trailingWidth: $trailingWidth) |
| 20 | + } |
| 21 | + |
| 22 | + func makeNSView(context: Context) -> NSSplitView { |
| 23 | + let splitView = NSSplitView() |
| 24 | + splitView.isVertical = true |
| 25 | + splitView.dividerStyle = .thin |
| 26 | + splitView.autosaveName = autosaveName |
| 27 | + splitView.delegate = context.coordinator |
| 28 | + |
| 29 | + let leadingHosting = NSHostingView(rootView: leading) |
| 30 | + leadingHosting.sizingOptions = [.minSize] |
| 31 | + |
| 32 | + let trailingHosting = NSHostingView(rootView: trailing) |
| 33 | + trailingHosting.sizingOptions = [.minSize] |
| 34 | + |
| 35 | + splitView.addArrangedSubview(leadingHosting) |
| 36 | + splitView.addArrangedSubview(trailingHosting) |
| 37 | + |
| 38 | + context.coordinator.leadingHosting = leadingHosting |
| 39 | + context.coordinator.trailingHosting = trailingHosting |
| 40 | + context.coordinator.lastCollapsedState = isTrailingCollapsed |
| 41 | + context.coordinator.minWidth = minTrailingWidth |
| 42 | + context.coordinator.maxWidth = maxTrailingWidth |
| 43 | + |
| 44 | + if isTrailingCollapsed { |
| 45 | + trailingHosting.isHidden = true |
| 46 | + } |
| 47 | + |
| 48 | + return splitView |
| 49 | + } |
| 50 | + |
| 51 | + func updateNSView(_ splitView: NSSplitView, context: Context) { |
| 52 | + context.coordinator.leadingHosting?.rootView = leading |
| 53 | + context.coordinator.trailingHosting?.rootView = trailing |
| 54 | + context.coordinator.trailingWidth = $trailingWidth |
| 55 | + context.coordinator.minWidth = minTrailingWidth |
| 56 | + context.coordinator.maxWidth = maxTrailingWidth |
| 57 | + |
| 58 | + guard let trailingView = context.coordinator.trailingHosting else { return } |
| 59 | + let wasCollapsed = context.coordinator.lastCollapsedState |
| 60 | + |
| 61 | + if isTrailingCollapsed != wasCollapsed { |
| 62 | + context.coordinator.lastCollapsedState = isTrailingCollapsed |
| 63 | + if isTrailingCollapsed { |
| 64 | + if splitView.subviews.count >= 2 { |
| 65 | + context.coordinator.savedDividerPosition = splitView.subviews[1].frame.width |
| 66 | + } |
| 67 | + splitView.setPosition(splitView.bounds.width, ofDividerAt: 0) |
| 68 | + trailingView.isHidden = true |
| 69 | + } else { |
| 70 | + trailingView.isHidden = false |
| 71 | + let targetWidth = context.coordinator.savedDividerPosition ?? trailingWidth |
| 72 | + splitView.adjustSubviews() |
| 73 | + splitView.setPosition(splitView.bounds.width - targetWidth, ofDividerAt: 0) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + final class Coordinator: NSObject, NSSplitViewDelegate { |
| 79 | + var leadingHosting: NSHostingView<Leading>? |
| 80 | + var trailingHosting: NSHostingView<Trailing>? |
| 81 | + var lastCollapsedState = false |
| 82 | + var savedDividerPosition: CGFloat? |
| 83 | + var minWidth: CGFloat = 0 |
| 84 | + var maxWidth: CGFloat = 0 |
| 85 | + var trailingWidth: Binding<CGFloat> |
| 86 | + |
| 87 | + init(trailingWidth: Binding<CGFloat>) { |
| 88 | + self.trailingWidth = trailingWidth |
| 89 | + } |
| 90 | + |
| 91 | + func splitView( |
| 92 | + _ splitView: NSSplitView, |
| 93 | + constrainMinCoordinate proposedMinimumPosition: CGFloat, |
| 94 | + ofSubviewAt dividerIndex: Int |
| 95 | + ) -> CGFloat { |
| 96 | + splitView.bounds.width - maxWidth |
| 97 | + } |
| 98 | + |
| 99 | + func splitView( |
| 100 | + _ splitView: NSSplitView, |
| 101 | + constrainMaxCoordinate proposedMaximumPosition: CGFloat, |
| 102 | + ofSubviewAt dividerIndex: Int |
| 103 | + ) -> CGFloat { |
| 104 | + splitView.bounds.width - minWidth |
| 105 | + } |
| 106 | + |
| 107 | + func splitView( |
| 108 | + _ splitView: NSSplitView, |
| 109 | + canCollapseSubview subview: NSView |
| 110 | + ) -> Bool { |
| 111 | + subview == trailingHosting |
| 112 | + } |
| 113 | + |
| 114 | + func splitView( |
| 115 | + _ splitView: NSSplitView, |
| 116 | + effectiveRect proposedEffectiveRect: NSRect, |
| 117 | + forDrawnRect drawnRect: NSRect, |
| 118 | + ofDividerAt dividerIndex: Int |
| 119 | + ) -> NSRect { |
| 120 | + if trailingHosting?.isHidden == true { |
| 121 | + return .zero |
| 122 | + } |
| 123 | + return proposedEffectiveRect |
| 124 | + } |
| 125 | + |
| 126 | + func splitView( |
| 127 | + _ splitView: NSSplitView, |
| 128 | + shouldHideDividerAt dividerIndex: Int |
| 129 | + ) -> Bool { |
| 130 | + trailingHosting?.isHidden == true |
| 131 | + } |
| 132 | + |
| 133 | + func splitViewDidResizeSubviews(_ notification: Notification) { |
| 134 | + guard let splitView = notification.object as? NSSplitView, |
| 135 | + splitView.subviews.count >= 2, |
| 136 | + trailingHosting?.isHidden != true |
| 137 | + else { return } |
| 138 | + let width = splitView.subviews[1].frame.width |
| 139 | + guard width > 0, abs(width - trailingWidth.wrappedValue) > 0.5 else { return } |
| 140 | + trailingWidth.wrappedValue = width |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments