From 8df7f7b374d2353ee70a7ccb384b803fcbc55da6 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 17:23:09 -0800 Subject: [PATCH 1/4] Toggle the Info panel and canvas Color Samplers --- crates/ui-egui/src/panels/info.rs | 7 + .../info_color_samplers_toggle_tests.rs | 155 ++++++++++++++++++ crates/ui-egui/src/panels/region_versions.rs | 6 +- crates/ui-egui/src/panels/registry.rs | 44 ++++- crates/ui-egui/src/tools/analysis/canvas.rs | 2 +- crates/ui-egui/src/tools/analysis/mod.rs | 4 + 6 files changed, 213 insertions(+), 5 deletions(-) create mode 100644 crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs diff --git a/crates/ui-egui/src/panels/info.rs b/crates/ui-egui/src/panels/info.rs index e02f88514e..a5a5375357 100644 --- a/crates/ui-egui/src/panels/info.rs +++ b/crates/ui-egui/src/panels/info.rs @@ -171,6 +171,9 @@ fn channels(v: &Value) -> Option<[u8; N]> { /// unit and the same RGB/CMYK readout as the pointer (one composite pixel each). A point with no colour /// to read is left out. pub(crate) fn sampler_rows(app: &mut PhotocraftApp) -> Vec<(SamplerReadout, [Label; 2])> { + if app.ui.analysis.hide_color_samplers { + return Vec::new(); + } let readouts = sampler_readouts(app); let Some(st) = app.session.active() else { return Vec::new() }; let (size, dpi) = (st.doc.size, f64::from(st.doc.resolution_dpi)); @@ -309,3 +312,7 @@ mod info_ruler_tests; #[cfg(test)] #[path = "info_unit_format_tests.rs"] mod info_unit_format_tests; + +#[cfg(test)] +#[path = "info_color_samplers_toggle_tests.rs"] +mod info_color_samplers_toggle_tests; diff --git a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs new file mode 100644 index 0000000000..99a9814143 --- /dev/null +++ b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs @@ -0,0 +1,155 @@ +use craft::ui::menu::PanelRow; +use craft::ui::runtime::commands::{CommandRef, CommandState}; +use egui::accesskit::{Role, Toggled}; +use egui::{Event, Modifiers, PointerButton, Pos2, Shape}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use super::sampler_rows; +use crate::canvas::testing::to_screen; +use crate::input::shortcut_dispatch::tests::app_harness; +use crate::panels::{panel_docking, region_versions, registry}; +use crate::{PhotocraftApp, Services}; + +const MENU_LABEL: &str = "Color Samplers"; +const POINT: [f64; 2] = [25.5, 18.5]; +type AppHarness = Harness<'static, Box>; + +fn app() -> Box { + PhotocraftApp::new(photocraft_engine::Session::new(), Services::default()) +} + +fn add_document(app: &mut PhotocraftApp) { + app.run("file.new", json!({"width": 64, "height": 48, "background": "white"})).unwrap(); +} + +fn add_sampler(app: &mut PhotocraftApp) { + app.run("view.colorSamplers.add", json!({"x": POINT[0], "y": POINT[1]})).unwrap(); +} + +fn command(app: &PhotocraftApp) -> CommandRef { + let info = registry::entry("info").unwrap(); + let rows = (info.rows)(app); + let [PanelRow::Command(command)] = rows.as_slice() else { panic!("one Info command") }; + command.clone() +} + +fn menu_state(app: &PhotocraftApp) -> CommandState { + let info = registry::entry("info").unwrap(); + let command = command(app); + (info.states)(app, info.id).iter().find(|(cmd, _)| cmd == &command).unwrap().1.clone() +} + +fn fire(app: &mut PhotocraftApp) { + let ctx = app.ctx().clone(); + let command = command(app); + assert!(registry::run_fired(app, &ctx, &command)); +} + +#[test] +fn color_samplers_toggle_is_session_wide_and_requires_points_in_the_active_document() { + let mut app = app(); + let state = |enabled, checked| CommandState::new().enabled(enabled).checked(checked); + assert_eq!(menu_state(&app), state(false, true)); + fire(&mut app); + add_document(&mut app); + assert_eq!(menu_state(&app), state(false, true)); + + add_sampler(&mut app); + assert_eq!(menu_state(&app), state(true, true)); + assert_eq!(sampler_rows(&mut app).len(), 1); + let info_version = region_versions::panel(&app, "info"); + let canvas_version = region_versions::canvas(&app); + let active = app.session.active().unwrap(); + let (revision, history) = (active.revision, active.history.past_len()); + fire(&mut app); + assert_ne!(region_versions::panel(&app, "info"), info_version, "the Info body is invalidated"); + assert_ne!(region_versions::canvas(&app), canvas_version, "the canvas body is invalidated"); + let active = app.session.active().unwrap(); + assert_eq!((active.revision, active.history.past_len()), (revision, history)); + assert_eq!(menu_state(&app), state(true, false)); + assert!(sampler_rows(&mut app).is_empty()); + assert_eq!(crate::analysis::sampler_at(&app, POINT), Some(0), "the sampler tools can still find the stored point"); + + add_document(&mut app); + assert_eq!(menu_state(&app), state(false, false)); + fire(&mut app); + add_sampler(&mut app); + assert_eq!(menu_state(&app), state(true, false)); + assert!(sampler_rows(&mut app).is_empty()); + fire(&mut app); + assert_eq!(sampler_rows(&mut app).len(), 1); + assert!(app.session.set_active(0)); + assert_eq!(menu_state(&app), state(true, true)); + assert_eq!(sampler_rows(&mut app).len(), 1); +} + +fn open_menu(h: &mut AppHarness) { + let at = h.get_by_role_and_label(Role::Tab, "Info").rect().center(); + h.event(Event::PointerMoved(at)); + h.run_steps(1); + for pressed in [true, false] { + h.event(Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE }); + h.run_steps(2); + } +} + +fn sampler_mark(shape: &Shape, at: Pos2) -> bool { + match shape { + Shape::Circle(circle) => circle.center.distance(at) < 0.5 && circle.stroke.width > 0.0, + Shape::Vec(shapes) => shapes.iter().any(|shape| sampler_mark(shape, at)), + Shape::PaintChunk(shapes) => shapes.iter().any(|shape| sampler_mark(&shape.shape, at)), + _ => false, + } +} + +fn shows_marker(h: &AppHarness) -> bool { + let at = to_screen(h.state(), POINT); + h.output().shapes.iter().any(|clipped| clipped.clip_rect.contains(at) && sampler_mark(&clipped.shape, at)) +} + +#[test] +fn the_info_flyout_hides_and_restores_the_readout_and_canvas_marker_without_history() { + let _language = crate::i18n::language_scope(crate::i18n::Lang::EN); + let mut app = app(); + add_document(&mut app); + add_sampler(&mut app); + for panel in ["navigator", "histogram", "info"] { + panel_docking::show_panel(&mut app, panel, Some(true)).unwrap(); + } + app.run("window.panel.iconic", json!({"panel": "info", "iconic": false})).unwrap(); + panel_docking::show_panel(&mut app, "info", Some(true)).unwrap(); + let mut h = app_harness(app); + + assert!(shows_marker(&h)); + assert_eq!(sampler_rows(h.state_mut()).len(), 1); + open_menu(&mut h); + let row = h.get_by_role_and_label(Role::MenuItemCheckBox, MENU_LABEL); + assert_eq!(row.accesskit_node().toggled(), Some(Toggled::True)); + assert!(!row.accesskit_node().is_disabled()); + let stored = h.state_mut().run("view.colorSamplers.list", json!({})).unwrap(); + let active = h.state().session.active().unwrap(); + let (revision, history) = (active.revision, active.history.past_len()); + h.get_by_role_and_label(Role::MenuItemCheckBox, MENU_LABEL).click(); + h.run_steps(4); + + assert!(!shows_marker(&h)); + assert!(sampler_rows(h.state_mut()).is_empty()); + assert_eq!(h.state_mut().run("view.colorSamplers.list", json!({})).unwrap(), stored); + let active = h.state().session.active().unwrap(); + assert_eq!((active.revision, active.history.past_len()), (revision, history)); + + open_menu(&mut h); + let row = h.get_by_role_and_label(Role::MenuItemCheckBox, MENU_LABEL); + assert_eq!(row.accesskit_node().toggled(), Some(Toggled::False)); + row.click(); + h.run_steps(4); + assert!(shows_marker(&h)); + assert_eq!(sampler_rows(h.state_mut()).len(), 1); + + h.state_mut().ui.view.extras = false; + h.run_steps(4); + assert!(!shows_marker(&h)); + assert_eq!(sampler_rows(h.state_mut()).len(), 1, "View > Extras only hides the canvas marker"); +} diff --git a/crates/ui-egui/src/panels/region_versions.rs b/crates/ui-egui/src/panels/region_versions.rs index a426a67991..18a6b21f20 100644 --- a/crates/ui-egui/src/panels/region_versions.rs +++ b/crates/ui-egui/src/panels/region_versions.rs @@ -125,7 +125,9 @@ pub(crate) fn panel(app: &PhotocraftApp, id: &str) -> u64 { } "info" => { let crop = crate::crop_ui::info_size(app).map(|size| size.map(f64::to_bits)); - version(app, (app.hover_doc.map(|at| at.map(f64::to_bits)), app.current_zoom().to_bits(), app.ui.tool_options.eyedropper.size.pixels(), crop)) + let pointer = app.hover_doc.map(|at| at.map(f64::to_bits)); + let sample = (app.current_zoom().to_bits(), app.ui.tool_options.eyedropper.size.pixels()); + version(app, (pointer, sample, crop, app.ui.analysis.hide_color_samplers)) } "cloneSource" => version(app, app.ui.presets_ui.clone_cursor.map(|(document, at)| (document, at.map(f64::to_bits)))), "modifierKeys" => version(app, app.ui.shell.latched), @@ -296,7 +298,7 @@ pub(crate) fn canvas(app: &PhotocraftApp) -> u64 { &ui.selected_guide, &ui.direct_selection, &ui.hidden_work_path, - (&ui.analysis, &ui.slices, &ui.frame_tool, &ui.artboard_tool), + (&ui.analysis, ui.analysis.hide_color_samplers, &ui.slices, &ui.frame_tool, &ui.artboard_tool), )); let sessions = (crate::transform_tool::bar_key(app), crate::puppet_ui::bar_key(app), crate::perspective_ui::bar_key(app), crate::crop_ui::bar::bar_key(app)); diff --git a/crates/ui-egui/src/panels/registry.rs b/crates/ui-egui/src/panels/registry.rs index df2ef2b344..26c1bd1a56 100644 --- a/crates/ui-egui/src/panels/registry.rs +++ b/crates/ui-egui/src/panels/registry.rs @@ -12,7 +12,7 @@ use craft::ui::contract::{Scroll, ShowContainer as _}; use craft::ui::docking::DockHeights; use craft::ui::layout_components::Stack; use craft::ui::menu::PanelRow; -use craft::ui::runtime::commands::{CommandRef, CommandSet as _}; +use craft::ui::runtime::commands::{CommandRef, CommandSet as _, CommandState}; use egui::{Rect, pos2, vec2}; use std::sync::Arc; @@ -145,7 +145,12 @@ pub(crate) static PANELS: &[PanelEntry] = &[ run: super::histogram_panel::run_fired, ..panel("histogram", || tl!("Histogram").to_string(), "chart_line", LEAST, |app, ui| scrolled(app, ui, super::histogram_panel::show)) }, - panel("info", || tl!("Info").to_string(), "info", LEAST, |app, ui| scrolled(app, ui, super::info::info_panel)), + PanelEntry { + rows: |_| vec![PanelRow::Command(CommandRef::from(InfoCommand::ColorSamplers))], + states: info_source_states, + run: info_run, + ..panel("info", || tl!("Info").to_string(), "info", LEAST, |app, ui| scrolled(app, ui, super::info::info_panel)) + }, PanelEntry { heights: Some(HISTORY_HEIGHTS), rows: super::history_menu_ui::panel_rows, @@ -226,6 +231,41 @@ pub(crate) static PANELS: &[PanelEntry] = &[ floating("notes", || tl!("Notes").to_string(), "square_plus", [260.0, 300.0], |app, ui| scrolled(app, ui, crate::tools::analysis::notes_panel::body)), ]; +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, craft::ui::contract::CommandSet)] +#[command(prefix = "info")] +enum InfoCommand { + #[command(label = "Color Samplers")] + ColorSamplers, +} + +fn has_color_samplers(app: &PhotocraftApp) -> bool { + app.session.active().is_some_and(|doc| !doc.color_samplers.is_empty()) +} + +#[derive(craft::ui::contract::MemoProps)] +struct InfoMenuFacts { + available: bool, + visible: bool, +} + +fn info_source_states(app: &PhotocraftApp, name: &'static str) -> Arc { + let facts = InfoMenuFacts { available: has_color_samplers(app), visible: !app.ui.analysis.hide_color_samplers }; + derived_states(app, name, &facts, || { + vec![(CommandRef::from(InfoCommand::ColorSamplers), CommandState::new().enabled(facts.available).checked(facts.visible))] + }) +} + +fn info_run(app: &mut PhotocraftApp, ctx: &egui::Context, command: &CommandRef) -> bool { + if InfoCommand::from_ref(command) != Some(InfoCommand::ColorSamplers) { + return false; + } + if has_color_samplers(app) { + app.ui.analysis.hide_color_samplers = !app.ui.analysis.hide_color_samplers; + ctx.request_repaint(); + } + true +} + /// The Color panel menu's states, from the panel's own state. fn color_states(app: &PhotocraftApp) -> SourceStates { let state = &app.ui.color_panel.panel; diff --git a/crates/ui-egui/src/tools/analysis/canvas.rs b/crates/ui-egui/src/tools/analysis/canvas.rs index 1c9953d725..1cd42e8dfa 100644 --- a/crates/ui-egui/src/tools/analysis/canvas.rs +++ b/crates/ui-egui/src/tools/analysis/canvas.rs @@ -224,7 +224,7 @@ pub(crate) fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect) { let view = &app.ui.view; let (show_count, show_notes, extras) = (view.shows(view.show.count), view.shows(view.show.notes), view.extras); let line = ruler_line(app).filter(|_| extras); - let samplers = samplers_of(app).map(|(s, _)| s).filter(|_| extras); + let samplers = if extras && !app.ui.analysis.hide_color_samplers { samplers_of(app).map(|(samplers, _)| samplers) } else { None }; let groups: Vec = if show_count { app.session.active().map_or_else(Vec::new, |d| { d.doc diff --git a/crates/ui-egui/src/tools/analysis/mod.rs b/crates/ui-egui/src/tools/analysis/mod.rs index d1cbd264ce..5f15ad18e4 100644 --- a/crates/ui-egui/src/tools/analysis/mod.rs +++ b/crates/ui-egui/src/tools/analysis/mod.rs @@ -96,6 +96,9 @@ pub struct AnalysisUi { pub note_color: [f32; 3], /// Ruler options bar: show values in the measurement scale's units. pub use_measurement_scale: bool, + /// The Info panel and canvas hide sampler readouts and marks for this app session. + #[serde(skip)] + pub(crate) hide_color_samplers: bool, } impl Default for AnalysisUi { @@ -107,6 +110,7 @@ impl Default for AnalysisUi { note_author: String::new(), note_color: photocraft_engine::notes_cmds::DEFAULT_NOTE_COLOR, use_measurement_scale: false, + hide_color_samplers: false, } } } From c35196905f007e1045d3ed18504db50946a71579 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 17:43:35 -0800 Subject: [PATCH 2/4] Keep the unavailable Color Samplers menu item checked --- crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs | 2 +- crates/ui-egui/src/panels/registry.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs index 99a9814143..7ed63c8dfa 100644 --- a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs +++ b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs @@ -73,7 +73,7 @@ fn color_samplers_toggle_is_session_wide_and_requires_points_in_the_active_docum assert_eq!(crate::analysis::sampler_at(&app, POINT), Some(0), "the sampler tools can still find the stored point"); add_document(&mut app); - assert_eq!(menu_state(&app), state(false, false)); + assert_eq!(menu_state(&app), state(false, true), "empty documents show the disabled item checked"); fire(&mut app); add_sampler(&mut app); assert_eq!(menu_state(&app), state(true, false)); diff --git a/crates/ui-egui/src/panels/registry.rs b/crates/ui-egui/src/panels/registry.rs index 26c1bd1a56..c7910f94d3 100644 --- a/crates/ui-egui/src/panels/registry.rs +++ b/crates/ui-egui/src/panels/registry.rs @@ -251,7 +251,7 @@ struct InfoMenuFacts { fn info_source_states(app: &PhotocraftApp, name: &'static str) -> Arc { let facts = InfoMenuFacts { available: has_color_samplers(app), visible: !app.ui.analysis.hide_color_samplers }; derived_states(app, name, &facts, || { - vec![(CommandRef::from(InfoCommand::ColorSamplers), CommandState::new().enabled(facts.available).checked(facts.visible))] + vec![(CommandRef::from(InfoCommand::ColorSamplers), CommandState::new().enabled(facts.available).checked(!facts.available || facts.visible))] }) } From 9123df29fc34325b5d69da95020c5dd35359bca2 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 18:13:24 -0800 Subject: [PATCH 3/4] Check the sampler marker at its painted pixel index --- crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs index 7ed63c8dfa..dff51d0749 100644 --- a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs +++ b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs @@ -105,7 +105,8 @@ fn sampler_mark(shape: &Shape, at: Pos2) -> bool { } fn shows_marker(h: &AppHarness) -> bool { - let at = to_screen(h.state(), POINT); + // The engine stores the pixel centre, but the shared ColorSamplers collection uses the pixel index to paint. + let at = to_screen(h.state(), POINT.map(f64::floor)); h.output().shapes.iter().any(|clipped| clipped.clip_rect.contains(at) && sampler_mark(&clipped.shape, at)) } From 6c88ac568f8a9faca1d4d138b9b85806c98db1cb Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 18:40:58 -0800 Subject: [PATCH 4/4] Use the Info panel menu button in the sampler fixture --- .../info_color_samplers_toggle_tests.rs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs index dff51d0749..d9ed6fb3e9 100644 --- a/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs +++ b/crates/ui-egui/src/panels/info_color_samplers_toggle_tests.rs @@ -85,14 +85,20 @@ fn color_samplers_toggle_is_session_wide_and_requires_points_in_the_active_docum assert_eq!(sampler_rows(&mut app).len(), 1); } -fn open_menu(h: &mut AppHarness) { - let at = h.get_by_role_and_label(Role::Tab, "Info").rect().center(); +fn click(h: &mut AppHarness, at: Pos2) { h.event(Event::PointerMoved(at)); h.run_steps(1); for pressed in [true, false] { - h.event(Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE }); - h.run_steps(2); + h.event(Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.run_steps(1); } + h.run_steps(4); +} + +fn open_menu(h: &mut AppHarness) { + let info = h.get_by_role_and_label(Role::TabPanel, "Info"); + let at = info.get_by_role_and_label(Role::Button, "Panel Menu").rect().center(); + click(h, at); } fn sampler_mark(shape: &Shape, at: Pos2) -> bool { @@ -125,15 +131,16 @@ fn the_info_flyout_hides_and_restores_the_readout_and_canvas_marker_without_hist assert!(shows_marker(&h)); assert_eq!(sampler_rows(h.state_mut()).len(), 1); + let stored = h.state_mut().run("view.colorSamplers.list", json!({})).unwrap(); + let active = h.state().session.active().unwrap(); + let (revision, history) = (active.revision, active.history.past_len()); + open_menu(&mut h); let row = h.get_by_role_and_label(Role::MenuItemCheckBox, MENU_LABEL); assert_eq!(row.accesskit_node().toggled(), Some(Toggled::True)); assert!(!row.accesskit_node().is_disabled()); - let stored = h.state_mut().run("view.colorSamplers.list", json!({})).unwrap(); - let active = h.state().session.active().unwrap(); - let (revision, history) = (active.revision, active.history.past_len()); - h.get_by_role_and_label(Role::MenuItemCheckBox, MENU_LABEL).click(); - h.run_steps(4); + let at = row.rect().center(); + click(&mut h, at); assert!(!shows_marker(&h)); assert!(sampler_rows(h.state_mut()).is_empty()); @@ -144,8 +151,8 @@ fn the_info_flyout_hides_and_restores_the_readout_and_canvas_marker_without_hist open_menu(&mut h); let row = h.get_by_role_and_label(Role::MenuItemCheckBox, MENU_LABEL); assert_eq!(row.accesskit_node().toggled(), Some(Toggled::False)); - row.click(); - h.run_steps(4); + let at = row.rect().center(); + click(&mut h, at); assert!(shows_marker(&h)); assert_eq!(sampler_rows(h.state_mut()).len(), 1);