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/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index 23314de5d0..19262911f5 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -3,8 +3,8 @@ //! document's colour mode and depth can't use (#13806, #14647). use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, Param, ParamKind, ParamResult}; -use craft::ui::runtime::env::Label; -use craft::ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; +use craft::ui::runtime::env::{Label, UnitKind}; +use craft::ui::runtime::memory::{MemoryStage, SaveKey, saved_value, set_saved_value}; use photocraft_color::BlendMode; use serde_json::{Map, Value, json}; @@ -42,7 +42,13 @@ pub(crate) fn blending_rows(app: &PhotocraftApp, forced: Option) -> Vec::new()).not_remembered(); } - vec![Param::section(tl!("Blending")), document_mode_param(app, tl!("Mode:")), super::new_layer_ui::opacity_param(), preserve] + vec![ + Param::section(tl!("Blending")), + document_mode_param(app, tl!("Mode:")), + // 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), + preserve, + ] } /// The Preserve Transparency that Fill's or Stroke's `target` (as `edit.fill` takes it) forces: off on the Background @@ -76,6 +82,18 @@ pub(crate) fn remember_blending(ctx: &egui::Context, result: &ParamResult) { } } +/// 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 bb5af01b19..07476b4dc8 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, forced_preserve_transparency, remember_blending}; +use crate::dialogs::blend_options::{blending_params, blending_rows, forced_preserve_transparency, opening_fields, remember_blending}; use crate::dialogs::params::{captured, open_param}; /// The command of the dialog, which is also its dialog key. @@ -90,7 +90,7 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId { let rows = rows(app, [r, g, b], first.clone(), preserve); 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), preserve); - 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)); @@ -168,6 +168,8 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes } } +#[cfg(test)] +mod opacity_alert_tests; #[cfg(test)] mod preserve_transparency_state_tests; #[cfg(test)] 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..c9b37f2475 --- /dev/null +++ b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs @@ -0,0 +1,147 @@ +//! #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. 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; +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); +} + +/// 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) { + let mut h = dialog(open); + // 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 { + 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}"); + 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"); + 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"); +} + +/// 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 038a770582..5eb8530522 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, forced_preserve_transparency, remember_blending}; +use crate::dialogs::blend_options::{blending_params, blending_rows, forced_preserve_transparency, opening_fields, remember_blending}; use crate::dialogs::fill_ui::hex; use crate::dialogs::params::{captured, open_param}; @@ -45,7 +45,8 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId { let [r, g, b, _] = app.session.tools.foreground; let rows = rows(app, [r, g, b], preserve); 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)) }) } 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, + } }