diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index 7bc06ebc8c..0ef5b77876 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -229,9 +229,9 @@ impl PhotocraftApp { AppShell::new(WindowKey::main()) .window_title(window_title) .screen_mode(Bind::bound(&mut mode)) - .title_bar_if(menu_bar, |ui, _cx, app| panels::region_versions::show_title_bar(app, ui)) - .options_bar(options_bar && !home, |ui, _cx, app| panels::region_versions::show_options_bar(app, ui)) - .status(status_bar && !home, |ui, _cx, app| panels::status_bar(app, ui)) + .title_bar_if(menu_bar, |app| Some(panels::region_versions::title_bar(app)), |ui, _cx, app| panels::title_bar(app, ui)) + .options_bar(options_bar && !home, |app| Some(panels::region_versions::options_bar(app)), |ui, _cx, app| panels::options_bar(app, ui)) + .status(status_bar && !home, |app| Some(panels::region_versions::status_bar(app)), |ui, _cx, app| panels::status_bar(app, ui)) .body(|ui, cx, app| { if home { canvas::document_area(app, ui); diff --git a/crates/ui-egui/src/brush_settle_trials.rs b/crates/ui-egui/src/brush_settle_trials.rs index 43a83c247d..1074ca244b 100644 --- a/crates/ui-egui/src/brush_settle_trials.rs +++ b/crates/ui-egui/src/brush_settle_trials.rs @@ -8,16 +8,17 @@ use egui_kittest::Harness; use serde_json::json; use crate::PhotocraftApp; +use crate::chrome_settle_trials::{ShellSlot, slot_runs}; use crate::state::Tool; type H = Harness<'static, Box>; -/// How the memoized view whose props type is named `props` took part in the last pass. A view inside the options bar's -/// own view (`region_versions::OptionsSlot`) is not decided when that view replays: it replays with it. +/// How the memoized view whose props type is named `props` took part in the last pass. A view inside the options bar is +/// not decided when the shell's options slot replays: it replays with it. fn run_of(h: &H, props: &str) -> Option { let runs = last_pass_memo_runs(&h.ctx); let own = runs.iter().find(|(name, _, _)| name.contains(props)).map(|(_, _, run)| *run); - own.or_else(|| runs.iter().any(|(name, _, run)| name.contains("OptionsSlot<") && *run == MemoRun::Replayed).then_some(MemoRun::Replayed)) + own.or_else(|| (slot_runs(&runs, ShellSlot::Options) == (0, 1)).then_some(MemoRun::Replayed)) } #[test] diff --git a/crates/ui-egui/src/chrome_settle_trials.rs b/crates/ui-egui/src/chrome_settle_trials.rs index 67c9ffd349..0e69330e7f 100644 --- a/crates/ui-egui/src/chrome_settle_trials.rs +++ b/crates/ui-egui/src/chrome_settle_trials.rs @@ -53,6 +53,35 @@ fn chrome_views(h: &H) -> Vec<(&'static str, MemoRun)> { last_frame_memo_runs(&h.ctx).into_iter().filter(|(name, _, _)| CHROME.iter().any(|c| name.contains(c))).map(|(name, _, run)| (name, run)).collect() } +/// The shell's slots of the harness's window, in the order the shell shows them (`AppShell`): the title bar, the +/// options bar, the status bar. +#[derive(Clone, Copy, Debug)] +pub(crate) enum ShellSlot { + Title, + Options, + Status, +} + +/// How the shell's view of `slot` took part in `runs`: `(ran, replayed)`. The shell's slot views share one props type, +/// so they are told apart by id, in the order the shell decides them in every pass, which is the order it shows its +/// slots in. +pub(crate) fn slot_runs(runs: &[(&'static str, egui::Id, MemoRun)], slot: ShellSlot) -> (usize, usize) { + let mut ids: Vec = Vec::new(); + for (name, id, _) in runs { + if name.contains("app_shell::SlotView<") && !ids.contains(id) { + ids.push(*id); + } + } + let Some(&id) = ids.get(slot as usize) else { return (0, 0) }; + let count = |how: MemoRun| runs.iter().filter(|(_, of, run)| *of == id && *run == how).count(); + (count(MemoRun::Ran), count(MemoRun::Replayed)) +} + +/// How the shell's view of `slot` took part in the last frame: `(ran, replayed)`. +fn slot_views(h: &H, slot: ShellSlot) -> (usize, usize) { + slot_runs(&last_frame_memo_runs(&h.ctx), slot) +} + /// The panel tab strips the last pass ran. fn tab_strip_runs(h: &H) -> Vec<(&'static str, u32)> { last_pass_runs(&h.ctx).into_iter().filter(|(ty, _)| ty.contains("TabStrip<'_, craft_ui_panels::docking::PanelKey>")).collect() @@ -68,8 +97,8 @@ fn a_hover_over_the_canvas_replays_the_dock_chrome_and_the_menu_bar() { for step in 1..=3 { h.event(Event::PointerMoved(at + vec2(step as f32, 0.0))); h.run_steps(1); - // The menu bar sits in the title bar's own view: while that replays, the menu bar replays with it. - let title_replayed = views(&h, "TitleSlot<", MemoRun::Replayed) == 1; + // The menu bar sits in the title slot's view: while that replays, the menu bar replays with it. + let title_replayed = slot_views(&h, ShellSlot::Title).1 == 1; let columns = (views_ran(&h, "area::ColumnView<"), views(&h, "area::ColumnView<", MemoRun::Replayed)); assert!(columns.0 == 0 && columns.1 >= 1, "hover step {step}: the dock's columns replay: {columns:?}"); let views = chrome_views(&h); @@ -94,8 +123,8 @@ fn a_hover_over_the_canvas_replays_the_bars_and_the_panel_bodies() { for step in 1..=3 { h.event(Event::PointerMoved(at + vec2(step as f32, 0.0))); h.run_steps(1); - for slot in ["TitleSlot<", "OptionsSlot<", "status_bar::StatusProps<"] { - assert_eq!((views_ran(&h, slot), views(&h, slot, MemoRun::Replayed)), (0, 1), "hover step {step}: the {slot} view replays"); + for slot in [ShellSlot::Title, ShellSlot::Options, ShellSlot::Status] { + assert_eq!(slot_views(&h, slot), (0, 1), "hover step {step}: the {slot:?} slot's view replays"); } let columns = (views_ran(&h, "area::ColumnView<"), views(&h, "area::ColumnView<", MemoRun::Replayed)); assert!( @@ -189,7 +218,7 @@ fn the_chrome_runs_when_what_it_shows_changes() { h.run_steps(1); // The new tab titles change label widths, so the frame takes a second pass that replays what the first ran: // the frame's views tell. - let (tabs, title) = (views_ran(&h, "docking::group::TabRow<"), views_ran(&h, "TitleSlot<")); + let (tabs, title) = (views_ran(&h, "docking::group::TabRow<"), slot_views(&h, ShellSlot::Title).0); assert!(tabs > 0 && title > 0, "a new language runs the tab rows and the title bar: {tabs} tab rows, {title} title bars"); h.run_steps(2); } @@ -204,7 +233,7 @@ fn the_chrome_runs_when_what_it_shows_changes() { h.state_mut().run("layer.new.layer", json!({})).unwrap(); h.run_steps(1); // The edit's frame takes two passes, and its second replays what the first ran: the frame's views tell. - assert!(views_ran(&h, "TitleSlot<") > 0, "an unsaved edit runs the title bar"); + assert!(slot_views(&h, ShellSlot::Title).0 > 0, "an unsaved edit runs the title bar"); } /// A dialog closed with Escape runs no command, and the menu bar takes the menu keys again: Alt+F opens the File menu. @@ -225,16 +254,26 @@ fn the_menu_keys_open_a_menu_after_a_dialog_closes_with_escape() { assert!(items(&h) > titles, "Alt+F opens the File menu: {} menu items, {titles} titles", items(&h)); } -/// The options bar and status bar views that ran over the next `steps` frames, summed. -fn ran_over(h: &mut H, steps: usize, name: &str) -> usize { +/// What `ran` counts in each of the next `steps` frames, summed. +fn ran_over(h: &mut H, steps: usize, ran: impl Fn(&H) -> usize) -> usize { (0..steps) .map(|_| { h.run_steps(1); - views_ran(h, name) + ran(h) }) .sum() } +/// The options bar's slot view, if it ran in the last frame. +fn options_ran(h: &H) -> usize { + slot_views(h, ShellSlot::Options).0 +} + +/// The status bar's body view, if it ran in the last frame. +fn status_ran(h: &H) -> usize { + views_ran(h, "status_bar::StatusProps<") +} + /// A pan with a tool whose bar shows no view field runs neither the options bar nor the status bar (it shows the zoom /// only); a zoom runs the status bar, and the options bar only while the Zoom tool's bar shows the zoom. #[test] @@ -243,12 +282,12 @@ fn a_pan_runs_neither_bar_and_a_zoom_runs_the_status_bar() { h.state_mut().ui.tool = crate::state::Tool::Move; h.run_steps(4); h.state_mut().ui.views[0].center[0] += 40.0; - assert_eq!(ran_over(&mut h, 3, "OptionsSlot<"), 0, "a pan runs no options bar"); + assert_eq!(ran_over(&mut h, 3, options_ran), 0, "a pan runs no options bar"); h.state_mut().ui.views[0].center[0] += 40.0; - assert_eq!(ran_over(&mut h, 3, "status_bar::StatusProps<"), 0, "a pan runs no status bar"); + assert_eq!(ran_over(&mut h, 3, status_ran), 0, "a pan runs no status bar"); h.state_mut().ui.views[0].zoom *= 2.0; - let status = ran_over(&mut h, 3, "status_bar::StatusProps<"); + let status = ran_over(&mut h, 3, status_ran); assert!(status > 0, "a zoom runs the status bar: {status}"); } @@ -259,7 +298,7 @@ fn a_zoom_runs_the_zoom_tools_options_bar() { h.state_mut().ui.tool = crate::state::Tool::Zoom; h.run_steps(4); h.state_mut().ui.views[0].zoom *= 2.0; - assert!(ran_over(&mut h, 3, "OptionsSlot<") > 0, "a zoom runs the Zoom tool's bar"); + assert!(ran_over(&mut h, 3, options_ran) > 0, "a zoom runs the Zoom tool's bar"); } /// A tool option changed outside the bar runs the bar, and the frames after it replay it. @@ -268,12 +307,12 @@ fn a_tool_option_runs_the_options_bar_once() { let mut h = harness(); h.state_mut().ui.tool = crate::state::Tool::Move; h.run_steps(4); - assert_eq!(ran_over(&mut h, 2, "OptionsSlot<"), 0, "settled"); + assert_eq!(ran_over(&mut h, 2, options_ran), 0, "settled"); let on = h.state().ui.tool_options.move_auto_select; h.state_mut().ui.tool_options.move_auto_select = !on; - assert!(ran_over(&mut h, 1, "OptionsSlot<") > 0, "a changed option runs the bar"); + assert!(ran_over(&mut h, 1, options_ran) > 0, "a changed option runs the bar"); // What the bar's frame remembers of the option (the saved preferences) moves no version. - assert_eq!(ran_over(&mut h, 3, "OptionsSlot<"), 0, "the frames after the changed one run no bar"); + assert_eq!(ran_over(&mut h, 3, options_ran), 0, "the frames after the changed one run no bar"); } /// A bar that runs and writes every control's value back leaves the tool options' revision alone, so idle frames never diff --git a/crates/ui-egui/src/menu_floor_trials.rs b/crates/ui-egui/src/menu_floor_trials.rs index 9b9e0ce880..268d033938 100644 --- a/crates/ui-egui/src/menu_floor_trials.rs +++ b/crates/ui-egui/src/menu_floor_trials.rs @@ -1,5 +1,5 @@ //! PhotoCraft's frame floor (frame-design-v2 2.2, 2.3): an idle frame keys the same bytes whatever the size of the -//! documents, presets and recent files, and builds no derived value; a status message runs only the status bar's view; +//! documents, presets and recent files, and builds no derived value; a status message runs only the status slot and its body view; //! the menu model is built only when a fact it shows changed, never for a tool option or a button release, and the //! held model always equals a fresh build. @@ -11,6 +11,7 @@ use photocraft_engine::presets::PresetsExt; use serde_json::json; use crate::PhotocraftApp; +use crate::chrome_settle_trials::{ShellSlot, slot_runs}; use crate::state::Tool; type H = Harness<'static, Box>; @@ -80,8 +81,14 @@ fn a_status_message_runs_only_the_status_bar() { h.run_steps(2); h.state_mut().ui.status = "Saved".into(); h.run_steps(1); - let ran: Vec<&'static str> = last_frame_memo_runs(&h.ctx).into_iter().filter(|(_, _, run)| *run == MemoRun::Ran).map(|(name, _, _)| name).collect(); - assert!(!ran.is_empty() && ran.iter().all(|name| name.contains("status_bar::StatusProps")), "only the status bar's view runs: {ran:?}"); + let runs = last_frame_memo_runs(&h.ctx); + let ran: Vec<&'static str> = runs.iter().filter(|(_, _, run)| *run == MemoRun::Ran).map(|(name, _, _)| *name).collect(); + let slots = [ShellSlot::Title, ShellSlot::Options, ShellSlot::Status].map(|slot| slot_runs(&runs, slot).0); + assert!(slots[0] == 0 && slots[1] == 0 && slots[2] > 0, "of the shell's slots only the status bar runs: {slots:?}"); + assert!( + ran.iter().all(|name| name.contains("status_bar::StatusProps") || name.contains("app_shell::SlotView<")), + "only the status slot and the status bar's body view run: {ran:?}" + ); } #[test] diff --git a/crates/ui-egui/src/panels/region_versions.rs b/crates/ui-egui/src/panels/region_versions.rs index f25581e8a9..c11f8a5709 100644 --- a/crates/ui-egui/src/panels/region_versions.rs +++ b/crates/ui-egui/src/panels/region_versions.rs @@ -1,51 +1,21 @@ -//! The shell's title bar and options bar and every docked panel body as memoized views (#16118). The shell runs a slot -//! every pass and the dock a panel body, and leave memoizing to the app; each reads the whole app, so it is a `MemoView` -//! whose key is a version of what it shows: while the version is the same and no input reaches the slot, the frame adds -//! its last run again instead of running every control in it. The status bar is a memoized view of its own -//! (`status_bar::StatusProps`), over the values it shows. A version covers the app data its slot shows that can change while no -//! input reaches the slot: what a command changes, the active document and layer and the tool in every version; what -//! the slot shows of the canvas view, a job or the brush and colours; and what an edit elsewhere changes without a -//! command, such as a box or pin dragged on the canvas. Input inside a slot runs it whatever its version, and so does a -//! repaint it asks for itself. +//! The versions of what the shell's slots, the docked panels and the document area show (#16118). The shell replays +//! a slot, and PhotoCraft's views a panel body or the document area, while its version holds and no input reaches it: +//! the frame adds its last run again instead of running every control in it. The title bar, the options bar and the +//! status bar are the shell's versioned slots ([`title_bar`], [`options_bar`], [`status_bar`]); each panel body and +//! the document area is a `MemoView` whose key is its version. A version covers the app data its region shows that +//! can change while no input reaches the region: what a command changes, the active document and layer and the tool in +//! every version; what the region shows of the canvas view, a job or the brush and colours; and what an edit elsewhere +//! changes without a command, such as a box or pin dragged on the canvas. Input inside a region runs it whatever its +//! version, and so does a repaint it asks for itself. use std::hash::{Hash, Hasher}; -use craft::ui::contract::{MemoProps, MemoView}; +use craft::ui::contract::{MemoKey, MemoProps, MemoView}; use super::PhotocraftApp; use crate::color_picker_ui::PickWindow; use crate::state::Tool; -/// The props of a shell slot's view: the app the slot reads, which its version stands for. One type per slot, so a -/// trial and the frame's memo log tell the views apart. -macro_rules! slot_props { - ($($name:ident: $what:literal),*) => {$( - #[doc = concat!("The ", $what, "'s view: the app it reads, which its version stands for.")] - #[derive(MemoProps)] - struct $name<'a> { - version: u64, - #[memo_props(covered_by = "version", reason = "the slot's version holds everything the slot shows of the app")] - app: &'a mut PhotocraftApp, - } - )*}; -} -slot_props!(TitleSlot: "title bar", OptionsSlot: "options bar"); - -/// The title bar in its slot, replaying while [`title_bar`]'s version holds. -pub(crate) fn show_title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { - let version = title_bar(app); - let _ = MemoView::new("photocraft.title_bar", TitleSlot { version, app }, |slot: &mut TitleSlot<'_>, ui: &mut egui::Ui| super::title_bar(slot.app, ui)) - .show(ui); -} - -/// The options bar in its slot, replaying while [`options_bar`]'s version holds. -pub(crate) fn show_options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { - let version = options_bar(app); - let _ = - MemoView::new("photocraft.options_bar", OptionsSlot { version, app }, |slot: &mut OptionsSlot<'_>, ui: &mut egui::Ui| super::options_bar(slot.app, ui)) - .show(ui); -} - /// A docked panel's body: the panel and the app it reads, which its version stands for. #[derive(MemoProps)] struct PanelBody<'a> { @@ -84,6 +54,18 @@ pub(crate) fn options_bar(app: &PhotocraftApp) -> u64 { version(app, (bar_view(app), sessions, shown, brush(app), colours(app), gesture, paths)) } +/// The version of the status bar: the readout's item and the zoom of the active canvas, the status message, the +/// render-path note, and the background redraw's and the job's progress. The readout's document values follow the +/// active document and its revision and name the tool, which every version holds; the language its labels are in +/// reaches the slot's view through its place. +pub(crate) fn status_bar(app: &PhotocraftApp) -> u64 { + let item = MemoKey::of(&super::chrome_ui::status_item(&app.ui.chrome)); + let zoom = crate::canvas::active_view(app).map(|view| view.zoom.to_bits()); + let message = (&app.ui.status, app.ui.status_error); + let progress = MemoKey::of(&(crate::background_redraw::progress(app), crate::jobs_ui::status_job(app).map(|(_, progress)| progress))); + version(app, (item, zoom, message, super::chrome_ui::render_path_label(app), progress)) +} + /// The version of the toolbar's footer: the colour chips' two colours, whether the active document is in Quick Mask and /// the screen mode the Screen Mode button shows. A shortcut changed in the keymap reaches the buttons' tooltips through /// the framework, which tracks the keymap a view reads. @@ -350,8 +332,8 @@ fn view_key(view: &crate::state::View) -> impl Hash + '_ { mod tests { use super::*; - /// The brush, the colours, the type defaults, the tool options, the type caret, the Move tool's rollover and an - /// adjustment's live preview move the versions of the bars and panels that show them and of no other, so a drag that + /// The brush, the colours, the type defaults, the tool options, the type caret, the Move tool's rollover, an + /// adjustment's live preview and the status message move the versions of the bars and panels that show them and of no other, so a drag that /// edits one leaves the rest replaying; a version that missed one would replay a stale bar or panel. #[test] fn what_a_region_shows_moves_only_its_version() { @@ -377,7 +359,7 @@ mod tests { .into_iter() .map(|id| (id, panel(app, id))) .collect(); - all.extend([("optionsBar", options_bar(app)), ("titleBar", title_bar(app))]); + all.extend([("optionsBar", options_bar(app)), ("titleBar", title_bar(app)), ("statusBar", status_bar(app))]); all }; let moved = |app: &mut PhotocraftApp, change: &dyn Fn(&mut PhotocraftApp)| -> Vec<&'static str> { @@ -415,6 +397,7 @@ mod tests { app.doc_hist = Some(crate::histogram_panel::PanelHist { key, at: 30.0, hist: Default::default() }); }); assert_eq!(landed, ["histogram"], "a histogram the worker landed"); + assert_eq!(moved(&mut app, &|app| app.ui.status = "Saved".into()), ["statusBar"], "a status message"); } /// Opening or closing a layer's effects list or a group is view state off the document's revision, and a command diff --git a/crates/ui-egui/src/panels/status_bar.rs b/crates/ui-egui/src/panels/status_bar.rs index 2049992d00..63a09680b1 100644 --- a/crates/ui-egui/src/panels/status_bar.rs +++ b/crates/ui-egui/src/panels/status_bar.rs @@ -70,9 +70,10 @@ enum StatusEvent { /// The status bar, in the shell's status slot: the zoom readout of the active document and the latest status /// message, and at the far end the render-path note, the background redraw and the background jobs' progress with -/// Cancel (#210). The body and the trailing group are a memoized view each, so a frame where neither changed replays -/// them. The bar itself stays the slot's direct child: it fills the slot's height and centres its content there, where -/// a memoized view around it would be moved down onto the slot row's line and clipped at the window's bottom edge. +/// Cancel (#210). The slot replays while `region_versions::status_bar` holds; when it runs, the body and the trailing +/// group are a memoized view each, so a frame that changed only one runs only that one. The bar itself stays the slot +/// pane's direct child: it fills the slot's height and centres its content there, where a memoized view around it +/// would be moved down onto the slot row's line and clipped at the window's bottom edge. pub fn status_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let job = crate::jobs_ui::status_job(app); let values = chrome_ui::status_values(app); diff --git a/crates/ui-egui/src/tools/vector_ui/pen_history_steps_tests.rs b/crates/ui-egui/src/tools/vector_ui/pen_history_steps_tests.rs index e9f07eed9c..1984880667 100644 --- a/crates/ui-egui/src/tools/vector_ui/pen_history_steps_tests.rs +++ b/crates/ui-egui/src/tools/vector_ui/pen_history_steps_tests.rs @@ -370,10 +370,15 @@ fn the_options_bar_row_follows_each_path_ingredient_on_its_own() { let steps = app.session.active().unwrap().history.past_len(); let enabled = |h: &egui_kittest::Harness<'_, Box>| ["Selection…", "Shape"].map(|label| !h.get_by_label(label).accesskit_node().is_disabled()); - let mut h = egui_kittest::Harness::builder() - .with_size(egui::vec2(1600.0, 300.0)) - .with_context(app.ctx().clone()) - .build_ui_state(|ui, app| crate::panels::region_versions::show_options_bar(app, ui), app); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1600.0, 300.0)).with_context(app.ctx().clone()).build_ui_state( + |ui, app| { + // The bar in the shell's options slot, keyed by its version as in the app's window. + let _ = craft::ui::shell::AppShell::embedded(craft::ui::runtime::env::WindowKey::new("options")) + .options_bar(true, |app| Some(crate::panels::region_versions::options_bar(app)), |ui, _, app| crate::panels::options_bar(app, ui)) + .show(ui, app); + }, + app, + ); h.run(); assert_eq!(enabled(&h), [false, false], "nothing selected, no Work Path, no draft");