From 9afc385877fc6bff7bb0c6ca80b9a933c99e3937 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sat, 28 Nov 2026 10:37:19 +0100 Subject: [PATCH] Options bar: a Search button at the right end runs Edit > Search (#16800) The options bar ends with a magnifier button, tooltip "Search for tools, help, and more", in the bar's trailing group: it stays for every tool and hides with the bar. A click runs edit.search, as Edit > Search... does; a second click closes the search. The bar's region version holds whether the search is open, so the button's selected look follows an Escape too. --- .../src/options_bar_search_button_tests.rs | 91 +++++++++++++++++++ crates/ui-egui/src/panels/options_bar.rs | 38 +++++++- crates/ui-egui/src/panels/region_versions.rs | 4 +- 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/options_bar_search_button_tests.rs diff --git a/crates/ui-egui/src/options_bar_search_button_tests.rs b/crates/ui-egui/src/options_bar_search_button_tests.rs new file mode 100644 index 00000000000..f856ddaa122 --- /dev/null +++ b/crates/ui-egui/src/options_bar_search_button_tests.rs @@ -0,0 +1,91 @@ +//! #16800: the Search button at the right end of the options bar. It runs `edit.search`, as Edit › Search… does, +//! stays for every tool and hides with the bar. + +use egui::accesskit::Role; +use egui::{Rect, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +/// The button's name and tooltip, as in the reference application. +const SEARCH: &str = "Search for tools, help, and more"; + +/// The window width the trials use, in points. +const WIDTH: f32 = 1440.0; + +/// The app with a new document and `tool` chosen. +fn harness(tool: Tool) -> Harness<'static, Box> { + let mut h = Harness::builder().with_size(vec2(WIDTH, 900.0)).with_max_steps(64).build_eframe(move |cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.ui.tool = tool; + app + }); + h.run_steps(8); + h +} + +/// The Search button's rect, if the button is shown. +fn search_button(h: &Harness<'_, Box>) -> Option { + h.query_by_role_and_label(Role::Button, SEARCH).map(|n| n.rect()) +} + +/// The options bar's Home button: the bar's first member, on the bar's line. +fn home(h: &Harness<'_, Box>) -> Rect { + h.get_by_label("Home").rect() +} + +#[test] +fn the_search_button_sits_at_the_right_end_of_the_options_bar() { + let h = harness(Tool::Move); + let search = search_button(&h).expect("the options bar has a Search button"); + let home = home(&h); + assert!(search.center().y > home.top() && search.center().y < home.bottom(), "on the bar's line: {search:?} vs Home {home:?}"); + assert!(search.right() > WIDTH - 48.0, "at the bar's right end: {search:?}"); + // Every other control on the bar's line sits left of it. + let line = |r: Rect| r.center().y > home.top() && r.center().y < home.bottom(); + let rightmost_other = h + .query_all_by_role(Role::Button) + .chain(h.query_all_by_role(Role::CheckBox)) + .chain(h.query_all_by_role(Role::ComboBox)) + .map(|n| n.rect()) + .filter(|r| line(*r) && *r != search) + .map(|r| r.right()) + .fold(f32::MIN, f32::max); + assert!(rightmost_other <= search.left(), "nothing on the bar is right of Search: {rightmost_other} > {}", search.left()); +} + +#[test] +fn a_click_opens_the_search_and_a_second_click_closes_it() { + let mut h = harness(Tool::Move); + assert!(!h.state().ui.palette.open); + h.get_by_role_and_label(Role::Button, SEARCH).click(); + h.run_steps(4); + assert!(h.state().ui.palette.open, "a click runs edit.search"); + h.get_by_role_and_label(Role::Button, SEARCH).click(); + h.run_steps(4); + assert!(!h.state().ui.palette.open, "a second click closes the search"); +} + +#[test] +fn the_search_button_stays_in_place_for_every_tool() { + let at = search_button(&harness(Tool::Move)).expect("Search with the Move tool"); + for tool in [Tool::Brush, Tool::Crop, Tool::Type, Tool::Gradient, Tool::Zoom] { + let rect = search_button(&harness(tool)).unwrap_or_else(|| panic!("Search with {tool:?}")); + assert!((rect.right() - at.right()).abs() < 0.5, "{tool:?}: {rect:?} vs {at:?}"); + } +} + +#[test] +fn the_search_button_hides_with_the_options_bar() { + let mut h = harness(Tool::Move); + assert!(search_button(&h).is_some()); + h.state_mut().ui.panels.options_bar = false; + h.run_steps(4); + assert!(search_button(&h).is_none(), "no Search button without the options bar"); + assert!(h.query_by_label("Home").is_none(), "the bar itself is hidden"); +} diff --git a/crates/ui-egui/src/panels/options_bar.rs b/crates/ui-egui/src/panels/options_bar.rs index 14c00e2281f..b682587d2ac 100644 --- a/crates/ui-egui/src/panels/options_bar.rs +++ b/crates/ui-egui/src/panels/options_bar.rs @@ -6,7 +6,7 @@ use std::ops::RangeInclusive; use craft::behavior::{EditEvents, IconId, NumberCodec}; use craft::ui::bars::{Bar, BarHome, BarKind, BarOutput, BarTool}; -use craft::ui::buttons::ToggleButton; +use craft::ui::buttons::{IconButton, ToggleButton}; use craft::ui::choice::Checkbox; use craft::ui::contract::{Bind, Entry, MemoProps, MemoView, Outbox, Props as _, Show as _, ShowContainer as _}; use craft::ui::display::Divider; @@ -100,15 +100,47 @@ pub(crate) fn edit_key(app: &PhotocraftApp) -> (Option, bool) { pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let tool = app.ui.tool; let ending = ending(app, tool); + // The bar's right end stays for every tool. It reads only copies of the app's state: the body holds the app. + let searching = app.ui.palette.open; + let mut ends = Vec::new(); let mut bar = Bar::new(tl!("Options").to_owned(), BarKind::Options); if let Some(ending) = &ending { bar = bar.commit_cancel(ending.commit.clone(), ending.cancel.clone()); } + bar = bar.trailing(|ui, _| right_end(ui, searching, &mut ends)); // A value control of the bar takes the wheel without a click, as the Mode dropdown and the fields did. let shown = bar.show(ui, |ui, _| with_env(ui, |set| set.wheel = Some(WheelScope::Free), |ui| body(app, ui))); if let Some(ending) = ending { answer(app, ui, tool, ending.end, &shown.output); } + // The bar may run its right end more than once in a pass (it measures it first); one click runs once. + if ends.contains(&RightEnd::Search) { + run_menu_command(app, ui.ctx(), "edit.search"); + } +} + +/// What the bar's right end asked for this pass. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum RightEnd { + /// The Search button was clicked. + Search, +} + +/// The bar's right end: the Search button, which opens and closes the search Edit › Search opens. +fn right_end(ui: &mut egui::Ui, searching: bool, ends: &mut Vec) { + let tip = tl!("Search for tools, help, and more"); + let search = IconButton::new(IconId::new("search")).label(tip.to_owned()).tooltip(tip).selected(searching).show(ui); + if search.clicked() && !ends.contains(&RightEnd::Search) { + ends.push(RightEnd::Search); + } +} + +/// Runs menu command `id` as its menu item does; a refusal shows in the status line. +fn run_menu_command(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str) { + if let Err(e) = crate::menus::invoke(app, ctx, id, json!({})) { + app.ui.status = e; + app.ui.status_error = true; + } } /// Runs the ending the bar's commit or cancel button asked for. @@ -437,3 +469,7 @@ mod art_history_bar_controls_tests; #[cfg(test)] #[path = "../healing_mode_menu_tests.rs"] mod healing_mode_menu_tests; + +#[cfg(test)] +#[path = "../options_bar_search_button_tests.rs"] +mod options_bar_search_button_tests; diff --git a/crates/ui-egui/src/panels/region_versions.rs b/crates/ui-egui/src/panels/region_versions.rs index 63e0688651f..99d81bf2710 100644 --- a/crates/ui-egui/src/panels/region_versions.rs +++ b/crates/ui-egui/src/panels/region_versions.rs @@ -51,7 +51,9 @@ pub(crate) fn options_bar(app: &PhotocraftApp) -> u64 { let gesture = crate::canvas::tools::in_gesture(app); let hidden = app.session.active().map(|st| (st.doc.id.0, app.ui.hidden_work_path.as_ref().map(|(id, _)| *id))); let paths = (app.ui.pen.as_ref().map(|p| p.draft.knots.len()), hidden); - version(app, (bar_view(app), sessions, shown, brush(app), colours(app), gesture, paths)) + // The Search button at the bar's right end shows whether the search is open; Escape closes it without a command. + let right_end = app.ui.palette.open; + version(app, (bar_view(app), sessions, shown, brush(app), colours(app), gesture, paths, right_end)) } /// The version of the status bar: the readout's item and the zoom of the active canvas, the status message, the