oficina / docs Public

Docs native S1b: bound the command queue (Fable's delta review 1, 2, 4–6)

The commands queued while held (d34e974):
- dropped when the hold greys the controls out (650 ms, anything but a
  composition): grey means the click did not take; nothing is queued
  while grey (ruling 1);
- dropped when the person moves the selection: a queued command applies
  only to the selection it was chosen with (2);
- kept in order: Bold then Italic in one wait gives both (4);
- an IME composition never greys the panel, toolbar or menu, however long
  (CJK writers compose constantly): commands chosen meanwhile run right
  after the commit, and are dropped if the selection moves or access or
  the document changes (ruling 5);
- the queue holds closures over DocumentCommands, which the model builds
  when they run; the views no longer capture the model (6).

Tests, each seen to fail with its part removed: a long non-composition
hold greys out and drops the queue; moving the selection drops it; two
commands run in order; a composition never greys and its command runs
after the commit (replaces the test that asserted greying during one).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 666ab071a6a8411a466c2282b74b5d3628679d24 parent c3a6816
5 files changed, +98 −39
  • native/apple/Sources/DocsKit/DocsEditorContext.swift +1 −2
  • native/apple/Sources/DocsKit/DocsFormatInspector.swift +1 −2
  • native/apple/Sources/DocsKit/DocsModel.swift +44 −25
  • native/apple/Sources/DocsKit/EditorToolbar.swift +1 −2
  • native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift +51 −8
diff --git a/native/apple/Sources/DocsKit/DocsEditorContext.swift b/native/apple/Sources/DocsKit/DocsEditorContext.swift
index e25036a..76a39e9 100644
--- a/native/apple/Sources/DocsKit/DocsEditorContext.swift
+++ b/native/apple/Sources/DocsKit/DocsEditorContext.swift
@@ -24,8 +24,7 @@ struct DocsEditorContext {
/// chosen has been drawn (`DocsModel.whenCommandsReady`).
@MainActor func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) {
guard actions != nil else { return }
- let model = self.model
- model.whenCommandsReady { command(DocumentCommands(model: model, content: model.surface.content)) }
+ model.whenCommandsReady(command)
}
}
extension FocusedValues { @Entry var docsEditor: DocsEditorContext? }
diff --git a/native/apple/Sources/DocsKit/DocsFormatInspector.swift b/native/apple/Sources/DocsKit/DocsFormatInspector.swift
index c7e7383..5772516 100644
--- a/native/apple/Sources/DocsKit/DocsFormatInspector.swift
+++ b/native/apple/Sources/DocsKit/DocsFormatInspector.swift
@@ -28,8 +28,7 @@ import SwiftUI
/// Runs a command now, or once the input pending when it was clicked
/// has been drawn (`DocsModel.whenCommandsReady`).
private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) {
- let model = self.model
- model.whenCommandsReady { command(DocumentCommands(model: model, content: model.surface.content)) }
+ model.whenCommandsReady(command)
}
var body: some View {
diff --git a/native/apple/Sources/DocsKit/DocsModel.swift b/native/apple/Sources/DocsKit/DocsModel.swift
index de6165b..98b644f 100644
--- a/native/apple/Sources/DocsKit/DocsModel.swift
+++ b/native/apple/Sources/DocsKit/DocsModel.swift
@@ -55,12 +55,14 @@ import RastrilloNative
/// The formatting at the selection, for the toolbar: one value,
/// assigned only when it differs (spec §7 Design 10).
private(set) var toolbarSelection = DocumentToolbarSelection()
- /// Commands have been held back (input pending, composing, a
- /// generation catching up) for longer than a glance: the toolbar,
- /// inspector and Format menu show disabled until they can run. Not for
- /// the few milliseconds every key holds them, which would make the
- /// panel blink and SwiftUI evaluate it on every key (S1b step 0). A
- /// click in those milliseconds runs when they end (`whenCommandsReady`).
+ /// Commands have been held back (input pending, a generation catching
+ /// up, a pause) for longer than a glance: the toolbar, inspector and
+ /// Format menu show disabled until they can run, and anything queued
+ /// meanwhile is dropped (grey means the click did not take). Not for the
+ /// few milliseconds every key holds them, which would make the panel
+ /// blink and SwiftUI evaluate it on every key (S1b step 0), and never for
+ /// an IME composition, however long (CJK writers compose constantly):
+ /// commands chosen then are queued and run after the commit.
@ObservationIgnored private var _commandsHeld = false
private(set) var commandsHeld: Bool {
get { access(keyPath: \.commandsHeld); return _commandsHeld }
@@ -70,7 +72,8 @@ import RastrilloNative
/// busy-button rule's 650 ms.
static var commandsHeldAfter: Duration = .milliseconds(650)
@ObservationIgnored private var holdTask: Task<Void, Never>?
- @ObservationIgnored private var readyAction: (@MainActor () -> Void)?
+ /// Commands asked for while held, in order (`whenCommandsReady`).
+ @ObservationIgnored private var readyQueue: [@MainActor (DocumentCommands) -> Void] = []
/// The image sources of the document drawn, for the image loader.
private(set) var imageSources: [String] = []
/// Moves when the editor asks for Open a document… (⌘K): the window
@@ -464,7 +467,7 @@ import RastrilloNative
}
editing.append(command)
if editingTask == nil { editingTask = Task { await drainEdits() } }
- holdCommands()
+ if !composing { holdCommands() }
return true
}
private func drainEdits() async {
@@ -494,17 +497,23 @@ import RastrilloNative
}
func compositionChanged(_ active: Bool) {
composing = active
- if active { holdCommands() }
if !active {
// Trigger (c): composition ended.
surface.flushIfDeferred()
refreshToolbarSelection()
+ // Still held after the commit (its input pending): from now the
+ // usual hold, which may grey out.
+ if !DocumentCommands(model: self, content: surface.content).isEnabled { holdCommands() }
if editingTask == nil { Task { await publish() } }
}
}
func canRender(_ generation: UInt64) -> Bool { !hasPendingInput && !composing && generation == projectionGeneration }
func currentSelection() -> NSRange { selection }
- func selectionMovedExplicitly() { typingMarks = nil }
+ func selectionMovedExplicitly() {
+ typingMarks = nil
+ // A queued command applies only to the selection it was chosen with.
+ readyQueue = []
+ }
func didRender(_ generation: UInt64) {
// Every draw, also of a generation already rendered (plain typing
// reuses its source): the toolbar may show the selection now.
@@ -875,22 +884,26 @@ import RastrilloNative
#endif
let commands = DocumentCommands(model: self, content: surface.content)
if commands.isEnabled || !commands.isAvailable { releaseCommands(run: commands.isEnabled) }
- guard let value = DocumentToolbarSelection(commands: commands),
+ guard let value = DocumentToolbarSelection(commands: DocumentCommands(model: self, content: surface.content)),
value != toolbarSelection else { return }
toolbarSelection = value
}
- /// A toolbar, inspector or menu command: now if commands can run, else
- /// as soon as they can (the input pending when it was clicked has been
- /// drawn), so a click never does nothing. The last one asked for wins.
- func whenCommandsReady(_ action: @escaping @MainActor () -> Void) {
+ /// A toolbar, inspector, menu or key command: now if commands can run,
+ /// else queued, in order, until they can: the input pending when it was
+ /// chosen has been drawn, or the composition committed. Dropped if the
+ /// selection then moves explicitly (it applies only to the selection it
+ /// was chosen with), if editing becomes unavailable, or if the hold
+ /// greys the controls out; and not queued while they are grey.
+ func whenCommandsReady(_ command: @escaping @MainActor (DocumentCommands) -> Void) {
let commands = DocumentCommands(model: self, content: surface.content)
- if commands.isEnabled { action(); return }
- guard commands.isAvailable else { return }
- readyAction = action
- holdCommands()
+ if commands.isEnabled { command(commands); return }
+ guard commands.isAvailable, !commandsHeld else { return }
+ readyQueue.append(command)
+ if !composing { holdCommands() }
}
/// Commands are held from now: after `commandsHeldAfter`, if they still
- /// are, they show disabled.
+ /// are and no composition holds them, they show disabled and the queue
+ /// is dropped.
private func holdCommands() {
guard holdTask == nil, !commandsHeld else { return }
holdTask = Task { @MainActor [weak self] in
@@ -898,17 +911,23 @@ import RastrilloNative
guard let self, !Task.isCancelled else { return }
self.holdTask = nil
let commands = DocumentCommands(model: self, content: self.surface.content)
- if !commands.isEnabled && commands.isAvailable { self.commandsHeld = true }
+ guard !commands.isEnabled, commands.isAvailable, !self.composing else { return }
+ self.readyQueue = []
+ self.commandsHeld = true
}
}
/// Commands can run (or never will, for now): stop holding, and run
- /// the command clicked meanwhile if they can.
+ /// what was queued meanwhile, in order, while they can (each may leave
+ /// input pending again, and the rest then waits for it).
private func releaseCommands(run: Bool) {
holdTask?.cancel(); holdTask = nil
commandsHeld = false
- guard let action = readyAction else { return }
- readyAction = nil
- if run { action() }
+ guard run else { readyQueue = []; return }
+ while !readyQueue.isEmpty {
+ let commands = DocumentCommands(model: self, content: surface.content)
+ guard commands.isEnabled else { if !composing { holdCommands() }; return }
+ readyQueue.removeFirst()(commands)
+ }
}
/// A command from the text view's keys.
func perform(_ action: String) -> Bool {
diff --git a/native/apple/Sources/DocsKit/EditorToolbar.swift b/native/apple/Sources/DocsKit/EditorToolbar.swift
index bc7974a..7e833ff 100644
--- a/native/apple/Sources/DocsKit/EditorToolbar.swift
+++ b/native/apple/Sources/DocsKit/EditorToolbar.swift
@@ -77,8 +77,7 @@ import SwiftUI
/// Runs a command now, or once the input pending when it was tapped
/// has been drawn (`DocsModel.whenCommandsReady`).
private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) {
- let model = self.model
- model.whenCommandsReady { command(DocumentCommands(model: model, content: model.surface.content)) }
+ model.whenCommandsReady(command)
}
}
diff --git a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
index c9accb6..3033707 100644
--- a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
+++ b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
@@ -74,27 +74,70 @@ import XCTest
func testACommandClickedWhileInputIsPendingRunsOnceTheInputIsDrawn() async throws {
typeInTheMiddle("x")
XCTAssertTrue(model.hasPendingInput)
- let model = self.model!
- model.whenCommandsReady { _ = DocumentCommands(model: model, content: model.surface.content).perform("bold") }
+ model.whenCommandsReady { $0.perform("bold") }
XCTAssertNil(model.typingMarks?["strong"], "not while the key is pending")
try await until("the bold clicked meanwhile") { model.typingMarks?["strong"] != nil }
XCTAssertFalse(model.commandsHeld)
}
- /// Commands held back longer than a glance show disabled; a key's few
- /// milliseconds never do (that would blink the panel on every key).
- func testCommandsHeldForLongShowDisabledAndAKeyNeverDoes() async throws {
+ /// Two commands in one wait both apply, in order (delta review 4).
+ func testCommandsQueuedInOneWaitRunInOrder() async throws {
+ typeInTheMiddle("x")
+ model.whenCommandsReady { $0.perform("bold") }
+ model.whenCommandsReady { $0.perform("italic") }
+ try await until("both") { model.typingMarks?["strong"] != nil && model.typingMarks?["em"] != nil }
+ }
+
+ /// A queued command applies only to the selection it was chosen with:
+ /// the person moving the selection drops it (delta review 2).
+ func testMovingTheSelectionDropsAQueuedCommand() async throws {
+ typeInTheMiddle("x")
+ model.whenCommandsReady { $0.perform("bold") }
+ Keyboard.select(NSRange(location: 3, length: 4), in: text)
+ try await until("the key drawn") { DocumentCommands(model: model, content: model.surface.content).isEnabled }
+ try await Task.sleep(for: .milliseconds(50))
+ XCTAssertNil(model.typingMarks?["strong"])
+ XCTAssertFalse(DocumentCommands(model: model, content: model.surface.content).hasMark("strong"), "the selection moved to is not made bold")
+ }
+
+ /// Held longer than a glance by anything but a composition, commands
+ /// show disabled and what was queued is dropped: grey means the click
+ /// did not take (delta review 1). A key's own wait never greys.
+ func testALongHoldGreysOutAndDropsTheQueue() async throws {
typeInTheMiddle("y")
try await until("the key drawn") { DocumentCommands(model: model, content: model.surface.content).isEnabled }
try await Task.sleep(for: .milliseconds(100))
- XCTAssertFalse(model.commandsHeld, "a key's pending input")
+ XCTAssertFalse(model.commandsHeld, "a key's pending input never greys")
let after = DocsModel.commandsHeldAfter
DocsModel.commandsHeldAfter = .milliseconds(30)
defer { DocsModel.commandsHeldAfter = after }
- model.compositionChanged(true)
+ // A generation that has not caught up: held, not composing.
+ let rendered = model.renderedGeneration
+ model.renderedGeneration = rendered &+ 1000
+ model.whenCommandsReady { $0.perform("bold") }
try await until("held commands show disabled") { model.commandsHeld }
+ model.whenCommandsReady { $0.perform("italic") }
+ model.renderedGeneration = rendered
+ model.refreshToolbarSelection()
+ XCTAssertFalse(model.commandsHeld)
+ XCTAssertNil(model.typingMarks?["strong"], "dropped when the controls went grey")
+ XCTAssertNil(model.typingMarks?["em"], "not queued while grey")
+ }
+
+ /// A composition never greys the controls, however long (CJK writers
+ /// compose constantly; ruling on #173): a command chosen meanwhile runs
+ /// right after the commit.
+ func testACompositionNeverGreysAndItsCommandRunsAfterTheCommit() async throws {
+ let after = DocsModel.commandsHeldAfter
+ DocsModel.commandsHeldAfter = .milliseconds(30)
+ defer { DocsModel.commandsHeldAfter = after }
+ model.compositionChanged(true)
+ model.whenCommandsReady { $0.perform("bold") }
+ try await Task.sleep(for: .milliseconds(150))
+ XCTAssertFalse(model.commandsHeld, "a composition never greys")
+ XCTAssertNil(model.typingMarks?["strong"])
model.compositionChanged(false)
- try await until("released") { !model.commandsHeld }
+ try await until("the bold chosen while composing") { model.typingMarks?["strong"] != nil }
}
/// Undo and redo read the observed history, set with each publication: