diff --git a/crates/ui-egui/src/prefs/prefs_ui/cursors_page_layout_tests.rs b/crates/ui-egui/src/prefs/prefs_ui/cursors_page_layout_tests.rs new file mode 100644 index 00000000000..a4b71f2115e --- /dev/null +++ b/crates/ui-egui/src/prefs/prefs_ui/cursors_page_layout_tests.rs @@ -0,0 +1,164 @@ +//! #16824: the real Cursors page edits the existing draft keys and picker lifecycle. +use craft::ui::dialog::DialogId; +use craft::ui::patterns::preferences::PrefValue; +use egui::accesskit::{Role, Toggled}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT as _, Queryable}; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; + +type H = Harness<'static, Box>; +const CROSSHAIR: &str = "Show Crosshair in Brush Tip"; +const ONLY: &str = "Show Only Crosshair While Painting"; + +fn harness(stored: bool) -> H { + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).with_max_steps(64).build_eframe(move |cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), Default::default()); + app.background_jobs = false; + app.run("prefs.set", json!({"values": {"cursors.showCrosshairInBrushTip": stored}})).unwrap(); + crate::prefs_ui::open_preferences(&mut app, "cursors"); + app + }); + h.run_steps(5); + h +} + +fn preferences(h: &H) -> DialogId { + h.state().framework.dialogs.open_dialogs().into_iter().find(|dialog| dialog.key.as_deref() == Some("preferences")).unwrap().id +} + +fn draft(h: &H) -> Value { + h.state().framework.dialogs.open_dialogs().into_iter().find(|dialog| dialog.key.as_deref() == Some("preferences")).unwrap().fields.unwrap()["values"]["cursors"].clone() +} + +fn painting(h: &mut H, label: &str) { + h.get_by_role_and_label(Role::Group, "Painting Cursors").get_by_role_and_label(Role::RadioButton, label).click(); + h.run_steps(3); +} + +fn check(h: &H, label: &str) -> (bool, bool) { + let node = h.get_by_role_and_label(Role::CheckBox, label); + let node = node.accesskit_node(); + (!node.is_disabled(), node.toggled() == Some(Toggled::True)) +} + +#[test] +fn three_groups_have_ordered_radios_title_case_labels_and_only_implemented_controls() { + let h = harness(false); + let paint = h.get_by_role_and_label(Role::Group, "Painting Cursors"); + let other = h.get_by_role_and_label(Role::Group, "Other Cursors"); + let preview = h.get_by_role_and_label(Role::Group, "Brush Preview"); + assert!(paint.rect().right() <= other.rect().left() && other.rect().right() <= preview.rect().left()); + let radios: Vec<_> = ["Standard", "Precise", "Normal Brush Tip", "Full Size Brush Tip"] + .into_iter() + .map(|label| paint.get_by_role_and_label(Role::RadioButton, label)) + .collect(); + assert!(radios.windows(2).all(|pair| pair[0].rect().center().y < pair[1].rect().center().y)); + assert_eq!(radios[2].accesskit_node().toggled(), Some(Toggled::True)); + assert_eq!(other.get_by_role_and_label(Role::RadioButton, "Standard").accesskit_node().toggled(), Some(Toggled::True)); + assert_eq!(check(&h, CROSSHAIR), (true, false)); + assert_eq!(check(&h, ONLY), (true, false)); + assert!(preview.query_all_by_label("Brush Preview Color").next().is_some()); + for (label, expected) in [ + (CROSSHAIR, "Always show crosshair in center of brush cursor"), + (ONLY, "Switch to only a crosshair while painting to improve performance with large brushes"), + ("Brush Preview Color", "Color to use for brush editing preview"), + ] { + assert_eq!(h.get_by_label(label).accesskit_node().description().as_deref(), Some(expected)); + } + for missing in ["Brush Tip Outline", "No Cursor While Painting", "Show Brush Leash While Smoothing", "Brush Leash Color"] { + assert!(h.query_all_by_label(missing).next().is_none(), "{missing}"); + } +} + +#[test] +fn precise_forces_only_the_shown_check_and_standard_keeps_the_stored_check() { + for stored in [false, true] { + let mut h = harness(stored); + painting(&mut h, "Standard"); + assert_eq!(check(&h, CROSSHAIR), (false, stored)); + assert_eq!(check(&h, ONLY), (false, false)); + painting(&mut h, "Precise"); + assert_eq!(check(&h, CROSSHAIR), (false, true)); + assert_eq!(check(&h, ONLY), (false, false)); + assert_eq!(draft(&h)["showCrosshairInBrushTip"], stored); + assert_eq!(h.state().session.prefs().cursors.show_crosshair_in_brush_tip, stored); + for label in ["Normal Brush Tip", "Full Size Brush Tip"] { + painting(&mut h, label); + assert_eq!(check(&h, CROSSHAIR), (true, stored)); + assert_eq!(check(&h, ONLY), (true, false)); + } + } +} + +#[test] +fn existing_keys_are_edited_by_radios_and_checks_and_cancel_discards_the_draft() { + let mut h = harness(false); + painting(&mut h, "Full Size Brush Tip"); + h.get_by_role_and_label(Role::CheckBox, CROSSHAIR).click(); + h.run_steps(2); + h.get_by_role_and_label(Role::CheckBox, ONLY).click(); + h.run_steps(2); + h.get_by_role_and_label(Role::Group, "Other Cursors").get_by_role_and_label(Role::RadioButton, "Precise").click(); + h.run_steps(3); + let values = draft(&h); + assert_eq!(values["painting"], "fullSizeTip"); + assert_eq!(values["other"], "precise"); + assert_eq!(values["showCrosshairInBrushTip"], true); + assert_eq!(values["showOnlyCrosshairWhilePainting"], true); + let ctx = h.ctx.clone(); + let id = preferences(&h); + crate::dialogs::cancel(h.state_mut(), &ctx, id).unwrap(); + h.run_steps(4); + let saved = &h.state().session.prefs().cursors; + assert_eq!( + serde_json::to_value(saved).unwrap(), + json!({"painting":"normalTip", "other":"standard", "showCrosshairInBrushTip":false, "showOnlyCrosshairWhilePainting":false, "brushPreviewColor":"#ff0000"}) + ); +} + +#[test] +fn preview_swatch_opens_the_named_existing_picker_and_cancel_preferences_discards_its_ok() { + let mut h = harness(false); + h.get_by_label("Brush Preview Color").click(); + h.run_steps(4); + h.get_by_role_and_label(Role::Dialog, "Color Picker (Brush Preview Color)"); + let picker = h.state().framework.dialogs.open_dialogs().last().unwrap().id; + let ctx = h.ctx.clone(); + crate::color_picker_ui::testing::set_color(h.state_mut(), picker, "#123456"); + crate::dialogs::confirm(h.state_mut(), &ctx, picker).unwrap(); + h.run_steps(4); + assert_eq!(draft(&h)["brushPreviewColor"], "#123456"); + assert_eq!(h.state().session.prefs().cursors.brush_preview_color, "#ff0000"); + // The next picker opens with the just-edited draft swatch, before Apply/OK on Preferences. + h.get_by_label("Brush Preview Color").click(); + h.run_steps(4); + h.get_by_role_and_label(Role::Dialog, "Color Picker (Brush Preview Color)"); + let picker = h.state().framework.dialogs.open_dialogs().last().unwrap().id; + assert_eq!(crate::color_picker_ui::testing::current(h.state(), picker).unwrap().color.to_hex(), "#123456"); + crate::dialogs::cancel(h.state_mut(), &ctx, picker).unwrap(); + let id = preferences(&h); + crate::dialogs::cancel(h.state_mut(), &ctx, id).unwrap(); + h.run_steps(4); + assert_eq!(h.state().session.prefs().cursors.brush_preview_color, "#ff0000"); + assert!(!crate::color_picker_ui::window_picker_open(h.state(), crate::color_picker_ui::PickWindow::Preferences)); +} + +#[test] +fn applying_precise_does_not_write_the_forced_crosshair_and_old_values_load() { + let mut h = harness(false); + assert!(crate::prefs_ui::set_draft(h.state_mut(), "cursors.painting", PrefValue::Choice("precise".into()))); + h.run_steps(3); + assert_eq!(check(&h, CROSSHAIR), (false, true)); + crate::prefs_ui::apply_window(h.state_mut()).unwrap(); + assert_eq!(serde_json::to_value(h.state().session.prefs().cursors.painting).unwrap(), "precise"); + assert!(!h.state().session.prefs().cursors.show_crosshair_in_brush_tip); + for value in ["standard", "precise", "normalTip", "fullSizeTip"] { + let mut old = serde_json::to_value(photocraft_engine::prefs::Cursors::default()).unwrap(); + old["painting"] = json!(value); + let loaded: photocraft_engine::prefs::Cursors = serde_json::from_value(old.clone()).unwrap(); + assert_eq!(serde_json::to_value(loaded).unwrap(), old); + } +} diff --git a/crates/ui-egui/src/prefs/prefs_ui/custom_rows.rs b/crates/ui-egui/src/prefs/prefs_ui/custom_rows.rs index 8682d6ebe82..68f3900ca06 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/custom_rows.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/custom_rows.rs @@ -9,18 +9,20 @@ use craft::theme::{ModeChoice, Size, ThemeId}; use craft::theme_store::ui::AppearancePage; use craft::theme_store::{SelectionChange, SuiteView}; use craft::ui::buttons::Button; -use craft::ui::choice::Checkbox; +use craft::ui::choice::{Checkbox, Radio, RadioGroup, Swatch}; use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; use craft::ui::display::Text; use craft::ui::form::Field; -use craft::ui::layout_components::Row; +use craft::ui::layout_components::{Row, Stack}; use craft::ui::patterns::preferences::PathSetting; use craft::ui::pickers::Select; -use craft::ui::runtime::env::{Env, system_appearance}; -use craft::ui::sections::Section; +use craft::ui::runtime::env::{Env, Word, system_appearance}; +use craft::ui::runtime::requests::ColorRequest; +use craft::ui::sections::{GroupBox, Section}; use craft::ui::style::TypeRole; use craft::ui::text::{NumberField, TextField, TextWords}; use serde_json::{Value, json}; +use std::sync::{Arc, Mutex}; use super::fields::{PrefPages, RESET_WARNING_DIALOGS_ROW, choice_label}; use super::{PhotocraftApp, prefs}; @@ -56,6 +58,8 @@ const SYSTEM_TEMP: &str = "(system temp)"; pub(super) struct PrefsCustom { suite: SuiteView, picker_open: bool, + // Shared only by clones for this Preferences pass; never a global pending picker request. + cursor_color: Arc>>, lang: crate::i18n::Lang, gpu_info: Vec, adapters: Vec, @@ -69,6 +73,10 @@ pub(super) struct PrefsCustom { } impl PrefsCustom { + pub(super) fn take_cursor_color(&self) -> Option { + self.cursor_color.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take() + } + /// The rows for the next pass. `picker_open` is the dialog host's answer: whether a Color Picker opened from the /// window is open. pub(super) fn new(app: &PhotocraftApp, picker_open: bool) -> Self { @@ -77,6 +85,7 @@ impl PrefsCustom { PrefsCustom { suite: app.framework.suite().view(), picker_open, + cursor_color: Arc::default(), lang: crate::i18n::Lang::from_pref(&app.session.prefs().interface.language), gpu_info, adapters: app.perf.gpu_info.adapters.clone(), @@ -116,8 +125,11 @@ impl CustomSettings for PrefsCustom { false } - #[cfg(not(target_arch = "wasm32"))] fn page(&mut self, ui: &mut egui::Ui, page: &str, draft: &mut SettingsDraft) -> bool { + if page == "cursors" { + cursors_rows(ui, draft, &self.cursor_color); + } + #[cfg(not(target_arch = "wasm32"))] if page == AGENTS_PAGE { craft::automation::ui::agents::AgentsPage::new(AGENTS_PAGE, draft, &self.agent_status).mcp_flag(self.mcp_flag).show(ui); } @@ -533,5 +545,92 @@ fn tablet_input_row(ui: &mut egui::Ui, draft: &mut SettingsDraft) { help(ui, tl!("Applies at next launch.")); } +const CURSOR_PAINTING: &str = "cursors.painting"; +const CURSOR_CROSSHAIR: &str = "cursors.showCrosshairInBrushTip"; +const CURSOR_ONLY: &str = "cursors.showOnlyCrosshairWhilePainting"; +const CURSOR_OTHER: &str = "cursors.other"; +const CURSOR_COLOR: &str = "cursors.brushPreviewColor"; + +/// Draws only settings already implemented by the engine. Disabled/forced checks are presentation, +/// never writes to the stored crosshair preference when Standard or Precise is selected. +fn cursors_rows(ui: &mut egui::Ui, draft: &mut SettingsDraft, pending: &Mutex>) { + Row::new().show(ui, |ui, _| { + GroupBox::new(tl!("Painting Cursors"), None).fill(false).show(ui, |ui, _| { + let mut painting: String = draft.get(CURSOR_PAINTING).unwrap_or_else(|| "normalTip".into()); + let before = painting.clone(); + RadioGroup::new(tl!("Painting Cursor"), Bind::bound(&mut painting)).show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + for (value, label) in [ + ("standard", tl!("Standard")), + ("precise", tl!("Precise")), + ("normalTip", tl!("Normal Brush Tip")), + ("fullSizeTip", tl!("Full Size Brush Tip")), + ] { + Radio::new(value.to_string()).label(label).show(ui); + } + }); + }); + if painting != before { + draft.set(CURSOR_PAINTING, &painting); + } + let enabled = matches!(painting.as_str(), "normalTip" | "fullSizeTip"); + let stored: bool = draft.get(CURSOR_CROSSHAIR).unwrap_or(false); + let mut checked = stored || painting == "precise"; + Checkbox::new(Bind::bound(&mut checked)) + .label(tl!("Show Crosshair in Brush Tip")) + .tooltip(Word::keyed("setting.cursors.showCrosshairInBrushTip.help", "Always show crosshair in center of brush cursor")) + .enabled(enabled) + .show(ui); + if enabled && checked != stored { + draft.set(CURSOR_CROSSHAIR, &checked); + } + let stored: bool = draft.get(CURSOR_ONLY).unwrap_or(false); + let mut checked = stored; + Checkbox::new(Bind::bound(&mut checked)) + .label(tl!("Show Only Crosshair While Painting")) + .tooltip(Word::keyed( + "setting.cursors.showOnlyCrosshairWhilePainting.help", + "Switch to only a crosshair while painting to improve performance with large brushes", + )) + .enabled(enabled) + .show(ui); + if enabled && checked != stored { + draft.set(CURSOR_ONLY, &checked); + } + }); + GroupBox::new(tl!("Other Cursors"), None).fill(false).show(ui, |ui, _| { + let mut other: String = draft.get(CURSOR_OTHER).unwrap_or_else(|| "standard".into()); + let before = other.clone(); + RadioGroup::new(tl!("Other Cursor"), Bind::bound(&mut other)).show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + Radio::new("standard".to_string()).label(tl!("Standard")).show(ui); + Radio::new("precise".to_string()).label(tl!("Precise")).show(ui); + }); + }); + if other != before { + draft.set(CURSOR_OTHER, &other); + } + }); + GroupBox::new(tl!("Brush Preview"), None).fill(false).show(ui, |ui, _| { + let value: String = draft.get(CURSOR_COLOR).unwrap_or_else(|| "#ff0000".into()); + let color = craft::theme::Srgba::from_hex(&value).unwrap_or_else(|| craft::theme::Srgba::from(craft::theme::Rgba([255, 0, 0, 255]))); + Row::new().show(ui, |ui, _| { + Text::new(tl!("Color:")).show(ui); + let shown = Swatch::new(tl!("Brush Preview Color"), color) + .picker(CURSOR_COLOR) + .tooltip(Word::keyed("setting.cursors.brushPreviewColor.help", "Color to use for brush editing preview")) + .show(ui); + if let Some(request) = shown.pick_color { + *pending.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(request.title(tl!("Brush Preview Color"))); + } + }); + }); + }); +} + +#[cfg(test)] +#[path = "cursors_page_layout_tests.rs"] +mod cursors_page_layout_tests; + #[cfg(test)] mod raw_resolution_alert_tests; diff --git a/crates/ui-egui/src/prefs/prefs_ui/dialogs.rs b/crates/ui-egui/src/prefs/prefs_ui/dialogs.rs index 0ea7e7ba5d4..4c17daa06b1 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/dialogs.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/dialogs.rs @@ -197,7 +197,7 @@ impl StackDialog for PrefsDialog { }, ); self.doc = Some(with_custom.custom(())); - if let Some(request) = out.pick_color { + if let Some(request) = out.pick_color.or_else(|| self.custom.take_cursor_color()) { self.pick = Some(request); } if let Some(message) = ui.data_mut(|data| data.remove_temp::(super::custom_rows::range_alert_id())) { @@ -260,7 +260,7 @@ pub fn open_preferences(app: &mut PhotocraftApp, section: &str) { } }; let pages: Vec = StandardPage::ALL.into_iter().map(StandardPage::info).collect(); - let doc = doc.pages(pages).rules(rules(app.ctx())); + let doc = doc.pages(pages).rules(rules(app.ctx())).custom_page("cursors"); // Agents reach the desktop app only; the web build shows the settings it had before as rows. #[cfg(not(target_arch = "wasm32"))] let doc = doc.custom_page(super::custom_rows::AGENTS_PAGE);