Improves helder
This commit is contained in:
@@ -9,7 +9,6 @@ public struct Splitter: View {
|
||||
var onEnd: () -> Void
|
||||
@State private var isHovered = false
|
||||
@State private var isDragging = false
|
||||
@State private var last: CGFloat = 0
|
||||
|
||||
/// `.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 = {}) {
|
||||
@@ -23,44 +22,117 @@ public struct Splitter: View {
|
||||
.fill(isHovered || isDragging ? Palette.amber : Palette.raised)
|
||||
.frame(width: axis == .vertical ? Metrics.hairline : nil, height: axis == .horizontal ? Metrics.hairline : nil)
|
||||
.overlay {
|
||||
Color.clear
|
||||
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)
|
||||
.contentShape(Rectangle())
|
||||
.onHover(perform: hover)
|
||||
.gesture(drag)
|
||||
}
|
||||
.zIndex(5)
|
||||
.animation(Metrics.smallControlTransition, value: isHovered || isDragging)
|
||||
}
|
||||
}
|
||||
|
||||
private var drag: some Gesture {
|
||||
DragGesture(minimumDistance: 0, coordinateSpace: .global)
|
||||
.onChanged { value in
|
||||
let position = axis == .vertical ? value.translation.width : value.translation.height
|
||||
if !isDragging {
|
||||
isDragging = true
|
||||
last = 0
|
||||
}
|
||||
onDrag(Double(position - last))
|
||||
last = position
|
||||
}
|
||||
.onEnded { _ in
|
||||
isDragging = false
|
||||
last = 0
|
||||
if !isHovered { NSCursor.pop() }
|
||||
onEnd()
|
||||
}
|
||||
/// 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
|
||||
}
|
||||
|
||||
private func hover(_ inside: Bool) {
|
||||
guard inside != isHovered else { return }
|
||||
isHovered = inside
|
||||
if isDragging { return }
|
||||
if inside {
|
||||
(axis == .vertical ? NSCursor.resizeLeftRight : NSCursor.resizeUpDown).push()
|
||||
} else {
|
||||
NSCursor.pop()
|
||||
}
|
||||
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 {
|
||||
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()
|
||||
}
|
||||
|
||||
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))
|
||||
(inside ? cursor : NSCursor.arrow).set()
|
||||
endDrag()
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user