From 863aedd7bdb01d1c67cea7eff942bcb01f58287b Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 11 Nov 2026 22:10:03 +0000 Subject: [PATCH] Dialog-local undo: FormDialog::history steps the dialog's own value A FormDialog with history keeps an EditHistory of its value while it is open. Undo and Redo reach it through Commands::take_step, after a focused text field's own steps. A change becomes a step once no gesture runs in the body: runtime::edit::watch_gestures says whether a component's gesture ran inside the body call, counted at the apply step. ParamDialog takes the history prop; the Contour editor and the ParamDialog stories turn it on. --- .../src/ui/layer_style/contour/editor.rs | 1 + .../ui-contract/src/contract/steps/apply.rs | 3 ++ crates/ui-contract/src/runtime/edit.rs | 32 ++++++++++-- .../src/patterns/form_dialogs/dialog.rs | 6 +++ .../patterns/form_dialogs/dialog/stories.rs | 2 +- .../src/patterns/form_dialogs/form.rs | 50 +++++++++++++++++-- .../tests/it/form_dialogs/dialog.rs | 40 +++++++++++++++ .../ui-patterns/tests/it/form_dialogs/mod.rs | 1 + 8 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 crates/ui-patterns/tests/it/form_dialogs/dialog.rs diff --git a/crates/imaging/src/ui/layer_style/contour/editor.rs b/crates/imaging/src/ui/layer_style/contour/editor.rs index c1d396cac..df55d57c9 100644 --- a/crates/imaging/src/ui/layer_style/contour/editor.rs +++ b/crates/imaging/src/ui/layer_style/contour/editor.rs @@ -124,6 +124,7 @@ impl StackDialog for ContourEditorDialog { let sampler = self.sampler.clone(); let shown = FormDialog::new(words.editor.clone(), Bind::bound(&mut self.draft)) .outer_gesture(true) + .history(true) .extra_buttons(extras.iter().map(|(_, label)| FooterButton::new((*label).clone(), DialogRole::Other))) .shaped(|dialog| dialog.side_buttons(true)) .show(ui, |ui, _, draft| { diff --git a/crates/ui-contract/src/contract/steps/apply.rs b/crates/ui-contract/src/contract/steps/apply.rs index 440c4b2e4..2a188f1b6 100644 --- a/crates/ui-contract/src/contract/steps/apply.rs +++ b/crates/ui-contract/src/contract/steps/apply.rs @@ -107,6 +107,9 @@ pub(crate) fn apply_values(comp: &mut C, cx: &mut Cx<'_, C>) -> Va }; let gesture = cx.core.memory.shown::>>(slot); let (running, watched) = (gesture.is_running().then_some(gesture.id), gesture.watched); + if running.is_some() { + crate::runtime::edit::note_running(&cx.core.ctx); + } if gesture.is_running() { gesture.emitted = emitted.or(gesture.emitted.take()); gesture.last_given = given_now.or(gesture.last_given.take()); diff --git a/crates/ui-contract/src/runtime/edit.rs b/crates/ui-contract/src/runtime/edit.rs index 9f5fc250a..354e26903 100644 --- a/crates/ui-contract/src/runtime/edit.rs +++ b/crates/ui-contract/src/runtime/edit.rs @@ -28,19 +28,43 @@ pub(crate) fn latest(ctx: &Context) -> (u64, Option) { pass_state::with::(ctx, |all| (all.serial, all.reason)) } -/// The newest gesture id handed out in this context. +/// The newest gesture id handed out in this context, and how many times a component has reported its gesture running +/// at an apply step. #[derive(Clone, Copy, Default)] -pub(crate) struct Gestures(GestureId); +pub(crate) struct Gestures { + newest: GestureId, + running: u64, +} /// A gesture id no gesture of this context has had: ids grow in the order gestures begin, across components and /// across a component's eviction, as `GestureHost` assumes. pub fn next_gesture_id(ctx: &Context) -> GestureId { pass_state::with::(ctx, |gestures| { - gestures.0 = gestures.0.next(); - gestures.0 + gestures.newest = gestures.newest.next(); + gestures.newest }) } +/// Notes that a component's gesture runs after its apply step, for [`watch_gestures`]. +pub(crate) fn note_running(ctx: &Context) { + if !crate::runtime::measure::active(ctx) { + pass_state::with::(ctx, |gestures| gestures.running += 1); + } +} + +/// Runs `body` and says whether a gesture of a component shown inside it was running after the component's apply +/// step: a drag or a typing run under way. A container that records its body's edits as undo steps waits for the +/// gesture to end, so a drag is one step. A measuring run sees none: gestures run in real runs. +pub fn watch_gestures(ctx: &Context, body: impl FnOnce() -> R) -> (R, bool) { + if crate::runtime::measure::active(ctx) { + return (body(), false); + } + let before = pass_state::with::(ctx, |gestures| gestures.running); + let out = body(); + let after = pass_state::with::(ctx, |gestures| gestures.running); + (out, after != before) +} + /// Notes that the component running gesture `id` was evicted: the gesture ends with `CancelReason::Removed`, which the /// edit hosts read from `FrameOutput::removed_gestures`. pub(crate) fn note_removed(ctx: &Context, id: GestureId) { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index fa851eb46..85cf8f9ab 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -159,6 +159,9 @@ pub struct ParamDialog<'a> { /// Whether the dialog holds one outer gesture over its fields, from open to close. #[prop] outer_gesture: bool, + /// Whether Cmd/Ctrl+Z and Shift+Cmd/Ctrl+Z step the dialog's own values ([`FormDialog::history`]). + #[prop] + history: bool, /// Whether the body is one collection that owns the scroll (a browse list): the dialog's own body scroll is off. #[prop] collection_body: bool, @@ -228,6 +231,7 @@ impl<'a> ParamDialog<'a> { cancel_label: None, alt_resets: true, outer_gesture: false, + history: false, collection_body: false, reset_to: None, layout: ParamLayout::default(), @@ -292,6 +296,7 @@ impl<'a> ParamDialog<'a> { cancel_label, alt_resets, outer_gesture, + history, collection_body, reset_to, layout, @@ -312,6 +317,7 @@ impl<'a> ParamDialog<'a> { .ok_enabled(ok_enabled) .alt_resets(alt_resets) .outer_gesture(outer_gesture) + .history(history) .collection_body(collection_body) .closable(closable) .answers(answers) diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs index 7fa246d11..fce970e0c 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs @@ -25,7 +25,7 @@ const LAYOUTS: [(&str, ParamLayout); 2] = [("Beside", ParamLayout::Beside), ("Be fn blur_story(ui: &mut Ui, data: &mut StoryData, preview: bool, layout: ParamLayout) -> StoryOutput { let preview = *data.value("preview", preview); let layout = *data.choice("layout", layout, &LAYOUTS); - let dialog = ParamDialog::new("blur", "Blur", Param::parse_list(SPEC)).opened(Bind::bound(data.value("open", true))); + let dialog = ParamDialog::new("blur", "Blur", Param::parse_list(SPEC)).opened(Bind::bound(data.value("open", true))).history(true); let dialog = if preview { dialog.preview(picture(ui)).layout(layout) } else { dialog }; dialog.show(ui).into() } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/form.rs b/crates/ui-patterns/src/patterns/form_dialogs/form.rs index 407345774..74ee1344d 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/form.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/form.rs @@ -5,6 +5,7 @@ use super::dialog::ResetFooter; use super::host_requests::HostRequests; use crate::contract::{Bind, BodyCx, Remember as _, ShowContainer as _}; +use crate::runtime::commands::Commands as _; use crate::runtime::requests::HostAnswer; use crate::dialog::{Dialog, DialogOutput}; @@ -16,7 +17,8 @@ use crate::runtime::words::CommonWords; use crate::sections::{DialogRole, FooterButton}; -use craft_behavior::{EditEvent, Mixed, Value}; +use craft_behavior::edit_history::EditHistory; +use craft_behavior::{CancelReason, EditEvent, Mixed, Value}; use egui::{Response, Ui}; @@ -30,6 +32,16 @@ use std::sync::Arc; #[cfg(feature = "testing")] mod stories; +/// The dialog's own undo history of its value while it is open: made on the opening pass, stepped by Undo and Redo. +#[derive(Clone)] +struct History(Option>); + +impl Default for History { + fn default() -> Self { + History(None) + } +} + /// The values the dialog opened with, taken after the body's first pass. #[derive(Clone)] struct Opened(Option); @@ -115,6 +127,8 @@ type ResetWithFn<'a, V> = Box V + 'a>; /// - Keys: Enter is OK unless the body owns it, Escape cancels. Holding Alt turns Cancel into Reset, which restores /// the value from when the dialog opened (and runs [`on_reset`](FormDialog::on_reset) for what the body keeps). /// - The body returns whether the value can be accepted; OK is enabled while it can and `ok_enabled` is true. +/// - With [`history`](FormDialog::history), Cmd/Ctrl+Z and Shift+Cmd/Ctrl+Z step the dialog's own value; a drag or a +/// typing run is one step. #[must_use = "show the dialog with `show`"] #[derive(crate::contract::Pattern)] #[pattern(probe(FormDialog<'static, bool>))] @@ -129,6 +143,7 @@ pub struct FormDialog<'a, V: Value + Default> { ok_reason: Option