fix: reload tint colors while running
This commit is contained in:
@@ -17,6 +17,60 @@ final class NotesTextView: NSTextView {
|
||||
private var markerColumn: CGFloat = 22
|
||||
private var checkboxes: [NSButton] = []
|
||||
private var listParagraphs: [(range: NSRange, kind: String, depth: Int, number: Int, checked: Bool)] = []
|
||||
private var themeObservation: ThemeObservation?
|
||||
|
||||
override init(frame frameRect: NSRect = .zero) {
|
||||
super.init(frame: frameRect)
|
||||
}
|
||||
|
||||
override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
|
||||
super.init(frame: frameRect, textContainer: container)
|
||||
themeObservation = ThemeObservation { [weak self] in self?.applyTheme() }
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) { fatalError("Conjure creates note editors programmatically") }
|
||||
|
||||
/// Only visual attributes change here; semantic marks, selection and undo sessions stay intact.
|
||||
private func applyTheme() {
|
||||
insertionPointColor = ThemePalette.accent
|
||||
selectedTextAttributes = [.backgroundColor: ThemePalette.selectionFill]
|
||||
let typing = currentTypingAttributes
|
||||
if let storage = textStorage {
|
||||
let full = NSRange(location: 0, length: storage.length)
|
||||
edits.disableUndoRegistration()
|
||||
storage.beginEditing()
|
||||
storage.enumerateAttributes(in: full) { attributes, range, _ in
|
||||
let codeBlock = attributes[NoteAttributes.block] as? String == "code"
|
||||
let inlineCode = attributes[NoteAttributes.inlineCode] as? Bool == true
|
||||
storage.addAttribute(.foregroundColor,
|
||||
value: inlineCode || attributes[.link] != nil ? ThemePalette.accent : ThemePalette.text,
|
||||
range: range)
|
||||
if codeBlock || inlineCode {
|
||||
storage.addAttribute(.backgroundColor, value: ThemePalette.selectionFill, range: range)
|
||||
}
|
||||
}
|
||||
applyCodeColors(to: storage)
|
||||
storage.endEditing()
|
||||
edits.enableUndoRegistration()
|
||||
}
|
||||
setTypingAttributes(typing)
|
||||
for button in checkboxes { button.contentTintColor = ThemePalette.accent }
|
||||
needsDisplay = true
|
||||
}
|
||||
|
||||
private func applyCodeColors(to storage: NSTextStorage) {
|
||||
let source = storage.string as NSString
|
||||
storage.enumerateAttribute(NoteAttributes.block, in: NSRange(location: 0, length: storage.length)) { value, range, _ in
|
||||
guard value as? String == "code" else { return }
|
||||
let code = source.substring(with: range)
|
||||
let language = storage.attribute(NoteAttributes.language, at: range.location, effectiveRange: nil) as? String
|
||||
guard let highlighted = Self.highlighter.highlight(code, as: language), highlighted.string == code else { return }
|
||||
highlighted.enumerateAttribute(.foregroundColor, in: NSRange(location: 0, length: highlighted.length)) { value, run, _ in
|
||||
if let value { storage.addAttribute(.foregroundColor, value: value,
|
||||
range: NSRange(location: range.location + run.location, length: run.length)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
var vimState = NotesVimState() {
|
||||
didSet { if oldValue.mode != vimState.mode { needsDisplay = true } }
|
||||
}
|
||||
@@ -517,16 +571,7 @@ final class NotesTextView: NSTextView {
|
||||
}
|
||||
index = NSMaxRange(row)
|
||||
}
|
||||
storage.enumerateAttribute(NoteAttributes.block, in: full) { value, range, _ in
|
||||
guard value as? String == "code" else { return }
|
||||
let code = source.substring(with: range)
|
||||
let language = storage.attribute(NoteAttributes.language, at: range.location, effectiveRange: nil) as? String
|
||||
guard let highlighted = Self.highlighter.highlight(code, as: language), highlighted.string == code else { return }
|
||||
highlighted.enumerateAttribute(.foregroundColor, in: NSRange(location: 0, length: highlighted.length)) { value, run, _ in
|
||||
if let value { storage.addAttribute(.foregroundColor, value: value,
|
||||
range: NSRange(location: range.location + run.location, length: run.length)) }
|
||||
}
|
||||
}
|
||||
applyCodeColors(to: storage)
|
||||
storage.enumerateAttribute(.attachment, in: full) { value, _, _ in
|
||||
guard let attachment = value as? NSTextAttachment,
|
||||
let cell = attachment.attachmentCell as? NSTextAttachmentCell, let image = cell.image else { return }
|
||||
|
||||
Reference in New Issue
Block a user