cursor
This commit is contained in:
@@ -72,6 +72,7 @@ private struct GroupButton: View {
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.onHover { isHovered = $0 }
|
||||
.accessibilityAddTraits(isOn ? .isSelected : [])
|
||||
.animation(Metrics.smallControlTransition, value: isHovered)
|
||||
@@ -122,6 +123,7 @@ public struct ToolbarIconButton: View {
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.onHover { isHovered = $0 }
|
||||
.animation(Metrics.smallControlTransition, value: isHovered)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public struct PrimaryButtonStyle: ButtonStyle {
|
||||
.padding(.vertical, 6)
|
||||
.background(destructive ? Palette.amberDeep : Palette.amber, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +43,7 @@ private struct SecondaryButtonBody: View {
|
||||
.background(isHovered || configuration.isPressed ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
.onHover { isHovered = $0 }
|
||||
.animation(Metrics.smallControlTransition, value: isHovered)
|
||||
}
|
||||
@@ -84,6 +86,8 @@ extension View {
|
||||
|
||||
/// Disabled is 40% opacity and nothing else: no recolouring.
|
||||
public func disabledLook(_ isDisabled: Bool) -> some View {
|
||||
opacity(isDisabled ? 0.4 : 1).allowsHitTesting(!isDisabled)
|
||||
opacity(isDisabled ? 0.4 : 1)
|
||||
.allowsHitTesting(!isDisabled)
|
||||
.transformEnvironment(\.allowsPointerHand) { if isDisabled { $0 = false } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ public struct ContextMenuRow: View {
|
||||
Rectangle().fill(isHighlighted ? Palette.amber : .clear).frame(width: Metrics.accentRule)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
}
|
||||
|
||||
private var labelColor: Color {
|
||||
|
||||
@@ -49,7 +49,7 @@ class PointerBoundTextView: NSTextView, PointerTextTarget {
|
||||
override func mouseEntered(with event: NSEvent) {
|
||||
if allowsIBeamRect { super.mouseEntered(with: event) }
|
||||
guard let window, !PointerPolicy.isSelecting else { return }
|
||||
PointerPolicy.updateCursor(in: window)
|
||||
PointerPolicy.scheduleUpdate(in: window)
|
||||
}
|
||||
|
||||
/// An update meant for another view is handed to the view under the mouse.
|
||||
@@ -58,7 +58,7 @@ class PointerBoundTextView: NSTextView, PointerTextTarget {
|
||||
if PointerPolicy.isSelecting || isTopmost(atWindowPoint: point) {
|
||||
updatePointer(atWindowPoint: point)
|
||||
} else {
|
||||
PointerPolicy.updateCursor(in: window)
|
||||
PointerPolicy.scheduleUpdate(in: window)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// The two pointers the rule picks between. A view may add its own extra on top: ⌘'s pointing hand, a splitter's resize.
|
||||
/// The pointers the rule picks between. A view may add its own extra on top: ⌘'s pointing hand, a splitter's resize.
|
||||
enum PointerShape: Equatable {
|
||||
case iBeam
|
||||
case arrow
|
||||
case pointingHand
|
||||
|
||||
var cursor: NSCursor { self == .iBeam ? .iBeam : .arrow }
|
||||
var cursor: NSCursor {
|
||||
switch self {
|
||||
case .iBeam: .iBeam
|
||||
case .arrow: .arrow
|
||||
case .pointingHand: .pointingHand
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a text-capable view lives: in a column, or in the open overlay.
|
||||
enum PointerLayer: Equatable {
|
||||
/// Where a text-capable or clickable view lives: in a column, or in the open overlay.
|
||||
enum PointerLayer: Equatable, Sendable {
|
||||
case workspace
|
||||
case overlay
|
||||
}
|
||||
@@ -27,12 +34,25 @@ struct PointerState: Equatable {
|
||||
var isSelecting: Bool
|
||||
}
|
||||
|
||||
/// The one pointer rule: the I-beam only where a single click places a caret or starts a text selection.
|
||||
/// The one pointer rule: the I-beam only where a single click places a caret or starts a text selection,
|
||||
/// the pointing hand only where a left click runs an action, the arrow everywhere else.
|
||||
enum PointerPolicy {
|
||||
static func shape(_ state: PointerState) -> PointerShape {
|
||||
if state.isSelecting { return .iBeam }
|
||||
guard state.isEnabled, state.isOverText else { return .arrow }
|
||||
return state.overlayOpen && state.layer == .workspace ? .arrow : .iBeam
|
||||
guard state.isEnabled, state.isOverText, !isBehindOverlay(state.layer, overlayOpen: state.overlayOpen) else { return .arrow }
|
||||
return .iBeam
|
||||
}
|
||||
|
||||
/// The pointer over a view that runs an action on a left click: a button, a clickable row.
|
||||
static func shape(forClickable layer: PointerLayer, overlayOpen: Bool, isEnabled: Bool, isSelecting: Bool = false) -> PointerShape {
|
||||
if isSelecting { return .iBeam }
|
||||
guard isEnabled, !isBehindOverlay(layer, overlayOpen: overlayOpen) else { return .arrow }
|
||||
return .pointingHand
|
||||
}
|
||||
|
||||
/// An open overlay takes the pointer from every column behind it.
|
||||
static func isBehindOverlay(_ layer: PointerLayer, overlayOpen: Bool) -> Bool {
|
||||
overlayOpen && layer == .workspace
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +95,7 @@ extension PointerTextTarget {
|
||||
}
|
||||
|
||||
func addPointerCursorRect(_ rect: NSRect) {
|
||||
guard allowsIBeamRect else { return }
|
||||
guard rect.isUsableCursorRect, allowsIBeamRect else { return }
|
||||
addCursorRect(rect, cursor: .iBeam)
|
||||
}
|
||||
}
|
||||
@@ -83,7 +103,7 @@ extension PointerTextTarget {
|
||||
extension NSView {
|
||||
/// The view a click at the window point would reach, when the point lies in this view's visible part.
|
||||
func hitView(atWindowPoint point: NSPoint) -> NSView? {
|
||||
guard let window, let content = window.contentView, visibleRect.contains(convert(point, from: nil)) else { return nil }
|
||||
guard let window, let content = window.contentView, visibleBounds.contains(convert(point, from: nil)) else { return nil }
|
||||
return content.hitTest(content.superview?.convert(point, from: nil) ?? point)
|
||||
}
|
||||
|
||||
@@ -93,6 +113,13 @@ extension NSView {
|
||||
hitView(atWindowPoint: point)?.isDescendant(of: self) ?? false
|
||||
}
|
||||
|
||||
/// The visible rect within the view's own bounds. SwiftUI's platform-view host does not clip,
|
||||
/// so a hosted view's visible rect alone can span the whole window.
|
||||
var visibleBounds: NSRect {
|
||||
let rect = visibleRect.intersection(bounds)
|
||||
return rect.isUsableCursorRect ? rect : .zero
|
||||
}
|
||||
|
||||
/// The visible rect minus the enclosing scroll view's scrollers and line-number ruler, which keep the arrow.
|
||||
/// The clip view runs under a visible ruler, so the visible rect alone would give the gutter the I-beam.
|
||||
var visibleRectWithoutScrollers: NSRect {
|
||||
@@ -100,15 +127,42 @@ extension NSView {
|
||||
let ruler: NSView? = scroll?.rulersVisible == true ? scroll?.verticalRulerView : nil
|
||||
let strips: [NSView?] = [scroll?.verticalScroller, scroll?.horizontalScroller, ruler]
|
||||
return strips.compactMap { $0 }.filter { !$0.isHidden && $0.superview != nil }
|
||||
.reduce(visibleRect) { $0.removing(convert($1.bounds, from: $1)) }
|
||||
.reduce(visibleBounds) { $0.removing(convert($1.bounds, from: $1)) }
|
||||
}
|
||||
}
|
||||
|
||||
extension NSView {
|
||||
fileprivate var enclosingClipView: NSClipView? {
|
||||
var view = superview
|
||||
while let current = view {
|
||||
if let clip = current as? NSClipView { return clip }
|
||||
view = current.superview
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
fileprivate var isInScroller: Bool {
|
||||
var view: NSView? = self
|
||||
while let current = view {
|
||||
if current is NSScroller { return true }
|
||||
view = current.superview
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
extension NSRect {
|
||||
/// False for an empty rect and for the null rect a missed intersection gives, whose origin is infinite;
|
||||
/// `addCursorRect` raises on a non-finite rect.
|
||||
var isUsableCursorRect: Bool {
|
||||
!isNull && !isEmpty && origin.x.isFinite && origin.y.isFinite && size.width.isFinite && size.height.isFinite
|
||||
}
|
||||
|
||||
/// The rect minus a strip that spans its full height or width along one edge, such as a scroller.
|
||||
func removing(_ strip: NSRect) -> NSRect {
|
||||
guard isUsableCursorRect else { return .zero }
|
||||
let overlap = intersection(strip)
|
||||
guard !overlap.isEmpty else { return self }
|
||||
guard overlap.isUsableCursorRect else { return self }
|
||||
var rest = self
|
||||
if overlap.height >= height {
|
||||
rest.origin.x = overlap.minX > minX ? minX : overlap.maxX
|
||||
@@ -139,7 +193,7 @@ extension PointerPolicy {
|
||||
|
||||
static func endSelection(in window: NSWindow?) {
|
||||
selectionDrag = false
|
||||
if let window { updateCursor(in: window) }
|
||||
scheduleUpdate(in: window)
|
||||
}
|
||||
|
||||
static func isOverlayOpen(in window: NSWindow?) -> Bool {
|
||||
@@ -170,8 +224,39 @@ extension PointerPolicy {
|
||||
updateCursor(in: window)
|
||||
}
|
||||
|
||||
/// Asks the view under the mouse for its pointer; anything that is not text gets the arrow.
|
||||
/// Views call this from `cursorUpdate`, so a nested call returns at once.
|
||||
private static var cursorUpdateMonitor: Any?
|
||||
private static var pendingUpdate: Set<Int> = []
|
||||
|
||||
/// Answers every cursor update and mouse move in a key window through `scheduleUpdate`. It swallows the cursor update:
|
||||
/// AppKit hands it to the hit view, and over a region view, which takes no clicks, the hosting view sets the arrow.
|
||||
/// Mouse moves pass on untouched; they cover views whose tracking areas AppKit never fires, such as an overlay's field.
|
||||
static func installCursorUpdateMonitor() {
|
||||
guard cursorUpdateMonitor == nil else { return }
|
||||
cursorUpdateMonitor = NSEvent.addLocalMonitorForEvents(matching: [.cursorUpdate, .mouseMoved]) { event in
|
||||
let swallow = MainActor.assumeIsolated {
|
||||
guard let window = event.window, window.isKeyWindow, window.areCursorRectsEnabled, !isSelecting else { return false }
|
||||
scheduleUpdate(in: window)
|
||||
return event.type == .cursorUpdate
|
||||
}
|
||||
return swallow ? nil : event
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `updateCursor` once, after the current event. Inside an event SwiftUI's hit test returns its root view,
|
||||
/// not the view under the mouse. It never resets the cursor rects: that would fire new cursor updates.
|
||||
static func scheduleUpdate(in window: NSWindow?) {
|
||||
guard let number = window?.windowNumber, pendingUpdate.insert(number).inserted else { return }
|
||||
DispatchQueue.main.async {
|
||||
MainActor.assumeIsolated {
|
||||
pendingUpdate.remove(number)
|
||||
guard let window = NSApp.window(withWindowNumber: number) else { return }
|
||||
updateCursor(in: window)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Asks the view under the mouse for its pointer; anything that is not text or clickable gets the arrow.
|
||||
/// Call it outside an event (`scheduleUpdate`); a nested call returns at once.
|
||||
static func updateCursor(in window: NSWindow) {
|
||||
guard !isUpdatingCursor, window.isKeyWindow, window.areCursorRectsEnabled, !isSelecting,
|
||||
let content = window.contentView else { return }
|
||||
@@ -180,56 +265,93 @@ extension PointerPolicy {
|
||||
let point = window.mouseLocationOutsideOfEventStream
|
||||
guard content.bounds.contains(content.convert(point, from: nil)),
|
||||
NSWindow.windowNumber(at: NSEvent.mouseLocation, belowWindowWithWindowNumber: 0) == window.windowNumber else { return }
|
||||
let overlayOpen = isOverlayOpen(in: window)
|
||||
let hit = content.hitTest(content.superview?.convert(point, from: nil) ?? point)
|
||||
var view = hit
|
||||
while let current = view {
|
||||
if let owner = current as? PointerOwner {
|
||||
owner.updatePointer(atWindowPoint: point)
|
||||
return
|
||||
// A column's text view under the open overlay yields to the overlay's own regions.
|
||||
let behind = (owner as? PointerTextTarget).map { isBehindOverlay($0.pointerLayer, overlayOpen: overlayOpen) } ?? false
|
||||
if !behind {
|
||||
owner.updatePointer(atWindowPoint: point)
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
view = current.superview
|
||||
}
|
||||
if let hit, let region = textRegion(in: content, at: point, under: hit) {
|
||||
if let hit, let region = region(in: content, at: point, under: hit, overlayOpen: overlayOpen) {
|
||||
region.updatePointer(atWindowPoint: point)
|
||||
} else {
|
||||
NSCursor.arrow.set()
|
||||
}
|
||||
}
|
||||
|
||||
/// A region counts only when the click would land on its own hosting or scroll view, not on a scroller or another view.
|
||||
private static func textRegion(in view: NSView, at point: NSPoint, under hit: NSView) -> PointerTextRegionView? {
|
||||
if let region = view as? PointerTextRegionView, !region.isHiddenOrHasHiddenAncestor, region.isDescendant(of: hit),
|
||||
region.isPointerOverText(atWindowPoint: point) { return region }
|
||||
for subview in view.subviews {
|
||||
if let region = textRegion(in: subview, at: point, under: hit) { return region }
|
||||
/// The topmost region under the point; with an overlay open, the overlay's regions come first.
|
||||
/// A column region counts when the hit view lies in its scroll view or in none, and never over a scroller.
|
||||
private static func region(in content: NSView, at point: NSPoint, under hit: NSView, overlayOpen: Bool) -> PointerRegionView? {
|
||||
guard !hit.isInScroller else { return nil }
|
||||
var found: [PointerRegionView] = []
|
||||
collectRegions(in: content, at: point, into: &found)
|
||||
// SwiftUI spreads a scroll view's platform views over several containers, and while it rebuilds a row
|
||||
// the hit view is its root graphics view, so a region need not sit under the hit view.
|
||||
let clip = hit.enclosingClipView
|
||||
let reachable = found.filter { region in
|
||||
clip == nil || region.enclosingClipView === clip || (overlayOpen && region.pointerLayer == .overlay)
|
||||
}
|
||||
let overlayRegions = overlayOpen ? reachable.filter { $0.pointerLayer == .overlay } : []
|
||||
return (overlayRegions.isEmpty ? reachable : overlayRegions).last
|
||||
}
|
||||
|
||||
/// Skips hidden subtrees only: SwiftUI's hosts do not clip, so a region may lie outside its ancestors' frames.
|
||||
private static func collectRegions(in view: NSView, at point: NSPoint, into found: inout [PointerRegionView]) {
|
||||
guard !view.isHidden else { return }
|
||||
if let region = view as? PointerRegionView, region.isPointerOverRegion(atWindowPoint: point) {
|
||||
found.append(region)
|
||||
}
|
||||
for subview in view.subviews {
|
||||
collectRegions(in: subview, at: point, into: &found)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI text
|
||||
// MARK: - SwiftUI regions
|
||||
|
||||
/// Gives SwiftUI text that selects on click, such as the Markdown preview, the policy's I-beam. It takes no clicks.
|
||||
struct PointerTextRegion: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> PointerTextRegionView {
|
||||
PointerTextRegionView()
|
||||
/// A SwiftUI-placed view that only marks an area for the pointer. It takes no clicks.
|
||||
class PointerRegionView: NSView, PointerOwner {
|
||||
var pointerLayer = PointerLayer.workspace {
|
||||
didSet { if pointerLayer != oldValue { pointerChanged() } }
|
||||
}
|
||||
|
||||
func updateNSView(_ view: PointerTextRegionView, context: Context) {}
|
||||
}
|
||||
|
||||
final class PointerTextRegionView: NSView, PointerTextTarget {
|
||||
let pointerLayer = PointerLayer.workspace
|
||||
var acceptsPointerText: Bool { true }
|
||||
private lazy var tracker = PointerTracker(view: self)
|
||||
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
|
||||
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
tracker.update()
|
||||
}
|
||||
|
||||
override func setFrameOrigin(_ newOrigin: NSPoint) {
|
||||
super.setFrameOrigin(newOrigin)
|
||||
tracker.update()
|
||||
}
|
||||
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
super.setFrameSize(newSize)
|
||||
tracker.update()
|
||||
}
|
||||
|
||||
func isPointerOverRegion(atWindowPoint point: NSPoint) -> Bool {
|
||||
visibleRectWithoutScrollers.contains(convert(point, from: nil))
|
||||
}
|
||||
|
||||
override func resetCursorRects() {
|
||||
addPointerCursorRect(visibleRectWithoutScrollers)
|
||||
func updatePointer(atWindowPoint point: NSPoint) {
|
||||
NSCursor.arrow.set()
|
||||
}
|
||||
|
||||
func pointerChanged() {
|
||||
window?.invalidateCursorRects(for: self)
|
||||
PointerPolicy.setNeedsRefresh(in: window)
|
||||
}
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
@@ -242,3 +364,125 @@ final class PointerTextRegionView: NSView, PointerTextTarget {
|
||||
super.viewWillMove(toWindow: newWindow)
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-reads the pointer whenever the mouse enters or leaves a view or crosses into its cursor area.
|
||||
/// A separate owner, because SwiftTerm's enter and exit handlers cannot be overridden.
|
||||
final class PointerTracker: NSResponder {
|
||||
private weak var view: NSView?
|
||||
private var area: NSTrackingArea?
|
||||
|
||||
init(view: NSView) {
|
||||
self.view = view
|
||||
super.init()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("PointerTracker is built in code")
|
||||
}
|
||||
|
||||
/// Fits the tracking area to the view's visible part; call it from every `updateTrackingAreas` and frame change.
|
||||
/// Not `.inVisibleRect`: in SwiftUI's non-clipping host the visible rect spans the window, so the mouse would never enter.
|
||||
func update() {
|
||||
guard let view else { return }
|
||||
let rect = view.visibleBounds
|
||||
// Re-added even when the rect is unchanged: an area added while its view was moving keeps a stale position.
|
||||
if let area { view.removeTrackingArea(area) }
|
||||
area = nil
|
||||
guard rect.isUsableCursorRect else { return }
|
||||
let area = NSTrackingArea(rect: rect, options: [.cursorUpdate, .mouseEnteredAndExited, .activeInKeyWindow], owner: self)
|
||||
view.addTrackingArea(area)
|
||||
self.area = area
|
||||
}
|
||||
|
||||
override func mouseEntered(with event: NSEvent) { pointerMoved() }
|
||||
override func mouseExited(with event: NSEvent) { pointerMoved() }
|
||||
override func cursorUpdate(with event: NSEvent) { pointerMoved() }
|
||||
|
||||
private func pointerMoved() {
|
||||
PointerPolicy.scheduleUpdate(in: view?.window)
|
||||
}
|
||||
}
|
||||
|
||||
extension EnvironmentValues {
|
||||
/// The overlay host sets `.overlay`, so the regions inside it keep their pointer while the columns lose theirs.
|
||||
@Entry var pointerLayer = PointerLayer.workspace
|
||||
/// False under `disabledLook`, whose hit-testing switch SwiftUI does not expose.
|
||||
@Entry var allowsPointerHand = true
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI text
|
||||
|
||||
/// Gives SwiftUI text that selects on click, such as the Markdown preview, the policy's I-beam. It takes no clicks.
|
||||
struct PointerTextRegion: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> PointerTextRegionView {
|
||||
PointerTextRegionView()
|
||||
}
|
||||
|
||||
func updateNSView(_ view: PointerTextRegionView, context: Context) {
|
||||
view.pointerLayer = context.environment.pointerLayer
|
||||
}
|
||||
}
|
||||
|
||||
final class PointerTextRegionView: PointerRegionView, PointerTextTarget {
|
||||
var acceptsPointerText: Bool { true }
|
||||
|
||||
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||
isPointerOverRegion(atWindowPoint: point)
|
||||
}
|
||||
|
||||
override func updatePointer(atWindowPoint point: NSPoint) {
|
||||
pointerShape(atWindowPoint: point).cursor.set()
|
||||
}
|
||||
|
||||
override func resetCursorRects() {
|
||||
addPointerCursorRect(visibleRectWithoutScrollers)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI clickables
|
||||
|
||||
extension View {
|
||||
/// Shows the pointing hand over this view; apply it to the exact view a left click acts on.
|
||||
/// A `.disabled` or `disabledLook` ancestor turns it into the arrow.
|
||||
func pointerHand(_ isEnabled: Bool = true) -> some View {
|
||||
background(PointerHandRegion(isEnabled: isEnabled))
|
||||
}
|
||||
}
|
||||
|
||||
struct PointerHandRegion: NSViewRepresentable {
|
||||
var isEnabled: Bool
|
||||
|
||||
func makeNSView(context: Context) -> PointerHandRegionView {
|
||||
let view = PointerHandRegionView()
|
||||
updateNSView(view, context: context)
|
||||
return view
|
||||
}
|
||||
|
||||
func updateNSView(_ view: PointerHandRegionView, context: Context) {
|
||||
let environment = context.environment
|
||||
view.pointerLayer = environment.pointerLayer
|
||||
view.isClickable = isEnabled && environment.isEnabled && environment.allowsPointerHand
|
||||
}
|
||||
}
|
||||
|
||||
final class PointerHandRegionView: PointerRegionView {
|
||||
var isClickable = true {
|
||||
didSet { if isClickable != oldValue { pointerChanged() } }
|
||||
}
|
||||
|
||||
private func shape(isSelecting: Bool) -> PointerShape {
|
||||
PointerPolicy.shape(forClickable: pointerLayer, overlayOpen: PointerPolicy.isOverlayOpen(in: window),
|
||||
isEnabled: isClickable, isSelecting: isSelecting)
|
||||
}
|
||||
|
||||
override func updatePointer(atWindowPoint point: NSPoint) {
|
||||
shape(isSelecting: PointerPolicy.isSelecting).cursor.set()
|
||||
}
|
||||
|
||||
/// Cursor rects ignore an overlay on top, so the hand rect exists only while the rule allows it.
|
||||
override func resetCursorRects() {
|
||||
let rect = visibleRectWithoutScrollers
|
||||
guard rect.isUsableCursorRect, shape(isSelecting: false) == .pointingHand else { return }
|
||||
addCursorRect(rect, cursor: .pointingHand)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ public struct SegmentButton: View {
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.fixedSize()
|
||||
.clipShape(RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
||||
|
||||
@@ -125,7 +125,7 @@ final class SplitterHandleView: NSView, PointerOwner {
|
||||
if inside {
|
||||
cursor.set()
|
||||
} else if let window {
|
||||
PointerPolicy.updateCursor(in: window)
|
||||
PointerPolicy.scheduleUpdate(in: window)
|
||||
}
|
||||
onHover(inside)
|
||||
onEnd()
|
||||
|
||||
@@ -251,6 +251,7 @@ private struct CollapseButton: View {
|
||||
.background(hovering ? Palette.hover : Color.clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).stroke(Palette.raised, lineWidth: Metrics.hairline))
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
.onTapGesture(perform: action)
|
||||
.onHover { hovering = $0 }
|
||||
.animation(Metrics.colorTransition, value: hovering)
|
||||
|
||||
@@ -116,6 +116,7 @@ struct SymbolPopupView: View {
|
||||
}
|
||||
.id(index)
|
||||
.onHover { if $0 { highlighted = index } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(SymbolTarget(path: def.path, line: def.line)) }
|
||||
}
|
||||
ForEach(Array(lookup.definitions.enumerated()), id: \.offset) { _, def in relations(def) }
|
||||
@@ -133,6 +134,7 @@ struct SymbolPopupView: View {
|
||||
.padding(.top, 8)
|
||||
.padding(.bottom, 2)
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
.onTapGesture { if let first = group.hits.first { open(SymbolTarget(path: group.path, line: first.line)) } }
|
||||
case let .hit(path, hit, index):
|
||||
SymbolRow(isHighlighted: highlighted == index) {
|
||||
@@ -147,6 +149,7 @@ struct SymbolPopupView: View {
|
||||
}
|
||||
.id(index)
|
||||
.onHover { if $0 { highlighted = index } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(SymbolTarget(path: path, line: hit.line)) }
|
||||
}
|
||||
}
|
||||
@@ -244,6 +247,7 @@ private struct SymbolChip: View {
|
||||
.overlay(RoundedRectangle(cornerRadius: 3).stroke(lit ? Palette.amberKeycapBorder : Palette.raised, lineWidth: Metrics.hairline))
|
||||
.contentShape(Rectangle())
|
||||
.onHover { hovering = $0 }
|
||||
.pointerHand(known)
|
||||
.onTapGesture { if known { follow() } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +154,7 @@ private struct TreeRowView: View {
|
||||
.frame(height: Metrics.explorerRow)
|
||||
.selectableRow(isSelected: selected, isHovered: hovered)
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
.onTapGesture(perform: onClick)
|
||||
.onHover { inside in
|
||||
hovered = inside
|
||||
|
||||
@@ -126,6 +126,7 @@ struct QuickActionButton: View {
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.onHover { hovered = $0 }
|
||||
.help(help)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ public struct OverlayHost: View {
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
overlay.environment(\.pointerLayer, .overlay)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var overlay: some View {
|
||||
switch model.overlay.current {
|
||||
case .none, .launcher:
|
||||
EmptyView()
|
||||
|
||||
@@ -107,6 +107,7 @@ final class CommandFieldBox: NSView, PointerTextTarget {
|
||||
let field: NSTextField
|
||||
let inset: CGFloat
|
||||
let pointerLayer = PointerLayer.overlay
|
||||
private lazy var pointerTracker = PointerTracker(view: self)
|
||||
|
||||
init(field: NSTextField, inset: CGFloat) {
|
||||
self.field = field
|
||||
@@ -137,6 +138,29 @@ final class CommandFieldBox: NSView, PointerTextTarget {
|
||||
addPointerCursorRect(bounds)
|
||||
}
|
||||
|
||||
/// Cursor rects alone never fire inside the SwiftUI overlay, so entering the box re-reads the pointer.
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
override func setFrameOrigin(_ newOrigin: NSPoint) {
|
||||
super.setFrameOrigin(newOrigin)
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
super.setFrameSize(newSize)
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
/// A mouse already resting where the field appears gets no enter event, so the pointer is read once it is shown.
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
pointerTracker.update()
|
||||
PointerPolicy.scheduleUpdate(in: window)
|
||||
}
|
||||
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
guard field.currentEditor() == nil else { return }
|
||||
window?.makeFirstResponder(field)
|
||||
|
||||
@@ -71,6 +71,7 @@ struct RecentFilesOverlay: View {
|
||||
row(path, selected: index == selection)
|
||||
.id(index)
|
||||
.onHover { inside in if inside { hover(index) } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(path) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ struct SearchOverlay: View {
|
||||
.strokeBorder(on ? Palette.amber : Palette.raised, lineWidth: Metrics.hairline))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.tip(SearchModel.chipTooltip(count: chip.count, isChosen: model.extensionFilter == chip.ext))
|
||||
}
|
||||
}
|
||||
@@ -151,6 +152,7 @@ struct SearchOverlay: View {
|
||||
hitLine(hit, selected: selected)
|
||||
.id(index)
|
||||
.onHover { inside in if inside { hover(.thisFile, row: index) } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(SearchModel.Target(path: path, line: hit.line)) }
|
||||
}
|
||||
}
|
||||
@@ -174,6 +176,7 @@ struct SearchOverlay: View {
|
||||
let selected = model.column == .project && model.projectSelection == row.index
|
||||
hitLine(row.hit, selected: selected)
|
||||
.onHover { inside in if inside { hover(.project, row: row.index) } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(SearchModel.Target(path: row.path, line: row.hit.line)) }
|
||||
}
|
||||
}
|
||||
@@ -205,6 +208,7 @@ struct SearchOverlay: View {
|
||||
.onHover { inside in
|
||||
if inside { hoveredGroup = group.path } else if hoveredGroup == group.path { hoveredGroup = nil }
|
||||
}
|
||||
.pointerHand()
|
||||
.onTapGesture {
|
||||
if let first = group.rows.first { open(SearchModel.Target(path: group.path, line: first.hit.line)) }
|
||||
}
|
||||
@@ -241,6 +245,7 @@ struct SearchOverlay: View {
|
||||
let selected = model.column == .files && model.fileSelection == index
|
||||
fileRow(match, selected: selected)
|
||||
.onHover { inside in if inside { hover(.files, row: index) } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(SearchModel.Target(path: match.path, line: nil)) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
|
||||
public func applicationDidFinishLaunching(_ notification: Notification) {
|
||||
PointerPolicy.installCursorUpdateMonitor()
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
}
|
||||
|
||||
|
||||
@@ -144,6 +144,7 @@ private struct LauncherRow<Icon: View, Label: View, Trailing: View>: View {
|
||||
isHovered = inside
|
||||
if inside { model.perform(.launcherSelect(index)) }
|
||||
}
|
||||
.pointerHand()
|
||||
.onTapGesture {
|
||||
model.perform(.launcherSelect(index))
|
||||
model.perform(.launcherActivate)
|
||||
|
||||
@@ -48,6 +48,7 @@ struct RecentProjectsOverlay: View {
|
||||
row(project, selected: index == selection)
|
||||
.id(index)
|
||||
.onHover { inside in if inside { hover(index) } }
|
||||
.pointerHand()
|
||||
.onTapGesture { open(project) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ private struct ProjectName: View {
|
||||
.foregroundStyle(isHovered ? Palette.text2 : Palette.text4)
|
||||
.lineLimit(1)
|
||||
.onHover { isHovered = $0 }
|
||||
.pointerHand()
|
||||
.onTapGesture(perform: action)
|
||||
.help("Open folder…")
|
||||
.animation(Metrics.colorTransition, value: isHovered)
|
||||
|
||||
@@ -31,7 +31,7 @@ struct WorkspaceView: View {
|
||||
}
|
||||
.overlay { OverlayHost(model: model) }
|
||||
.overlay {
|
||||
if model.overlay.current == .launcher { RecentProjectsOverlay(model: model) }
|
||||
if model.overlay.current == .launcher { RecentProjectsOverlay(model: model).environment(\.pointerLayer, .overlay) }
|
||||
}
|
||||
.background(PointerRefresher(trigger: PointerRefresher.Trigger(
|
||||
overlay: model.overlay.current, activePath: model.editor?.activePath, visibility: model.layout.visibility,
|
||||
|
||||
@@ -89,6 +89,7 @@ private struct CommitButton: View {
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.disabled(!isEnabled)
|
||||
.onHover { hovered = $0 }
|
||||
.animation(Metrics.colorTransition, value: hovered)
|
||||
@@ -117,6 +118,7 @@ private struct PushButton: View {
|
||||
.opacity(busy ? 0.4 : 1)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.disabled(busy)
|
||||
.onHover { hovered = $0 }
|
||||
.animation(Metrics.colorTransition, value: hovered)
|
||||
|
||||
@@ -246,6 +246,7 @@ private struct GitRowView: View {
|
||||
.frame(height: Metrics.sourceControlRow)
|
||||
.selectableRow(isSelected: selected, isHovered: hovered)
|
||||
.contentShape(Rectangle())
|
||||
.pointerHand()
|
||||
.onTapGesture(perform: onOpen)
|
||||
.onHover { inside in
|
||||
hovered = inside
|
||||
|
||||
@@ -10,6 +10,7 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle,
|
||||
var appliedSettings: HelderConfig.Terminal?
|
||||
var appliedScale = 1.0
|
||||
private var closeObserver: NSObjectProtocol?
|
||||
private lazy var pointerTracker = PointerTracker(view: self)
|
||||
private let events = ProcessEvents()
|
||||
|
||||
init(kind: TerminalKind, store: TerminalStore) {
|
||||
@@ -116,12 +117,21 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle,
|
||||
/// The first click in an inactive window already places the selection or reaches the program.
|
||||
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||
|
||||
/// SwiftTerm's own tracking area has no cursor updates, so entering the grid would not re-read the pointer.
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
/// SwiftTerm's `resetCursorRects` is not open, so its I-beam rect is filtered here and leaves out the scroller.
|
||||
override func addCursorRect(_ rect: NSRect, cursor object: NSCursor) {
|
||||
guard rect.isUsableCursorRect else { return }
|
||||
guard object == .iBeam else { return super.addCursorRect(rect, cursor: object) }
|
||||
guard allowsIBeamRect else { return }
|
||||
let scroller = subviews.first { $0 is NSScroller && !$0.isHidden }
|
||||
super.addCursorRect(scroller.map { rect.removing($0.frame) } ?? rect, cursor: object)
|
||||
let area = scroller.map { rect.removing($0.frame) } ?? rect
|
||||
guard area.isUsableCursorRect else { return }
|
||||
super.addCursorRect(area, cursor: object)
|
||||
}
|
||||
|
||||
/// Gives this class its own `cursorUpdate:` that asks the policy; Swift cannot override SwiftTerm's I-beam one,
|
||||
@@ -134,7 +144,7 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle,
|
||||
if PointerPolicy.isSelecting || view.isTopmost(atWindowPoint: point) {
|
||||
view.updatePointer(atWindowPoint: point)
|
||||
} else {
|
||||
PointerPolicy.updateCursor(in: window)
|
||||
PointerPolicy.scheduleUpdate(in: window)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import AppKit
|
||||
@testable import HelderUI
|
||||
import Testing
|
||||
|
||||
@@ -31,4 +32,65 @@ import Testing
|
||||
#expect(shape(isOverText: false, isSelecting: true) == .iBeam)
|
||||
#expect(shape(overlayOpen: true, isEnabled: false, isOverText: false, isSelecting: true) == .iBeam)
|
||||
}
|
||||
|
||||
private func clickable(layer: PointerLayer = .workspace, overlayOpen: Bool = false, isEnabled: Bool = true,
|
||||
isSelecting: Bool = false) -> PointerShape {
|
||||
PointerPolicy.shape(forClickable: layer, overlayOpen: overlayOpen, isEnabled: isEnabled, isSelecting: isSelecting)
|
||||
}
|
||||
|
||||
@Test func aClickableInAColumnTakesTheHand() {
|
||||
#expect(clickable() == .pointingHand)
|
||||
}
|
||||
|
||||
@Test func anOpenOverlayGivesTheClickablesBehindItTheArrow() {
|
||||
#expect(clickable(overlayOpen: true) == .arrow)
|
||||
}
|
||||
|
||||
@Test func aClickableInsideTheOverlayKeepsTheHand() {
|
||||
#expect(clickable(layer: .overlay, overlayOpen: true) == .pointingHand)
|
||||
}
|
||||
|
||||
@Test func aDisabledClickableKeepsTheArrow() {
|
||||
#expect(clickable(isEnabled: false) == .arrow)
|
||||
#expect(clickable(layer: .overlay, overlayOpen: true, isEnabled: false) == .arrow)
|
||||
}
|
||||
|
||||
@Test func aDragSelectionKeepsTheIBeamOverAClickable() {
|
||||
#expect(clickable(isSelecting: true) == .iBeam)
|
||||
#expect(clickable(overlayOpen: true, isEnabled: false, isSelecting: true) == .iBeam)
|
||||
}
|
||||
|
||||
@Test func textAndClickablesShareTheOverlayGate() {
|
||||
#expect(PointerPolicy.isBehindOverlay(.workspace, overlayOpen: true))
|
||||
#expect(!PointerPolicy.isBehindOverlay(.workspace, overlayOpen: false))
|
||||
#expect(!PointerPolicy.isBehindOverlay(.overlay, overlayOpen: true))
|
||||
}
|
||||
|
||||
@MainActor @Test func aRegionOutOfViewHasAnEmptyFiniteAreaAndNoCursorRect() {
|
||||
let area = OffscreenRegion(frame: NSRect(x: 0, y: 0, width: 210, height: 24)).visibleRectWithoutScrollers
|
||||
#expect(area.isEmpty)
|
||||
#expect(area.origin.x.isFinite && area.origin.y.isFinite)
|
||||
#expect(!area.isUsableCursorRect)
|
||||
}
|
||||
|
||||
@Test func removingAStripFromANullRectStaysFinite() {
|
||||
let rest = NSRect.null.removing(NSRect(x: 0, y: 0, width: 10, height: 10))
|
||||
#expect(rest.origin.x.isFinite && rest.origin.y.isFinite)
|
||||
#expect(!NSRect.null.isUsableCursorRect)
|
||||
}
|
||||
|
||||
@MainActor @Test func aRegionsAreaNeverExceedsItsBounds() {
|
||||
let region = UnclippedRegion(frame: NSRect(x: 0, y: 0, width: 49, height: 27))
|
||||
#expect(region.visibleRectWithoutScrollers == region.bounds)
|
||||
}
|
||||
}
|
||||
|
||||
/// Stands in for a view in SwiftUI's platform-view host, which does not clip its visible rect.
|
||||
private final class UnclippedRegion: PointerRegionView {
|
||||
override var visibleRect: NSRect { NSRect(x: -629, y: -904.5, width: 1512, height: 940) }
|
||||
}
|
||||
|
||||
/// A region scrolled out of view: its visible rect misses its bounds.
|
||||
private final class OffscreenRegion: PointerRegionView {
|
||||
override var visibleRect: NSRect { NSRect(x: 500, y: 500, width: 10, height: 10) }
|
||||
}
|
||||
|
||||
@@ -104,3 +104,4 @@ How the suites are organised and how to run one: see the Testing section of the
|
||||
- **Config.** `files.followGitignore` defaults to **false** (the main-process value). There is no CSS layer: code font and size come from config/Typography.
|
||||
- **One file, no Original.** The editor deliberately has no tabs and no Original mode. `git.defaultDiffMode: "original"` in an older config decodes as `"updated"`.
|
||||
- **SwiftTerm** has no line-height or letter-spacing setting, so `terminal.lineHeight`/`letterSpacing` from the config are ignored.
|
||||
- **Pointer.** `Components/PointerPolicy.swift` is the one rule: I-beam, pointing hand or arrow. Text views conform to `PointerTextTarget`; a SwiftUI button or row gets the hand with `.pointerHand()` on the exact view a left click acts on, never on a container. Its region view takes no clicks and reads `\.isEnabled`, `disabledLook` and `\.pointerLayer` (`.overlay` under `OverlayHost` and the ⇧⌘O list), so a disabled button and every column behind an overlay keep the arrow. `updateCursor` asks the `PointerOwner` under the mouse first, so a text field inside a clickable row keeps the I-beam. AppKit hands a cursor update to the hit view, and a region view takes no hits, so `NSHostingView` would set the arrow; `PointerPolicy.installCursorUpdateMonitor()` (called at launch) swallows every cursor update in the key window, passes every mouse move on untouched, and for both runs `updateCursor` once afterwards (`scheduleUpdate`, coalesced per window). Inside an event SwiftUI's hit test returns its root view, so nothing calls `updateCursor` from inside an event, and the deferred call never resets cursor rects, which would fire new updates. Region views and the terminals keep a `PointerTracker` area (cursor update, enter, exit) so AppKit still sends an update when the mouse crosses their edge.
|
||||
|
||||
@@ -214,6 +214,16 @@ Inside the overlay:
|
||||
- When nothing matches, each column shows a brief no-results message.
|
||||
- `Esc` closes the overlay. The respected excludes are the same as the Explorer (`files.exclude` and, when on, gitignore).
|
||||
|
||||
### Mouse pointer
|
||||
|
||||
The pointer shows what a left click does at that spot. It depends only on what lies under the mouse.
|
||||
|
||||
- **Pointing hand** only where a left click runs an action: buttons (title-bar toggles and tools, `?`, Preview, Diff, Commit, Push, stage `+` / unstage `−`, Stage all / Unstage all, dialog buttons, Collapse, search chips), clickable rows (Git file rows, Explorer rows, search results, recent files and projects, launcher rows, symbol popup rows, context-menu items) and the project name in the title bar. Never on a view that only reacts to a right-click, a tooltip or hover.
|
||||
- **I-beam** where a click places a caret or starts a selection: the code (also below the last line), the Markdown preview, both terminals, the commit message and every overlay input.
|
||||
- **Arrow** everywhere else: labels, headers, folder labels, the gutter, the status bar, scrims, toasts, empty states, and a disabled button such as Commit with nothing staged.
|
||||
- With an overlay open, every column behind it shows the arrow; the overlay's own buttons, rows and inputs keep theirs.
|
||||
- A splitter shows a resize arrow; `Cmd` over a known PHP class name in the code shows the hand. No other pointers are used.
|
||||
|
||||
### Pane resizing
|
||||
|
||||
Dragging any splitter resizes the adjacent panes. Positions persist across launches.
|
||||
|
||||
@@ -87,6 +87,11 @@ A Helder window holds one project. It has a title bar on top, four columns in th
|
||||
- In the Git and Explorer columns, `Up` and `Down` move the row cursor and `Return` opens the row. See [Git](git.md) and [Explorer](explorer.md).
|
||||
- Clicking a row in Git or Explorer takes the keyboard out of a text field in another column.
|
||||
|
||||
### The mouse pointer
|
||||
- A pointing hand means a left click runs an action: a button, a clickable row, the project name.
|
||||
- An I-beam means a click places the cursor or selects text: the code, the Markdown preview, the terminals and every input.
|
||||
- Everywhere else the pointer is an arrow, also over a disabled button and over the columns behind an open dialog or search.
|
||||
|
||||
### Refresh (`Cmd+R`)
|
||||
- Re-reads git, the file tree and the open file from disk. The disk wins over unsaved edits in the open file.
|
||||
- Every column flashes lighter for a moment, so you can see the refresh happened even when nothing changed.
|
||||
|
||||
Reference in New Issue
Block a user