Skip to content

Commit

Permalink
use weak vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Dec 17, 2024
1 parent 0e10b68 commit c344582
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 5 additions & 3 deletions Sources/IBeam/TextSystemCursorCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AppKit
public final class TextSystemCursorCoordinator<System: TextSystem> where System.TextRange == NSRange {
typealias CursorState = MultiCursorState<System>

private let textView: NSTextView
private weak var textView: NSTextView?
private let indicatorState: TextViewIndicatorState
private let cursorState: CursorState
private var selectionNotification: NSObjectProtocol?
Expand Down Expand Up @@ -40,7 +40,7 @@ public final class TextSystemCursorCoordinator<System: TextSystem> where System.
}

private func selectionChanged() {
if mutatingSelection {
guard let textView, mutatingSelection == false else {
return
}

Expand Down Expand Up @@ -68,7 +68,7 @@ public final class TextSystemCursorCoordinator<System: TextSystem> where System.
mutatingSelection = false
}

public func processOperation(_ operation: InputOperation) {
public func processOperation(_ operation: InputOperation) -> Bool {
mutatingSelection = true
cursorState.apply(operation)
mutatingSelection = false
Expand All @@ -78,6 +78,8 @@ public final class TextSystemCursorCoordinator<System: TextSystem> where System.
if cursorState.cursors.isEmpty {
selectionChanged()
}

return true
}

public func mutateCursors(with operation: CursorOperation<NSRange>) {
Expand Down
14 changes: 10 additions & 4 deletions Sources/IBeam/TextViewIndicatorState.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#if os(macOS)
import AppKit

/// Manages cursor view isntances within an NSTextView
/// Manages cursor view isntances within an NSTextView.
///
/// This type maintains a weak reference to the underlying text view.
@available(macOS 14.0, *)
@MainActor
public final class TextViewIndicatorState {
public typealias BoundingRectProvider = (NSRange) -> CGRect?

public let textView: NSTextView
private weak var textView: NSTextView?
public let viewCursorId: UUID

private var indicators: [UUID: NSTextInsertionIndicator] = [:]
Expand All @@ -19,7 +21,9 @@ public final class TextViewIndicatorState {
}

private var indicatorViews: [NSTextInsertionIndicator] {
textView.subviews
guard let textView else { return [] }

return textView.subviews
.compactMap { $0 as? NSTextInsertionIndicator }
.sorted { a, b in
a.frame.minY < b.frame.minY
Expand All @@ -32,7 +36,7 @@ public final class TextViewIndicatorState {
view.displayMode = .automatic
}

textView.updateInsertionPointStateAndRestartTimer(true)
textView?.updateInsertionPointStateAndRestartTimer(true)
}

public func removeIndicator(with id: UUID) {
Expand All @@ -45,6 +49,8 @@ public final class TextViewIndicatorState {
}

public func updateIndictor(with range: NSRange, for id: UUID) {
guard let textView else { return }

if id == viewCursorId {
textView.setSelectedRange(range, affinity: .upstream, stillSelecting: false)
return
Expand Down

0 comments on commit c344582

Please sign in to comment.