fix(editor): typography knob changes reach open surfaces (#71 follow-up 9)

updateNSView never fires on UserDefaults writes from the Settings pane,
so the flowKey check there was dead code. Both surfaces now observe
UserDefaults.didChangeNotification directly: the editor re-attributes
(selection preserved), the reading canvas rebuilds its document. Also
closes the parked stale-reading-view defect from the handoff notes.
This commit is contained in:
AlexKucera committed 2026-10-06 14:17:53 +02:00
1 parent 677dc8c073
commit 0d3d5af78a
2 files changed
+53 -9

No files matched your search

+29 -9
View File
@@ -297,21 +297,18 @@ struct ProseTextView: NSViewRepresentable {
func updateNSView(_ scroll: NSScrollView, context: Context) {
guard let tv = scroll.documentView as? NSTextView else { return }
// Typography/flow knob change: re-attribute everything, keep text
// and selection (settings live in another pane; the editor is idle).
if context.coordinator.appliedFlowKey != ProseTypography.flowKey {
context.coordinator.appliedFlowKey = ProseTypography.flowKey
let sel = tv.selectedRange()
let storage = tv.textStorage as! NSMutableAttributedString
storage.setAttributedString(ProsePresenter.attribute(tv.string))
tv.setSelectedRange(sel)
}
// Typography/flow knob changes arrive via the coordinator's
// UserDefaults observer (SwiftUI doesn't invalidate on defaults
// writes from another pane).
context.coordinator.watchDefaults(tv)
context.coordinator.appliedFlowKey = ProseTypography.flowKey
if let session {
context.coordinator.sessionRef = session
// Load only when the generation moved (open, Yes-reload) — never
// per keystroke: the text view owns the live text.
if context.coordinator.loadedGeneration != session.loadGeneration {
context.coordinator.loadedGeneration = session.loadGeneration
context.coordinator.appliedFlowKey = ProseTypography.flowKey
// Seed from session.text (LIVE), not session.loadText (the
// open-time snapshot): the representable is recreated on the
// reading⇄editing swap, and seeding from the snapshot showed
@@ -358,6 +355,29 @@ struct ProseTextView: NSViewRepresentable {
var loadedGeneration = -1
var appliedCaretToken = -1
var appliedFlowKey = ""
private var defaultsObserver: NSObjectProtocol?
func watchDefaults(_ tv: NSTextView) {
guard defaultsObserver == nil else { return }
defaultsObserver = NotificationCenter.default.addObserver(
forName: UserDefaults.didChangeNotification,
object: nil,
queue: .main,
) { [weak self] _ in
self?.applyTypographyIfChanged(tv)
}
}
private func applyTypographyIfChanged(_ tv: NSTextView) {
let key = ProseTypography.flowKey
guard key != appliedFlowKey else { return }
appliedFlowKey = key
let sel = tv.selectedRange()
if let storage = tv.textStorage as? NSMutableAttributedString {
storage.setAttributedString(ProsePresenter.attribute(tv.string))
tv.setSelectedRange(sel)
}
}
init(representable: ProseTextView) {
onEdit = representable.onEdit
+24
View File
@@ -79,6 +79,7 @@ struct ReadingSurface: NSViewRepresentable {
tv.onEditIntent = onEditIntent
tv.stateBox = stateBox
context.coordinator.canvas = tv
context.coordinator.watchDefaults(tv, representable: self)
return scroll
}
@@ -110,6 +111,29 @@ struct ReadingSurface: NSViewRepresentable {
weak var canvas: ReadingCanvasView?
var builtGeneration = -1
var builtText = ""
private var defaultsObserver: NSObjectProtocol?
private var appliedFlowKey = ""
/// Rebuild the rendered document when typography/flow knobs move
/// (SwiftUI doesn't re-call updateNSView on UserDefaults writes).
func watchDefaults(_: ReadingCanvasView, representable: ReadingSurface) {
guard defaultsObserver == nil else { return }
appliedFlowKey = ProseTypography.flowKey
defaultsObserver = NotificationCenter.default.addObserver(
forName: UserDefaults.didChangeNotification,
object: nil,
queue: .main,
) { [weak self] _ in
guard let self, let canvas else { return }
let key = ProseTypography.flowKey
guard key != appliedFlowKey else { return }
appliedFlowKey = key
let doc = ReadingPresenter.document(for: representable.text, resolvingImagesIn: representable.postFolder)
canvas.textStorage?.setAttributedString(doc.content)
canvas.blockBounds = doc.bounds
canvas.needsDisplay = true
}
}
}
}