From 2e581946d5936df7d6493a367c73621fef63a0f4 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:49:19 +0100 Subject: [PATCH 1/5] fix(ui-egui): Fill and Stroke Opacity takes 1 to 100 with the range alert (#14649) The Opacity row Edit > Fill and Edit > Stroke share now takes whole percents from 1 to 100, so a typed 0 is clamped to 1 with the framework's range alert, titled after the dialog, as 150 and 12.3 already were for 100. New Layer's own Opacity row keeps 0 to 100. Scripts are unchanged. --- crates/ui-egui/src/dialogs/blend_options.rs | 5 +- crates/ui-egui/src/dialogs/fill_ui.rs | 3 + .../dialogs/fill_ui/opacity_alert_tests.rs | 111 ++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs diff --git a/crates/ui-egui/src/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index 6d48c6ec82..955be98b05 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -2,7 +2,7 @@ //! Layers panel's, so a mode has one key and one shortcut everywhere. use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamKind}; -use craft::ui::runtime::env::Label; +use craft::ui::runtime::env::{Label, UnitKind}; use photocraft_color::BlendMode; use serde_json::{Map, Value, json}; @@ -25,7 +25,8 @@ pub(crate) fn blending_rows() -> Vec { vec![ Param::section(tl!("Blending")), blend_param("mode", tl!("Mode:"), false, BlendMode::Normal), - super::new_layer_ui::opacity_param(), + // Whole percents from 1 (New Layer's row starts at 0): a typed 0 is clamped to 1 with the range alert (#14649). + Param::new("opacity", tl!("Opacity:"), ParamKind::number(Some(1.0), Some(100.0), 100.0, 1.0, 0)).unit(UnitKind::Percent), Param::new("preserveTransparency", tl!("Preserve Transparency"), ParamKind::bool(false)), ] } diff --git a/crates/ui-egui/src/dialogs/fill_ui.rs b/crates/ui-egui/src/dialogs/fill_ui.rs index 7b460e473a..c8de3238f3 100644 --- a/crates/ui-egui/src/dialogs/fill_ui.rs +++ b/crates/ui-egui/src/dialogs/fill_ui.rs @@ -148,5 +148,8 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes } } +#[cfg(test)] +mod opacity_alert_tests; + #[cfg(test)] mod tests; diff --git a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs new file mode 100644 index 0000000000..52d7fa585f --- /dev/null +++ b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs @@ -0,0 +1,111 @@ +//! #14649: the Opacity row that Edit › Fill and Edit › Stroke share takes whole percents from 1 to 100. A typed value +//! past them is clamped, once the field is left, with the range alert titled after the dialog; letters restore the +//! previous value without one. + +use egui::accesskit::Role; +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; + +type H = Harness<'static, Box>; + +/// A document with a selection and the dialog `open` opens, hosted with its alerts. +fn dialog(open: fn(&mut PhotocraftApp)) -> H { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 40, "height": 30})).unwrap(); + app.run("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); + crate::theme::install_fonts(app.ctx()); + crate::theme::apply(app.ctx(), &crate::theme::AppTheme::pro()); + open(&mut app); + let mut h = Harness::builder() + .with_context(app.ctx().clone()) + .with_size(vec2(1200.0, 900.0)) + .with_step_dt(1.0 / 60.0) + .build_ui_state(|ui, app: &mut Box| crate::dialogs::run(app, ui), app); + h.run_steps(4); + h +} + +fn fill(app: &mut PhotocraftApp) { + super::open(app); +} + +fn stroke(app: &mut PhotocraftApp) { + crate::dialogs::stroke_ui::open(app); +} + +fn opacity<'a>(h: &'a H) -> egui_kittest::Node<'a> { + h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().is_some_and(|l| l.trim_end_matches(':') == "Opacity")).expect("the Opacity field") +} + +fn value(h: &H) -> Option { + opacity(h).accesskit_node().numeric_value() +} + +/// Types `text` over Opacity and leaves the field with Tab. +fn type_and_tab(h: &mut H, text: &str) { + let at = opacity(h).rect().center(); + h.hover_at(at); + h.run_steps(1); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); + h.run_steps(1); + } + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.event(egui::Event::Text(text.into())); + h.run_steps(1); + h.key_press(egui::Key::Tab); + h.run_steps(4); +} + +/// The message of the open alert, if any. +fn alert(h: &H) -> Option { + let dialogs = h.state().framework.dialogs.open_dialogs(); + dialogs.iter().find(|d| d.key.as_deref() == Some("alert")).and_then(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_string)) +} + +/// Answers the open alert, leaving the dialog `command` open. +fn dismiss(h: &mut H, command: &str) { + let dialogs = h.state().framework.dialogs.open_dialogs(); + for id in dialogs.iter().filter(|d| d.key.as_deref() != Some(command)).map(|d| d.id) { + h.state_mut().framework.dialogs.close_by_id(id); + } + h.run_steps(3); +} + +fn titles(h: &H) -> Vec { + h.query_all_by_role(Role::Dialog).chain(h.query_all_by_role(Role::AlertDialog)).filter_map(|n| n.accesskit_node().label()).collect() +} + +fn check(open: fn(&mut PhotocraftApp), command: &str, title: &str) { + let mut h = dialog(open); + for (typed, kept) in [("0", 1.0), ("150", 100.0), ("50", 50.0), ("12.3", 100.0)] { + type_and_tab(&mut h, typed); + assert_eq!(value(&h), Some(kept), "{title}: typed {typed}"); + if kept == 50.0 { + assert_eq!(alert(&h), None, "{title}: {typed} is in range"); + continue; + } + let message = alert(&h).unwrap_or_else(|| panic!("{title}: {typed} raises the range alert")); + assert!(message.contains("integer between 1") && message.contains("100"), "{title}: {message}"); + assert!(titles(&h).iter().all(|t| t == title), "{title}: the alert is titled after the dialog: {:?}", titles(&h)); + dismiss(&mut h, command); + } + type_and_tab(&mut h, "42"); + type_and_tab(&mut h, "abc"); + assert_eq!(value(&h), Some(42.0), "{title}: letters restore the previous value"); + assert_eq!(alert(&h), None, "{title}: letters raise no alert"); +} + +#[test] +fn fill_opacity_takes_1_to_100_with_the_range_alert() { + check(fill, super::COMMAND, "Fill"); +} + +#[test] +fn stroke_opacity_takes_1_to_100_with_the_range_alert() { + check(stroke, crate::dialogs::stroke_ui::COMMAND, "Stroke"); +} From 72c5986acecba22b8d39f2bb579476bb9e9c1106 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:26:47 +0100 Subject: [PATCH 2/5] test(ui-egui): leave the locale-dependent 12.3 case out of the Fill opacity test (#14649) The framework reads a typed 12.3 with the system's decimal separator: on a comma system it reads 123 and is clamped to 100, on a dot system it is kept as 12.3. That case needs craft-libs, so the test no longer asserts it. --- crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs index 52d7fa585f..7dc9c7398d 100644 --- a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs +++ b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs @@ -1,6 +1,6 @@ //! #14649: the Opacity row that Edit › Fill and Edit › Stroke share takes whole percents from 1 to 100. A typed value //! past them is clamped, once the field is left, with the range alert titled after the dialog; letters restore the -//! previous value without one. +//! previous value without one. A typed fraction (the reference drops the dot) is left to craft-libs. use egui::accesskit::Role; use egui::vec2; @@ -82,7 +82,8 @@ fn titles(h: &H) -> Vec { fn check(open: fn(&mut PhotocraftApp), command: &str, title: &str) { let mut h = dialog(open); - for (typed, kept) in [("0", 1.0), ("150", 100.0), ("50", 50.0), ("12.3", 100.0)] { + // Not `12.3`: the framework reads it with the system's decimal separator, so the result depends on the machine. + for (typed, kept) in [("0", 1.0), ("150", 100.0), ("50", 50.0)] { type_and_tab(&mut h, typed); assert_eq!(value(&h), Some(kept), "{title}: typed {typed}"); if kept == 50.0 { From ffe56740a236fad68c4c58da98e540d8ab8d441f Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:16:07 +0100 Subject: [PATCH 3/5] test(ui-egui): the Fill opacity alert test fails on an unnamed dialog window (#14649) --- crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs index 7dc9c7398d..99473e18cc 100644 --- a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs +++ b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs @@ -76,8 +76,9 @@ fn dismiss(h: &mut H, command: &str) { h.run_steps(3); } -fn titles(h: &H) -> Vec { - h.query_all_by_role(Role::Dialog).chain(h.query_all_by_role(Role::AlertDialog)).filter_map(|n| n.accesskit_node().label()).collect() +/// The names of the open dialog windows, unnamed ones included. +fn titles(h: &H) -> Vec> { + h.query_all_by_role(Role::Dialog).chain(h.query_all_by_role(Role::AlertDialog)).map(|n| n.accesskit_node().label()).collect() } fn check(open: fn(&mut PhotocraftApp), command: &str, title: &str) { @@ -92,7 +93,8 @@ fn check(open: fn(&mut PhotocraftApp), command: &str, title: &str) { } let message = alert(&h).unwrap_or_else(|| panic!("{title}: {typed} raises the range alert")); assert!(message.contains("integer between 1") && message.contains("100"), "{title}: {message}"); - assert!(titles(&h).iter().all(|t| t == title), "{title}: the alert is titled after the dialog: {:?}", titles(&h)); + let windows = titles(&h); + assert!(windows.len() == 2 && windows.iter().all(|t| t.as_deref() == Some(title)), "{title}: the dialog and its alert: {windows:?}"); dismiss(&mut h, command); } type_and_tab(&mut h, "42"); From 5d7185cf3b2455d9f2045ffe0f715b349e5ca666 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:49:09 +0100 Subject: [PATCH 4/5] fix(ui-egui): a remembered 0% Fill or Stroke opacity reopens at 1% (#14649) The Opacity row now starts at 1, and the framework drops a remembered value the row can't hold, so a 0% saved before this change reopened at the 100% default: a full-strength edit if confirmed unnoticed. Fill and Stroke now open such an Opacity at 1%. A dialog that never remembered an Opacity still opens at 100%, and an Opacity in range is kept. --- crates/ui-egui/src/dialogs/blend_options.rs | 13 +++++++ crates/ui-egui/src/dialogs/fill_ui.rs | 4 +-- .../dialogs/fill_ui/opacity_alert_tests.rs | 35 ++++++++++++++++++- crates/ui-egui/src/dialogs/stroke_ui.rs | 5 +-- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index 06c24feedb..75c3fd07ab 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -4,6 +4,7 @@ use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamKind}; use craft::ui::runtime::env::{Label, UnitKind}; +use craft::ui::runtime::memory::{MemoryStage, SaveKey}; use photocraft_color::BlendMode; use serde_json::{Map, Value, json}; @@ -42,6 +43,18 @@ pub(crate) fn blending_rows(app: &PhotocraftApp) -> Vec { ] } +/// The opening fields of the dialog `key` (Fill or Stroke) over [`blending_rows`]: Opacity 1 when the remembered +/// Opacity is below the row's 1% minimum, empty otherwise. The framework drops a remembered value the row can't hold, +/// so a 0% saved before the row started at 1 would reopen at the 100% default, a full-strength edit (#14649). +pub(crate) fn opening_fields(ctx: &egui::Context, key: &str) -> DialogFields { + let saved: Option = MemoryStage::peek_saved(ctx, &SaveKey::named(super::params::memory_scope(key))); + let mut fields = DialogFields::new(); + if saved.and_then(|saved| saved.number("opacity")).is_some_and(|opacity| opacity < 1.0) { + fields.set("opacity", 1.0); + } + fields +} + /// The engine params of [`blending_rows`]. pub(crate) fn blending_params(fields: &DialogFields) -> Map { let mut p = Map::new(); diff --git a/crates/ui-egui/src/dialogs/fill_ui.rs b/crates/ui-egui/src/dialogs/fill_ui.rs index 975690bb10..1b8a35f7c1 100644 --- a/crates/ui-egui/src/dialogs/fill_ui.rs +++ b/crates/ui-egui/src/dialogs/fill_ui.rs @@ -13,7 +13,7 @@ use photocraft_color::BlendMode; use serde_json::{Value, json}; use crate::PhotocraftApp; -use crate::dialogs::blend_options::{blending_params, blending_rows}; +use crate::dialogs::blend_options::{blending_params, blending_rows, opening_fields}; use crate::dialogs::params::{captured, open_param}; /// The command of the dialog, which is also its dialog key. @@ -88,7 +88,7 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId { let rows = rows(app, [r, g, b], first.clone()); let inks = crate::color_picker_ui::ink_value(craft::theme::Srgba::rgb(r, g, b), app.session.tools.picked_role_cmyk(0)); let reset = defaults([r, g, b], first, app.session.tools.picked_role_cmyk(0)); - let mut fields = DialogFields::new(); + let mut fields = opening_fields(app.ctx(), COMMAND); let remembered: Option = MemoryStage::peek_saved(app.ctx(), &SaveKey::named(REMEMBER_INKS)); let remembered_inks = remembered.as_ref().and_then(|saved| saved.get(&crate::color_picker_ui::ink_field("color"))).cloned(); fields.set(crate::color_picker_ui::ink_field("color"), remembered_inks.unwrap_or(inks)); diff --git a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs index 99473e18cc..c9b37f2475 100644 --- a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs +++ b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs @@ -1,7 +1,11 @@ //! #14649: the Opacity row that Edit › Fill and Edit › Stroke share takes whole percents from 1 to 100. A typed value //! past them is clamped, once the field is left, with the range alert titled after the dialog; letters restore the -//! previous value without one. A typed fraction (the reference drops the dot) is left to craft-libs. +//! previous value without one. A typed fraction (the reference drops the dot) is left to craft-libs. A 0% remembered +//! before the row started at 1 reopens at 1%. +use craft::ui::dialog::DialogId; +use craft::ui::patterns::form_dialogs::{DialogFields, HostedParamDialog}; +use craft::ui::runtime::memory::{SaveKey, set_saved_value}; use egui::accesskit::Role; use egui::vec2; use egui_kittest::Harness; @@ -112,3 +116,32 @@ fn fill_opacity_takes_1_to_100_with_the_range_alert() { fn stroke_opacity_takes_1_to_100_with_the_range_alert() { check(stroke, crate::dialogs::stroke_ui::COMMAND, "Stroke"); } + +/// A dialog's opener. +type Open = fn(&mut PhotocraftApp) -> DialogId; + +/// The Opacity the dialog `open` (saving under `command`) opens at once `saved` is its remembered Opacity, or with no +/// remembered Opacity for `None`. +fn reopened(open: Open, command: &str, saved: Option) -> Option { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 40, "height": 30})).unwrap(); + if let Some(opacity) = saved { + let mut fields = DialogFields::new(); + fields.set("opacity", opacity); + set_saved_value::>(app.ctx(), &SaveKey::named(crate::dialogs::params::memory_scope(command)), Some(fields)); + } + let id = open(&mut app); + app.framework.dialogs.get_by_id::(id).expect("the dialog is open").fields().number("opacity") +} + +/// A 0% remembered while the row started at 0 reopens at the new 1% minimum, not at the 100% default. A dialog that +/// never remembered an Opacity still opens at 100%, and a remembered Opacity in range is kept. +#[test] +fn a_remembered_0_percent_reopens_at_1_percent() { + let dialogs: [(Open, &str); 2] = [(super::open, super::COMMAND), (crate::dialogs::stroke_ui::open, crate::dialogs::stroke_ui::COMMAND)]; + for (open, command) in dialogs { + assert_eq!(reopened(open, command, Some(0.0)), Some(1.0), "{command}: a remembered 0"); + assert_eq!(reopened(open, command, None), Some(100.0), "{command}: nothing remembered"); + assert_eq!(reopened(open, command, Some(50.0)), Some(50.0), "{command}: a remembered 50"); + } +} diff --git a/crates/ui-egui/src/dialogs/stroke_ui.rs b/crates/ui-egui/src/dialogs/stroke_ui.rs index 0cb6aa7710..4c5594130b 100644 --- a/crates/ui-egui/src/dialogs/stroke_ui.rs +++ b/crates/ui-egui/src/dialogs/stroke_ui.rs @@ -11,7 +11,7 @@ use craft::ui::runtime::memory::SaveKey; use serde_json::{Value, json}; use crate::PhotocraftApp; -use crate::dialogs::blend_options::{blending_params, blending_rows}; +use crate::dialogs::blend_options::{blending_params, blending_rows, opening_fields}; use crate::dialogs::fill_ui::hex; use crate::dialogs::params::{captured, open_param}; @@ -44,7 +44,8 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId { let [r, g, b, _] = app.session.tools.foreground; let rows = rows(app, [r, g, b]); let title = Label::from(tl!("Stroke")); - open_param(app, COMMAND, DialogFields::new(), held, move |fields| { + let fields = opening_fields(app.ctx(), COMMAND); + open_param(app, COMMAND, fields, held, move |fields| { ParamDialog::new(COMMAND, title.clone(), Cow::Owned(rows.clone())).values(Bind::bound(fields)).remember(SaveKey::named(REMEMBER)) }) } From b76792ce962f51487d738f3cba7b799dd13ba195 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:44:14 +0100 Subject: [PATCH 5/5] Quick Selection: Alt over a selection marks the ring's centre with a minus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With a selection, holding Alt with Quick Selection now shows a "−" at the centre of its ring, the operation Alt's stroke already has. Shift, no selection, and the unmeasured Shift+Alt, Ctrl+Alt and non-ring painting cursors keep today's cursor. Fixes #16822 --- crates/ui-egui/src/canvas/cursor_policy.rs | 14 ++- .../canvas/quick_selection_cursor_tests.rs | 86 +++++++++++++++++++ crates/ui-egui/src/tools/brush_cursor.rs | 12 ++- 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/src/canvas/quick_selection_cursor_tests.rs diff --git a/crates/ui-egui/src/canvas/cursor_policy.rs b/crates/ui-egui/src/canvas/cursor_policy.rs index ede27e0eef..deefb27b95 100644 --- a/crates/ui-egui/src/canvas/cursor_policy.rs +++ b/crates/ui-egui/src/canvas/cursor_policy.rs @@ -2,7 +2,7 @@ //! which let ⌥ sample or load paint, the Pencil's tip shape, and the pointers of the tools whose //! cursor is no brush tip. -use craft::behavior::{Cursor, PaintedCursor, PointerGlyph}; +use craft::behavior::{Cursor, CursorBadge, PaintedCursor, PointerGlyph}; use craft::keymap::Mods; use photocraft_engine::prefs::PaintingCursor as PaintingPref; @@ -35,6 +35,14 @@ pub(crate) fn brush_tip_centre(tool: Tool, option: bool, show_crosshair: bool) - show_crosshair || matches!(tool, Tool::BackgroundEraser | Tool::ColorReplacement | Tool::Remove) } +/// The mark at the centre of Quick Selection's ring with `mods` held: a "−" while ⌥ would subtract +/// from a selection (#16822). ⇧ alone and no selection keep the plain ring, as in the reference; +/// ⇧⌥, Control+⌥ (the brush-resize drag) and the options bar's mode were not measured and keep it +/// too. `None` for any other tool. +pub(crate) fn quick_selection_badge(app: &PhotocraftApp, mods: Mods) -> Option { + (app.ui.tool == Tool::QuickSelection && mods.alt && !mods.shift && !mods.ctrl && super::tool_feedback::has_selection(app)).then_some(CursorBadge::Subtract) +} + /// Whether the brush cursor is the reference's fixed sample target (a small circle with a cross /// through the hotspot) at any tip size: the Healing Brush's ⌥, the source point, with Painting /// Cursors = Normal Brush Tip (#12113). The other Painting Cursors settings were not measured and @@ -159,6 +167,10 @@ mod healing_alt_cursor_tests; #[path = "paint_bucket_cursor_tests.rs"] mod paint_bucket_cursor_tests; +#[cfg(test)] +#[path = "quick_selection_cursor_tests.rs"] +mod quick_selection_cursor_tests; + #[cfg(test)] #[path = "rectangle_alt_eyedropper_tests.rs"] mod rectangle_alt_eyedropper_tests; diff --git a/crates/ui-egui/src/canvas/quick_selection_cursor_tests.rs b/crates/ui-egui/src/canvas/quick_selection_cursor_tests.rs new file mode 100644 index 0000000000..03fd91ea79 --- /dev/null +++ b/crates/ui-egui/src/canvas/quick_selection_cursor_tests.rs @@ -0,0 +1,86 @@ +//! #16822: with a selection, holding ⌥ with Quick Selection puts a "−" at the centre of its ring; +//! ⇧, or no selection, keeps the plain ring. Driven through real egui input on the real canvas; the +//! assertions read the cursor the tool asked the tool host for. + +use craft::behavior::{Cursor, CursorBadge, PaintedCursor}; +use egui::Modifiers; +use egui_kittest::Harness; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::guide_cursor_tests::{harness, hover}; +use crate::state::Tool; + +/// A point away from the harness's guide at x = 200. +const AT: [f32; 2] = [100.0, 150.0]; + +/// The cursor the tool host was asked for at `AT` with `m` held. +fn cursor(h: &mut Harness<'static, Box>, m: Modifiers) -> Option { + hover(h, AT[0], AT[1], m); + h.state().canvas_tools.last_response.as_ref().and_then(|r| r.cursor.clone()) +} + +/// The ring's badge, `Some(None)` for a ring without one; `None` for any other cursor. +fn ring_badge(c: &Option) -> Option> { + match c { + Some(Cursor::Painted(PaintedCursor::Circle { badge, .. } | PaintedCursor::Outline { badge, .. })) => Some(*badge), + _ => None, + } +} + +/// The ring without its centre mark and badge: the tip's shape alone. +fn ring(c: &Option) -> Option { + match c { + Some(Cursor::Painted(p @ (PaintedCursor::Circle { .. } | PaintedCursor::Outline { .. }))) => Some(p.clone().marked(false, None)), + _ => None, + } +} + +/// Quick Selection with a 100 px tip, over a document with a selection when `selected`. +fn quick_selection(selected: bool) -> Harness<'static, Box> { + let mut h = harness(Tool::QuickSelection); + h.state_mut().session.tools.brush.size = 100.0; + if selected { + h.state_mut().run("select.rect", json!({"x": 20, "y": 20, "width": 120, "height": 120})).unwrap(); + } + h +} + +#[test] +fn alt_over_a_selection_marks_the_centre_of_the_ring() { + let mut h = quick_selection(true); + let plain = cursor(&mut h, Modifiers::NONE); + assert_eq!(ring_badge(&plain), Some(None), "no key: the plain ring"); + let alt = cursor(&mut h, Modifiers::ALT); + assert_eq!(ring_badge(&alt), Some(Some(CursorBadge::Subtract)), "⌥: the ring with a − at its centre"); + assert_eq!(ring(&alt), ring(&plain), "the ring itself is unchanged"); + assert_eq!(ring_badge(&cursor(&mut h, Modifiers::NONE)), Some(None), "⌥ released: the plain ring again"); +} + +/// ⇧ with a selection, and ⌥ with none, were measured to show the plain ring; ⇧⌥ was not measured and +/// keeps it. +#[test] +fn shift_or_no_selection_keeps_the_plain_ring() { + let mut h = quick_selection(true); + let plain = cursor(&mut h, Modifiers::NONE); + for (m, what) in [(Modifiers::SHIFT, "⇧"), (Modifiers::SHIFT | Modifiers::ALT, "⇧⌥")] { + assert_eq!(cursor(&mut h, m), plain, "{what} with a selection"); + } + let mut h = quick_selection(false); + let plain = cursor(&mut h, Modifiers::NONE); + assert_eq!(ring_badge(&plain), Some(None), "the plain ring"); + assert_eq!(cursor(&mut h, Modifiers::ALT), plain, "⌥ with nothing selected"); +} + +/// The Standard and Precise painting cursors are no ring and were not measured: ⌥ leaves them as +/// they were. +#[test] +fn cursors_without_a_ring_keep_their_pointer() { + let mut h = quick_selection(true); + for value in ["standard", "precise"] { + h.state_mut().run("prefs.set", json!({"path": "cursors.painting", "value": value})).unwrap(); + let plain = cursor(&mut h, Modifiers::NONE); + assert_eq!(ring_badge(&plain), None, "{value}: no ring"); + assert_eq!(cursor(&mut h, Modifiers::ALT), plain, "{value}: ⌥ changes nothing"); + } +} diff --git a/crates/ui-egui/src/tools/brush_cursor.rs b/crates/ui-egui/src/tools/brush_cursor.rs index 0dfd344722..d65986e217 100644 --- a/crates/ui-egui/src/tools/brush_cursor.rs +++ b/crates/ui-egui/src/tools/brush_cursor.rs @@ -7,6 +7,7 @@ use craft::behavior::kurbo::Rect; use craft::behavior::{Cursor, CursorIcon, PointerSample}; +use craft::keymap::Mods; use craft::ui::canvas::ToolContext; use craft::ui::canvas_controls::brush::{BrushCursor, BrushCursorOptions, BrushTip, BrushTipOutline, PaintingCursor, TipPose, TipRings}; use photocraft_engine::prefs::PaintingCursor as PaintingPref; @@ -157,9 +158,16 @@ pub(crate) fn cursor(app: &PhotocraftApp, cx: &ToolContext<'_>) -> Cursor { return BrushCursor::new(&tip).centre(centre).painting(painting).icon(icon).cursor(cx, &options, &space.view); } let Some((_, tip)) = &app.brush_tip else { return arrow }; - BrushCursor::new(tip).pose(pose(brush, cx.pointer.as_ref().map(|p| p.sample))).centre(centre).painting(painting).icon(icon).cursor( + let cursor = BrushCursor::new(tip).pose(pose(brush, cx.pointer.as_ref().map(|p| p.sample))).centre(centre).painting(painting).icon(icon).cursor( cx, &options, &space.view, - ) + ); + // Quick Selection's "−" goes on its ring only: the icon and crosshair the other Painting Cursors settings, Caps + // Lock or a small tip give were not measured and stay as they are. + let held = Mods { alt: option, shift: cx.mods.shift || app.ui.shell.latched.shift, ..cx.mods }; + match (cursor, crate::canvas::cursor_policy::quick_selection_badge(app, held)) { + (Cursor::Painted(ring), Some(badge)) => Cursor::Painted(ring.marked(centre, Some(badge))), + (cursor, _) => cursor, + } }