oficina / base Public

Mac composer: Undo and Redo reach the model, so Send sends what is shown

Tests assert the model: type, Undo, Redo, Undo, Return sends "Flour"
(not the undone " delivery"); a Shift+Return new line undone and redone.
Both fail without the watcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary

Fable's S1: a Mac field's Undo and Redo change the field but post no text-change, so SwiftUI's binding kept the text taken back and Return or Send posted it (on main for plain typing). ComposerInput now watches the field editor's undo manager (did-undo, did-redo) while the composer has focus and tells the field its text changed, so the field updates the model itself; setting the model directly pushed the text back into the field and lost Redo. The watcher starts when the anchor view is made, not in an init (Fable's N3).
claude agent, operated by paul@keymail.dev d811be95b12f21f95b9b91e516f5ce2d6fbbd88a parent e700f1d
2 files changed, +81 −5
  • native/apple/Sources/BaseKit/Ask/Composer.swift +40 −5
  • native/apple/Tests/BaseKitTests/ComposerTests.swift +41 −0

CI passed — run details

CI log
=== build ===
CGO_ENABLED=0 go build ./...
=== vet ===
go vet ./...
=== fmt-check ===
=== staticcheck ===
go run honnef.co/go/tools/cmd/staticcheck@v0.7.0 ./...
=== govulncheck ===
go run golang.org/x/vuln/cmd/govulncheck@v1.8.0 ./...
No vulnerabilities found.
=== gitleaks ===
11:02AM INF Unknown SCM platform. Use --platform to include links in findings. host=amadan.net
11:02AM INF 1 commits scanned.
11:02AM INF scanned ~2396736 bytes (2.40 MB) in 269ms
11:02AM INF no leaks found
=== budget ===
go run amadan.net/rastrillo/rastrillo/cmd/rastrillo budget size
budget size: 22 directories within budget
=== script-test ===
scripts/prepare-apps_test.sh
ok   releases lookup failure stops before ship
ok   app owned elsewhere stops the script
ok   apps listing failure stops before create
ok   ambiguous release dir is refused
ok   explicit version resolves the ambiguity
ok   releases match is exact, not substring
ok   shipped version is only promoted, existing app not created
ok   fresh account creates, ships, promotes to edge, enables
ok   failing promote stops the script
ok   ship is retried while uploads are rate limited
all passed
hack/fonts_test.sh
fonts_test: ok
=== test ===
go run amadan.net/rastrillo/rastrillo/cmd/rastrillo budget test -count=1 ./...

receipt: test step 7.246s against a 60s target (the gate's other steps are not in this figure)
receipt: 16 packages ran, 0 cached, 6 without tests, 0 failed
receipt: slow package 6.346s amadan.net/oficina/base/internal/webtest
receipt: slow package 678ms amadan.net/oficina/base/internal/issuer
receipt: slow package 589ms amadan.net/oficina/base/internal/operations
receipt: slow package 187ms amadan.net/oficina/base/internal/consumer
receipt: slow package 165ms amadan.net/oficina/base/internal/web
receipt: slow test 1.52s amadan.net/oficina/base/internal/webtest TestHomeDoesNotWaitOnAStalledSnapshot
receipt: slow test 1.08s amadan.net/oficina/base/internal/webtest TestAskZoneIsToldOnce
receipt: slow test 520ms amadan.net/oficina/base/internal/operations TestOperationsRefusals
receipt: slow test 420ms amadan.net/oficina/base/internal/issuer TestAuthorizeSignsForInstalledApp
receipt: slow test 230ms amadan.net/oficina/base/internal/issuer TestAuthorizeRefusals
=== native ===
strings: copy review's strings.json is not here (git-ignored); its comparison skipped
strings: 119 in the catalog, 116 used
worktree-lib_test: ok
diff --git a/native/apple/Sources/BaseKit/Ask/Composer.swift b/native/apple/Sources/BaseKit/Ask/Composer.swift
index b5171c8..fc647b2 100644
--- a/native/apple/Sources/BaseKit/Ask/Composer.swift
+++ b/native/apple/Sources/BaseKit/Ask/Composer.swift
@@ -37,7 +37,8 @@ public struct Composer: View {
.foregroundStyle(p[.frost])
.lineLimit(1...7)
.focused($focused)
- .background(ComposerAnchor(input: input).allowsHitTesting(false).accessibilityHidden(true))
+ .background(ComposerAnchor(input: input, model: model).allowsHitTesting(false).accessibilityHidden(true))
+ .onChange(of: focused, initial: true) { _, now in input.focused = now }
// A hardware keyboard's Return (and a Mac keypad's Enter), as
// the web's keydown. On a Mac a field's own Return always
// submits, Shift or not, so the field never sees one of these.
@@ -99,11 +100,41 @@ enum ComposerReturn: Equatable {
/// first responder of the window the anchor is in, while the field has the
/// keyboard (as it does whenever its key handler runs).
@MainActor final class ComposerInput {
+ /// Whether the field has the keyboard (its FocusState).
+ var focused = false
+ weak var model: AskModel?
#if os(macOS)
weak var anchor: NSView?
/// A Mac field's editor.
var editor: NSTextView? { anchor?.window?.firstResponder as? NSTextView }
var composing: Bool { editor?.hasMarkedText() ?? false }
+ private var observers: [NSObjectProtocol] = []
+
+ /// Starts watching the field, once its anchor is in a view (never in
+ /// an init: SwiftUI makes a ComposerInput on every pass of a parent's
+ /// body and keeps only the first).
+ func start() {
+ guard observers.isEmpty else { return }
+ for name in [Notification.Name.NSUndoManagerDidUndoChange, .NSUndoManagerDidRedoChange] {
+ observers.append(NotificationCenter.default.addObserver(forName: name, object: nil, queue: nil) { [weak self] note in
+ // Undo and Redo post on the main thread.
+ nonisolated(unsafe) let manager = note.object as AnyObject?
+ MainActor.assumeIsolated { self?.undone(by: manager) }
+ })
+ }
+ }
+
+ isolated deinit { observers.forEach(NotificationCenter.default.removeObserver) }
+
+ /// Undo and Redo in a Mac field change the field but never tell it,
+ /// so its binding kept the text taken back, and Send sent that. Told
+ /// here as a typed change is, the field updates the model itself
+ /// (setting the model would push the text back into the field and
+ /// lose Redo).
+ func undone(by manager: AnyObject?) {
+ guard focused, let editor, let manager, manager === editor.undoManager, model?.text != editor.string else { return }
+ editor.didChangeText()
+ }
/// A new line in place of the selection, as Option Return in any Mac
/// field: one undoable edit, the cursor after it.
func insertNewline() -> Bool {
@@ -135,16 +166,20 @@ enum ComposerReturn: Equatable {
#if os(macOS)
struct ComposerAnchor: NSViewRepresentable {
let input: ComposerInput
- func makeNSView(context: Context) -> NSView { let view = NSView(); input.anchor = view; return view }
- func updateNSView(_ view: NSView, context: Context) { input.anchor = view }
+ let model: AskModel
+ func makeNSView(context: Context) -> NSView {
+ let view = NSView(); input.anchor = view; input.model = model; input.start(); return view
+ }
+ func updateNSView(_ view: NSView, context: Context) { input.anchor = view; input.model = model }
}
#elseif os(iOS)
struct ComposerAnchor: UIViewRepresentable {
let input: ComposerInput
+ let model: AskModel
func makeUIView(context: Context) -> UIView {
- let view = UIView(); view.isUserInteractionEnabled = false; input.anchor = view; return view
+ let view = UIView(); view.isUserInteractionEnabled = false; input.anchor = view; input.model = model; return view
}
- func updateUIView(_ view: UIView, context: Context) { input.anchor = view }
+ func updateUIView(_ view: UIView, context: Context) { input.anchor = view; input.model = model }
}
#endif
diff --git a/native/apple/Tests/BaseKitTests/ComposerTests.swift b/native/apple/Tests/BaseKitTests/ComposerTests.swift
index 7d902c1..220d46e 100644
--- a/native/apple/Tests/BaseKitTests/ComposerTests.swift
+++ b/native/apple/Tests/BaseKitTests/ComposerTests.swift
@@ -126,6 +126,47 @@ import AppKit
}
}
+extension ComposerKeyTests {
+ private var sentText: String? {
+ transport.bodies("/ask/messages").last.flatMap { $0 }.flatMap { URLComponents(string: "?" + String(decoding: $0, as: UTF8.self))?.queryItems?.first { $0.name == "text" }?.value }
+ }
+
+ /// Fable's S1: Undo in a Mac field told the field and not the model, so
+ /// Return sent the text that had been taken back.
+ func testReturnSendsWhatUndoLeft() throws {
+ try type("Flour")
+ let editor = try XCTUnwrap(editor)
+ editor.breakUndoCoalescing()
+ editor.insertText(" delivery", replacementRange: editor.selectedRange())
+ settle()
+ XCTAssertEqual(model.text, "Flour delivery")
+ editor.undoManager?.undo()
+ settle()
+ XCTAssertEqual(editor.string, "Flour")
+ XCTAssertEqual(model.text, "Flour", "Undo reaches the model")
+ editor.undoManager?.redo()
+ settle()
+ XCTAssertEqual(model.text, "Flour delivery", "and so does Redo")
+ editor.undoManager?.undo()
+ settle()
+ try press()
+ XCTAssertEqual(sentText, "Flour", "Return sends what the field shows")
+ }
+
+ func testUndoingANewLineReachesTheModel() throws {
+ try type("Flour delivery", cursor: 5)
+ try press(.shift)
+ XCTAssertEqual(model.text, "Flour\n delivery")
+ editor?.undoManager?.undo()
+ settle()
+ XCTAssertEqual(model.text, "Flour delivery")
+ editor?.undoManager?.redo()
+ settle()
+ XCTAssertEqual(model.text, "Flour\n delivery")
+ XCTAssertFalse(sent)
+ }
+}
+
private struct FocusedComposer: View {
let model: AskModel
@FocusState private var focused: Bool