147 lines
4.8 KiB
Swift
147 lines
4.8 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
/// A 1pt hairline divider that resizes what is next to it. The pointer target reaches 3pt past the line
|
|
/// on both sides; hover and drag turn the line amber. `onDrag` receives the movement since the last call.
|
|
public struct Splitter: View {
|
|
var axis: Axis
|
|
var onDrag: (Double) -> Void
|
|
var onEnd: () -> Void
|
|
@State private var isHovered = false
|
|
@State private var isDragging = false
|
|
|
|
/// `.vertical` is a column divider dragged left and right; `.horizontal` a row divider dragged up and down.
|
|
public init(_ axis: Axis = .vertical, onDrag: @escaping (Double) -> Void, onEnd: @escaping () -> Void = {}) {
|
|
self.axis = axis
|
|
self.onDrag = onDrag
|
|
self.onEnd = onEnd
|
|
}
|
|
|
|
public var body: some View {
|
|
Rectangle()
|
|
.fill(isHovered || isDragging ? Palette.amber : Palette.raised)
|
|
.frame(width: axis == .vertical ? Metrics.hairline : nil, height: axis == .horizontal ? Metrics.hairline : nil)
|
|
.overlay {
|
|
SplitterHandle(axis: axis, onHover: { isHovered = $0 }, onDragging: { isDragging = $0 }, onDrag: onDrag, onEnd: onEnd)
|
|
.frame(width: axis == .vertical ? Metrics.splitterHitArea : nil,
|
|
height: axis == .horizontal ? Metrics.splitterHitArea : nil)
|
|
}
|
|
.zIndex(5)
|
|
.animation(Metrics.smallControlTransition, value: isHovered || isDragging)
|
|
}
|
|
}
|
|
|
|
/// The splitter's hit area as an AppKit view, so it owns its resize cursor instead of sharing a cursor stack.
|
|
private struct SplitterHandle: NSViewRepresentable {
|
|
var axis: Axis
|
|
var onHover: (Bool) -> Void
|
|
var onDragging: (Bool) -> Void
|
|
var onDrag: (Double) -> Void
|
|
var onEnd: () -> Void
|
|
|
|
func makeNSView(context: Context) -> SplitterHandleView {
|
|
let view = SplitterHandleView()
|
|
updateNSView(view, context: context)
|
|
return view
|
|
}
|
|
|
|
func updateNSView(_ view: SplitterHandleView, context: Context) {
|
|
view.axis = axis
|
|
view.onHover = onHover
|
|
view.onDragging = onDragging
|
|
view.onDrag = onDrag
|
|
view.onEnd = onEnd
|
|
}
|
|
}
|
|
|
|
final class SplitterHandleView: NSView, PointerOwner {
|
|
var axis: Axis = .vertical {
|
|
didSet { if axis != oldValue { window?.invalidateCursorRects(for: self) } }
|
|
}
|
|
var onHover: (Bool) -> Void = { _ in }
|
|
var onDragging: (Bool) -> Void = { _ in }
|
|
var onDrag: (Double) -> Void = { _ in }
|
|
var onEnd: () -> Void = {}
|
|
private var dragStart: NSPoint?
|
|
private var last: CGFloat = 0
|
|
|
|
private var cursor: NSCursor {
|
|
axis == .vertical ? .resizeLeftRight : .resizeUpDown
|
|
}
|
|
|
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
|
|
override func resetCursorRects() {
|
|
addCursorRect(bounds, cursor: cursor)
|
|
}
|
|
|
|
override func updateTrackingAreas() {
|
|
super.updateTrackingAreas()
|
|
trackingAreas.forEach(removeTrackingArea)
|
|
addTrackingArea(NSTrackingArea(rect: .zero, options: [.mouseEnteredAndExited, .cursorUpdate, .activeInActiveApp, .inVisibleRect],
|
|
owner: self))
|
|
}
|
|
|
|
override func cursorUpdate(with event: NSEvent) {
|
|
cursor.set()
|
|
}
|
|
|
|
func updatePointer(atWindowPoint point: NSPoint) {
|
|
cursor.set()
|
|
}
|
|
|
|
override func mouseEntered(with event: NSEvent) {
|
|
onHover(true)
|
|
cursor.set()
|
|
}
|
|
|
|
override func mouseExited(with event: NSEvent) {
|
|
guard dragStart == nil else { return }
|
|
onHover(false)
|
|
}
|
|
|
|
/// Cursor rects stay off for the whole drag: the columns resize under the pointer and would rebuild their I-beam.
|
|
override func mouseDown(with event: NSEvent) {
|
|
dragStart = event.locationInWindow
|
|
last = 0
|
|
window?.disableCursorRects()
|
|
cursor.set()
|
|
onDragging(true)
|
|
}
|
|
|
|
override func mouseDragged(with event: NSEvent) {
|
|
guard let dragStart else { return }
|
|
cursor.set()
|
|
let point = event.locationInWindow
|
|
let position = axis == .vertical ? point.x - dragStart.x : dragStart.y - point.y
|
|
onDrag(Double(position - last))
|
|
last = position
|
|
}
|
|
|
|
override func mouseUp(with event: NSEvent) {
|
|
guard dragStart != nil else { return }
|
|
let inside = bounds.contains(convert(event.locationInWindow, from: nil))
|
|
endDrag()
|
|
if inside {
|
|
cursor.set()
|
|
} else if let window {
|
|
PointerPolicy.updateCursor(in: window)
|
|
}
|
|
onHover(inside)
|
|
onEnd()
|
|
}
|
|
|
|
override func viewWillMove(toWindow newWindow: NSWindow?) {
|
|
if dragStart != nil { endDrag() }
|
|
super.viewWillMove(toWindow: newWindow)
|
|
}
|
|
|
|
private func endDrag() {
|
|
dragStart = nil
|
|
last = 0
|
|
onDragging(false)
|
|
window?.enableCursorRects()
|
|
window?.resetCursorRects()
|
|
}
|
|
}
|