diff --git a/crates/behavior/src/edit_history.rs b/crates/behavior/src/edit_history.rs index 88459dbf5..0dd299939 100644 --- a/crates/behavior/src/edit_history.rs +++ b/crates/behavior/src/edit_history.rs @@ -117,6 +117,11 @@ impl EditHistory { pub fn can_redo(&self) -> bool { !self.redo.is_empty() } + + /// How many steps a redo can go forward. + pub fn redo_len(&self) -> usize { + self.redo.len() + } } #[cfg(test)] @@ -145,4 +150,19 @@ mod tests { assert!(history.step(&mut now, Step::Undo)); assert_eq!(now, 0, "the undo takes the step before the one taken back"); } + + #[test] + fn redo_len_counts_the_undone_steps_until_a_new_step() { + let mut history = EditHistory::new(&0); + history.record(&set(0, 1)); + history.record(&set(1, 2)); + let mut now = 2; + assert!(history.step(&mut now, Step::Undo)); + assert!(history.step(&mut now, Step::Undo)); + assert_eq!(history.redo_len(), 2); + assert!(history.step(&mut now, Step::Redo)); + assert_eq!(history.redo_len(), 1); + history.record(&set(1, 7)); + assert_eq!(history.redo_len(), 0, "a new step drops the redo steps"); + } } diff --git a/crates/imaging/src/ui/fill/mod.rs b/crates/imaging/src/ui/fill/mod.rs index 038b6bf90..87ad0ccc9 100644 --- a/crates/imaging/src/ui/fill/mod.rs +++ b/crates/imaging/src/ui/fill/mod.rs @@ -73,6 +73,7 @@ use craft_ui::primitives::{Checker, IconSpec}; use craft_ui::rails::{LastUsed, RailTool, ToolRail, ToolSlot}; use craft_ui::runtime::commands::{CommandSet as _, Commands as _}; use craft_ui::runtime::env::{Env, Label}; +use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; use craft_ui::runtime::pending::{Job, Pending}; use craft_ui::runtime::requests::ColorRequest; use craft_ui::runtime::words::CommonWords; @@ -263,6 +264,10 @@ pub struct ContentAwareFillWorkspace { ants: Option, /// The events the next pass reports first: what the methods between passes queued, then the pass's own. events: Vec, + /// Where the settings are remembered ([`remember`](ContentAwareFillWorkspace::remember)). + remember: Option, + /// The first pass has read the remembered settings. + recalled: bool, } impl ContentAwareFillWorkspace { @@ -305,6 +310,37 @@ impl ContentAwareFillWorkspace { settings_gesture: false, ants: None, events: Vec::new(), + remember: None, + recalled: false, + } + } + + /// Remembers the fill settings under `key`: the first pass opens with the stored settings when there are any, in + /// place of [`FillOpen::settings`], and Apply, OK and Cancel store them, as production did. + #[must_use] + pub fn remember(mut self, key: SaveKey) -> Self { + self.remember = Some(key); + self + } + + /// The first pass: the stored settings replace the opening ones, with the sampling option they carry as the + /// session's starting point (not a step to undo). The pass reports the change as `SettingsChanged`. + fn recall(&mut self, ctx: &Context) { + if std::mem::replace(&mut self.recalled, true) { + return; + } + let Some(key) = &self.remember else { return }; + let Some(stored) = saved_value::>(ctx, key) else { return }; + self.settings = stored; + self.session.sampling = self.settings.sampling; + self.options.brush_add = self.settings.sampling.brush_adds(); + self.history = EditHistory::new(&self.session); + } + + /// Stores the settings under the remembered key, if any. + fn store(&self, ctx: &Context) { + if let Some(key) = &self.remember { + set_saved_value(ctx, key, Some(self.settings.clone())); } } @@ -344,6 +380,7 @@ impl ContentAwareFillWorkspace { fn pass(&mut self, ui: &mut Ui, cx: WorkspaceCx<'_, dyn FillSource>) -> WorkspacePass { // pc: crates/ui-egui/src/dialogs/caf_ui/view.rs:265 `show`; caf_ui/controls.rs:151 `keys`. let WorkspaceCx { source, open, hold_ok, ok_waits, .. } = cx; + self.recall(ui.ctx()); let env = Env::of(ui); let common = env.words::(); let dialog = env.words::(); @@ -393,6 +430,9 @@ impl ContentAwareFillWorkspace { } else { (shown.output.result(|| work.command()), None) }; + if picked == Some(PICK_APPLY) || held.is_some() || result.is_some() { + work.store(ui.ctx()); + } let events = std::mem::take(&mut work.events); WorkspacePass::new(shown.output.response, events).held(held).result(result) } diff --git a/crates/imaging/src/ui/liquify/mod.rs b/crates/imaging/src/ui/liquify/mod.rs index b71c78073..a1cf5fd0f 100644 --- a/crates/imaging/src/ui/liquify/mod.rs +++ b/crates/imaging/src/ui/liquify/mod.rs @@ -11,11 +11,11 @@ //! displacement field and its preview: it applies each [`LiquifyEvent`] to the field as it comes, //! replays the list after undo, redo and Restore All, and shows the result through //! [`LiquifySource`]. The workspace is a [`FullWindowWorkspace`] that the app's dialog host owns as a -//! `HostedWorkspace`: OK answers [`LiquifyDone`] (the steps `filter.liquify` runs and the settings to -//! remember); Cancel changes nothing. +//! `HostedWorkspace`: OK answers [`LiquifyDone`] (the steps `filter.liquify` runs and the settings); +//! Cancel changes nothing. With [`LiquifyWorkspace::remember`] the workspace stores its settings itself. //! //! ```ignore -//! dialogs.open(HostedWorkspace::new(LiquifyWorkspace::new(open), Arc::new(source))); +//! dialogs.open(HostedWorkspace::new(LiquifyWorkspace::new(open).remember(SETTINGS), Arc::new(source))); //! dialogs.run(ui); //! if let Some(liquify) = dialogs.get_mut::>() { //! for event in liquify.take_events() { @@ -25,14 +25,12 @@ //! LiquifyEvent::Replay(steps) => app.liquify_engine.replay(&steps), //! LiquifyEvent::BackdropWanted(of) => app.load_backdrop(of), //! LiquifyEvent::Preview(on) => app.liquify_engine.show_preview(on), -//! LiquifyEvent::SettingsChanged(settings) => app.prefs.liquify = settings, //! _ => {} //! } //! } //! } //! if let Some(DialogResult::Ok(done)) = dialogs.take::>() { //! app.run("filter.liquify", done.steps); -//! app.prefs.liquify = done.settings; //! } //! ``` //! @@ -84,6 +82,7 @@ use craft_ui::primitives::Checker; use craft_ui::rails::{LastUsed, ToolRail}; use craft_ui::runtime::commands::{CommandSet as _, Commands as _}; use craft_ui::runtime::env::Env; +use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; use craft_ui::runtime::words::CommonWords; use craft_ui::sections::{DialogRole, FooterButton}; @@ -129,8 +128,8 @@ pub struct LiquifyOpen { pub size: [u32; 2], /// The layer's name: the title's subject. pub subject: String, - /// The remembered settings, the SAVED tier (PhotoCraft: `prefs.dialogs["filter.liquify"]`). The Preview check box - /// is not among them: it is on at each opening. + /// The settings the workspace opens with; stored settings under [`LiquifyWorkspace::remember`]'s key replace them. + /// The Preview check box is not among them: it is on at each opening. pub settings: LiquifySettings, /// The recorded steps of a smart filter opened again; empty for a new Liquify. pub steps: Vec, @@ -148,7 +147,7 @@ impl LiquifyOpen { Self { size, subject: subject.into(), ..Self::default() } } - /// The remembered settings. + /// The settings the workspace opens with. #[must_use] pub fn settings(mut self, settings: LiquifySettings) -> Self { self.settings = settings; @@ -266,6 +265,10 @@ pub struct LiquifyWorkspace { /// The settings as the app last heard them, so a change made between passes (a control request, a colour answer) /// is reported once by the next pass. reported: LiquifySettings, + /// Where the settings are remembered ([`remember`](LiquifyWorkspace::remember)). + remember: Option, + /// The first pass has read the remembered settings. + recalled: bool, } /// The parts of the workspace the canvas tool's gestures and the history change together: kept apart from the view so a @@ -302,9 +305,25 @@ impl LiquifyWorkspace { timing: None, cursor: None, preview: true, + remember: None, + recalled: false, } } + /// Remembers the settings under `key` (PhotoCraft's Liquify settings, kept across openings and restarts): the first + /// pass opens with the stored settings when there are any, in place of [`LiquifyOpen::settings`], and every + /// settings change is stored, as production stored them on every change. + #[must_use] + pub fn remember(mut self, key: SaveKey) -> Self { + self.remember = Some(key); + self + } + + /// How many steps Redo can go forward. + pub fn redo_len(&self) -> usize { + self.edit.history.redo_len() + } + /// The cursor the canvas tool asked for in the last pass. pub fn cursor(&self) -> Option<&Cursor> { self.cursor.as_ref() @@ -401,6 +420,13 @@ impl LiquifyWorkspace { fn pass(&mut self, ui: &mut Ui, source: &dyn LiquifySource, open: Bind<'_, bool>) -> WorkspacePass { // pc: crates/ui-egui/src/dialogs/liquify_ui.rs:950 `show` (title, strip, panel, footer, // preview, brush circle); :730 `history_keys`; :740 `keys`. + if !std::mem::replace(&mut self.recalled, true) + && let Some(key) = &self.remember + && let Some(stored) = saved_value::>(ui.ctx(), key) + { + // The pass reports the change as `SettingsChanged`, as it does for any other change before it. + self.edit.settings = stored; + } let env = Env::of(ui); let words = env.words::(); let common = env.words::(); @@ -568,6 +594,9 @@ impl LiquifyWorkspace { if this.edit.settings != this.reported { this.reported = this.edit.settings.clone(); this.edit.events.push(LiquifyEvent::SettingsChanged(this.reported.clone())); + if let Some(key) = &this.remember { + set_saved_value(ui.ctx(), key, Some(this.reported.clone())); + } } let result = shown.output.result(|| this.done()); let events = std::mem::take(&mut this.edit.events); @@ -579,7 +608,7 @@ impl LiquifyWorkspace { &self.edit.session } - /// What the app persists (always: PhotoCraft remembers Liquify's settings on every close). + /// The current settings. pub fn settings(&self) -> &LiquifySettings { &self.edit.settings } diff --git a/crates/imaging/tests/it/fill.rs b/crates/imaging/tests/it/fill.rs index 83cea8672..1f018ba88 100644 --- a/crates/imaging/tests/it/fill.rs +++ b/crates/imaging/tests/it/fill.rs @@ -19,6 +19,7 @@ use craft_ui::canvas::{PixelRect, RasterSource}; use craft_ui::dialog::{DialogHost, DialogResult, HostedWorkspace}; use craft_ui::primitives::Revision; use craft_ui::runtime::access::Role; +use craft_ui::runtime::memory::{SaveKey, saved_value}; use craft_ui::runtime::pending::Job; use craft_ui::testing::{Fixture, pointer, trial}; @@ -95,9 +96,14 @@ struct Rig { impl Rig { fn new(settings: FillSettings, limits: FillLimits) -> Rig { let open = FillOpen::new([LAYER.width, LAYER.height], "Layer 1", limits).settings(settings); + Rig::over(ContentAwareFillWorkspace::new(open)) + } + + /// `workspace` on the host, settled. + fn over(workspace: ContentAwareFillWorkspace) -> Rig { let source: Arc = Arc::new(Source); let mut host = DialogHost::new(); - host.open(HostedWorkspace::new(ContentAwareFillWorkspace::new(open), source)); + host.open(HostedWorkspace::new(workspace, source)); let host = Rc::new(RefCell::new(host)); let events = Rc::new(RefCell::new(Vec::new())); let (shown, kept) = (Rc::clone(&host), Rc::clone(&events)); @@ -261,3 +267,26 @@ fn fill_overlay_tints_the_sampling_area_or_the_excluded_area() { // Excluded Area: the cells inside the plane that are not sampled and not filled. assert_eq!(tinted(true), [false, false, true, false, false, false]); } + +const REMEMBERED: SaveKey = SaveKey::new("trial.fill"); + +/// With `remember`, Cancel stores the settings and the next Content-Aware Fill opens with them. +#[trial] +fn fill_remembers_its_settings_at_cancel() { + let remembered = || ContentAwareFillWorkspace::new(FillOpen::new([LAYER.width, LAYER.height], "Layer 1", FillLimits::new(10, 1000))).remember(REMEMBERED); + let mut rig = Rig::over(remembered()); + rig.with(|work| work.settings_mut().opacity = 10.0); + rig.fixture.idle(2); + assert_eq!(saved_value::>(rig.fixture.ctx(), &REMEMBERED), None, "a change alone stores nothing"); + + let cancel = rig.fixture.by_label("Cancel").expect("the footer has Cancel"); + rig.fixture.click(&cancel); + let stored = saved_value::>(rig.fixture.ctx(), &REMEMBERED); + assert!(stored.is_some_and(|settings| settings.opacity == 10.0), "Cancel stores the settings"); + + let _ = rig.host.borrow_mut().take::(); + let source: Arc = Arc::new(Source); + rig.host.borrow_mut().open(HostedWorkspace::new(remembered(), source)); + rig.fixture.idle(3); + assert_eq!(rig.with(|work| work.settings().opacity), 10.0, "the next opening takes the stored settings"); +} diff --git a/crates/imaging/tests/it/liquify.rs b/crates/imaging/tests/it/liquify.rs index 7b1ab3524..5e9c86352 100644 --- a/crates/imaging/tests/it/liquify.rs +++ b/crates/imaging/tests/it/liquify.rs @@ -6,12 +6,14 @@ use std::sync::Arc; use craft_behavior::{CancelReason, PixelSize}; use craft_imaging::ui::liquify::{ - LiquifyBrush, LiquifyEvent, LiquifyMesh, LiquifyOpen, LiquifySource, LiquifyTool, LiquifyWorkspace, MeshSize, StrokeEvent, StrokeRecorder, + LiquifyBrush, LiquifyEvent, LiquifyMesh, LiquifyOpen, LiquifySettings, LiquifySource, LiquifyTool, LiquifyWorkspace, MeshSize, StrokeEvent, + StrokeRecorder, }; use craft_ui::canvas::{PixelRect, RasterSource}; use craft_ui::dialog::{DialogHost, HostedWorkspace}; use craft_ui::primitives::Revision; use craft_ui::runtime::access::Role; +use craft_ui::runtime::memory::{SaveKey, saved_value}; use craft_ui::testing::input::key_press; use craft_ui::testing::{Fixture, pointer, trial}; use egui::{Color32, ColorImage, Event, Key, Modifiers, Pos2, Vec2}; @@ -86,10 +88,19 @@ impl App { /// The workspace over a solid picture, settled. // A pass's `WorkspaceCx` is built by the host only, so this runs the workspace on a `DialogHost`. fn open() -> (Fixture, Rc>) { + open_workspace(LiquifyWorkspace::new(LiquifyOpen::new(DOCUMENT, "Photo"))) +} + +/// The app's source: a solid picture of the document's size. +fn source() -> Arc { let size = PixelSize::new(DOCUMENT[0], DOCUMENT[1]); - let source: Arc = Arc::new(Source { image: Solid { size, colour: Color32::from_rgb(40, 120, 200) } }); + Arc::new(Source { image: Solid { size, colour: Color32::from_rgb(40, 120, 200) } }) +} + +/// `workspace` over a solid picture, settled. +fn open_workspace(workspace: LiquifyWorkspace) -> (Fixture, Rc>) { let mut host = DialogHost::new(); - host.open(HostedWorkspace::new(LiquifyWorkspace::new(LiquifyOpen::new(DOCUMENT, "Photo")), source)); + host.open(HostedWorkspace::new(workspace, source())); let app = Rc::new(RefCell::new(App { host, events: Vec::new() })); let shown = Rc::clone(&app); let mut fixture = Fixture::new(move |ui| { @@ -212,3 +223,27 @@ fn liquify_preview_key_and_box_each_toggle_the_preview_once() { } // Cmd+I and Cmd+D record the global step in the order pressed; Restore All empties the session. + +const REMEMBERED: SaveKey = SaveKey::new("trial.liquify"); + +/// With `remember`, a settings change is stored at once and the next Liquify opens with the stored settings. +#[trial] +fn liquify_remembers_its_settings_on_every_change() { + let remembered = || LiquifyWorkspace::new(LiquifyOpen::new(DOCUMENT, "Photo")).remember(REMEMBERED); + let (mut fixture, app) = open_workspace(remembered()); + let stored = |fixture: &Fixture| saved_value::>(fixture.ctx(), &REMEMBERED); + assert_eq!(stored(&fixture), None, "nothing changed, nothing stored"); + if let Some(hosted) = app.borrow_mut().host.get_mut::>() { + hosted.workspace_mut().settings_mut().show_mesh = true; + } + fixture.frame(Vec::new()); + assert!(stored(&fixture).is_some_and(|settings| settings.show_mesh), "the change is stored by the pass that reports it"); + + let ctx = fixture.ctx().clone(); + assert!(app.borrow_mut().host.cancel::>(&ctx), "the workspace was open"); + fixture.settle(); + let _ = app.borrow_mut().host.take::>(); + app.borrow_mut().host.open(HostedWorkspace::new(remembered(), source())); + fixture.settle(); + assert!(app.borrow().hosted().workspace().settings().show_mesh, "the next opening takes the stored settings"); +}