fixes cursor
This commit is contained in:
@@ -1,22 +1,72 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
|
|
||||||
/// An NSTextView that only sets its I-beam while it is the view under the pointer.
|
/// An NSTextView that takes its pointer from `PointerPolicy`, and only while it is the view under the pointer.
|
||||||
/// On macOS 26 SwiftTerm makes the window send every mouse move to the first responder, and NSTextView
|
/// On macOS 26 SwiftTerm makes the window send every mouse move to the first responder, and NSTextView
|
||||||
/// sets the I-beam on each one, which fights the arrow over the other columns and a splitter's resize cursor.
|
/// sets the I-beam on each one, which fights the arrow over the other columns and a splitter's resize cursor.
|
||||||
class PointerBoundTextView: NSTextView {
|
class PointerBoundTextView: NSTextView, PointerTextTarget {
|
||||||
|
var pointerLayer = PointerLayer.workspace
|
||||||
|
/// False while the view stays alive but is not shown, so its text takes no I-beam.
|
||||||
|
var isPointerActive = true {
|
||||||
|
didSet { if isPointerActive != oldValue { PointerPolicy.setNeedsRefresh(in: window) } }
|
||||||
|
}
|
||||||
/// Grows the view to at least the clip view's height, so the empty area below the text still takes the I-beam and clicks.
|
/// Grows the view to at least the clip view's height, so the empty area below the text still takes the I-beam and clicks.
|
||||||
var fillsClipView = false {
|
var fillsClipView = false {
|
||||||
didSet { fitClipView() }
|
didSet { fitClipView() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override func mouseMoved(with event: NSEvent) {
|
var acceptsPointerText: Bool { isPointerActive && isSelectable }
|
||||||
guard isUnderPointer else { return }
|
|
||||||
super.mouseMoved(with: event)
|
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||||
|
isTopmost(atWindowPoint: point)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updatePointer(atWindowPoint point: NSPoint) {
|
||||||
|
applyPointer(pointerShape(atWindowPoint: point), atWindowPoint: point)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the cursor for a shape the policy picked; subclasses add their extras here.
|
||||||
|
func applyPointer(_ shape: PointerShape, atWindowPoint point: NSPoint) {
|
||||||
|
shape.cursor.set()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The first click in an inactive window already places the caret or starts a selection.
|
||||||
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||||
|
|
||||||
|
override func resetCursorRects() {
|
||||||
|
addPointerCursorRect(visibleRectWithoutScrollers)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func mouseMoved(with event: NSEvent) {
|
||||||
|
guard let point = mouseLocation, isTopmost(atWindowPoint: point) else { return }
|
||||||
|
super.mouseMoved(with: event)
|
||||||
|
applyPointer(pointerShape(isOverText: true), atWindowPoint: point)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An update meant for another view is handed to the view under the mouse.
|
||||||
override func cursorUpdate(with event: NSEvent) {
|
override func cursorUpdate(with event: NSEvent) {
|
||||||
guard isUnderPointer else { return }
|
guard let window, let point = mouseLocation else { return }
|
||||||
super.cursorUpdate(with: event)
|
if PointerPolicy.isSelecting || isTopmost(atWindowPoint: point) {
|
||||||
|
updatePointer(atWindowPoint: point)
|
||||||
|
} else {
|
||||||
|
PointerPolicy.updateCursor(in: window)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// NSTextView tracks the whole drag inside `mouseDown`, so the selection ends when it returns.
|
||||||
|
override func mouseDown(with event: NSEvent) {
|
||||||
|
PointerPolicy.beginSelection()
|
||||||
|
super.mouseDown(with: event)
|
||||||
|
PointerPolicy.endSelection(in: window)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func viewWillMove(toWindow newWindow: NSWindow?) {
|
||||||
|
if newWindow == nil { PointerPolicy.setNeedsRefresh(in: window) }
|
||||||
|
super.viewWillMove(toWindow: newWindow)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func viewDidMoveToWindow() {
|
||||||
|
super.viewDidMoveToWindow()
|
||||||
|
PointerPolicy.setNeedsRefresh(in: window)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidMoveToSuperview() {
|
override func viewDidMoveToSuperview() {
|
||||||
@@ -39,12 +89,7 @@ class PointerBoundTextView: NSTextView {
|
|||||||
sizeToFit()
|
sizeToFit()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A splitter's hit area lies on top of the text view's edge, so the visible rect alone is not enough.
|
var mouseLocation: NSPoint? {
|
||||||
private var isUnderPointer: Bool {
|
window?.mouseLocationOutsideOfEventStream
|
||||||
guard let window, let content = window.contentView else { return false }
|
|
||||||
let location = window.mouseLocationOutsideOfEventStream
|
|
||||||
guard visibleRect.contains(convert(location, from: nil)),
|
|
||||||
let hit = content.hitTest(content.superview?.convert(location, from: nil) ?? location) else { return false }
|
|
||||||
return hit.isDescendant(of: self)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,241 @@
|
|||||||
|
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.
|
||||||
|
enum PointerShape: Equatable {
|
||||||
|
case iBeam
|
||||||
|
case arrow
|
||||||
|
|
||||||
|
var cursor: NSCursor { self == .iBeam ? .iBeam : .arrow }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where a text-capable view lives: in a column, or in the open overlay.
|
||||||
|
enum PointerLayer: Equatable {
|
||||||
|
case workspace
|
||||||
|
case overlay
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What the rule looks at for one point.
|
||||||
|
struct PointerState: Equatable {
|
||||||
|
var layer: PointerLayer
|
||||||
|
var overlayOpen: Bool
|
||||||
|
/// False for a disabled field, or a text view kept alive behind another pane.
|
||||||
|
var isEnabled: Bool
|
||||||
|
/// The point lies on the view's text area and no other view covers it.
|
||||||
|
var isOverText: Bool
|
||||||
|
/// A drag-selection that began in a text area is still held.
|
||||||
|
var isSelecting: Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The one pointer rule: the I-beam only where a single click places a caret or starts a text selection.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - AppKit
|
||||||
|
|
||||||
|
/// A view that sets its own cursor when the pointer is re-read without a mouse move.
|
||||||
|
@MainActor
|
||||||
|
protocol PointerOwner: NSView {
|
||||||
|
func updatePointer(atWindowPoint point: NSPoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A view with selectable or editable text. It takes its pointer from `PointerPolicy` and never decides itself.
|
||||||
|
@MainActor
|
||||||
|
protocol PointerTextTarget: PointerOwner {
|
||||||
|
var pointerLayer: PointerLayer { get }
|
||||||
|
var acceptsPointerText: Bool { get }
|
||||||
|
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
extension PointerTextTarget {
|
||||||
|
func pointerShape(atWindowPoint point: NSPoint) -> PointerShape {
|
||||||
|
pointerShape(isOverText: isPointerOverText(atWindowPoint: point))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The rule's pointer when the caller already knows whether the point lies on the text.
|
||||||
|
func pointerShape(isOverText: Bool) -> PointerShape {
|
||||||
|
PointerPolicy.shape(PointerState(layer: pointerLayer, overlayOpen: PointerPolicy.isOverlayOpen(in: window),
|
||||||
|
isEnabled: acceptsPointerText, isOverText: isOverText,
|
||||||
|
isSelecting: PointerPolicy.isSelecting))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cursor rects ignore an overlay on top, so the I-beam rect exists only while the rule allows it.
|
||||||
|
var allowsIBeamRect: Bool {
|
||||||
|
PointerPolicy.shape(PointerState(layer: pointerLayer, overlayOpen: PointerPolicy.isOverlayOpen(in: window),
|
||||||
|
isEnabled: acceptsPointerText, isOverText: true, isSelecting: false)) == .iBeam
|
||||||
|
}
|
||||||
|
|
||||||
|
func updatePointer(atWindowPoint point: NSPoint) {
|
||||||
|
pointerShape(atWindowPoint: point).cursor.set()
|
||||||
|
}
|
||||||
|
|
||||||
|
func addPointerCursorRect(_ rect: NSRect) {
|
||||||
|
guard allowsIBeamRect else { return }
|
||||||
|
addCursorRect(rect, cursor: .iBeam)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 }
|
||||||
|
return content.hitTest(content.superview?.convert(point, from: nil) ?? point)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// True when a click at the window point reaches this view or a subview.
|
||||||
|
/// A splitter's hit area lies on top of a text view's edge, so the visible rect alone is not enough.
|
||||||
|
func isTopmost(atWindowPoint point: NSPoint) -> Bool {
|
||||||
|
hitView(atWindowPoint: point)?.isDescendant(of: self) ?? false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The visible rect minus the enclosing scroll view's scrollers, which keep the arrow.
|
||||||
|
var visibleRectWithoutScrollers: NSRect {
|
||||||
|
let scrollers = [enclosingScrollView?.verticalScroller, enclosingScrollView?.horizontalScroller]
|
||||||
|
return scrollers.compactMap { $0 }.filter { !$0.isHidden && $0.superview != nil }
|
||||||
|
.reduce(visibleRect) { $0.removing(convert($1.bounds, from: $1)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension NSRect {
|
||||||
|
/// The rect minus a strip that spans its full height or width along one edge, such as a scroller.
|
||||||
|
func removing(_ strip: NSRect) -> NSRect {
|
||||||
|
let overlap = intersection(strip)
|
||||||
|
guard !overlap.isEmpty else { return self }
|
||||||
|
var rest = self
|
||||||
|
if overlap.height >= height {
|
||||||
|
rest.origin.x = overlap.minX > minX ? minX : overlap.maxX
|
||||||
|
rest.size.width = overlap.minX > minX ? overlap.minX - minX : maxX - overlap.maxX
|
||||||
|
} else if overlap.width >= width {
|
||||||
|
rest.origin.y = overlap.minY > minY ? minY : overlap.maxY
|
||||||
|
rest.size.height = overlap.minY > minY ? overlap.minY - minY : maxY - overlap.maxY
|
||||||
|
}
|
||||||
|
return rest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
extension PointerPolicy {
|
||||||
|
private static var selectionDrag = false
|
||||||
|
private static var pendingRefresh: Set<Int> = []
|
||||||
|
private static var isUpdatingCursor = false
|
||||||
|
|
||||||
|
/// The button check ends a drag whose mouse-up went to another view or window.
|
||||||
|
static var isSelecting: Bool {
|
||||||
|
if selectionDrag, NSEvent.pressedMouseButtons & 1 == 0 { selectionDrag = false }
|
||||||
|
return selectionDrag
|
||||||
|
}
|
||||||
|
|
||||||
|
static func beginSelection() {
|
||||||
|
selectionDrag = true
|
||||||
|
}
|
||||||
|
|
||||||
|
static func endSelection(in window: NSWindow?) {
|
||||||
|
selectionDrag = false
|
||||||
|
if let window { updateCursor(in: window) }
|
||||||
|
}
|
||||||
|
|
||||||
|
static func isOverlayOpen(in window: NSWindow?) -> Bool {
|
||||||
|
guard let window else { return false }
|
||||||
|
return WorkspaceRegistry.model(for: window)?.overlay.isPresented ?? false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Re-reads the pointer for a mouse that has not moved, once SwiftUI has applied the change that asked for it.
|
||||||
|
static func setNeedsRefresh(in window: NSWindow?) {
|
||||||
|
// Teardown calls this from a deallocating window, where forming a weak reference aborts; keep only its number.
|
||||||
|
guard let number = window?.windowNumber, pendingRefresh.insert(number).inserted else { return }
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
MainActor.assumeIsolated {
|
||||||
|
defer { pendingRefresh.remove(number) }
|
||||||
|
guard let window = NSApp.window(withWindowNumber: number) else { return }
|
||||||
|
refresh(in: window)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Rebuilds every cursor rect, then sets the cursor for where the mouse rests.
|
||||||
|
static func refresh(in window: NSWindow) {
|
||||||
|
// A splitter drag turns the rects off and owns the cursor until it ends.
|
||||||
|
guard window.areCursorRectsEnabled else { return }
|
||||||
|
window.contentView?.layoutSubtreeIfNeeded()
|
||||||
|
window.resetCursorRects()
|
||||||
|
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.
|
||||||
|
static func updateCursor(in window: NSWindow) {
|
||||||
|
guard !isUpdatingCursor, window.isKeyWindow, window.areCursorRectsEnabled, !isSelecting,
|
||||||
|
let content = window.contentView else { return }
|
||||||
|
isUpdatingCursor = true
|
||||||
|
defer { isUpdatingCursor = false }
|
||||||
|
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 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
|
||||||
|
}
|
||||||
|
view = current.superview
|
||||||
|
}
|
||||||
|
if let hit, let region = textRegion(in: content, at: point, under: hit) {
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
final class PointerTextRegionView: NSView, PointerTextTarget {
|
||||||
|
let pointerLayer = PointerLayer.workspace
|
||||||
|
var acceptsPointerText: Bool { true }
|
||||||
|
|
||||||
|
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||||
|
|
||||||
|
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||||
|
visibleRectWithoutScrollers.contains(convert(point, from: nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
override func resetCursorRects() {
|
||||||
|
addPointerCursorRect(visibleRectWithoutScrollers)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func viewDidMoveToWindow() {
|
||||||
|
super.viewDidMoveToWindow()
|
||||||
|
PointerPolicy.setNeedsRefresh(in: window)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func viewWillMove(toWindow newWindow: NSWindow?) {
|
||||||
|
if newWindow == nil { PointerPolicy.setNeedsRefresh(in: window) }
|
||||||
|
super.viewWillMove(toWindow: newWindow)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,7 +54,7 @@ private struct SplitterHandle: NSViewRepresentable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final class SplitterHandleView: NSView {
|
final class SplitterHandleView: NSView, PointerOwner {
|
||||||
var axis: Axis = .vertical {
|
var axis: Axis = .vertical {
|
||||||
didSet { if axis != oldValue { window?.invalidateCursorRects(for: self) } }
|
didSet { if axis != oldValue { window?.invalidateCursorRects(for: self) } }
|
||||||
}
|
}
|
||||||
@@ -86,6 +86,10 @@ final class SplitterHandleView: NSView {
|
|||||||
cursor.set()
|
cursor.set()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updatePointer(atWindowPoint point: NSPoint) {
|
||||||
|
cursor.set()
|
||||||
|
}
|
||||||
|
|
||||||
override func mouseEntered(with event: NSEvent) {
|
override func mouseEntered(with event: NSEvent) {
|
||||||
onHover(true)
|
onHover(true)
|
||||||
cursor.set()
|
cursor.set()
|
||||||
@@ -117,8 +121,12 @@ final class SplitterHandleView: NSView {
|
|||||||
override func mouseUp(with event: NSEvent) {
|
override func mouseUp(with event: NSEvent) {
|
||||||
guard dragStart != nil else { return }
|
guard dragStart != nil else { return }
|
||||||
let inside = bounds.contains(convert(event.locationInWindow, from: nil))
|
let inside = bounds.contains(convert(event.locationInWindow, from: nil))
|
||||||
(inside ? cursor : NSCursor.arrow).set()
|
|
||||||
endDrag()
|
endDrag()
|
||||||
|
if inside {
|
||||||
|
cursor.set()
|
||||||
|
} else if let window {
|
||||||
|
PointerPolicy.updateCursor(in: window)
|
||||||
|
}
|
||||||
onHover(inside)
|
onHover(inside)
|
||||||
onEnd()
|
onEnd()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,6 +153,7 @@ final class CodeEditorController: NSObject, NSTextViewDelegate, NSLayoutManagerD
|
|||||||
applyScale(next.fontScale)
|
applyScale(next.fontScale)
|
||||||
textView.isEditable = next.isEditable
|
textView.isEditable = next.isEditable
|
||||||
textView.isSelectable = true
|
textView.isSelectable = true
|
||||||
|
textView.isPointerActive = next.isShown
|
||||||
|
|
||||||
var reloaded = false
|
var reloaded = false
|
||||||
if document?.id != next.documentID {
|
if document?.id != next.documentID {
|
||||||
|
|||||||
@@ -54,9 +54,8 @@ final class CodeTextView: PointerBoundTextView {
|
|||||||
|
|
||||||
override func flagsChanged(with event: NSEvent) {
|
override func flagsChanged(with event: NSEvent) {
|
||||||
super.flagsChanged(with: event)
|
super.flagsChanged(with: event)
|
||||||
let command = event.modifierFlags.contains(.command)
|
controller?.commandKeyChanged(event.modifierFlags.contains(.command))
|
||||||
controller?.commandKeyChanged(command)
|
if let point = mouseLocation, isTopmost(atWindowPoint: point) { updatePointer(atWindowPoint: point) }
|
||||||
if let window { updatePointer(at: window.mouseLocationOutsideOfEventStream, command: command) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func updateTrackingAreas() {
|
override func updateTrackingAreas() {
|
||||||
@@ -67,23 +66,12 @@ final class CodeTextView: PointerBoundTextView {
|
|||||||
pointerTracking = area
|
pointerTracking = area
|
||||||
}
|
}
|
||||||
|
|
||||||
override func mouseMoved(with event: NSEvent) {
|
/// The pointing hand over a symbol ⌘-click would follow, else the policy's pointer.
|
||||||
super.mouseMoved(with: event)
|
override func applyPointer(_ shape: PointerShape, atWindowPoint point: NSPoint) {
|
||||||
updatePointer(at: event.locationInWindow, command: event.modifierFlags.contains(.command))
|
if shape == .iBeam, NSEvent.modifierFlags.contains(.command), controller?.symbol(atWindowPoint: point) != nil {
|
||||||
}
|
|
||||||
|
|
||||||
override func cursorUpdate(with event: NSEvent) {
|
|
||||||
super.cursorUpdate(with: event)
|
|
||||||
updatePointer(at: event.locationInWindow, command: event.modifierFlags.contains(.command))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The pointing hand over a symbol ⌘-click would follow, the I-beam elsewhere in the text.
|
|
||||||
private func updatePointer(at windowPoint: NSPoint, command: Bool) {
|
|
||||||
guard visibleRect.contains(convert(windowPoint, from: nil)) else { return }
|
|
||||||
if command, controller?.symbol(atWindowPoint: windowPoint) != nil {
|
|
||||||
NSCursor.pointingHand.set()
|
NSCursor.pointingHand.set()
|
||||||
} else {
|
} else {
|
||||||
NSCursor.iBeam.set()
|
shape.cursor.set()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ struct MarkdownPreviewView: View {
|
|||||||
.padding(EdgeInsets(top: 18, leading: 20, bottom: 48, trailing: 20))
|
.padding(EdgeInsets(top: 18, leading: 20, bottom: 48, trailing: 20))
|
||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
.textSelection(.enabled)
|
.textSelection(.enabled)
|
||||||
|
.background(PointerTextRegion())
|
||||||
}
|
}
|
||||||
.background(Palette.editor)
|
.background(Palette.editor)
|
||||||
.environment(\.openURL, OpenURLAction { url in open(url) })
|
.environment(\.openURL, OpenURLAction { url in open(url) })
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ private struct NoteEditor: NSViewRepresentable {
|
|||||||
scroll.hasVerticalScroller = true
|
scroll.hasVerticalScroller = true
|
||||||
scroll.autohidesScrollers = true
|
scroll.autohidesScrollers = true
|
||||||
let textView = NoteTextView(frame: .zero)
|
let textView = NoteTextView(frame: .zero)
|
||||||
|
textView.pointerLayer = .overlay
|
||||||
textView.isVerticallyResizable = true
|
textView.isVerticallyResizable = true
|
||||||
textView.isHorizontallyResizable = false
|
textView.isHorizontallyResizable = false
|
||||||
textView.autoresizingMask = [.width]
|
textView.autoresizingMask = [.width]
|
||||||
|
|||||||
@@ -102,10 +102,11 @@ struct OverlayCommandField: NSViewRepresentable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The box around an `OverlayCommandField`: an I-beam over all of it, and a click anywhere focuses the field.
|
/// The box around an `OverlayCommandField`: a click anywhere in it focuses the field, so all of it takes the I-beam.
|
||||||
final class CommandFieldBox: NSView {
|
final class CommandFieldBox: NSView, PointerTextTarget {
|
||||||
let field: NSTextField
|
let field: NSTextField
|
||||||
let inset: CGFloat
|
let inset: CGFloat
|
||||||
|
let pointerLayer = PointerLayer.overlay
|
||||||
|
|
||||||
init(field: NSTextField, inset: CGFloat) {
|
init(field: NSTextField, inset: CGFloat) {
|
||||||
self.field = field
|
self.field = field
|
||||||
@@ -124,8 +125,16 @@ final class CommandFieldBox: NSView {
|
|||||||
field.frame = NSRect(x: inset, y: ((bounds.height - height) / 2).rounded(), width: max(0, bounds.width - 2 * inset), height: height)
|
field.frame = NSRect(x: inset, y: ((bounds.height - height) / 2).rounded(), width: max(0, bounds.width - 2 * inset), height: height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var acceptsPointerText: Bool { field.isEnabled }
|
||||||
|
|
||||||
|
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||||
|
isTopmost(atWindowPoint: point)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||||
|
|
||||||
override func resetCursorRects() {
|
override func resetCursorRects() {
|
||||||
addCursorRect(bounds, cursor: .iBeam)
|
addPointerCursorRect(bounds)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func mouseDown(with event: NSEvent) {
|
override func mouseDown(with event: NSEvent) {
|
||||||
@@ -185,6 +194,8 @@ private final class FocusingTextField: NSTextField {
|
|||||||
set {}
|
set {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||||
|
|
||||||
override func viewDidMoveToWindow() {
|
override func viewDidMoveToWindow() {
|
||||||
super.viewDidMoveToWindow()
|
super.viewDidMoveToWindow()
|
||||||
guard !focused, window != nil else { return }
|
guard !focused, window != nil else { return }
|
||||||
@@ -205,6 +216,7 @@ private final class PointerBoundFieldCell: NSTextFieldCell {
|
|||||||
private lazy var editor: PointerBoundTextView = {
|
private lazy var editor: PointerBoundTextView = {
|
||||||
let editor = PointerBoundTextView()
|
let editor = PointerBoundTextView()
|
||||||
editor.isFieldEditor = true
|
editor.isFieldEditor = true
|
||||||
|
editor.pointerLayer = .overlay
|
||||||
return editor
|
return editor
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -352,6 +352,10 @@ enum WorkspaceRegistry {
|
|||||||
if lastKey === model { lastKey = nil }
|
if lastKey === model { lastKey = nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func model(for window: NSWindow) -> WorkspaceModel? {
|
||||||
|
entries.first { $0.window === window }?.model
|
||||||
|
}
|
||||||
|
|
||||||
static func becameKey(_ model: WorkspaceModel) {
|
static func becameKey(_ model: WorkspaceModel) {
|
||||||
lastKey = model
|
lastKey = model
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ struct WorkspaceView: View {
|
|||||||
.overlay {
|
.overlay {
|
||||||
if model.overlay.current == .launcher { RecentProjectsOverlay(model: model) }
|
if model.overlay.current == .launcher { RecentProjectsOverlay(model: model) }
|
||||||
}
|
}
|
||||||
|
.background(PointerRefresher(trigger: PointerRefresher.Trigger(
|
||||||
|
overlay: model.overlay.current, activePath: model.editor?.activePath, visibility: model.layout.visibility,
|
||||||
|
scale: model.layout.textScale)))
|
||||||
.onAppear { model.layout.windowResized(width: proxy.size.width) }
|
.onAppear { model.layout.windowResized(width: proxy.size.width) }
|
||||||
.onChange(of: proxy.size.width) { _, width in model.layout.windowResized(width: width) }
|
.onChange(of: proxy.size.width) { _, width in model.layout.windowResized(width: width) }
|
||||||
}
|
}
|
||||||
@@ -142,6 +145,37 @@ private struct HiddenPaneFocusGuard: NSViewRepresentable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Re-reads the pointer when what lies under a still mouse changes: an overlay, the open file, a column shown or hidden.
|
||||||
|
private struct PointerRefresher: NSViewRepresentable {
|
||||||
|
struct Trigger: Equatable {
|
||||||
|
var overlay: Overlay
|
||||||
|
var activePath: String?
|
||||||
|
var visibility: ColumnVisibility
|
||||||
|
var scale: Double
|
||||||
|
}
|
||||||
|
|
||||||
|
let trigger: Trigger
|
||||||
|
|
||||||
|
func makeNSView(context: Context) -> RefresherView {
|
||||||
|
RefresherView()
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateNSView(_ view: RefresherView, context: Context) {
|
||||||
|
view.trigger = trigger
|
||||||
|
}
|
||||||
|
|
||||||
|
final class RefresherView: NSView {
|
||||||
|
var trigger: Trigger? {
|
||||||
|
didSet {
|
||||||
|
guard let oldValue, oldValue != trigger else { return }
|
||||||
|
PointerPolicy.setNeedsRefresh(in: window)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// ⌘R lightens a column for a moment and lets it settle back; each new tick replays it.
|
/// ⌘R lightens a column for a moment and lets it settle back; each new tick replays it.
|
||||||
private struct RefreshFlash: ViewModifier {
|
private struct RefreshFlash: ViewModifier {
|
||||||
let tick: Int
|
let tick: Int
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import HelderCore
|
|||||||
import SwiftTerm
|
import SwiftTerm
|
||||||
|
|
||||||
/// One pane's terminal: a SwiftTerm view bound to a PTY child, reporting to the `TerminalStore`.
|
/// One pane's terminal: a SwiftTerm view bound to a PTY child, reporting to the `TerminalStore`.
|
||||||
final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle {
|
final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle, PointerTextTarget {
|
||||||
let kind: TerminalKind
|
let kind: TerminalKind
|
||||||
private weak var store: TerminalStore?
|
private weak var store: TerminalStore?
|
||||||
private(set) var hasStarted = false
|
private(set) var hasStarted = false
|
||||||
@@ -18,7 +18,7 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle
|
|||||||
super.init(frame: NSRect(x: 0, y: 0, width: 480, height: 320),
|
super.init(frame: NSRect(x: 0, y: 0, width: 480, height: 320),
|
||||||
font: TerminalAppearance.font(store.config.terminal),
|
font: TerminalAppearance.font(store.config.terminal),
|
||||||
options: TerminalAppearance.options(store.config.terminal))
|
options: TerminalAppearance.options(store.config.terminal))
|
||||||
Self.arrowCursorUpdate
|
Self.policyCursorUpdate
|
||||||
events.owner = self
|
events.owner = self
|
||||||
processDelegate = events
|
processDelegate = events
|
||||||
appliedSettings = store.config.terminal
|
appliedSettings = store.config.terminal
|
||||||
@@ -104,23 +104,68 @@ final class HelderTerminalView: LocalProcessTerminalView, TerminalSessionHandle
|
|||||||
|
|
||||||
// MARK: Pointer
|
// MARK: Pointer
|
||||||
|
|
||||||
/// The terminal shows the arrow, so SwiftTerm's I-beam rect is dropped; its `resetCursorRects` is not open.
|
let pointerLayer = PointerLayer.workspace
|
||||||
override func addCursorRect(_ rect: NSRect, cursor object: NSCursor) {
|
var acceptsPointerText: Bool { true }
|
||||||
guard object != .iBeam else { return }
|
|
||||||
super.addCursorRect(rect, cursor: object)
|
/// The grid takes the I-beam: its output selects and its prompt takes input. SwiftTerm's scroller keeps the arrow.
|
||||||
|
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||||
|
guard let hit = hitView(atWindowPoint: point) else { return false }
|
||||||
|
return hit.isDescendant(of: self) && !(hit is NSScroller)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gives this class its own `cursorUpdate:` that sets the arrow; Swift cannot override SwiftTerm's I-beam one,
|
/// 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 `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 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gives this class its own `cursorUpdate:` that asks the policy; Swift cannot override SwiftTerm's I-beam one,
|
||||||
/// which runs when an update reaches the pane through a subview or a neighbour's tracking area.
|
/// which runs when an update reaches the pane through a subview or a neighbour's tracking area.
|
||||||
private static let arrowCursorUpdate: Void = {
|
private static let policyCursorUpdate: Void = {
|
||||||
let block: @convention(block) (AnyObject, NSEvent) -> Void = { _, _ in NSCursor.arrow.set() }
|
let block: @convention(block) (AnyObject, NSEvent) -> Void = { view, _ in
|
||||||
|
MainActor.assumeIsolated {
|
||||||
|
guard let view = view as? HelderTerminalView, let window = view.window else { return }
|
||||||
|
let point = window.mouseLocationOutsideOfEventStream
|
||||||
|
if PointerPolicy.isSelecting || view.isTopmost(atWindowPoint: point) {
|
||||||
|
view.updatePointer(atWindowPoint: point)
|
||||||
|
} else {
|
||||||
|
PointerPolicy.updateCursor(in: window)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let selector = #selector(NSResponder.cursorUpdate(with:))
|
let selector = #selector(NSResponder.cursorUpdate(with:))
|
||||||
let types = method_getTypeEncoding(class_getInstanceMethod(NSView.self, selector)!)
|
let types = method_getTypeEncoding(class_getInstanceMethod(NSView.self, selector)!)
|
||||||
class_addMethod(HelderTerminalView.self, selector, imp_implementationWithBlock(block), types)
|
class_addMethod(HelderTerminalView.self, selector, imp_implementationWithBlock(block), types)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
/// A drag that starts on the grid selects text, so it keeps the I-beam until mouse-up wherever it goes.
|
||||||
|
override func mouseDown(with event: NSEvent) {
|
||||||
|
PointerPolicy.beginSelection()
|
||||||
|
super.mouseDown(with: event)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func mouseDragged(with event: NSEvent) {
|
||||||
|
super.mouseDragged(with: event)
|
||||||
|
if PointerPolicy.isSelecting { NSCursor.iBeam.set() }
|
||||||
|
}
|
||||||
|
|
||||||
|
override func mouseUp(with event: NSEvent) {
|
||||||
|
super.mouseUp(with: event)
|
||||||
|
PointerPolicy.endSelection(in: window)
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Lifetime
|
// MARK: Lifetime
|
||||||
|
|
||||||
|
override func viewWillMove(toWindow newWindow: NSWindow?) {
|
||||||
|
PointerPolicy.endSelection(in: nil)
|
||||||
|
super.viewWillMove(toWindow: newWindow)
|
||||||
|
}
|
||||||
|
|
||||||
/// Closing the window kills the child; moving the view between containers does not.
|
/// Closing the window kills the child; moving the view between containers does not.
|
||||||
override func viewDidMoveToWindow() {
|
override func viewDidMoveToWindow() {
|
||||||
super.viewDidMoveToWindow()
|
super.viewDidMoveToWindow()
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
@testable import HelderUI
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@Suite struct PointerPolicyTests {
|
||||||
|
private func shape(layer: PointerLayer = .workspace, overlayOpen: Bool = false, isEnabled: Bool = true,
|
||||||
|
isOverText: Bool = true, isSelecting: Bool = false) -> PointerShape {
|
||||||
|
PointerPolicy.shape(PointerState(layer: layer, overlayOpen: overlayOpen, isEnabled: isEnabled,
|
||||||
|
isOverText: isOverText, isSelecting: isSelecting))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func textUnderThePointerTakesTheIBeam() {
|
||||||
|
#expect(shape() == .iBeam)
|
||||||
|
#expect(shape(layer: .overlay, overlayOpen: true) == .iBeam)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func everythingThatIsNotTextKeepsTheArrow() {
|
||||||
|
#expect(shape(isOverText: false) == .arrow)
|
||||||
|
#expect(shape(layer: .overlay, overlayOpen: true, isOverText: false) == .arrow)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func anOpenOverlayGivesTheColumnsBehindItTheArrow() {
|
||||||
|
#expect(shape(overlayOpen: true) == .arrow)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func aDisabledOrHiddenTextViewKeepsTheArrow() {
|
||||||
|
#expect(shape(isEnabled: false) == .arrow)
|
||||||
|
#expect(shape(layer: .overlay, overlayOpen: true, isEnabled: false) == .arrow)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func aDragSelectionKeepsTheIBeamUntilMouseUpWhereverItGoes() {
|
||||||
|
#expect(shape(isOverText: false, isSelecting: true) == .iBeam)
|
||||||
|
#expect(shape(overlayOpen: true, isEnabled: false, isOverText: false, isSelecting: true) == .iBeam)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user