Merge pull request #18894

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 04:47:51 +00:00
commit 8bb27e24a6
6 files changed
+221 -5

No files matched your search

+7
View File
@@ -171,6 +171,9 @@ fn channels<const N: usize>(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;
@@ -0,0 +1,163 @@
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<PhotocraftApp>>;
fn app() -> Box<PhotocraftApp> {
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, true), "empty documents show the disabled item checked");
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 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::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 {
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 {
// 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))
}
#[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);
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 at = row.rect().center();
click(&mut h, at);
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));
let at = row.rect().center();
click(&mut h, at);
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");
}
+4 -2
View File
@@ -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));
+42 -2
View File
@@ -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<SourceStates> {
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.available || 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;
+1 -1
View File
@@ -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<CountGroupMarks> = if show_count {
app.session.active().map_or_else(Vec::new, |d| {
d.doc
+4
View File
@@ -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,
}
}
}