Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix First Responder Issues, Fix Custom Undo Manager #12

Merged
merged 3 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Sources/CodeEditTextView/TextView/TextView+Mouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ extension TextView {
self?.autoscroll(with: event)
}
}

if !self.isFirstResponder {
self.window?.makeFirstResponder(self)
}
}

override public func mouseUp(with event: NSEvent) {
Expand Down
1 change: 1 addition & 0 deletions Sources/CodeEditTextView/TextView/TextView+UndoRedo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import AppKit
extension TextView {
public func setUndoManager(_ newManager: CEUndoManager) {
self._undoManager = newManager
self._undoManager?.setTextView(self)
}

override public var undoManager: UndoManager? {
Expand Down
36 changes: 36 additions & 0 deletions Sources/CodeEditTextView/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ public class TextView: NSView, NSTextContent {
super.canBecomeKeyView && acceptsFirstResponder && !isHiddenOrHasHiddenAncestor
}

/// Sent to the window's first responder when `NSWindow.makeKey()` occurs.
@objc private func becomeKeyWindow() {
_ = becomeFirstResponder()
}

/// Sent to the window's first responder when `NSWindow.resignKey()` occurs.
@objc private func resignKeyWindow() {
_ = resignFirstResponder()
}

open override var needsPanelToBecomeKey: Bool {
isSelectable || isEditable
}
Expand Down Expand Up @@ -362,6 +372,23 @@ public class TextView: NSView, NSTextContent {
updateFrameIfNeeded()
}

// MARK: - Hit test

/// Returns the responding view for a given point.
/// - Parameter point: The point to find.
/// - Returns: A view at the given point, if any.
override public func hitTest(_ point: NSPoint) -> NSView? {
// For our purposes, cursor and line fragment views should be transparent from the point of view of
// all other views. So, if the normal hitTest returns one of them, we return `self` instead.
let hitView = super.hitTest(point)

if let hitView, hitView != self,
type(of: hitView) == CursorView.self || type(of: hitView) == LineFragmentView.self {
return self
}
return hitView
}

// MARK: - Key Down

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

// MARK: - Layout

open override class var isCompatibleWithResponsiveScrolling: Bool {
true
}

open override func prepareContent(in rect: NSRect) {
needsLayout = true
super.prepareContent(in: rect)
}

override public func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
if isSelectable {
Expand Down
8 changes: 8 additions & 0 deletions Sources/CodeEditTextView/Utils/CEUndoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,12 @@ public class CEUndoManager {
public func enable() {
isDisabled = false
}

// MARK: - Internal

/// Sets a new text view to use for mutation registration, undo/redo operations.
/// - Parameter newTextView: The new text view.
func setTextView(_ newTextView: TextView) {
self.textView = newTextView
}
}