Merge #17275 into the merge queue batch

Options bar: a Search button at the right end runs Edit › Search
This commit is contained in:
David Mann committed 2026-11-28 13:29:01 +01:00
commit 8e3eedeb0d
3 files changed
+131 -2

No files matched your search

@@ -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<PhotocraftApp>> {
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<PhotocraftApp>>) -> Option<Rect> {
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<PhotocraftApp>>) -> 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");
}
+37 -1
View File
@@ -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<End>, 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<RightEnd>) {
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;
+3 -1
View File
@@ -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