From 0d3d5af78ace9adfdbf9ffe5f5d37c253b547c91 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Tue, 6 Oct 2026 14:17:53 +0200 Subject: [PATCH] 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. --- Museion/PostEditorView.swift | 38 +++++++++++++++++++++++++++--------- Museion/ReadingSurface.swift | 24 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Museion/PostEditorView.swift b/Museion/PostEditorView.swift index 952d89e..89133d0 100644 --- a/Museion/PostEditorView.swift +++ b/Museion/PostEditorView.swift @@ -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 diff --git a/Museion/ReadingSurface.swift b/Museion/ReadingSurface.swift index 6cc9e47..0a9b73f 100644 --- a/Museion/ReadingSurface.swift +++ b/Museion/ReadingSurface.swift @@ -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 + } + } } }