@@ -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. async After( deadline: . now( ) + fadeAnimation. duration) { [ weak layer , weak textLayer ] in
296+ layer? . removeFromSuperlayer( )
298297 textLayer? . removeFromSuperlayer( )
299298 }
300299 }
0 commit comments