diff --git a/assets/locales/pending/config-column-filter.json b/assets/locales/pending/config-column-filter.json new file mode 100644 index 000000000..8d4a495ce --- /dev/null +++ b/assets/locales/pending/config-column-filter.json @@ -0,0 +1,10 @@ +{ + "en": { "config.column_filter_empty": "No matching columns" }, + "ar": { "config.column_filter_empty": "لا توجد أعمدة مطابقة" }, + "de": { "config.column_filter_empty": "Keine passenden Spalten" }, + "he": { "config.column_filter_empty": "אין עמודות תואמות" }, + "ja": { "config.column_filter_empty": "一致する列がありません" }, + "ko": { "config.column_filter_empty": "일치하는 열이 없습니다" }, + "vi": { "config.column_filter_empty": "Không có cột phù hợp" }, + "zh": { "config.column_filter_empty": "没有匹配的列" } +} diff --git a/crates/ui-egui/src/configuration_rules_ui.rs b/crates/ui-egui/src/configuration_rules_ui.rs index e92316817..1fe0f88f8 100644 --- a/crates/ui-egui/src/configuration_rules_ui.rs +++ b/crates/ui-egui/src/configuration_rules_ui.rs @@ -18,6 +18,10 @@ pub struct Editor { pub(super) drafts: std::collections::BTreeMap<(String, String), String>, /// The configuration table at a design revision: the command's answer and its shown cells. pub(super) table: Option>, + /// The Add Column menu's filter text, and whether the menu was open last frame (the filter + /// takes focus when the menu opens and is cleared when it closes). + pub(super) column_filter: String, + pub(super) column_menu_open: bool, } impl Default for Editor { fn default() -> Self { @@ -39,6 +43,8 @@ impl Default for Editor { focus_new_row: false, drafts: Default::default(), table: None, + column_filter: String::new(), + column_menu_open: false, } } } diff --git a/crates/ui-egui/src/configurations_ui.rs b/crates/ui-egui/src/configurations_ui.rs index 9e8ce817a..108b99066 100644 --- a/crates/ui-egui/src/configurations_ui.rs +++ b/crates/ui-egui/src/configurations_ui.rs @@ -79,7 +79,7 @@ pub(super) fn configurations_rows( ui_kit::table_custom(ui, "configuration_table", &columns, crate::i18n::tr("manage.empty"), |ui, table| { table.headers(ui, |ui, index| { if index == labels.len() - 1 { - column_menu(app, ui, error, &tb.columns); + column_menu(app, ui, error, &tb.columns, rule_editor); true } else if index > 0 { if let (Some(column), Some(label)) = (tb.columns.get(index - 1), labels.get(index)) @@ -301,7 +301,13 @@ fn with_units(app: &SolveApp, mut tb: super::Table) -> super::Table { tb } -fn column_menu(app: &mut SolveApp, ui: &mut egui::Ui, error: &mut Option, columns: &[String]) { +fn column_menu( + app: &mut SolveApp, + ui: &mut egui::Ui, + error: &mut Option, + columns: &[String], + rule_editor: &mut configuration_rules_ui::Editor, +) { let run = |app: &mut SolveApp, error: &mut Option, id: &str, params: Value| { *error = app.run(id, params).err(); }; @@ -335,14 +341,41 @@ fn column_menu(app: &mut SolveApp, ui: &mut egui::Ui, error: &mut Option } } let mut pick: Option = None; - ui_kit::plus_header(ui, |ui| { - egui::ScrollArea::vertical().max_height(crate::theme::Tokens::get().space_24 * 12.0).show(ui, |ui| { - for (label, p) in &options { + let filter = &mut rule_editor.column_filter; + let was_open = rule_editor.column_menu_open; + let face_appearance = crate::i18n::tr_label("Face Appearance"); + let shown = ui_kit::plus_header(ui, |ui| { + // A design has many parameters, features and bodies: a filter narrows the list as the + // Parameters dialog's does. It takes focus as the menu opens, so typing filters at once. + let search = ui_kit::menu_filter(ui, ui.make_persistent_id("config_column_filter"), filter, "Filter"); + if !was_open { + search.request_focus(); + } + crate::scenario::publish_handle("dialog:menu:Add Column:filter", search.rect.center()); + let query = filter.trim().to_lowercase(); + let matching: Vec<&(String, Value)> = options.iter().filter(|(label, _)| query.is_empty() || label.to_lowercase().contains(&query)).collect(); + // Enter with one match left picks it, as in the command search. + if matching.len() == 1 && search.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) { + pick = matching.first().map(|(_, p)| p.clone()); + ui.close(); + } + let faces = query.is_empty() || face_appearance.to_lowercase().contains(&query); + if matching.is_empty() && !faces { + ui_kit::text(ui, crate::i18n::tr("config.column_filter_empty"), ui_kit::Text::Note("column_filter_empty")); + } + // The popup remembers its size: once the filter has emptied the list, a list that fills + // again would be clipped to that small height, so a list that scrolls takes the full one. + let list_height = crate::theme::Tokens::get().space_24 * 12.0; + egui::ScrollArea::vertical().max_height(list_height).min_scrolled_height(list_height).show(ui, |ui| { + for (label, p) in &matching { if ui_kit::button(ui, label.as_str(), ui_kit::Button::Normal).clicked() { pick = Some(p.clone()); ui.close(); } } + if !faces { + return; + } ui_kit::action_menu(ui, "Face Appearance", |ui| { ui_kit::text( ui, @@ -393,6 +426,10 @@ fn column_menu(app: &mut SolveApp, ui: &mut egui::Ui, error: &mut Option }); }); }); + rule_editor.column_menu_open = shown.is_some(); + if shown.is_none() { + rule_editor.column_filter.clear(); + } if let Some(p) = pick { // The first column starts the table (with a Default row of the current values). let (id, p) = if columns.is_empty() { diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index af867f5ac..1c7d3003a 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -1275,6 +1275,7 @@ mod tests { KitCall { name: "cell_editor", args: &[3], id: false, trim: &[] }, KitCall { name: "bounded_cell_editor", args: &[3], id: false, trim: &[] }, KitCall { name: "search_field", args: &[3], id: false, trim: &[] }, + KitCall { name: "menu_filter", args: &[3], id: false, trim: &[] }, KitCall { name: "placement_field", args: &[3], id: false, trim: &[] }, KitCall { name: "library_cell", args: &[3], id: false, trim: &[] }, // actions.rs @@ -1948,7 +1949,7 @@ mod tests { /// The names the hand-written kit probes import, exactly as a UI file would. const KIT_IMPORTS: &str = concat!( "use crate::ui_kit::{row, status_line, expr_row, hint_row, text_row, choice_row, section, section_count, ", - "multiline_text_field, dropdown, cell_editor, bounded_cell_editor, search_field, placement_field, ", + "multiline_text_field, dropdown, cell_editor, bounded_cell_editor, search_field, menu_filter, placement_field, ", "library_cell, icon_button_at, icon_button_colored, footer, button, primary_button, ", "wide_primary_button, action_menu, compact_button, icon_label_button, command_menu_button, ", "checkbox_grid, options, checkbox_row, toggle_icon, removable_header, bin_button, ", @@ -1986,6 +1987,7 @@ mod tests { ("cell_editor(ui, id, value, \"cell_editor\", invalid)", &["cell_editor"]), ("bounded_cell_editor(ui, id, value, \"bounded_cell_editor\", invalid, limit)", &["bounded_cell_editor"]), ("search_field(ui, id, value, \"search_field\")", &["search_field"]), + ("menu_filter(ui, id, value, \"menu_filter\")", &["menu_filter"]), ("placement_field(ui, id, value, \"placement_field\")", &["placement_field"]), ("library_cell(ui, id, value, \"library_cell\", |ui| {})", &["library_cell"]), ("icon_button_at(ui, rect, icon, \"icon_button_at\")", &["icon_button_at"]), diff --git a/crates/ui-egui/src/manage_dialogs_tests.rs b/crates/ui-egui/src/manage_dialogs_tests.rs index cc41cbf46..d07b141bb 100644 --- a/crates/ui-egui/src/manage_dialogs_tests.rs +++ b/crates/ui-egui/src/manage_dialogs_tests.rs @@ -492,6 +492,56 @@ fn configuration_inventory_keeps_library_rules_preview_and_inline_row_lifecycle( assert_eq!(h.app.session.doc.configs.rows.len(), 1, "bin retains immediate deletion"); } +#[test] +fn configuration_add_column_menu_filters_as_typed_and_resets_on_close() { + // A design has many parameters, features and bodies: the Add Column menu's filter narrows + // its list as the user types, Enter picks a lone match, and the next opening starts blank. + let mut h = fixture(); + h.app.run("config.configure", json!({"params":["width"]})).unwrap(); + h.app.start("config.table"); + h.frames(4); + h.step(&json!({"click":{"handle":"dialog:menu:Add Column"}})).unwrap(); + h.frames(2); + assert!(scenario::handle_at("dialog:menu:Add Column:filter").is_some(), "the menu opens with a filter field"); + h.step(&json!({"click":{"handle":"dialog:menu:Add Column:filter"}})).unwrap(); + h.frames(2); + assert!(scenario::handle_at("dialog:menu:Add Column:filter").is_some(), "a click into the filter keeps the menu open"); + let feature = h.app.session.doc.features[0].name.clone(); + assert!(h.widget(&format!("Suppress {feature}")).is_some()); + assert!(h.widget("Body1 (material)").is_some()); + assert!(h.widget("Face Appearance").is_some()); + h.step(&json!({"text":"mater"})).unwrap(); + h.frames(2); + assert!(h.widget("Body1 (material)").is_some(), "the match stays"); + assert!(h.widget(&format!("Suppress {feature}")).is_none(), "a feature does not match `mater`"); + assert!(h.widget("Body1 (appearance)").is_none()); + assert!(h.widget("Face Appearance").is_none(), "the submenu hides when it does not match"); + h.step(&json!({"key":"A","cmd":true})).unwrap(); + h.step(&json!({"text":"zzz"})).unwrap(); + h.frames(2); + assert!(h.widget("No matching columns").is_some()); + h.step(&json!({"key":"A","cmd":true})).unwrap(); + h.step(&json!({"text":"face app"})).unwrap(); + h.frames(2); + assert!(h.widget("Face Appearance").is_some(), "the submenu matches by its own label"); + h.step(&json!({"key":"A","cmd":true})).unwrap(); + h.step(&json!({"text":"Suppress"})).unwrap(); + h.step(&json!({"key":"Enter"})).unwrap(); + h.frames(3); + assert!( + h.app.session.doc.configs.columns.iter().any(|c| matches!(c, solidcraft_engine::doc::config::Column::Suppress(_))), + "Enter picks the one match" + ); + assert!(h.app.dialog.is_some(), "Enter in the filter does not submit the card"); + assert!(scenario::handle_at("dialog:menu:Add Column:filter").is_none(), "picking closes the menu"); + h.step(&json!({"click":{"handle":"dialog:menu:Add Column"}})).unwrap(); + h.frames(2); + assert!(h.widget("Body1 (material)").is_some(), "a reopened menu starts unfiltered"); + assert!(h.widget(&format!("Suppress {feature}")).is_none(), "an added column leaves the menu"); + h.step(&json!({"key":"Escape"})).unwrap(); + assert!(h.app.dialog.is_some(), "menu Escape leaves the card open"); +} + #[test] fn draft_bins_do_not_delete_saved_rows_named_new() { for command in ["parameters.change", "config.table"] { diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs index eb4e3dc50..d68770ff1 100644 --- a/crates/ui-egui/src/ui_kit/fields.rs +++ b/crates/ui-egui/src/ui_kit/fields.rs @@ -569,6 +569,45 @@ pub(crate) fn searchable_icon_dropdown( searchable_dropdown_with(ui, "icon_search_menu", label, filter, options, SearchMenu { custom: None, pending: None, icon: Some(icon) }) } +/// The filter at the top of a long menu: a search glyph, a frameless editor and a rule under +/// them, so it reads as the menu's search box and not as one more entry. Tokens: `space_8/16, +/// font_label, form_dim, form_ink, dialog_rule`. +pub(crate) fn menu_filter(ui: &mut Ui, id: egui::Id, value: &mut String, hint: &str) -> Response { + let t = Tokens::get(); + let (rect, _) = ui.allocate_exact_size(vec2(dimension(ui.available_width()), row_height()), egui::Sense::hover()); + let glyph = egui::Rect::from_center_size(egui::pos2(rect.left() + t.space_8 + t.space_16 / 2.0, rect.center().y), vec2(t.space_16, t.space_16)); + crate::icons::paint_ink(ui.painter(), glyph, "zoom", t.form_dim); + let field = egui::Rect::from_min_max(egui::pos2((glyph.right() + t.space_8).min(rect.right()), rect.top()), rect.max); + let hint_text = crate::i18n::tr_label(hint); + let response = ui + .scope_builder(egui::UiBuilder::new().max_rect(field).layout(egui::Layout::left_to_right(egui::Align::Center)), |ui| { + ui.set_clip_rect(ui.clip_rect().intersect(field)); + ui.add_sized( + field.size(), + text_input(egui::TextEdit::singleline(value)) + .id(id) + .char_limit(256) + .hint_text(hint_text) + .font(egui::FontId::proportional(t.font_label)) + .text_color(t.form_ink) + .frame(egui::Frame::NONE) + .clip_text(true), + ) + }) + .inner; + response.widget_info(|| { + let mut info = egui::WidgetInfo::labeled(egui::WidgetType::TextEdit, ui.is_enabled(), hint_text); + info.current_text_value = Some(value.clone()); + info + }); + super::layout::publish_geometry(ui, &response); + if ui.rect_contains_pointer(rect) { + ui.ctx().set_cursor_icon(egui::CursorIcon::Text); + } + super::Menu::separator(ui); + response +} + /// Behaviour of the one kit slider. Appearance is always owned by `slider`. /// Values/ranges stay in caller units; formatting never rounds saved values during drawing. pub(crate) struct Slider { diff --git a/crates/ui-egui/src/ui_kit/gallery.rs b/crates/ui-egui/src/ui_kit/gallery.rs index 76e77bb31..5eab9dee5 100644 --- a/crates/ui-egui/src/ui_kit/gallery.rs +++ b/crates/ui-egui/src/ui_kit/gallery.rs @@ -1086,6 +1086,9 @@ const ELEMENTS: &[Element] = &[ element!("search_field", |ui, s| { search_field(ui, ui.id().with("search"), &mut s.value, "Search"); }), + element!("menu_filter", |ui, s| { + menu_filter(ui, ui.id().with("menu_filter"), &mut s.value, "Filter"); + }), element!("cell_editor", |ui, s| { cell_editor(ui, ui.id().with("cell"), &mut s.value, "Expression", true); }), diff --git a/crates/ui-egui/src/ui_kit/table.rs b/crates/ui-egui/src/ui_kit/table.rs index 32ceb8f79..674be3f10 100644 --- a/crates/ui-egui/src/ui_kit/table.rs +++ b/crates/ui-egui/src/ui_kit/table.rs @@ -343,7 +343,9 @@ pub(crate) fn bin_button(ui: &mut Ui, tooltip: &str) -> Response { response } -/// Last header cell opens the unchanged column menu. Hover brackets indicate insertion. +/// Last header cell opens the unchanged column menu. Hover brackets indicate insertion. The +/// menu holds a filter field, so a click inside it (into the field) keeps it open: entries +/// close it themselves when picked. pub(crate) fn plus_header(ui: &mut Ui, add: impl FnOnce(&mut Ui) -> R) -> Option { let t = Tokens::get(); let rect = egui::Rect::from_center_size(ui.max_rect().center(), vec2(t.form_close_size - t.space_4, t.form_close_size - t.space_4)); @@ -357,7 +359,7 @@ pub(crate) fn plus_header(ui: &mut Ui, add: impl FnOnce(&mut Ui) -> R) -> Opt } scenario::publish_handle("dialog:menu:Add Column", rect.center()); scenario::publish_rect("dialog:menu:Add Column", rect); - let popup = Container::new(ui.ctx(), Surface::Popup).menu(&response); + let popup = Container::new(ui.ctx(), Surface::Popup).menu(&response).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside); let open = popup.is_open(); let result = popup.show(|ui| super::scope(ui, add)).map(|r| r.inner); if open { diff --git a/docs/ui-kit.md b/docs/ui-kit.md index b200fd69b..23f5edcdf 100644 --- a/docs/ui-kit.md +++ b/docs/ui-kit.md @@ -157,6 +157,7 @@ table. Panel/Window/Confirm compositions also have dedicated geometry and intera | `password_field` | Stable secret editor; excludes the secret from scenario/accessibility metadata | field tokens | | `message_field` | Growing message editor; owner retains scroll and Enter routing | field tokens | | `cell_editor`, `bounded_cell_editor`, `search_field`, `library_cell` | Stable-ID inline/search editors; bounded variant caps Unicode characters; `library_cell` attaches a Library dropdown inside the editor border | field tokens, `text/error` | +| `menu_filter` | The filter at the top of a long menu: search glyph, frameless editor and a rule under them, so it does not read as an entry; the caller filters and owns focus | `space_8/16`, `font_label`, `form_dim/ink`, `dialog_rule` | | `text_area` | Bounded multiline JSON/data editor | field tokens | | `dropdown`, `dropdown_menu`, `compact_dropdown_menu` | Indexed choices or caller-supplied menu controls | `control_height`, `text_dim`, form tokens | | `searchable_dropdown`, `SearchOption` | Filtered choices, custom-value action and pending message | dropdown tokens |