Skip to content

Commit c1fed34

Browse files
Add Text Attachments Delegate (#110)
### Description Adds an API for receiving events from the text attachments manager. ### Related Issues N/A ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots N/A
1 parent df485cb commit c1fed34

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Sources/CodeEditTextView/TextLayoutManager/TextAttachments/TextAttachmentManager.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public final class TextAttachmentManager {
1717
weak var layoutManager: TextLayoutManager?
1818
private var selectionObserver: (any NSObjectProtocol)?
1919

20+
public weak var delegate: TextAttachmentManagerDelegate?
21+
2022
/// Adds a new attachment, keeping `orderedAttachments` sorted by range.location.
2123
/// If two attachments overlap, the layout phase will later ignore the one with the higher start.
2224
/// - Complexity: `O(n log(n))` due to array insertion. Could be improved with a binary tree.
@@ -47,6 +49,8 @@ public final class TextAttachmentManager {
4749
}
4850

4951
layoutManager?.setNeedsLayout()
52+
53+
delegate?.textAttachmentDidAdd(attachment.attachment, for: range)
5054
}
5155

5256
/// Removes an attachment and invalidates layout for the removed range.
@@ -62,6 +66,9 @@ public final class TextAttachmentManager {
6266

6367
let attachment = orderedAttachments.remove(at: index)
6468
layoutManager?.invalidateLayoutForRange(attachment.range)
69+
70+
delegate?.textAttachmentDidRemove(attachment.attachment, for: attachment.range)
71+
6572
return attachment
6673
}
6774

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// TextAttachmentManagerDelegate.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Khan Winter on 6/25/25.
6+
//
7+
8+
import Foundation
9+
10+
public protocol TextAttachmentManagerDelegate: AnyObject {
11+
func textAttachmentDidAdd(_ attachment: any TextAttachment, for range: NSRange)
12+
func textAttachmentDidRemove(_ attachment: any TextAttachment, for range: NSRange)
13+
}

Sources/CodeEditTextView/TextLine/LineFragmentRenderer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public final class LineFragmentRenderer {
9292
in: context,
9393
rect: NSRect(
9494
x: currentPosition,
95-
y: yPos,
95+
y: yPos + (lineFragment.heightDifference/2),
9696
width: attachment.width,
97-
height: lineFragment.scaledHeight
97+
height: lineFragment.height
9898
)
9999
)
100100
}

0 commit comments

Comments
 (0)