Skip to content

Commit 475002d

Browse files
committed
Merge branch 'feat/in-doc-search' of https://github.com/CodeEditApp/CodeEditTextView into feat/in-doc-search
2 parents 50081c0 + 05ef1cd commit 475002d

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

Sources/CodeEditTextView/EmphasisManager/EmphasisManager.swift

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public final class EmphasisManager {
5454
// Handle flash animations
5555
for (index, emphasis) in emphases.enumerated() where emphasis.flash {
5656
let layer = layers[index]
57-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
57+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
58+
guard let self = self else { return }
5859
self.applyFadeOutAnimation(to: layer.layer, textLayer: layer.textLayer)
5960
// Remove the emphasis from the group
6061
if var emphases = self.emphasisGroups[id] {
@@ -122,28 +123,26 @@ public final class EmphasisManager {
122123

123124
/// Updates the positions and bounds of all emphasis layers to match the current text layout.
124125
public func updateLayerBackgrounds() {
125-
for (_, layers) in emphasisGroups {
126-
for layer in layers {
127-
if let shapePath = textView?.layoutManager?.roundedPathForRange(layer.emphasis.range) {
128-
if #available(macOS 14.0, *) {
129-
layer.layer.path = shapePath.cgPath
130-
} else {
131-
layer.layer.path = shapePath.cgPathFallback
132-
}
126+
for layer in emphasisGroups.flatMap(\.value) {
127+
if let shapePath = textView?.layoutManager?.roundedPathForRange(layer.emphasis.range) {
128+
if #available(macOS 14.0, *) {
129+
layer.layer.path = shapePath.cgPath
130+
} else {
131+
layer.layer.path = shapePath.cgPathFallback
132+
}
133133

134-
// Update bounds and position
135-
if let cgPath = layer.layer.path {
136-
let boundingBox = cgPath.boundingBox
137-
layer.layer.bounds = boundingBox
138-
layer.layer.position = CGPoint(x: boundingBox.midX, y: boundingBox.midY)
139-
}
134+
// Update bounds and position
135+
if let cgPath = layer.layer.path {
136+
let boundingBox = cgPath.boundingBox
137+
layer.layer.bounds = boundingBox
138+
layer.layer.position = CGPoint(x: boundingBox.midX, y: boundingBox.midY)
139+
}
140140

141-
// Update text layer if it exists
142-
if let textLayer = layer.textLayer {
143-
var bounds = shapePath.bounds
144-
bounds.origin.y += 1 // Move down by 1 pixel
145-
textLayer.frame = bounds
146-
}
141+
// Update text layer if it exists
142+
if let textLayer = layer.textLayer {
143+
var bounds = shapePath.bounds
144+
bounds.origin.y += 1 // Move down by 1 pixel
145+
textLayer.frame = bounds
147146
}
148147
}
149148
}
@@ -286,15 +285,15 @@ public final class EmphasisManager {
286285

287286
layer.add(fadeAnimation, forKey: "fadeOutAnimation")
288287

289-
if let textLayer = textLayer {
290-
if let textFadeAnimation = fadeAnimation.copy() as? CABasicAnimation {
288+
if let textLayer = textLayer, let textFadeAnimation = fadeAnimation.copy() as? CABasicAnimation {
289+
textLayer.add(textFadeAnimation, forKey: "fadeOutAnimation")
291290
textLayer.add(textFadeAnimation, forKey: "fadeOutAnimation")
292291
}
293292
}
294293

295294
// Remove both layers after animation completes
296-
DispatchQueue.main.asyncAfter(deadline: .now() + fadeAnimation.duration) {
297-
layer.removeFromSuperlayer()
295+
DispatchQueue.main.asyncAfter(deadline: .now() + fadeAnimation.duration) { [weak layer, weak textLayer] in
296+
layer?.removeFromSuperlayer()
298297
textLayer?.removeFromSuperlayer()
299298
}
300299
}

Sources/CodeEditTextView/TextView/TextView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public class TextView: NSView, NSTextContent {
240240
/// The selection manager for the text view.
241241
package(set) public var selectionManager: TextSelectionManager!
242242

243-
/// Manages text highlights within the text view
243+
/// Managed emphasized text ranges in the text view
244244
public var emphasisManager: EmphasisManager?
245245

246246
// MARK: - Private Properties

0 commit comments

Comments
 (0)