From c732ecd5b07b6bf24e88be1fe203bb921c891141 Mon Sep 17 00:00:00 2001 From: LloydNicholson <34131158+LloydNicholson@users.noreply.github.com> Date: Sun, 11 Oct 2026 13:46:46 +0200 Subject: [PATCH 1/2] Dropdowns: press, drag onto an option and release to choose it (#2735) (#2854) A dropdown needed two clicks: egui's ComboBox opens its list on a whole click and its rows only take clicks, so press-drag-release (as the menus do since #775) chose nothing. press_menu::PressCombo gives combo boxes the gesture without replacing egui's widget: - after the box, a primary press on it opens the closed list (egui counts a popup opened in a pass as open, so it is drawn from the next frame); - a row released on by that press-drag is chosen and closes the list; - on the click that ends the press (pointer still on the box), egui would toggle the list shut, so it is closed just before the box and egui's toggle reopens it, drawn that frame: a plain click still leaves the list open for a second click. widgets::dropdown (and its hovered/wheel variants), dropdown_with_tooltips, the font family picker, the kerning presets, the Gradient Map presets, the shape stroke choices and the credits sort all use it. Co-authored-by: lloyd Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/adjust_editors.rs | 18 ++- crates/ui-egui/src/credits.rs | 7 +- crates/ui-egui/src/dropdown_press_tests.rs | 76 +++++++++ crates/ui-egui/src/lib.rs | 3 + crates/ui-egui/src/press_menu.rs | 179 +++++++++++++++++++++ crates/ui-egui/src/shape_stroke_ui.rs | 18 ++- crates/ui-egui/src/type_tool.rs | 28 +++- crates/ui-egui/src/widgets.rs | 92 ++++++----- 8 files changed, 357 insertions(+), 64 deletions(-) create mode 100644 crates/ui-egui/src/dropdown_press_tests.rs diff --git a/crates/ui-egui/src/adjust_editors.rs b/crates/ui-egui/src/adjust_editors.rs index 5ce9d1bd63..53964d8939 100644 --- a/crates/ui-egui/src/adjust_editors.rs +++ b/crates/ui-egui/src/adjust_editors.rs @@ -1147,9 +1147,14 @@ fn gradient_map(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Edit { ui.horizontal(|ui| { label(ui, tl!("Preset:")); let mut picked = None; - egui::ComboBox::from_id_salt(cx.mem.with("gm-preset")).selected_text(tl!("Custom")).width(190.0).height(420.0).icon(widgets::chevron_icon).show_ui( - ui, - |ui| { + let (press, close) = crate::press_menu::PressCombo::before(ui, cx.mem.with("gm-preset")); + let combo = egui::ComboBox::from_id_salt(cx.mem.with("gm-preset")) + .selected_text(tl!("Custom")) + .width(190.0) + .height(420.0) + .icon(widgets::chevron_icon) + .close_behavior(close) + .show_ui(ui, |ui| { for g in &cx.gradients { ui.label(RichText::new(&g.name).color(t.text_faint)); for p in &g.items { @@ -1159,14 +1164,15 @@ fn gradient_map(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Edit { let (r, swatch) = ui.allocate_exact_size(vec2(48.0, 16.0), Sense::click()); crate::preset_panels::paint_gradient(ui, r, &rgba); // Both, not short-circuited: the name is drawn either way. - if swatch.clicked() | ui.selectable_label(false, &p.name).clicked() { + let name = ui.selectable_label(false, &p.name); + if press.chosen(ui, &swatch) | press.chosen(ui, &name) { picked = Some(rgba.iter().map(|(at, c)| (*at, [c[0], c[1], c[2]])).collect::>()); } }); } } - }, - ); + }); + press.after(&combo.response); if let Some(p) = picked { stops = p; write_stops(v, &stops); diff --git a/crates/ui-egui/src/credits.rs b/crates/ui-egui/src/credits.rs index 1d45861c84..b31aa40578 100644 --- a/crates/ui-egui/src/credits.rs +++ b/crates/ui-egui/src/credits.rs @@ -265,14 +265,17 @@ fn contributors_in(app: &mut PhotocraftApp, ui: &mut egui::Ui, contributors: &[C } ui.separator(); ui.label(tl!("Sort")); - egui::ComboBox::from_id_salt("credits_sort").selected_text(v.key.label().0).show_ui(ui, |ui| { + let (press, close) = crate::press_menu::PressCombo::before(ui, "credits_sort"); + let sort = egui::ComboBox::from_id_salt("credits_sort").selected_text(v.key.label().0).close_behavior(close).show_ui(ui, |ui| { for k in SortKey::ALL { - if ui.selectable_label(v.key == k, k.label().0).clicked() { + let item = ui.selectable_label(v.key == k, k.label().0); + if press.chosen(ui, &item) { v.key = k; v.ascending = k.default_ascending(); } } }); + press.after(&sort.response); if ui.button(if v.ascending { "▲" } else { "▼" }).on_hover_text(tl!("Reverse the order")).clicked() { v.ascending = !v.ascending; } diff --git a/crates/ui-egui/src/dropdown_press_tests.rs b/crates/ui-egui/src/dropdown_press_tests.rs new file mode 100644 index 0000000000..c9d87116c0 --- /dev/null +++ b/crates/ui-egui/src/dropdown_press_tests.rs @@ -0,0 +1,76 @@ +//! #2735: a dropdown takes one press-drag-release, like the menus (#775): press the box, drag onto +//! an option, release to choose it. + +use egui::accesskit::Role; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use photocraft_doc::BlendMode; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +fn app_with_a_layer() -> Harness<'static, PhotocraftApp> { + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_max_steps(64).build_eframe(|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": 64, "height": 64})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + // No tool options bar Mode dropdown showing "Normal" too. + app.ui.tool = Tool::Move; + app + }); + h.run_steps(6); + h +} + +fn blend(h: &Harness<'_, PhotocraftApp>) -> BlendMode { + let st = h.state().session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().blend +} + +/// The Layers panel's Blend Mode dropdown (showing `value`). +fn blend_box(h: &Harness<'_, PhotocraftApp>, value: &str) -> egui::Rect { + let named = |n: &egui_kittest::Node<'_>| { + let a = n.accesskit_node(); + a.value().as_deref() == Some(value) || a.label().as_deref() == Some(value) + }; + h.query_all_by_role(Role::ComboBox).find(|n| named(n)).expect("the Layers panel's Blend Mode dropdown").rect() +} + +#[test] +fn press_drag_release_picks_a_blend_mode() { + let mut h = app_with_a_layer(); + assert_eq!(blend(&h), BlendMode::Normal); + let at = blend_box(&h, "Normal").center(); + h.hover_at(at); + h.run_steps(1); + h.drag_at(at); + h.run_steps(3); + let multiply = h.query_by_label("Multiply").expect("the list opened on the press").rect().center(); + for k in 1..=6 { + h.hover_at(at + (multiply - at) * (k as f32 / 6.0)); + h.run_steps(1); + } + h.drop_at(multiply); + h.run_steps(3); + assert_eq!(blend(&h), BlendMode::Multiply, "releasing on Multiply picked it"); + assert!(h.query_by_label("Color Burn").is_none(), "and closed the list"); +} + +#[test] +fn a_click_still_opens_the_list_for_a_second_click() { + let mut h = app_with_a_layer(); + let at = blend_box(&h, "Normal").center(); + h.hover_at(at); + h.run_steps(1); + h.drag_at(at); + h.run_steps(1); + h.drop_at(at); + h.run_steps(3); + assert_eq!(blend(&h), BlendMode::Normal, "the click chose nothing"); + let screen = h.query_by_label("Screen").expect("the list stays open after a click"); + screen.click(); + h.run_steps(3); + assert_eq!(blend(&h), BlendMode::Screen, "a second click picks"); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 85255bdd27..0b84ada001 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2159,6 +2159,9 @@ mod warp_text_dialog_tests; #[cfg(test)] mod blend_dropdown_wheel_tests; +#[cfg(test)] +mod dropdown_press_tests; + #[cfg(test)] mod marquee_tests; diff --git a/crates/ui-egui/src/press_menu.rs b/crates/ui-egui/src/press_menu.rs index 5654c81cc3..9f124afe91 100644 --- a/crates/ui-egui/src/press_menu.rs +++ b/crates/ui-egui/src/press_menu.rs @@ -84,6 +84,63 @@ pub fn show(button: &Response, content: impl FnOnce(&mut Ui, &PressMenu) -> R shown } +/// The same press-drag-release for an `egui::ComboBox` (#2735): the list opens on the press, so one +/// gesture can press the box, drag onto an option and release to choose it, and a plain click +/// still opens the list for a second click. egui's combo box shows its own list and toggles it on +/// a whole click, so this works around it: [`PressCombo::before`] runs before the box (and gives +/// it its close behaviour), [`PressCombo::chosen`] picks its options, [`PressCombo::after`] runs +/// after it. +pub struct PressCombo { + popup: egui::Id, +} + +impl PressCombo { + /// Before showing the combo box made with `ComboBox::from_id_salt(salt)` in `ui`; returns the + /// close behaviour to give it. + pub fn before(ui: &Ui, salt: impl egui::AsIdSalt) -> (Self, PopupCloseBehavior) { + // egui's ids for the box (`ComboBox::show_ui`) and its list (`widget_to_popup_id`). + let combo = PressCombo { popup: ui.make_persistent_id(egui::IdSalt::new(salt)).with("popup") }; + let ctx = ui.ctx(); + // The click ending the press that opened the list (the pointer didn't leave the box): egui + // toggles the list on it, so close it first and the toggle leaves it open, drawn this + // frame. No click may close it either. + if combo.owns_gesture(ctx) && ctx.input(|i| i.pointer.primary_clicked()) { + Popup::close_id(ctx, combo.popup); + return (combo, PopupCloseBehavior::IgnoreClicks); + } + (combo, PopupCloseBehavior::CloseOnClick) + } + + fn owns_gesture(&self, ctx: &egui::Context) -> bool { + gesture_owner(ctx) == Some(self.popup) + } + + /// Was option `item` (in the open list `ui`) chosen this frame: clicked, or the press-drag + /// gesture that opened the list released on it? The list closes when it was. + pub fn chosen(&self, ui: &Ui, item: &Response) -> bool { + let released_on = item.enabled() && item.contains_pointer() && ui.input(|i| i.pointer.primary_released()) && self.owns_gesture(ui.ctx()); + if released_on { + ui.close(); + } + item.clicked() || released_on + } + + /// After the combo box (`button`: its response). A primary press on the box opens a closed + /// list, starting a press-drag gesture; the gesture ends with the button. + pub fn after(&self, button: &Response) { + let ctx = &button.ctx; + let pressed = ctx.input(|i| i.pointer.primary_pressed()) && button.is_pointer_button_down_on(); + if pressed && !Popup::is_id_open(ctx, self.popup) { + // Opened now, the list counts as open this frame and egui shows it from the next. + Popup::open_id(ctx, self.popup); + ctx.data_mut(|d| d.insert_temp(gesture_id(), self.popup)); + ctx.request_repaint(); + } else if self.owns_gesture(ctx) && ctx.input(|i| i.pointer.primary_released() || !i.pointer.primary_down()) { + ctx.data_mut(|d| d.remove::(gesture_id())); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -241,4 +298,126 @@ mod tests { assert_eq!(rig.press(stroke, false).chosen, Some("Stroke"), "releasing on Stroke chose it"); assert!(!rig.frame(Vec::new()).open, "and closed the menu"); } + + /// A bare context with one `egui::ComboBox` of three options, driven with [`PressCombo`]. + struct ComboRig { + ctx: egui::Context, + value: usize, + button: Rect, + options: Vec, + } + + const OPTIONS: [&str; 3] = ["RGB", "Red", "Green"]; + + impl ComboRig { + fn new() -> Self { + let mut rig = ComboRig { ctx: egui::Context::default(), value: 0, button: Rect::NOTHING, options: Vec::new() }; + rig.frame(Vec::new()); + rig + } + + fn frame(&mut self, events: Vec) -> Frame { + let mut out = Frame::default(); + let mut options = Vec::new(); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 400.0)); + let mut value = self.value; + let mut button = Rect::NOTHING; + let mut output = self.ctx.run_ui(egui::RawInput { screen_rect: Some(rect), events, ..Default::default() }, |ui| { + ui.add_space(20.0); + let (combo, close) = PressCombo::before(ui, "channel"); + let shown = egui::ComboBox::from_id_salt("channel").selected_text(OPTIONS[value]).close_behavior(close).show_ui(ui, |ui| { + out.open = true; + for (i, label) in OPTIONS.into_iter().enumerate() { + let item = ui.selectable_label(value == i, label); + options.push(item.rect); + if combo.chosen(ui, &item) { + value = i; + out.chosen = Some(label); + } + } + }); + combo.after(&shown.response); + button = shown.response.rect; + }); + output.textures_delta.clear(); + self.value = value; + self.button = button; + if !options.is_empty() { + self.options = options; + } + out + } + + fn press(&mut self, pos: Pos2, pressed: bool) -> Frame { + self.frame(vec![Event::PointerMoved(pos), Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }]) + } + + fn drag_to(&mut self, from: Pos2, to: Pos2) { + for k in 1..=8 { + self.frame(vec![Event::PointerMoved(from + (to - from) * (k as f32 / 8.0))]); + } + } + } + + #[test] + fn press_drag_release_on_a_combo_box_chooses_an_option() { + let mut rig = ComboRig::new(); + let at = rig.button.center(); + rig.frame(vec![Event::PointerMoved(at)]); + rig.press(at, true); + assert!(rig.frame(Vec::new()).open, "the list opens on the press"); + // its first frame only sizes it: the options are where they are drawn from the next + rig.frame(Vec::new()); + let red = rig.options[1].center(); + rig.drag_to(at, red); + let f = rig.press(red, false); + assert_eq!((f.chosen, rig.value), (Some("Red"), 1), "releasing on Red chose it"); + assert!(!rig.frame(Vec::new()).open, "and closed the list"); + } + + #[test] + fn a_click_on_a_combo_box_leaves_its_list_open_for_a_second_click() { + let mut rig = ComboRig::new(); + let at = rig.button.center(); + rig.frame(vec![Event::PointerMoved(at)]); + rig.press(at, true); + let f = rig.press(at, false); + assert!(f.open && f.chosen.is_none(), "the click's release keeps the list drawn and chooses nothing"); + assert!(rig.frame(Vec::new()).open, "the list stays open"); + let green = rig.options[2].center(); + rig.frame(vec![Event::PointerMoved(green)]); + rig.press(green, true); + assert_eq!(rig.press(green, false).chosen, Some("Green"), "a second click chooses"); + assert_eq!(rig.value, 2); + assert!(!rig.frame(Vec::new()).open); + } + + #[test] + fn releasing_off_a_combo_box_list_chooses_nothing() { + let mut rig = ComboRig::new(); + let at = rig.button.center(); + rig.frame(vec![Event::PointerMoved(at)]); + rig.press(at, true); + rig.frame(Vec::new()); + rig.frame(Vec::new()); + let away = pos2(380.0, 380.0); + rig.drag_to(at, away); + let f = rig.press(away, false); + assert!(f.chosen.is_none() && rig.value == 0, "a release off the options chooses nothing"); + assert!(rig.frame(Vec::new()).open, "and keeps the list open"); + } + + #[test] + fn clicking_a_combo_box_again_closes_its_list() { + let mut rig = ComboRig::new(); + let at = rig.button.center(); + rig.frame(vec![Event::PointerMoved(at)]); + rig.press(at, true); + rig.press(at, false); + assert!(rig.frame(Vec::new()).open); + rig.press(at, true); + rig.press(at, false); + assert!(!rig.frame(Vec::new()).open, "a second click on the box closes the list"); + assert_eq!(rig.value, 0); + } } diff --git a/crates/ui-egui/src/shape_stroke_ui.rs b/crates/ui-egui/src/shape_stroke_ui.rs index efae5146d6..d5a0c42cf9 100644 --- a/crates/ui-egui/src/shape_stroke_ui.rs +++ b/crates/ui-egui/src/shape_stroke_ui.rs @@ -311,14 +311,20 @@ pub fn button( fn choice(ui: &mut egui::Ui, label: &str, value: &mut T, choices: &[(T, &str)]) { ui.horizontal(|ui| { ui.label(tl!(label)); - egui::ComboBox::from_id_salt(label).width(140.0).selected_text(choices.iter().find(|(v, _)| v == value).map_or("", |(_, n)| tl!(*n))).show_ui( - ui, - |ui| { + let (press, close) = crate::press_menu::PressCombo::before(ui, label); + let combo = egui::ComboBox::from_id_salt(label) + .width(140.0) + .selected_text(choices.iter().find(|(v, _)| v == value).map_or("", |(_, n)| tl!(*n))) + .close_behavior(close) + .show_ui(ui, |ui| { for (v, n) in choices { - ui.selectable_value(value, *v, tl!(*n)); + let item = ui.selectable_label(*value == *v, tl!(*n)); + if press.chosen(ui, &item) { + *value = *v; + } } - }, - ); + }); + press.after(&combo.response); }); } diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs index aaec963dca..aa11ac5b2b 100644 --- a/crates/ui-egui/src/type_tool.rs +++ b/crates/ui-egui/src/type_tool.rs @@ -977,6 +977,8 @@ pub(crate) fn family_picker_in( ) -> bool { let mut changed = false; let search_id = ui.id().with(("font-search", salt)); + // The list opens on the press, so a press-drag-release picks a font too (#2735). + let (press, close) = crate::press_menu::PressCombo::before(ui, salt); let combo = egui::ComboBox::from_id_salt(salt) .selected_text(current.as_str()) .width(width) @@ -984,7 +986,7 @@ pub(crate) fn family_picker_in( .icon(crate::widgets::chevron_icon) // Clicks inside the menu (the search field, the scroll bar) must not close it (#1369); // picking a font closes it explicitly below. - .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside); + .close_behavior(if matches!(close, egui::PopupCloseBehavior::IgnoreClicks) { close } else { egui::PopupCloseBehavior::CloseOnClickOutside }); let response = combo.show_ui(ui, |ui| { let (pass_id, focus_id, height_id) = (search_id.with("pass"), search_id.with("focus"), search_id.with("height")); @@ -1054,7 +1056,7 @@ pub(crate) fn family_picker_in( if response.hovered() { hovered = Some(f.clone()); } - if response.clicked() { + if press.chosen(ui, &response) { *current = f.clone(); // A served family not fetched yet: start now, before any text needs it. photocraft_text::served::request(f); @@ -1075,6 +1077,7 @@ pub(crate) fn family_picker_in( } if accept { None } else { hovered } }); + press.after(&response.response); let hovered_font = response.inner.unwrap_or_default(); if salt == "type-font" { @@ -1397,13 +1400,22 @@ fn kerning_field(ui: &mut egui::Ui, shown: &str, width: f32) -> Option(ui: &mut Ui, id: &str, current: &mut T, o // after a step over the button. let reveal_id = wheel_id.with("reveal"); let mut reveal = wheel && ui.data_mut(|d| d.remove_temp::(reveal_id)).unwrap_or(false); - let response = egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).show_ui(ui, |ui| { - fit_widest_option(ui, options.iter().map(|(_, l)| tl!(l))); - // Over the open list the wheel steps the value too, instead of scrolling the list. - over_list = wheel && ui.rect_contains_pointer(ui.clip_rect()); - if over_list { - let stepped = wheel_steps(ui, wheel_id, true, current, options); - wheeled = !stepped.is_empty(); - reveal |= wheeled; - chosen.extend(stepped); - } - // The row nearest the pointer, by its distance to each row. - let pointer = ui.ctx().pointer_hover_pos().filter(|_| ui.rect_contains_pointer(ui.clip_rect())); - let mut nearest: Option<(f32, &T)> = None; - for (v, l) in options { - let item = ui.selectable_label(v == current, tl!(l)); - if reveal && v == current { - item.scroll_to_me(None); + // The list opens on the press: press, drag onto an option and release to choose it (#2735). + let (press, close) = crate::press_menu::PressCombo::before(ui, id); + let response = + egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).close_behavior(close).show_ui(ui, |ui| { + fit_widest_option(ui, options.iter().map(|(_, l)| tl!(l))); + // Over the open list the wheel steps the value too, instead of scrolling the list. + over_list = wheel && ui.rect_contains_pointer(ui.clip_rect()); + if over_list { + let stepped = wheel_steps(ui, wheel_id, true, current, options); + wheeled = !stepped.is_empty(); + reveal |= wheeled; + chosen.extend(stepped); } - if item.hovered() { - hovered = Some(v.clone()); - } - if let Some(p) = pointer { - let d = (item.rect.top() - p.y).max(p.y - item.rect.bottom()).max(0.0); - if nearest.is_none_or(|(n, _)| d < n) { - nearest = Some((d, v)); + // The row nearest the pointer, by its distance to each row. + let pointer = ui.ctx().pointer_hover_pos().filter(|_| ui.rect_contains_pointer(ui.clip_rect())); + let mut nearest: Option<(f32, &T)> = None; + for (v, l) in options { + let item = ui.selectable_label(v == current, tl!(l)); + if reveal && v == current { + item.scroll_to_me(None); + } + if item.hovered() { + hovered = Some(v.clone()); + } + if let Some(p) = pointer { + let d = (item.rect.top() - p.y).max(p.y - item.rect.bottom()).max(0.0); + if nearest.is_none_or(|(n, _)| d < n) { + nearest = Some((d, v)); + } + } + if press.chosen(ui, &item) { + *current = v.clone(); + chosen.push(v.clone()); } } - if item.clicked() { - *current = v.clone(); - chosen.push(v.clone()); + // Between two rows (item spacing) the pointer hovers neither; it is still over the list, + // so keep previewing the nearest row instead of flashing the current value back (#2553). + if hovered.is_none() { + hovered = nearest.map(|(_, v)| v.clone()); } - } - // Between two rows (item spacing) the pointer hovers neither; it is still over the list, - // so keep previewing the nearest row instead of flashing the current value back (#2553). - if hovered.is_none() { - hovered = nearest.map(|(_, v)| v.clone()); - } - }); + }); + press.after(&response.response); if combo_box_arrow_keys(ui, &response.response, current, options) { chosen.push(current.clone()); } @@ -1140,15 +1144,19 @@ pub fn swatch_popup(swatch: &Response) -> egui::Popup<'static> { pub fn dropdown_with_tooltips(ui: &mut Ui, id: &str, current: &mut T, options: &[(T, &str, &str)], width: f32) -> bool { let label = options.iter().find(|(v, _, _)| v == current).map(|(_, l, _)| tl!(l)).unwrap_or("—"); let mut changed = false; - let response = egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).show_ui(ui, |ui| { - fit_widest_option(ui, options.iter().map(|(_, l, _)| tl!(l))); - for (v, l, tip) in options { - if ui.selectable_label(v == current, tl!(l)).on_hover_text(tl!(tip)).clicked() { - *current = v.clone(); - changed = true; + let (press, close) = crate::press_menu::PressCombo::before(ui, id); + let response = + egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).close_behavior(close).show_ui(ui, |ui| { + fit_widest_option(ui, options.iter().map(|(_, l, _)| tl!(l))); + for (v, l, tip) in options { + let item = ui.selectable_label(v == current, tl!(l)).on_hover_text(tl!(tip)); + if press.chosen(ui, &item) { + *current = v.clone(); + changed = true; + } } - } - }); + }); + press.after(&response.response); if let Some((_, _, tip)) = options.iter().find(|(v, _, _)| v == current) { let _ = response.response.on_hover_text(tl!(tip)); } From 9b3c6359aea69c95b1a043b834d7ad237400d7fc Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sun, 11 Oct 2026 13:48:51 +0200 Subject: [PATCH 2/2] perf: Select > Modify > Feather/Smooth on 24 MP within budget (#2873) (#2882) The blur itself was already parallel (9-15 ms on 24 MP); most of the command was serial glue around it. Modify copied the mask into a padded buffer and back out sample by sample even with no padding, and combine built a full-canvas byte buffer, scanned it, then copied it into tiles on one thread. - modify: use the mask as is when the selection is clear of the canvas edge (pad 0); pad and crop row by row otherwise. - combine / mask_to_surface: fill selection tiles in parallel straight from the mask, a row at a time; fully selected tiles share one copy-on-write tile. Bit-identical: the per-sample oracles (combine, now with whole tiles, and the old pad/crop) are compared in tests. Co-authored-by: Claude Opus 5.5 --- crates/algo/src/selection.rs | 106 +++++++++++++++------------- crates/engine/src/selection_cmds.rs | 70 ++++++++++++++---- perf/budgets.toml | 4 +- 3 files changed, 118 insertions(+), 62 deletions(-) diff --git a/crates/algo/src/selection.rs b/crates/algo/src/selection.rs index 3649b1c658..f985a06796 100644 --- a/crates/algo/src/selection.rs +++ b/crates/algo/src/selection.rs @@ -3,12 +3,14 @@ //! modify (expand, contract, border, smooth, feather), anti-aliased lasso, //! and combining with an existing selection. +use std::sync::Arc; + use photocraft_color::PixelFormat; use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; use photocraft_raster::Surface; use serde::{Deserialize, Serialize}; -use crate::photo_util::par_rows; +use crate::photo_util::{par_map, par_rows}; /// How a new selection combines with the current one. #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] @@ -79,49 +81,57 @@ fn coverage8(v: f32) -> u8 { (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8 } -/// Builds a GRAY8 selection surface from 8-bit coverage over `area`, allocating only the tiles -/// that hold something (as [`Surface::prune`] would leave them). -fn surface_from_coverage8(bytes: &[u8], area: Rect) -> Surface { +/// Builds a GRAY8 selection surface over `area` (masks row-major over it) whose coverage at +/// each sample is `f(prev, new)`, `prev` reading 0 without a `prev` mask and missing samples +/// reading 0. Tiles are filled in parallel straight from the masks, a row at a time (no +/// full-canvas byte buffer); only the tiles that hold something are kept (as [`Surface::prune`] +/// would leave them), and fully selected tiles share one tile (copy-on-write), so the inside of a +/// large selection costs no memory per tile (#211). +fn surface_from_masks(area: Rect, new: &[f32], prev: Option<&[f32]>, f: impl Fn(f32, f32) -> f32 + Sync + Send) -> Surface { let mut s = Surface::new(PixelFormat::GRAY8); + if area.is_empty() { + return s; + } let w = area.width() as usize; let ts = TILE_SIZE as usize; - for tc in area.tiles() { - let tr = tc.rect().intersect(&area); - let (lx, span) = ((tr.x0 - area.x0) as usize, tr.width() as usize); - let row = |y: i32| bytes.get((y - area.y0) as usize * w + lx..(y - area.y0) as usize * w + lx + span); - if !(tr.y0..tr.y1).any(|y| row(y).is_some_and(|r| r.iter().any(|b| *b != 0))) { - continue; - } - let data = s.tile_mut(tc).bytes_mut(); - for y in tr.y0..tr.y1 { - let o = (y - tc.ty * TILE_SIZE) as usize * ts + (tr.x0 - tc.tx * TILE_SIZE) as usize; - if let (Some(src), Some(dst)) = (row(y), data.get_mut(o..o + span)) { - dst.copy_from_slice(src); + let full = s.solid_tile(&[1.0]); + let row = |i0: usize, dst: &mut [u8]| { + let n = dst.len(); + match (new.get(i0..i0 + n), prev.map(|p| p.get(i0..i0 + n))) { + (Some(sn), None) => dst.iter_mut().zip(sn).for_each(|(d, v)| *d = coverage8(f(0.0, *v))), + (Some(sn), Some(Some(sp))) => dst.iter_mut().zip(sn.iter().zip(sp)).for_each(|(d, (v, p))| *d = coverage8(f(*p, *v))), + _ => { + let at = |m: &[f32], i: usize| m.get(i).copied().unwrap_or(0.0); + for (i, d) in (i0..).zip(dst.iter_mut()) { + *d = coverage8(f(prev.map_or(0.0, |p| at(p, i)), at(new, i))); + } } } - } + }; + // Every tile starts as one shared blank tile; `Arc::make_mut` gives each task its own copy. + let blanks = s.take_tiles(area); + let built = par_map(blanks.len(), |k| { + let (tc, blank) = blanks.get(k)?; + let tr = tc.rect().intersect(&area); + let (lx, span) = ((tr.x0 - area.x0) as usize, tr.width() as usize); + let mut tile = Arc::clone(blank); + let data = Arc::make_mut(&mut tile).bytes_mut(); + let (mut lo, mut hi) = (if tr == tc.rect() { u8::MAX } else { 0 }, 0u8); + for y in tr.y0..tr.y1 { + let o = (y - tc.ty * TILE_SIZE) as usize * ts + (tr.x0 - tc.tx * TILE_SIZE) as usize; + let Some(dst) = data.get_mut(o..o + span) else { continue }; + row((y - area.y0) as usize * w + lx, dst); + (lo, hi) = dst.iter().fold((lo, hi), |(lo, hi), b| (lo.min(*b), hi.max(*b))); + } + (hi > 0).then(|| (*tc, if lo == u8::MAX { Arc::clone(&full) } else { tile })) + }); + s.put_tiles(built.into_iter().flatten()); s } -/// 8-bit coverage of `m` (missing samples are 0), computed in parallel. -fn to_coverage8(m: &[f32], area: Rect, f: impl Fn(usize, f32) -> f32 + Sync + Send) -> Vec { - let w = area.width() as usize; - let mut bytes = vec![0u8; w * area.height() as usize]; - par_rows(&mut bytes, w, 1, |y, row| { - for (x, b) in row.iter_mut().enumerate() { - let i = y * w + x; - *b = coverage8(f(i, m.get(i).copied().unwrap_or(0.0))); - } - }); - bytes -} - -/// Builds a GRAY8 selection surface from a mask over `area`. +/// Builds a GRAY8 selection surface from a mask over `area` (missing samples are 0). pub fn mask_to_surface(m: &[f32], area: Rect) -> Surface { - if area.is_empty() { - return Surface::new(PixelFormat::GRAY8); - } - surface_from_coverage8(&to_coverage8(m, area, |_, v| v), area) + surface_from_masks(area, m, None, |_, v| v) } /// Combines `new` with `old` by `mode`. Returns `None` when nothing is selected. @@ -132,22 +142,18 @@ pub fn combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMo // Quantize to the 8-bit grid selections are stored on, so combining a mask with itself is // exact. Below half an 8-bit step is nothing. let q = |v: f32| f32::from(coverage8(v)) / 255.0; - let bytes = match mode { - SelectionMode::Replace => to_coverage8(new, area, |_, v| v), + let s = match mode { + SelectionMode::Replace => surface_from_masks(area, new, None, |_, v| v), _ => { let prev = mask_from_surface(old, area); - let at = |i: usize| prev.get(i).copied().unwrap_or(0.0); match mode { - SelectionMode::Add => to_coverage8(new, area, |i, v| at(i).max(q(v))), - SelectionMode::Subtract => to_coverage8(new, area, |i, v| (at(i) - q(v)).max(0.0)), - _ => to_coverage8(new, area, |i, v| at(i).min(q(v))), + SelectionMode::Add => surface_from_masks(area, new, Some(&prev), |p, v| p.max(q(v))), + SelectionMode::Subtract => surface_from_masks(area, new, Some(&prev), |p, v| (p - q(v)).max(0.0)), + _ => surface_from_masks(area, new, Some(&prev), |p, v| p.min(q(v))), } } }; - if bytes.iter().all(|b| *b == 0) { - return None; - } - Some(surface_from_coverage8(&bytes, area)) + (s.tile_count() > 0).then_some(s) } fn close(a: [f32; 4], b: [f32; 4], tol: f32) -> bool { @@ -1202,7 +1208,10 @@ mod tests { let mut rng = crate::photo_util::Rng(7); let mut u = move || (rng.next() >> 11) as f32 / (1u64 << 53) as f32; // Off-grid areas spanning several tiles, including negative origins. - for area in [Rect::new(0, 0, 300, 20), Rect::new(-37, 250, 530, 263), Rect::new(255, -3, 258, 600), Rect::new(5, 5, 6, 6)] { + // The last one holds whole tiles, which fully selected masks fill (shared tiles). + for area in + [Rect::new(0, 0, 300, 20), Rect::new(-37, 250, 530, 263), Rect::new(255, -3, 258, 600), Rect::new(5, 5, 6, 6), Rect::new(-37, -300, 530, 263)] + { let n = area.width() as usize * area.height() as usize; let old_m: Vec = (0..n).map(|i| if i % 97 < 40 { u() } else { 0.0 }).collect(); let old = old_mask_to_surface(&old_m, area); @@ -1211,7 +1220,7 @@ mod tests { let mut slow_read = Vec::new(); old.read_region_into(area, &mut slow_read); assert_eq!(mask_from_surface(Some(&old), area), slow_read); - for kind in 0..3 { + for kind in 0..4 { let new: Vec = (0..n) .map(|i| match kind { 0 => u() * 1.2 - 0.1, @@ -1222,7 +1231,8 @@ mod tests { 0.0 } } - _ => 0.001, + 2 => 0.001, + _ => f32::from(u8::from(i >= 1000)), }) .collect(); for mode in [SelectionMode::Replace, SelectionMode::Add, SelectionMode::Subtract, SelectionMode::Intersect] { diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs index 948ef07632..3a677f522c 100644 --- a/crates/engine/src/selection_cmds.rs +++ b/crates/engine/src/selection_cmds.rs @@ -398,7 +398,9 @@ fn modify(s: &mut Session, p: &Value, op: &str) -> Result { if pw.checked_mul(ph).is_none_or(|n| n > MAX_MODIFY_PIXELS) { return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: "the selection is too large to modify".into() }); } - let padded = pad_mask(&m, w, h, pad, at_bounds); + // Unpadded (the usual case) the mask is used as it is: two serial full-canvas copies were + // most of a 24 MP Feather or Smooth (#211). + let padded = if pad == 0 { m } else { pad_mask(&m, w, h, pad, at_bounds) }; let out = match op { "expand" => sel::expand(&padded, pw, ph, r), "contract" => sel::contract(&padded, pw, ph, r), @@ -406,7 +408,7 @@ fn modify(s: &mut Session, p: &Value, op: &str) -> Result { "smooth" => sel::smooth(&padded, pw, ph, r), _ => sel::feather(&padded, pw, ph, r), }; - let out = crop_mask(&out, pw, pad, w, h); + let out = if pad == 0 { out } else { crop_mask(&out, pw, pad, w, h) }; let label = match op { "expand" => "Expand Selection", "contract" => "Contract Selection", @@ -435,19 +437,18 @@ fn pad_mask(m: &[f32], w: usize, h: usize, pad: usize, outside_empty: bool) -> V if w == 0 || h == 0 { return out; } + // Row by row (a copy and two fills), not sample by sample (#211). for (py, row) in out.chunks_exact_mut(pw).enumerate() { - let inside_y = (pad..pad + h).contains(&py); - if outside_empty && !inside_y { + if outside_empty && !(pad..pad + h).contains(&py) { continue; } let y = py.saturating_sub(pad).min(h - 1); - for (px, v) in row.iter_mut().enumerate() { - let inside_x = (pad..pad + w).contains(&px); - if outside_empty && !inside_x { - continue; - } - let x = px.saturating_sub(pad).min(w - 1); - *v = m.get(y * w + x).copied().unwrap_or(0.0); + let (Some(src), Some((left, rest))) = (m.get(y * w..(y + 1) * w), row.split_at_mut_checked(pad)) else { continue }; + let Some((mid, right)) = rest.split_at_mut_checked(w) else { continue }; + mid.copy_from_slice(src); + if !outside_empty { + left.fill(src.first().copied().unwrap_or(0.0)); + right.fill(src.last().copied().unwrap_or(0.0)); } } out @@ -455,7 +456,11 @@ fn pad_mask(m: &[f32], w: usize, h: usize, pad: usize, outside_empty: bool) -> V /// The `w`×`h` centre of a mask padded by `pad_mask`. fn crop_mask(m: &[f32], pw: usize, pad: usize, w: usize, h: usize) -> Vec { - m.chunks_exact(pw).skip(pad).take(h).flat_map(|row| row.iter().skip(pad).take(w).copied()).collect() + let mut out = Vec::with_capacity(w.saturating_mul(h)); + for row in m.chunks_exact(pw.max(1)).skip(pad).take(h) { + out.extend_from_slice(row.get(pad..pad + w).unwrap_or_default()); + } + out } /// Grow (contiguous) / Similar (anywhere): pixels whose colour lies within @@ -1007,4 +1012,45 @@ mod tests { s.execute("edit.undo", json!({})).unwrap(); assert_eq!(coverage(&s, 2, 2), 1.0); } + + /// The previous per-sample padding and cropping, kept as the oracle for the row-wise ones. + fn old_pad_mask(m: &[f32], w: usize, h: usize, pad: usize, outside_empty: bool) -> Vec { + let pw = w + 2 * pad; + let mut out = vec![0.0f32; pw * (h + 2 * pad)]; + for (py, row) in out.chunks_exact_mut(pw).enumerate() { + let inside_y = (pad..pad + h).contains(&py); + if outside_empty && !inside_y { + continue; + } + let y = py.saturating_sub(pad).min(h - 1); + for (px, v) in row.iter_mut().enumerate() { + if outside_empty && !(pad..pad + w).contains(&px) { + continue; + } + *v = m.get(y * w + px.saturating_sub(pad).min(w - 1)).copied().unwrap_or(0.0); + } + } + out + } + + #[test] + fn row_wise_pad_and_crop_match_per_sample_ones() { + let mut seed = 0x2545_f491_4f6c_dd1du64; + let (w, h) = (37, 23); + let m: Vec = (0..w * h) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed % 1000) as f32 / 999.0 + }) + .collect(); + for pad in [0, 1, 5, 40] { + for outside_empty in [false, true] { + let padded = pad_mask(&m, w, h, pad, outside_empty); + assert_eq!(padded, old_pad_mask(&m, w, h, pad, outside_empty), "pad {pad} {outside_empty}"); + assert_eq!(crop_mask(&padded, w + 2 * pad, pad, w, h), m, "pad {pad}"); + } + } + } } diff --git a/perf/budgets.toml b/perf/budgets.toml index 5ff4fb390e..a95c1c83dc 100644 --- a/perf/budgets.toml +++ b/perf/budgets.toml @@ -324,7 +324,7 @@ issue = 211 bench = "perf_scenarios" row = "Select > Modify > Smooth r 50 on 24 MP" budget_ms = 100 -enforce = false # #211: fastest samples 30-33 ms; p95 still over budget at load avg > 100 +enforce = false # #2873: p95 20-45 ms on 16 cores (load up to ~15), over 100 ms only at load avg > 20; CI cores unmeasured [[scenario]] id = "P27" @@ -333,7 +333,7 @@ issue = 211 bench = "perf_scenarios" row = "Select > Modify > Feather r 50 on 24 MP" budget_ms = 100 -enforce = false # #211: fastest samples 30-33 ms; p95 still over budget at load avg > 100 +enforce = false # #2873: p95 20-45 ms on 16 cores (load up to ~15), over 100 ms only at load avg > 20; CI cores unmeasured [[scenario]] id = "P28"