diff --git a/crates/ui-egui/src/cam_options_bar_tests.rs b/crates/ui-egui/src/cam_options_bar_tests.rs new file mode 100644 index 0000000000..e4080c1938 --- /dev/null +++ b/crates/ui-egui/src/cam_options_bar_tests.rs @@ -0,0 +1,187 @@ +//! #12193: Content-Aware Move's Structure and Color open a whole-number slider popup, the bar's +//! controls carry the reference tooltips, a typed value outside the range alerts and keeps the +//! closest value, and a live Transform On Drop box greys the bar with Cancel then Commit at the +//! right end. Changing these controls is not a history state. + +use egui::accesskit::Role; +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::{DialogKind, Tool}; + +use super::{COLOR_RANGE_ALERT, STRUCTURE_RANGE_ALERT}; + +fn app_harness(live_box: bool) -> Harness<'static, PhotocraftApp> { + let mut h = Harness::builder().with_size(vec2(1600.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(move |cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 150})).unwrap(); + app.run("select.rect", json!({"x": 10, "y": 10, "width": 40, "height": 30})).unwrap(); + app.ui.tool = Tool::ContentAwareMove; + if live_box { + app.ui.tool_options.cam_transform_on_drop = true; + assert!(crate::cam_transform::begin(&mut app, [20, 8]), "the dropped patch gets a box"); + } + app + }); + h.run_steps(5); + h +} + +fn spin<'a>(h: &'a Harness<'static, PhotocraftApp>, label: &str) -> egui_kittest::Node<'a> { + h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some(label)).unwrap_or_else(|| panic!("the {label} field")) +} + +/// Types `text` into the named field, then leaves it with Tab or Enter. +fn type_field(h: &mut Harness<'static, PhotocraftApp>, label: &str, text: &str, key: egui::Key) { + spin(h, label).click(); + 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(key); + h.run_steps(3); +} + +fn alert(h: &Harness<'static, PhotocraftApp>) -> Option { + h.state().ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).and_then(|d| d.fields.get("message")).and_then(|m| m.as_str()).map(str::to_owned) +} + +fn steps(h: &Harness<'static, PhotocraftApp>) -> usize { + h.state().session.active().unwrap().history.past_len() +} + +fn click_at(h: &mut Harness<'static, PhotocraftApp>, p: egui::Pos2) { + h.hover_at(p); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() }); + h.run_steps(3); +} + +#[test] +fn structure_and_color_default_to_whole_numbers_and_open_a_slider() { + let mut h = app_harness(false); + assert_eq!(h.state().ui.tool_options.cam_structure, 4.0); + assert_eq!(h.state().ui.tool_options.cam_color, 0.0); + assert_eq!(spin(&h, "Structure:").accesskit_node().numeric_value(), Some(4.0)); + assert_eq!(spin(&h, "Color:").accesskit_node().numeric_value(), Some(0.0)); + assert!(h.query_all_by_role(Role::Slider).next().is_none(), "the slider stays closed"); + let before = steps(&h); + h.get_by_role_and_label(Role::Button, "Structure").click(); + h.run_steps(4); + let slider = h.get_by_role_and_label(Role::Slider, "Structure").rect(); + click_at(&mut h, egui::pos2(slider.right() - 3.0, slider.center().y)); + assert_eq!(h.state().ui.tool_options.cam_structure, 7.0, "the slider's far end is 7"); + assert_eq!(h.state().ui.tool_options.cam_structure.fract(), 0.0); + assert_eq!(alert(&h), None, "the slider does not alert"); + assert_eq!(steps(&h), before, "the slider is not a history state"); + h.get_by_role_and_label(Role::Button, "Color").click(); + h.run_steps(4); + let color = h.get_by_role_and_label(Role::Slider, "Color").rect(); + click_at(&mut h, egui::pos2(color.right() - 3.0, color.center().y)); + assert_eq!(h.state().ui.tool_options.cam_color, 10.0); + assert_eq!(h.state().ui.tool_options.cam_color.fract(), 0.0); + assert_eq!(steps(&h), before); +} + +#[test] +fn a_typed_value_outside_the_range_alerts_and_keeps_the_closest() { + for (label, typed, key, want, message) in [ + ("Structure:", "99", egui::Key::Enter, 7.0, STRUCTURE_RANGE_ALERT), + ("Structure:", "0", egui::Key::Tab, 1.0, STRUCTURE_RANGE_ALERT), + ("Color:", "99", egui::Key::Enter, 10.0, COLOR_RANGE_ALERT), + ("Color:", "-5", egui::Key::Tab, 0.0, COLOR_RANGE_ALERT), + ] { + let mut h = app_harness(false); + let before = steps(&h); + type_field(&mut h, label, typed, key); + let got = if label.starts_with("Structure") { h.state().ui.tool_options.cam_structure } else { h.state().ui.tool_options.cam_color }; + assert_eq!(got, want, "{label} {typed}"); + assert_eq!(alert(&h).as_deref(), Some(message), "{label} {typed}"); + assert_eq!(steps(&h), before, "{label} {typed}: not a history state"); + h.get_by_label("OK").click(); + h.run_steps(4); + assert_eq!(alert(&h), None, "{label} {typed}: OK closes the alert"); + let got = if label.starts_with("Structure") { h.state().ui.tool_options.cam_structure } else { h.state().ui.tool_options.cam_color }; + assert_eq!(got, want, "{label} {typed}: the closest value stays"); + } +} + +#[test] +fn a_typed_value_inside_the_range_is_a_whole_number_without_an_alert() { + let mut h = app_harness(false); + let before = steps(&h); + type_field(&mut h, "Structure:", "4.2", egui::Key::Tab); + assert_eq!(h.state().ui.tool_options.cam_structure, 4.0); + type_field(&mut h, "Structure:", "6", egui::Key::Enter); + assert_eq!(h.state().ui.tool_options.cam_structure, 6.0); + type_field(&mut h, "Color:", "3.6", egui::Key::Tab); + assert_eq!(h.state().ui.tool_options.cam_color, 4.0); + type_field(&mut h, "Color:", "10", egui::Key::Enter); + assert_eq!(h.state().ui.tool_options.cam_color, 10.0); + type_field(&mut h, "Color:", "0", egui::Key::Tab); + assert_eq!(h.state().ui.tool_options.cam_color, 0.0); + assert_eq!(alert(&h), None); + assert_eq!(steps(&h), before); +} + +#[test] +fn the_bar_has_the_reference_tooltips() { + for (role, label, tip) in [ + (Role::ComboBox, "Mode:", "Choose a remix mode"), + (Role::SpinButton, "Structure:", "Adjust how strictly the source structure is preserved"), + (Role::SpinButton, "Color:", "Adjust how much the source color can be modified"), + (Role::CheckBox, "Sample All Layers", "Enable to remix all layers"), + (Role::CheckBox, "Transform On Drop", "Allows rotation and scaling of selection"), + ] { + let mut h = app_harness(false); + assert!(h.query_by_label(tip).is_none(), "{label}: no tooltip before the hover"); + let at = h.query_all_by_role(role).find(|n| n.accesskit_node().label().as_deref() == Some(label)).unwrap_or_else(|| panic!("{label}")).rect().center(); + h.hover_at(at); + // The tooltip waits out its delay; the harness steps at 1/60 s. + h.run_steps(60); + assert!(h.query_by_label(tip).is_some(), "{label}: resting on it shows {tip:?}"); + } +} + +#[test] +fn a_live_transform_box_greys_the_bar_and_puts_cancel_then_commit_at_the_right() { + let h = app_harness(false); + assert!(h.query_by_label("Commit transform").is_none()); + assert!(h.query_by_label("Cancel transform").is_none()); + assert!(h.query_by_label("Lasso around an area, then drag the selection").is_some()); + + let mut h = app_harness(true); + for (role, label) in [ + (Role::Button, "New selection"), + (Role::ComboBox, "Mode:"), + (Role::SpinButton, "Structure:"), + (Role::SpinButton, "Color:"), + (Role::CheckBox, "Sample All Layers"), + (Role::CheckBox, "Transform On Drop"), + ] { + let node = h.query_all_by_role(role).find(|n| n.accesskit_node().label().as_deref() == Some(label)).unwrap_or_else(|| panic!("{label}")); + assert!(node.accesskit_node().is_disabled(), "{label} is greyed while the box is up"); + } + assert!(h.query_by_label("Lasso around an area, then drag the selection").is_none()); + let cancel = h.get_by_label("Cancel transform"); + let commit = h.get_by_label("Commit transform"); + assert!(!cancel.accesskit_node().is_disabled() && !commit.accesskit_node().is_disabled()); + let (cancel, commit) = (cancel.rect(), commit.rect()); + assert!(cancel.right() <= commit.left() + 1.0, "Cancel then Commit: {cancel:?} {commit:?}"); + let sample = h.get_by_role_and_label(Role::CheckBox, "Transform On Drop").rect(); + assert!(cancel.left() > sample.right(), "the pair sits to the right of the controls"); + assert!(commit.right() > 1200.0, "Commit is at the right end of the bar: {commit:?}"); + + let before = steps(&h); + h.get_by_label("Cancel transform").click(); + h.run_steps(4); + assert!(!crate::cam_transform::active(h.state())); + assert_eq!(steps(&h), before, "Cancel drops the box without a history state"); + assert!(!spin(&h, "Structure:").accesskit_node().is_disabled(), "the bar is usable again"); +} diff --git a/crates/ui-egui/src/tools/retouch_ui.rs b/crates/ui-egui/src/tools/retouch_ui.rs index 884c775e29..42b3679346 100644 --- a/crates/ui-egui/src/tools/retouch_ui.rs +++ b/crates/ui-egui/src/tools/retouch_ui.rs @@ -536,6 +536,128 @@ fn pct(ui: &mut egui::Ui, label: &str, v: &mut f32) { crate::widgets::value_field(ui, v, 1.0..=100.0, "%", 58.0); } +const STRUCTURE_RANGE_ALERT: &str = "An integer between 1 and 7 is required. Closest value inserted."; +const COLOR_RANGE_ALERT: &str = "An integer between 0 and 10 is required. Closest value inserted."; +const STRUCTURE_TIP: &str = "Adjust how strictly the source structure is preserved"; +const COLOR_TIP: &str = "Adjust how much the source color can be modified"; + +/// Structure or Color on the Content-Aware Move bar (#12193): a whole-number field and a slider +/// popup. A typed value outside `lo..=hi` becomes the closest value; returns true once the field +/// is left, so the caller opens the alert. A scrub stops at the end without an alert. +fn cam_integer_popup(ui: &mut egui::Ui, id: &str, name: &str, value: &mut f32, lo: f32, hi: f32, tip: &str) -> bool { + *value = if value.is_finite() { value.round().clamp(lo, hi) } else { lo }; + let draft_id = ui.make_persistent_id(egui::IdSalt::new(id)).with("typed"); + let draft = ui.ctx().data(|d| d.get_temp::<(f32, f32)>(draft_id)); + let mut typed = draft.filter(|(_, stored)| *stored == *value).map_or(*value, |(typed, _)| typed); + // Wide range so a typed 99 reaches here unclamped. The caption just before this names the field. + let r = crate::widgets::value_field(ui, &mut typed, -1.0e9..=1.0e9, "", 40.0).on_hover_text(tl!(tip)); + let out_id = r.id.with("out-of-range"); + if r.gained_focus() { + ui.ctx().data_mut(|d| d.remove_temp::(out_id)); + } + if r.changed() && typed.is_finite() { + *value = typed.round().clamp(lo, hi); + let typing = (r.has_focus() || r.lost_focus()) && ui.ctx().dragged_id().is_none(); + let out = typing && !(lo..=hi).contains(&typed); + ui.ctx().data_mut(|d| d.insert_temp(out_id, out)); + if typing && r.has_focus() { + ui.ctx().data_mut(|d| d.insert_temp(draft_id, (typed, *value))); + } else { + ui.ctx().data_mut(|d| d.remove_temp::<(f32, f32)>(draft_id)); + } + } + if !r.has_focus() { + ui.ctx().data_mut(|d| d.remove_temp::<(f32, f32)>(draft_id)); + } + cam_amount_slider(ui, name, value, lo, hi, tip); + r.lost_focus() && ui.ctx().data_mut(|d| d.remove_temp::(out_id)).unwrap_or(false) +} + +/// The ▾ beside a Content-Aware Move amount: its pop-up slider snaps to whole numbers in `lo..=hi`. +fn cam_amount_slider(ui: &mut egui::Ui, name: &str, value: &mut f32, lo: f32, hi: f32, tip: &str) { + let (rect, resp) = ui.allocate_exact_size(vec2(14.0, 22.0), egui::Sense::click()); + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), name)); + let resp = resp.on_hover_text(tl!(tip)); + let popup = egui::Popup::from_toggle_button_response(&resp).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside); + let open = popup.is_open(); + if resp.hovered() || open { + let t = Tokens::get(ui.ctx()); + ui.painter().rect_filled(rect, t.radius_sm, t.field_border.gamma_multiply(0.6)); + } + crate::widgets::chevron_icon(ui, rect, ui.style().interact(&resp), open); + let parent_layer = resp.layer_id; + popup.show(|ui| { + crate::press_menu::above_parent_ui(parent_layer, ui); + ui.set_min_width(160.0); + let mut v = *value; + let s = crate::widgets::slider(ui, &mut v, lo..=hi, None); + if v.is_finite() { + let snapped = v.round().clamp(lo, hi); + if snapped != *value { + *value = snapped; + } + } + s.widget_info(|| egui::WidgetInfo::slider(ui.is_enabled(), f64::from(*value), name)); + }); +} + +fn open_cam_range_alert(app: &mut PhotocraftApp, message: &str) { + let message = tl!(message).to_owned(); + let shown = + app.ui.dialogs.iter().any(|d| d.kind == crate::state::DialogKind::Error && d.fields.get("message").and_then(|m| m.as_str()) == Some(message.as_str())); + if !shown { + app.ui.open_dialog(crate::state::DialogKind::Error, json!({ "message": message }).as_object().cloned().unwrap_or_default()); + } +} + +/// Content-Aware Move's options (#12193). While the Transform On Drop box is up, the controls are +/// greyed and Cancel then Commit sit at the right end. +fn content_aware_move_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { + let live = crate::cam_transform::active(app); + ui.add_enabled_ui(!live, |ui| { + crate::panels::selection_mode_buttons(&mut app.ui.selection_mode, ui); + crate::widgets::vline(ui, 22.0); + opt(ui, tl!("Mode:")); + let modes = [("move".to_string(), tl!("Move")), ("extend".to_string(), tl!("Extend"))]; + crate::widgets::dropdown(ui, "cam-mode", tl!("Mode"), &mut app.ui.tool_options.cam_mode, &modes, 90.0); + if let Some(mode) = ui.ctx().read_response(ui.make_persistent_id(egui::IdSalt::new("cam-mode"))) { + mode.on_hover_text(tl!("Choose a remix mode")); + } + opt(ui, tl!("Structure:")); + if cam_integer_popup(ui, "cam-structure", "Structure", &mut app.ui.tool_options.cam_structure, 1.0, 7.0, STRUCTURE_TIP) { + open_cam_range_alert(app, STRUCTURE_RANGE_ALERT); + } + opt(ui, tl!("Color:")); + if cam_integer_popup(ui, "cam-color", "Color", &mut app.ui.tool_options.cam_color, 0.0, 10.0, COLOR_TIP) { + open_cam_range_alert(app, COLOR_RANGE_ALERT); + } + crate::widgets::checkbox(ui, &mut app.ui.tool_options.sample_all.content_aware_move, tl!("Sample All Layers")) + .on_hover_text(tl!("Enable to remix all layers")); + crate::widgets::checkbox(ui, &mut app.ui.tool_options.cam_transform_on_drop, tl!("Transform On Drop")) + .on_hover_text(tl!("Allows rotation and scaling of selection")); + }); + crate::widgets::vline(ui, 22.0); + if live { + // right_to_left places the first widget at the right, so Commit is the rightmost and + // Cancel sits just to its left. + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let tip = crate::i18n::fmt(tl!("Commit transform ({key})"), &[("key", &crate::shortcuts::pretty("Enter"))]); + let commit = crate::icons::button(ui, "check", 26.0, false, &tip); + commit.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, "Commit transform")); + if commit.clicked() { + crate::cam_transform::commit(app); + } + let cancel = crate::icons::button(ui, "ban", 26.0, false, tl!("Cancel transform (Esc)")); + cancel.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, "Cancel transform")); + if cancel.clicked() { + crate::cam_transform::cancel(app); + } + }); + } else { + opt(ui, tl!("Lasso around an area, then drag the selection")); + } +} + /// Options bar for the retouching and smart-selection tools. Returns false for other tools. pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bool { if !tool.is_brushlike() && !matches!(tool, Tool::QuickSelection | Tool::ObjectSelection | Tool::Patch | Tool::ContentAwareMove | Tool::RedEye) @@ -543,6 +665,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo { return false; } + if tool == Tool::ContentAwareMove { + content_aware_move_bar(app, ui); + return true; + } let composite = crate::canvas::paint_target(app).as_str() == Some("pixels"); let o = &mut app.ui.tool_options; match tool { @@ -603,37 +729,6 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo crate::widgets::vline(ui, 22.0); opt(ui, tl!("Lasso around an area, then drag the selection")); } - Tool::ContentAwareMove => { - // The lasso's selection mode leads, as on the Patch tool's bar (#11024). - crate::panels::selection_mode_buttons(&mut app.ui.selection_mode, ui); - crate::widgets::vline(ui, 22.0); - opt(ui, tl!("Mode:")); - let modes = [("move".to_string(), tl!("Move")), ("extend".to_string(), tl!("Extend"))]; - crate::widgets::dropdown(ui, "cam-mode", tl!("Mode"), &mut o.cam_mode, &modes, 90.0); - opt(ui, tl!("Structure:")); - crate::widgets::value_field(ui, &mut o.cam_structure, 1.0..=7.0, "", 40.0); - opt(ui, tl!("Color:")); - crate::widgets::value_field(ui, &mut o.cam_color, 0.0..=10.0, "", 40.0); - crate::widgets::checkbox(ui, &mut o.sample_all.content_aware_move, tl!("Sample All Layers")); - crate::widgets::checkbox(ui, &mut o.cam_transform_on_drop, tl!("Transform On Drop")); - crate::widgets::vline(ui, 22.0); - if crate::cam_transform::active(app) { - // The box on the dropped patch: ✓ computes the move, ⊘ drops it (as ↩ / Esc). - let tip = crate::i18n::fmt(tl!("Commit transform ({key})"), &[("key", &crate::shortcuts::pretty("Enter"))]); - let commit = crate::icons::button(ui, "check", 26.0, false, &tip); - commit.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, "Commit transform")); - if commit.clicked() { - crate::cam_transform::commit(app); - } - let cancel = crate::icons::button(ui, "ban", 26.0, false, tl!("Cancel transform (Esc)")); - cancel.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, "Cancel transform")); - if cancel.clicked() { - crate::cam_transform::cancel(app); - } - } else { - opt(ui, tl!("Lasso around an area, then drag the selection")); - } - } Tool::PatternStamp => { pattern_stamp_options(app, ui); } @@ -811,3 +906,7 @@ fn pattern_stamp_options(app: &mut PhotocraftApp, ui: &mut egui::Ui) { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "../cam_options_bar_tests.rs"] +mod cam_options_bar_tests;