diff --git a/crates/thing-core/src/command/mod.rs b/crates/thing-core/src/command/mod.rs index 0777e8d..0b320ca 100644 --- a/crates/thing-core/src/command/mod.rs +++ b/crates/thing-core/src/command/mod.rs @@ -251,8 +251,8 @@ impl Commands { ledger: &mut Ledger, ) -> Vec { let surface = tree.resolved().document().record_key(); - if self.confirming.is_some() { - return vec![refused(&surface, &act.node, "a command is waiting for confirmation")]; + if let Some(waiting) = self.confirming_refusal(&surface, &act.node) { + return vec![waiting]; } let Some(located) = tree.locate(&act.node) else { return vec![refused(&surface, &act.node, "the node is not in the surface")]; @@ -262,15 +262,16 @@ impl Commands { return vec![refused(&surface, &act.node, "the node fires no command for this activation")]; }; if let ActivationKind::Change(value) = &act.kind { + // A control that takes no change holds no value the user did not send: the gate is before the draft. + if let Some(held) = self.refuses_change(tree, &act.node) { + return vec![held]; + } match capture::change(tree, &act.node, located.node, value) { Ok(taken) => drafts.set(&surface, &act.node, taken), Err(detail) => return vec![refused(&surface, &act.node, detail)], } } - if matches!(located.node, Node::Button(_)) && tree.value(&act.node, Field::Enabled) == Some(&Bound::Bool(false)) { - return Vec::new(); - } - if self.busy(&surface, &act.node) { + if self.holds(tree, &act.node) { return Vec::new(); } let Some(decl) = tree.resolved().document().commands.iter().find(|decl| decl.id == command.id) else { @@ -289,6 +290,28 @@ impl Commands { self.begin(prepared, act.node, decl.confirm.as_ref(), now, ids, port, ledger) } + /// Why `node` takes no change now, said as a refusal: a command waits for confirmation, or the + /// control is disabled, or its own invocation waits for a result. A change that is refused writes + /// no draft, so a control that looks disabled never holds a value that was not sent. A press on + /// such a control stays silent (`activate`); a change is an edit the user made and is told. + pub(crate) fn refuses_change(&self, tree: &BoundTree, node: &NodeId) -> Option { + let surface = tree.resolved().document().record_key(); + if let Some(waiting) = self.confirming_refusal(&surface, node) { + return Some(waiting); + } + self.holds(tree, node).then(|| refused(&surface, node, "the control is disabled or waiting for a result")) + } + + fn confirming_refusal(&self, surface: &str, node: &NodeId) -> Option { + self.confirming.is_some().then(|| refused(surface, node, "a command is waiting for confirmation")) + } + + /// Whether `node` is a disabled Button or has an invocation waiting for its result. + fn holds(&self, tree: &BoundTree, node: &NodeId) -> bool { + let disabled = tree.value(node, Field::Enabled) == Some(&Bound::Bool(false)); + disabled || self.busy(&tree.resolved().document().record_key(), node) + } + /// Answer the command waiting on confirmation. Accepting mints with the captures taken at the /// user's action; rejecting drops it and draws no id. pub fn confirm(&mut self, accept: bool, now: Timestamp, ids: &mut dyn IdSource, port: &mut dyn StatePort, ledger: &mut Ledger) { diff --git a/crates/thing-core/src/input/pointer.rs b/crates/thing-core/src/input/pointer.rs index 79d5f99..69d792c 100644 --- a/crates/thing-core/src/input/pointer.rs +++ b/crates/thing-core/src/input/pointer.rs @@ -91,8 +91,8 @@ impl Input { pub fn event(&mut self, event: &InputEvent, seen: &Seen<'_>, owners: &mut Owners<'_>, now: Timestamp) -> Vec { let surface = seen.tree.resolved().document().record_key(); match event { - InputEvent::PointerDown { at, button: PointerButton::Primary, .. } => self.down(*at, seen, owners, &surface), - InputEvent::PointerMove { at, .. } => self.drag(*at, seen, owners.drafts, &surface), + InputEvent::PointerDown { at, button: PointerButton::Primary, .. } => return self.down(*at, seen, owners, &surface), + InputEvent::PointerMove { at, .. } => return self.drag(*at, seen, owners, &surface), InputEvent::PointerUp { at, button: PointerButton::Primary, .. } => return self.up(*at, seen, owners, now, &surface), InputEvent::PointerCancel => self.cancel(&surface, owners.drafts), InputEvent::Wheel { at, delta, unit, .. } => wheel(*at, *delta, *unit, seen, owners.scroll), @@ -101,28 +101,39 @@ impl Input { Vec::new() } - fn down(&mut self, at: (f32, f32), seen: &Seen<'_>, owners: &mut Owners<'_>, surface: &str) { + fn down(&mut self, at: (f32, f32), seen: &Seen<'_>, owners: &mut Owners<'_>, surface: &str) -> Vec { // A press never starts over another: the one in progress ends first. self.cancel(surface, owners.drafts); let index = seen.tree.index(); + let mut said = Vec::new(); self.pointer.press = target(seen.rendered, seen.laid, &index, at).and_then(|hit| match (hit.control, index.get(&hit.node)) { (Control::Slider, Some(Node::Slider(slider))) => { let value = placed_rect(seen.rendered, &hit.node).and_then(|rect| slider_value(slider, rect, at.0))?; + // A slider that takes no change takes no press: nothing is drafted, so nothing is left unsent. + if let Some(held) = owners.commands.refuses_change(seen.tree, &hit.node) { + said.push(held); + return None; + } let prior = owners.drafts.get(surface, &hit.node).cloned(); owners.drafts.set(surface, &hit.node, InputValue::Number(NumberValue { value })); Some(Press::Drag { node: hit.node, prior }) } _ => Some(Press::Target(hit)), }); + said } - fn drag(&mut self, at: (f32, f32), seen: &Seen<'_>, drafts: &mut Drafts, surface: &str) { - let Some(Press::Drag { node, .. }) = &self.pointer.press else { return }; + fn drag(&mut self, at: (f32, f32), seen: &Seen<'_>, owners: &mut Owners<'_>, surface: &str) -> Vec { + let Some(Press::Drag { node, .. }) = &self.pointer.press else { return Vec::new() }; let index = seen.tree.index(); - let Some(Node::Slider(slider)) = index.get(node) else { return }; - if let Some(value) = placed_rect(seen.rendered, node).and_then(|rect| slider_value(slider, rect, at.0)) { - drafts.set(surface, node, InputValue::Number(NumberValue { value })); + let Some(Node::Slider(slider)) = index.get(node) else { return Vec::new() }; + if let Some(held) = owners.commands.refuses_change(seen.tree, node) { + return vec![held]; } + if let Some(value) = placed_rect(seen.rendered, node).and_then(|rect| slider_value(slider, rect, at.0)) { + owners.drafts.set(surface, node, InputValue::Number(NumberValue { value })); + } + Vec::new() } fn up(&mut self, at: (f32, f32), seen: &Seen<'_>, owners: &mut Owners<'_>, now: Timestamp, surface: &str) -> Vec { diff --git a/crates/thing-core/tests/command.rs b/crates/thing-core/tests/command.rs index fac8a63..aa23642 100644 --- a/crates/thing-core/tests/command.rs +++ b/crates/thing-core/tests/command.rs @@ -1198,20 +1198,18 @@ fn tg_surface() -> SurfaceDocument { } #[test] -fn a_completion_clears_the_drafts_it_captured_and_a_newer_edit_survives() { - for newer in [Some(false), None] { - let mut rig = Rig::new(MemState::new(), &tg_surface()); - let mut ids = SeqIds::new(); - rig.act(&nid("t"), ActivationKind::Change(bool_value(true)), PINNED_NOW, &mut ids); - let first = rig.commands.take_outbound().pop().expect("minted"); - if let Some(value) = newer { - // The node is busy: this edit mints nothing, and it is the draft now. - rig.act(&nid("t"), ActivationKind::Change(bool_value(value)), at(10), &mut ids); - assert!(rig.commands.take_outbound().is_empty()); - } - rig.feed(&completed(&first, 1, true, None), at(20)); - assert_eq!(rig.draft(&nid("t")), newer.map(bool_value), "newer edit {newer:?}"); - } +fn a_change_to_a_node_that_waits_is_refused_writes_no_draft_and_the_completion_clears_what_was_sent() { + let mut rig = Rig::new(MemState::new(), &tg_surface()); + let mut ids = SeqIds::new(); + rig.act(&nid("t"), ActivationKind::Change(bool_value(true)), PINNED_NOW, &mut ids); + let first = rig.commands.take_outbound().pop().expect("minted"); + // The node waits for its result: this edit is refused and mints nothing, and it is not a draft. + let said = rig.act(&nid("t"), ActivationKind::Change(bool_value(false)), at(10), &mut ids); + assert_eq!(said.iter().map(|d| d.code).collect::>(), vec![Code::ActivationRefused]); + assert!(rig.commands.take_outbound().is_empty()); + assert_eq!(rig.draft(&nid("t")), Some(bool_value(true))); + rig.feed(&completed(&first, 1, true, None), at(20)); + assert_eq!(rig.draft(&nid("t")), None); } #[test] diff --git a/crates/thing-core/tests/input.rs b/crates/thing-core/tests/input.rs index aeb0846..6c23a10 100644 --- a/crates/thing-core/tests/input.rs +++ b/crates/thing-core/tests/input.rs @@ -10,7 +10,7 @@ use thing_core::command::Commands; use thing_core::diagnostic::{Code, Diagnostic}; use thing_core::input::{Drafts, Input, InputEvent, Modifiers, Owners, PointerButton, Seen, WheelUnit, hit_test, target}; use thing_core::layout::{LaidTree, LayoutFrame, Metrics, Rect, layout}; -use thing_core::port::Ledger; +use thing_core::port::{Address, Change, Entry, Ledger, Record}; use thing_core::render::{RenderInput, Rendered, ScrollOffsets, Session, render}; use thing_core::resolve::{Limits, resolve}; use thing_core::theme::Theme; @@ -615,11 +615,12 @@ fn a_toggle_flips_its_draft_and_fires_on_change_once_per_press() { assert_eq!(commands_of(&first), ["flip"], "{mode:?}"); assert_eq!(first[0].captured[0].value, InputValue::Bool(BoolValue { value: true }), "{mode:?}"); assert_eq!(rig.draft("flag"), Some(InputValue::Bool(BoolValue { value: true }))); - // A second press flips what is shown now (the draft), not the bound value it was. The node still - // has an invocation waiting, so it fires nothing more (as a busy button), and the draft stays flipped. - assert_eq!(rig.click(flag), vec![]); + // A second press is a change to a control with an invocation waiting: it is refused and says so, it + // fires nothing more, and it leaves the draft that was sent (a busy control holds no unsent value). + let said = rig.click(flag); + assert_eq!(said.iter().map(|d| (d.code, d.node.as_ref().map(NodeId::as_str))).collect::>(), vec![(Code::ActivationRefused, Some("flag"))], "{mode:?}"); assert_eq!(rig.minted(), vec![], "{mode:?}"); - assert_eq!(rig.draft("flag"), Some(InputValue::Bool(BoolValue { value: false }))); + assert_eq!(rig.draft("flag"), Some(InputValue::Bool(BoolValue { value: true })), "{mode:?}"); } } @@ -706,6 +707,72 @@ fn a_click_while_a_command_waits_for_confirmation_is_a_refusal_the_event_returns } } +// ----- a control that takes no change holds no unsent value ----- + +fn result_change(sent: &CommandInvocation, clear: bool) -> Change { + let result = CommandResult::completed(sent, seq(1), None, clear, None); + let address = Address { provider: ProviderId::new(PROVIDER), schema: SchemaId::new(CommandResult::SCHEMA_NAME), key: RecordKey::new(result.record_key()) }; + Change::Upsert(Entry { address, record: Record::Result(result) }) +} + +fn refusals(said: &[Diagnostic]) -> Vec<(Code, Option<&str>)> { + said.iter().map(|d| (d.code, d.node.as_ref().map(NodeId::as_str))).collect() +} + +#[test] +fn a_slider_that_waits_for_its_result_takes_no_press_or_drag_and_a_completion_leaves_no_stray_draft() { + for mode in MODES { + let mut rig = Rig::new(MemState::new(), &controls_surface(), mode); + let track = rig.rect("level"); + let along = |fraction: f32| (track.x + fraction * track.w, track.y + 0.5); + let value = |v: f64| Some(InputValue::Number(NumberValue { value: v })); + rig.send(down(along(0.3))); + rig.send(up(along(0.3))); + let sent = rig.minted().remove(0); + assert_eq!(rig.draft("level"), value(30.0), "{mode:?}"); + // The slider waits: a second press and its drag are refused, say so, and write nothing. + assert_eq!(refusals(&rig.send(down(along(0.8)))), vec![(Code::ActivationRefused, Some("level"))], "{mode:?}"); + assert_eq!(rig.draft("level"), value(30.0), "{mode:?}: the press wrote nothing"); + rig.send(mv(along(0.9))); + assert_eq!(rig.draft("level"), value(30.0), "{mode:?}: a refused press starts no drag"); + assert_eq!(rig.send(up(along(0.9))), vec![], "{mode:?}"); + assert_eq!(rig.minted(), vec![], "{mode:?}"); + // The result clears the draft that was sent, and nothing else is left drawn on the slider. + let tree = &rig.tree; + rig.commands.apply(&result_change(&sent, true), Timestamp(5), &|_| Some(tree), &mut rig.drafts, &mut rig.state, &mut rig.ledger); + assert_eq!(rig.draft("level"), None, "{mode:?}"); + } +} + +#[test] +fn a_toggle_pressed_while_it_waits_is_refused_and_a_completion_leaves_the_provider_true() { + for mode in MODES { + let mut rig = Rig::new(MemState::new(), &controls_surface(), mode); + let flag = inside(rig.rect("flag")); + rig.click(flag); + let sent = rig.minted().remove(0); + assert_eq!(refusals(&rig.click(flag)), vec![(Code::ActivationRefused, Some("flag"))], "{mode:?}"); + let tree = &rig.tree; + rig.commands.apply(&result_change(&sent, true), Timestamp(5), &|_| Some(tree), &mut rig.drafts, &mut rig.state, &mut rig.ledger); + assert_eq!(rig.draft("flag"), None, "{mode:?}: nothing survives to show a checked box the provider does not hold"); + } +} + +#[test] +fn a_slider_press_while_a_command_waits_for_confirmation_writes_no_draft() { + for mode in MODES { + let mut document = controls_surface(); + document.commands.iter_mut().find(|c| c.id.as_str() == "go").unwrap().confirm = Some("Sure?".into()); + let mut rig = Rig::new(MemState::new(), &document, mode); + rig.click(inside(rig.rect("a"))); + assert!(rig.commands.confirming().is_some(), "{mode:?}"); + let track = rig.rect("level"); + let said = rig.send(down((track.x + 0.5 * track.w, track.y + 0.5))); + assert_eq!(refusals(&said), vec![(Code::ActivationRefused, Some("level"))], "{mode:?}"); + assert_eq!((rig.draft("level"), rig.drafts.is_empty()), (None, true), "{mode:?}"); + } +} + // ----- through Core::frame ----- struct Host {