Mouse gestures: keep the tool search open while editing (#7121) (#7132)

* claim: #7121

* Fix mouse gesture tool-search focus
This commit is contained in:
Colin Faulkingham
2026-10-27 12:33:33 +00:00
committed by GitHub
parent 7895c14e3f
commit 22ede1bde3
3 changed files with 105 additions and 19 deletions
+84 -19
View File
@@ -663,26 +663,37 @@ fn editor(app: &mut SolveApp, ctx: &egui::Context) {
let shown = label_of(&list[i]).unwrap_or_default();
ui.scope(|ui| {
ui.set_max_width(200.0);
crate::ui_kit::dropdown_menu(ui, ("gesture_tool", i), &shown, |ui| {
let filter_id = ui.id().with("filter");
let mut filter: String = ui.data_mut(|d| d.get_temp(filter_id).unwrap_or_default());
crate::ui_kit::text_field(ui, &mut filter, crate::i18n::tr_label("Filter…"));
ui.data_mut(|d| d.insert_temp(filter_id, filter.clone()));
let needle = filter.trim().to_lowercase();
egui::ScrollArea::vertical().max_height(260.0).show(ui, |ui| {
for (id, label) in &catalog {
if !needle.is_empty() && !label.to_lowercase().contains(&needle) && !id.to_lowercase().contains(&needle) {
continue;
let picker = crate::ui_kit::dropdown_menu_with_close_behavior(
ui,
("gesture_tool", i),
&shown,
egui::PopupCloseBehavior::CloseOnClickOutside,
|ui| {
let filter_id = ui.id().with("filter");
let mut filter: String = ui.data_mut(|d| d.get_temp(filter_id).unwrap_or_default());
crate::ui_kit::text_field(ui, &mut filter, crate::i18n::tr_label("Filter…"));
ui.data_mut(|d| d.insert_temp(filter_id, filter.clone()));
let needle = filter.trim().to_lowercase();
egui::ScrollArea::vertical().max_height(260.0).show(ui, |ui| {
for (id, label) in &catalog {
if !needle.is_empty()
&& !label.to_lowercase().contains(&needle)
&& !id.to_lowercase().contains(&needle)
{
continue;
}
let taken = list.contains(id) && *id != list[i];
let r =
ui.add_enabled_ui(!taken, |ui| crate::ui_kit::list_picker(ui, id, label, *id == list[i])).inner;
if r.clicked() {
op = Some(Op::Set(i, id.clone()));
ui.close();
}
}
let taken = list.contains(id) && *id != list[i];
let r = ui.add_enabled_ui(!taken, |ui| crate::ui_kit::list_picker(ui, id, label, *id == list[i])).inner;
if r.clicked() {
op = Some(Op::Set(i, id.clone()));
ui.close();
}
}
});
});
});
},
);
crate::scenario::publish_target(&format!("gestures:tool:{i}"), picker.rect);
});
if crate::ui_kit::button(ui, "↑", crate::ui_kit::Button::Enabled(i > 0)).clicked() {
op = Some(Op::Up(i));
@@ -982,6 +993,60 @@ mod tests {
}
}
#[test]
fn tool_search_keeps_focus_after_click_and_closes_only_when_finished() {
use crate::scenario::{Harness, handle_at};
use crate::test_support::{click, pointer};
use serde_json::json;
let mut h = Harness::new();
h.app.home.open = false;
h.app.preferences.mouse_gestures.set_tools(Studio::Part, vec!["undo".into(), "solid.extrude".into()]);
h.app.gesture.editor.open = true;
h.app.gesture.editor.studio = Studio::Part;
h.frames(6);
click(&mut h, "gestures:tool:0");
let search = h
.shapes
.iter()
.find_map(|shape| match &shape.shape {
egui::Shape::Text(text) if text.galley.job.text == "Filter…" => Some(text.pos + text.galley.size() * 0.5),
_ => None,
})
.expect("the picker displays its search field");
h.frame_with(pointer(search, true));
h.frame_with(pointer(search, false));
h.frames(2);
assert!(egui::Popup::is_any_open(&h.ctx), "releasing a click in the search field must keep the picker open");
h.step(&json!({"text":"solid.extrude"})).unwrap();
h.frames(2);
assert!(handle_at("dialog:list:solid.extrude").is_some(), "search matches command ids");
assert!(handle_at("dialog:list:solid.box").is_none(), "unmatched tools are filtered out");
click(&mut h, "dialog:list:solid.extrude");
assert!(egui::Popup::is_any_open(&h.ctx), "a tool already in another slot is disabled");
assert_eq!(h.app.preferences.mouse_gestures.tools(Studio::Part), ["undo", "solid.extrude"]);
h.frame_with(pointer(search, true));
h.frame_with(pointer(search, false));
h.step(&json!({"key":"A","cmd":true})).unwrap();
h.step(&json!({"text":"Box"})).unwrap();
h.frames(2);
click(&mut h, "dialog:list:solid.box");
assert!(!egui::Popup::is_any_open(&h.ctx), "choosing a tool closes the picker");
assert_eq!(h.app.preferences.mouse_gestures.tools(Studio::Part), ["solid.box", "solid.extrude"]);
click(&mut h, "gestures:tool:0");
h.step(&json!({"key":"Escape"})).unwrap();
h.frames(2);
assert!(!egui::Popup::is_any_open(&h.ctx), "Escape dismisses the picker");
click(&mut h, "gestures:tool:0");
h.frame_with(pointer(egui::pos2(20.0, 400.0), true));
h.frame_with(pointer(egui::pos2(20.0, 400.0), false));
h.frames(2);
assert!(!egui::Popup::is_any_open(&h.ctx), "clicking outside dismisses the picker");
}
#[test]
fn the_editor_changes_how_many_tools_and_where_they_sit() {
let mut list: Vec<String> = ["solid.extrude", "undo", "solid.hole"].iter().map(|s| s.to_string()).collect();
+13
View File
@@ -278,11 +278,24 @@ pub(crate) fn dropdown(ui: &mut Ui, id: &str, selected: &mut usize, options: &[&
/// Dropdown with caller-supplied kit choices/actions, including disabled entries and stable
/// application handles. Tokens: dropdown tokens. Use `dropdown` for a simple indexed list.
pub(crate) fn dropdown_menu(ui: &mut Ui, id: impl egui::AsIdSalt, current: &str, add: impl FnOnce(&mut Ui)) -> Response {
dropdown_menu_with_close_behavior(ui, id, current, egui::PopupCloseBehavior::CloseOnClick, add)
}
/// Dropdown with explicit dismissal behavior. For editable content use `CloseOnClickOutside`
/// and close the popup when a choice is accepted.
pub(crate) fn dropdown_menu_with_close_behavior(
ui: &mut Ui,
id: impl egui::AsIdSalt,
current: &str,
close_behavior: egui::PopupCloseBehavior,
add: impl FnOnce(&mut Ui),
) -> Response {
let response = egui::ComboBox::from_id_salt(id)
.width(dimension(ui.available_width()))
.height(Tokens::get().space_24 * 14.0)
.selected_text(current)
.icon(dropdown_arrow)
.close_behavior(close_behavior)
.show_ui(ui, add)
.response;
publish(ui, &response, egui::WidgetType::ComboBox);
+8
View File
@@ -682,6 +682,14 @@ const ELEMENTS: &[Element] = &[
list_picker(ui, "preset", "Preset", true);
});
}),
element!("dropdown_menu_with_close_behavior", |ui, s| {
dropdown_menu_with_close_behavior(ui, "gallery_editable_menu", "Preset", egui::PopupCloseBehavior::CloseOnClickOutside, |ui| {
text_field(ui, &mut s.value, "Filter…");
if list_picker(ui, "preset", "Preset", true).clicked() {
ui.close();
}
});
}),
element!("message_field", |ui, s| {
message_field(ui, ui.id().with("message"), &mut s.value, "Message");
}),