Refactor TextContainer

This commit is contained in:
1024jp 2024-06-22 16:31:43 +09:00
parent 1f160ae717
commit 58ff98f56b
2 changed files with 14 additions and 19 deletions

View File

@ -858,6 +858,17 @@ final class EditorTextView: NSTextView, Themable, CurrentLineHighlighting, Multi
}
override var typingAttributes: [NSAttributedString.Key: Any] {
didSet {
// -> The font can differ from the specified text font while typing marked text.
guard self.hasMarkedText() != true else { return }
(self.textContainer as? TextContainer)?.indentAttributes = typingAttributes
}
}
override var font: NSFont? {
get {

View File

@ -32,33 +32,17 @@ final class TextContainer: NSTextContainer {
var isHangingIndentEnabled = false { didSet { self.invalidateLayout() } }
var hangingIndentWidth = 0 { didSet { self.invalidateLayout() } }
var indentAttributes: [NSAttributedString.Key: Any] = [:] { didSet { self.indentWidthCache.removeAll() } }
// MARK: Private Properties
private var indentWidthCache: [String: CGFloat] = [:]
private var indentAttributes: [NSAttributedString.Key: Any] = [:]
private var typingAttributesObservation: NSKeyValueObservation?
private var indentWidthCache: [String: Double] = [:]
// MARK: Text Container Methods
override weak var textView: NSTextView? {
didSet {
self.typingAttributesObservation = textView?.observe(\.typingAttributes, options: [.initial, .new]) { [weak self] (textView, change) in
guard let typingAttributes = change.newValue else { return }
// -> The font can differ from the specified text font while typing marked text.
guard textView.hasMarkedText() != true else { return }
self?.indentAttributes = typingAttributes
self?.indentWidthCache.removeAll()
}
}
}
override var isSimpleRectangularTextContainer: Bool {
// -> According to the reference, this property should return `false` when `.isHangingIndentEnabled`