Skip to content

Commit 6abce20

Browse files
Fix First Responder Issues, Custom Undo Manager (#12)
2 parents 7656007 + 7eb429f commit 6abce20

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

Sources/CodeEditTextView/TextView/TextView+Mouse.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ extension TextView {
4848
self?.autoscroll(with: event)
4949
}
5050
}
51-
52-
if !self.isFirstResponder {
53-
self.window?.makeFirstResponder(self)
54-
}
5551
}
5652

5753
override public func mouseUp(with event: NSEvent) {

Sources/CodeEditTextView/TextView/TextView+UndoRedo.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import AppKit
1010
extension TextView {
1111
public func setUndoManager(_ newManager: CEUndoManager) {
1212
self._undoManager = newManager
13+
self._undoManager?.setTextView(self)
1314
}
1415

1516
override public var undoManager: UndoManager? {

Sources/CodeEditTextView/TextView/TextView.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@ public class TextView: NSView, NSTextContent {
326326
super.canBecomeKeyView && acceptsFirstResponder && !isHiddenOrHasHiddenAncestor
327327
}
328328

329+
/// Sent to the window's first responder when `NSWindow.makeKey()` occurs.
330+
@objc private func becomeKeyWindow() {
331+
_ = becomeFirstResponder()
332+
}
333+
334+
/// Sent to the window's first responder when `NSWindow.resignKey()` occurs.
335+
@objc private func resignKeyWindow() {
336+
_ = resignFirstResponder()
337+
}
338+
329339
open override var needsPanelToBecomeKey: Bool {
330340
isSelectable || isEditable
331341
}
@@ -362,6 +372,23 @@ public class TextView: NSView, NSTextContent {
362372
updateFrameIfNeeded()
363373
}
364374

375+
// MARK: - Hit test
376+
377+
/// Returns the responding view for a given point.
378+
/// - Parameter point: The point to find.
379+
/// - Returns: A view at the given point, if any.
380+
override public func hitTest(_ point: NSPoint) -> NSView? {
381+
// For our purposes, cursor and line fragment views should be transparent from the point of view of
382+
// all other views. So, if the normal hitTest returns one of them, we return `self` instead.
383+
let hitView = super.hitTest(point)
384+
385+
if let hitView, hitView != self,
386+
type(of: hitView) == CursorView.self || type(of: hitView) == LineFragmentView.self {
387+
return self
388+
}
389+
return hitView
390+
}
391+
365392
// MARK: - Key Down
366393

367394
override public func keyDown(with event: NSEvent) {
@@ -381,6 +408,15 @@ public class TextView: NSView, NSTextContent {
381408

382409
// MARK: - Layout
383410

411+
open override class var isCompatibleWithResponsiveScrolling: Bool {
412+
true
413+
}
414+
415+
open override func prepareContent(in rect: NSRect) {
416+
needsLayout = true
417+
super.prepareContent(in: rect)
418+
}
419+
384420
override public func draw(_ dirtyRect: NSRect) {
385421
super.draw(dirtyRect)
386422
if isSelectable {

Sources/CodeEditTextView/Utils/CEUndoManager.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,12 @@ public class CEUndoManager {
224224
public func enable() {
225225
isDisabled = false
226226
}
227+
228+
// MARK: - Internal
229+
230+
/// Sets a new text view to use for mutation registration, undo/redo operations.
231+
/// - Parameter newTextView: The new text view.
232+
func setTextView(_ newTextView: TextView) {
233+
self.textView = newTextView
234+
}
227235
}

0 commit comments

Comments
 (0)