Skip to content

Commit

Permalink
Address/Fix selection glitches
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed May 23, 2023
1 parent 3fca3f3 commit 6df77c3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ extension NSTextLayoutManager {
extension NSTextLayoutManager {

/// Returns a location of text produced by a tap or click at the point you specify.
/// - Parameters:
/// - point: A CGPoint that represents the location of the tap or click.
/// - containerLocation: A NSTextLocation that describes the contasiner location.
/// - Returns: A location
public func location(interactingAt point: CGPoint, inContainerAt containerLocation: NSTextLocation) -> NSTextLocation? {
guard let lineFragmentRange = lineFragmentRange(for: point, inContainerAt: containerLocation)
else {
guard let lineFragmentRange = lineFragmentRange(for: point, inContainerAt: containerLocation) else {
return nil
}

Expand Down
5 changes: 0 additions & 5 deletions Sources/STTextView/STTextLayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ final class STTextLayoutManager: NSTextLayoutManager {
}
}

// lineFragmentRange return invalid ranges FB11898356 that result in broken selection
//override func lineFragmentRange(for point: CGPoint, inContainerAt location: NSTextLocation) -> NSTextRange? {
// let textRange = super.lineFragmentRange(for: point, inContainerAt: location)
// return textRange
//}
}
13 changes: 11 additions & 2 deletions Sources/STTextView/STTextView+Mouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,23 @@ extension STTextView {
}
}

open override func mouseUp(with event: NSEvent) {
super.mouseUp(with: event)
mouseDraggingSelectionAnchors = nil
}

open override func mouseDragged(with event: NSEvent) {
if isSelectable, event.type == .leftMouseDragged, (!event.deltaY.isZero || !event.deltaX.isZero) {
let point = convert(event.locationInWindow, from: nil)

if mouseDraggingSelectionAnchors == nil {
mouseDraggingSelectionAnchors = textLayoutManager.textSelections
}

updateTextSelection(
interactingAt: point,
inContainerAt: textLayoutManager.documentRange.location,
anchors: textLayoutManager.textSelections,
inContainerAt: mouseDraggingSelectionAnchors?.first?.textRanges.first?.location ?? textLayoutManager.documentRange.location,
anchors: mouseDraggingSelectionAnchors!,
extending: true,
isDragging: true,
visual: event.modifierFlags.contains(.option)
Expand Down
17 changes: 0 additions & 17 deletions Sources/STTextView/STTextView+Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,23 +435,6 @@ extension STTextView {
modifiers.insert(.visual)
}

// FB11898356
// Something if wrong with textSelectionsInteractingAtPoint
//
// When drag mouse down it move text range to the previous line
// that is unexpected. This happens only when the anchor location
// is at the beginning of the line/paragraph
//
// Mouse position: (8.140625, 82.99609375)
// [NSTextSelection:<0x60000153fb10> granularity=character, affinity=upstream, transient, anchor position offset=5.000000, anchor location 512, textRanges=(
// "512...1106"
// )]
//
// Mouse position: (8.484375, 83.20703125)
// [NSTextSelection:<0x60000152f570> granularity=character, affinity=upstream, transient, anchor position offset=5.000000, anchor location 512, textRanges=(
// "511...1106"
// )]
//
let selections = textLayoutManager.textSelectionNavigation.textSelections(
interactingAt: point,
inContainerAt: location,
Expand Down
10 changes: 9 additions & 1 deletion Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Cocoa

/// A TextKit2 text view without NSTextView baggage
open class STTextView: NSView, NSTextInput {

/// Posted before an object performs any operation that changes characters or formatting attributes.
public static let textWillChangeNotification = NSNotification.Name("NSTextWillChangeNotification")

Expand Down Expand Up @@ -410,6 +409,15 @@ open class STTextView: NSView, NSTextInput {
}
}

/// A dragging selection anchor
///
/// FB11898356 - Something if wrong with textSelectionsInteractingAtPoint
/// it expects that the dragging operation does not change anchor selections
/// significantly. Specifically it does not play well if anchor and current
/// location is too close to each other, therefore `mouseDraggingSelectionAnchors`
/// keep the anchors unchanged while dragging.
internal var mouseDraggingSelectionAnchors: [NSTextSelection]? = nil

open override class var defaultMenu: NSMenu? {
let menu = super.defaultMenu ?? NSMenu()

Expand Down

0 comments on commit 6df77c3

Please sign in to comment.