Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

UI: make the application bar's search field find and run commands

aiongga73d106ab451
diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs
index 2896fcdc4..4100f7586 100644
--- a/crates/ui-egui/src/chrome.rs
+++ b/crates/ui-egui/src/chrome.rs
@@ -10,6 +10,149 @@ use crate::theme::{Tokens, semibold};
use crate::widgets::{caption, measure, number};
use crate::{DesignApp, icons};
+fn search_id() -> egui::Id {
+ egui::Id::new("app_bar_search")
+}
+
+/// Whether the application bar's search field had the keyboard last frame. Escape takes egui's
+/// focus away before any widget runs, so that press must not reach the global shortcuts.
+pub fn search_had_focus(ctx: &egui::Context) -> bool {
+ ctx.memory(|m| m.had_focus_last_frame(search_id()))
+}
+
+/// The search field's state between frames: the query, the highlighted match, whether the matches
+/// are shown, and the widget that had the keyboard before the field took it.
+#[derive(Clone, Default)]
+struct SearchState {
+ query: String,
+ highlight: usize,
+ open: bool,
+ previous_focus: Option<egui::Id>,
+}
+
+/// The application bar's search field: finds menu commands by their labels in the interface
+/// language. ↑/↓ move through the matches; Return or a click runs one as its menu item would.
+/// Running one or pressing Escape clears the field and gives the keyboard back to the widget that
+/// had it. While the field has the keyboard, no key reaches the shortcuts or the document.
+fn search_field(app: &mut DesignApp, ui: &mut egui::Ui) {
+ let t = Tokens::get(ui.ctx());
+ let id = search_id();
+ let state_id = id.with("state");
+ let mut st: SearchState = ui.data(|d| d.get_temp(state_id)).unwrap_or_default();
+ let (r, _) = ui.allocate_exact_size(vec2(125.0, 18.0), Sense::hover());
+ ui.painter().rect(r, 1.0, t.input, Stroke::new(1.0, t.field_border), egui::StrokeKind::Inside);
+ icons::paint(ui.painter(), egui::Rect::from_min_size(r.min + vec2(3.0, 2.0), vec2(14.0, 14.0)), "search", t.icon);
+ let focused = ui.memory(|m| m.has_focus(id));
+ let had_focus = search_had_focus(ui.ctx());
+ if !focused && !had_focus {
+ st.previous_focus = ui.memory(|m| m.focused());
+ }
+ let none = egui::Modifiers::NONE;
+ let (down, up, enter, escape) = if focused || had_focus {
+ ui.input_mut(|i| {
+ (
+ i.consume_key(none, egui::Key::ArrowDown),
+ i.consume_key(none, egui::Key::ArrowUp),
+ i.consume_key(none, egui::Key::Enter),
+ i.consume_key(none, egui::Key::Escape),
+ )
+ })
+ } else {
+ (false, false, false, false)
+ };
+ let lang = app.ui.language.clone();
+ let hint = crate::rtl::widget(ui, egui::RichText::new(crate::i18n::tr(&lang, "Search")).color(t.text_dim));
+ let field = egui::Rect::from_min_max(r.min + vec2(19.0, 0.0), r.max - vec2(3.0, 0.0));
+ let resp = ui
+ .put(
+ field,
+ egui::TextEdit::singleline(&mut st.query)
+ .id(id)
+ .frame(egui::Frame::NONE)
+ .margin(egui::Margin::ZERO)
+ .font(egui::FontId::proportional(11.0))
+ .text_color(t.text)
+ .hint_text(hint)
+ .desired_width(field.width())
+ .vertical_align(egui::Align::Center)
+ // Return runs the highlighted match instead of leaving the field.
+ .return_key(None),
+ )
+ .on_hover_text(crate::i18n::tr(&lang, "Search commands"));
+ if resp.changed() {
+ st.highlight = 0;
+ st.open = true;
+ }
+ if resp.gained_focus() {
+ st.open = true;
+ }
+ let items = if st.open { crate::menus::command_search(app, &st.query) } else { Vec::new() };
+ let last = items.len().saturating_sub(1);
+ if down {
+ st.highlight = st.highlight.saturating_add(1);
+ }
+ if up {
+ st.highlight = st.highlight.saturating_sub(1);
+ }
+ st.highlight = st.highlight.min(last);
+ let mut run = None;
+ if enter && let Some(it) = items.get(st.highlight).filter(|it| crate::menus::menu_enabled(app, &it.id)) {
+ run = Some(it.id.clone());
+ }
+ let mut over_matches = false;
+ if st.open && !st.query.trim().is_empty() {
+ let shown = egui::Area::new(id.with("matches"))
+ .order(egui::Order::Foreground)
+ .pivot(egui::Align2::RIGHT_TOP)
+ .fixed_pos(r.right_bottom() + vec2(0.0, 2.0))
+ .show(ui.ctx(), |ui| {
+ egui::Frame::menu(ui.style()).show(ui, |ui| {
+ let size = vec2(300.0, 22.0);
+ ui.set_width(size.x);
+ if items.is_empty() {
+ crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&lang, "No matching commands")).color(t.text_dim));
+ }
+ for (k, it) in items.iter().enumerate() {
+ let mut b = egui::Button::new(crate::rtl::widget(ui, it.label.as_str())).frame(false).min_size(size);
+ if let Some(sc) = &it.shortcut {
+ b = b.shortcut_text(crate::menus::shortcut_text(sc));
+ }
+ if k == st.highlight {
+ ui.painter().rect_filled(egui::Rect::from_min_size(ui.cursor().min, size), 2.0, t.hover);
+ }
+ let enabled = crate::menus::menu_enabled(app, &it.id);
+ // The button aligns its label by the layout it's in: left, like the menus.
+ let row = ui.allocate_ui_with_layout(
+ size,
+ egui::Layout::left_to_right(egui::Align::Center).with_main_align(egui::Align::Min),
+ |ui| ui.add_enabled(enabled, b),
+ );
+ if row.inner.clicked() {
+ run = Some(it.id.clone());
+ }
+ }
+ });
+ });
+ over_matches = shown.response.contains_pointer();
+ }
+ if escape || run.is_some() {
+ st.query.clear();
+ st.open = false;
+ let previous = st.previous_focus.take();
+ ui.memory_mut(|m| match previous {
+ Some(p) => m.request_focus(p),
+ None => m.surrender_focus(id),
+ });
+ } else if !resp.has_focus() && !over_matches {
+ // A click elsewhere or Tab took the keyboard: the matches close, the query stays.
+ st.open = false;
+ }
+ ui.data_mut(|d| d.insert_temp(state_id, st));
+ if let Some(cmd) = run {
+ crate::menus::activate(app, &cmd, &serde_json::Value::Null);
+ }
+}
+
pub fn app_bar(app: &mut DesignApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
let left = if app.integrated_titlebar { 78 } else { 8 };
@@ -43,18 +186,7 @@ pub fn app_bar(app: &mut DesignApp, ui: &mut egui::Ui) {
let size = vec2(ui.available_width().max(tools_w), ui.available_height());
let tools = ui.allocate_ui_with_layout(size, egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.add_space(4.0);
- // Search field.
- let (r, _) = ui.allocate_exact_size(vec2(125.0, 18.0), Sense::click());
- ui.painter().rect(r, 1.0, t.input, Stroke::new(1.0, t.field_border), egui::StrokeKind::Inside);
- icons::paint(ui.painter(), egui::Rect::from_min_size(r.min + vec2(3.0, 2.0), vec2(14.0, 14.0)), "search", t.icon);
- crate::rtl::paint(
- ui.painter(),
- r.min + vec2(20.0, 9.0),
- egui::Align2::LEFT_CENTER,
- crate::i18n::tr(&app.ui.language, "Search"),
- egui::FontId::proportional(11.0),
- t.text_dim,
- );
+ search_field(app, ui);
ui.add_space(8.0);
let current = app.ui.workspace.clone();
let shown = crate::i18n::workspace_name(&app.ui.language, &current);
@@ -765,7 +897,7 @@ fn community_card(app: &mut DesignApp, ui: &mut egui::Ui) {
#[cfg(test)]
mod tests {
use egui::{pos2, vec2};
- use egui_kittest::kittest::Queryable;
+ use egui_kittest::kittest::{NodeT, Queryable};
use crate::test_window::{self, wheel};
@@ -783,6 +915,92 @@ mod tests {
h.get_by_label(label).rect()
}
+ /// The application bar's search field (German interface).
+ fn search_node<'h>(h: &'h egui_kittest::Harness<'static, test_window::Window>) -> egui_kittest::Node<'h> {
+ h.get_by(|n| n.role() == egui::accesskit::Role::TextInput && n.placeholder() == Some("Suchen"))
+ }
+
+ /// A document with two frames, the Selection tool, German menus, and the search field clicked.
+ fn searching() -> egui_kittest::Harness<'static, test_window::Window> {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.ui.language = "de".into();
+ let mut h = test_window::open(app, vec2(1440.0, 900.0));
+ let app = &mut h.state_mut().app;
+ app.run("frame.create", serde_json::json!({"rect": [36, 36, 200, 200]})).unwrap();
+ app.run("frame.create", serde_json::json!({"rect": [236, 36, 400, 200]})).unwrap();
+ app.run("edit.deselectAll", serde_json::json!({})).unwrap();
+ h.run_steps(4);
+ let field = search_node(&h).rect();
+ test_window::click_at(&mut h, field.center());
+ assert!(h.ctx.memory(|m| m.has_focus(super::search_id())), "a click gives the field the keyboard");
+ h
+ }
+
+ /// A match in the search's list, whose accessible label ends in its shortcut.
+ fn row<'h>(h: &'h egui_kittest::Harness<'static, test_window::Window>, label: &'static str) -> Option<egui_kittest::Node<'h>> {
+ h.query_by(move |n| n.label().is_some_and(|l| l == label || l.starts_with(&format!("{label} "))))
+ }
+
+ fn items(h: &egui_kittest::Harness<'static, test_window::Window>) -> (usize, usize) {
+ let d = h.state().app.session.active().unwrap();
+ (d.doc.spreads.iter().flat_map(|sp| &sp.items).count(), d.selection.items.len())
+ }
+
+ #[test]
+ fn the_search_field_finds_and_runs_menu_commands() {
+ let mut h = searching();
+ h.event(egui::Event::Text("gruppier".into()));
+ h.run_steps(4);
+ assert!(row(&h, "Gruppieren").is_some(), "the German label is listed");
+ assert!(row(&h, "Gruppierung aufheben").is_some());
+ // Nothing is selected, so Group is off and Return doesn't run it.
+ assert!(row(&h, "Gruppieren").is_some_and(|n| n.accesskit_node().is_disabled()));
+ h.key_press(egui::Key::Enter);
+ h.run_steps(4);
+ assert_eq!(items(&h), (2, 0), "a disabled command doesn't run");
+ // Select both frames, then ↓ ↑ Return runs the first match, as Object › Group would.
+ h.state_mut().app.run("edit.selectAll", serde_json::json!({})).unwrap();
+ h.key_press(egui::Key::ArrowDown);
+ h.run_steps(2);
+ h.key_press(egui::Key::ArrowUp);
+ h.run_steps(2);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(4);
+ assert_eq!(items(&h).0, 1, "the two frames are grouped");
+ assert!(row(&h, "Gruppieren").is_none(), "the matches closed");
+ assert!(!h.ctx.memory(|m| m.has_focus(super::search_id())), "the field gave the keyboard back");
+ }
+
+ #[test]
+ fn keys_typed_in_the_search_field_stay_out_of_the_document() {
+ let mut h = searching();
+ h.state_mut().app.run("edit.selectAll", serde_json::json!({})).unwrap();
+ // Escape would switch this tool back to the Selection tool.
+ h.state_mut().app.run("tool.select", serde_json::json!({"tool": "rectangleFrame"})).unwrap();
+ h.run_steps(2);
+ // Tool keys, Delete and Backspace edit the query; the tool and the frames stay.
+ for key in [egui::Key::P, egui::Key::T, egui::Key::M, egui::Key::Delete, egui::Key::Backspace] {
+ h.key_press(key);
+ h.run_steps(2);
+ }
+ h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
+ h.run_steps(2);
+ assert_eq!(h.state().app.session.tool_id(), "rectangleFrame");
+ assert_eq!(items(&h), (2, 2), "nothing was deleted or deselected");
+ h.event(egui::Event::Text("zz-no-such-command".into()));
+ h.run_steps(4);
+ assert!(h.query_by_label("Keine passenden Befehle").is_some());
+ // Escape closes the matches and clears the field without touching the document.
+ h.key_press(egui::Key::Escape);
+ h.run_steps(4);
+ assert!(h.query_by_label("Keine passenden Befehle").is_none());
+ assert!(!h.ctx.text_edit_focused());
+ assert_eq!(h.state().app.session.tool_id(), "rectangleFrame");
+ assert_eq!(items(&h), (2, 2));
+ let field = search_node(&h);
+ assert_eq!(field.value().unwrap_or_default(), "", "the query is cleared");
+ }
+
#[test]
fn a_narrow_window_scrolls_the_control_bar() {
let w = 700.0;
diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs
index e5e8a8d08..4826c4e78 100644
--- a/crates/ui-egui/src/i18n.rs
+++ b/crates/ui-egui/src/i18n.rs
@@ -2063,7 +2063,7 @@ const TABLE: &[(&str, [&str; 5])] = &[
],
),
("No text selected", ["No text selected", "No text selected", "No text selected", "No text selected", "未选择文本"]),
- ("Search", ["Search", "Search", "Search", "Search", "搜索"]),
+ ("Search", ["Suchen", "Rechercher", "Buscar", "Search", "搜索"]),
("Essentials", ["Essentials", "Essentials", "Essentials", "Essentials", "基本功能"]),
("Advanced", ["Advanced", "Advanced", "Advanced", "Advanced", "高级"]),
("Digital Publishing", ["Digital Publishing", "Digital Publishing", "Digital Publishing", "Digital Publishing", "数字出版"]),
@@ -2202,7 +2202,11 @@ const TABLE: &[(&str, [&str; 5])] = &[
("Run", ["Run", "Run", "Run", "Run", "运行"]),
("Scale X Percentage", ["Scale X Percentage", "Scale X Percentage", "Scale X Percentage", "Scale X Percentage", "水平缩放百分比"]),
("Scale Y Percentage", ["Scale Y Percentage", "Scale Y Percentage", "Scale Y Percentage", "Scale Y Percentage", "垂直缩放百分比"]),
- ("Search commands", ["Search commands", "Search commands", "Search commands", "Search commands", "搜索命令"]),
+ ("Search commands", ["Befehle suchen", "Rechercher des commandes", "Buscar comandos", "Search commands", "搜索命令"]),
+ (
+ "No matching commands",
+ ["Keine passenden Befehle", "Aucune commande correspondante", "Ningún comando coincide", "一致するコマンドはありません", "没有匹配的命令"],
+ ),
("Search fonts", ["Search fonts", "Search fonts", "Search fonts", "Search fonts", "搜索字体"]),
(
"Search styles and commands…",
diff --git a/crates/ui-egui/src/i18n/ar.rs b/crates/ui-egui/src/i18n/ar.rs
index 10e393cd2..eba2e69a6 100644
--- a/crates/ui-egui/src/i18n/ar.rs
+++ b/crates/ui-egui/src/i18n/ar.rs
@@ -501,6 +501,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[
("Scale X Percentage", "نسبة التحجيم الأفقي"),
("Scale Y Percentage", "نسبة التحجيم العمودي"),
("Search commands", "البحث عن الأوامر"),
+ ("No matching commands", "لا توجد أوامر مطابقة"),
("Search fonts", "البحث عن الخطوط"),
("Search styles and commands…", "البحث عن الأنماط والأوامر…"),
("Search: character or U+code", "البحث بحرف أو رمز U+"),
diff --git a/crates/ui-egui/src/i18n/it.rs b/crates/ui-egui/src/i18n/it.rs
index c91afd6ba..4097acf56 100644
--- a/crates/ui-egui/src/i18n/it.rs
+++ b/crates/ui-egui/src/i18n/it.rs
@@ -1100,6 +1100,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[
("Scale X Percentage", "Percentuale scala X"),
("Scale Y Percentage", "Percentuale scala Y"),
("Search commands", "Cerca comandi"),
+ ("No matching commands", "Nessun comando corrispondente"),
("Search fonts", "Cerca font"),
("Search styles and commands…", "Cerca stili e comandi…"),
("Search: character or U+code", "Cerca: carattere o codice U+"),
diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs
index be6e2c4da..13cd0edb3 100644
--- a/crates/ui-egui/src/i18n/ja.rs
+++ b/crates/ui-egui/src/i18n/ja.rs
@@ -1099,6 +1099,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[
("Scale X Percentage", "X 方向の拡大・縮小率"),
("Scale Y Percentage", "Y 方向の拡大・縮小率"),
("Search commands", "コマンドを検索"),
+ ("No matching commands", "一致するコマンドはありません"),
("Search fonts", "フォントを検索"),
("Search styles and commands…", "スタイルとコマンドを検索…"),
("Search: character or U+code", "検索: 文字または U+ コード"),
diff --git a/crates/ui-egui/src/i18n/pt_br.rs b/crates/ui-egui/src/i18n/pt_br.rs
index d25717dc6..2114636e8 100644
--- a/crates/ui-egui/src/i18n/pt_br.rs
+++ b/crates/ui-egui/src/i18n/pt_br.rs
@@ -500,6 +500,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[
("Scale X Percentage", "Percentual de escala X"),
("Scale Y Percentage", "Percentual de escala Y"),
("Search commands", "Pesquisar comandos"),
+ ("No matching commands", "Nenhum comando correspondente"),
("Search fonts", "Pesquisar fontes"),
("Search styles and commands…", "Pesquisar estilos e comandos…"),
("Search: character or U+code", "Pesquisar: caractere ou código U+"),
diff --git a/crates/ui-egui/src/i18n/uk.rs b/crates/ui-egui/src/i18n/uk.rs
index 9a9fb89ba..d1d32bc4d 100644
--- a/crates/ui-egui/src/i18n/uk.rs
+++ b/crates/ui-egui/src/i18n/uk.rs
@@ -1097,6 +1097,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[
("Scale X Percentage", "Масштаб X у відсотках"),
("Scale Y Percentage", "Масштаб Y у відсотках"),
("Search commands", "Пошук команд"),
+ ("No matching commands", "Немає відповідних команд"),
("Search fonts", "Пошук шрифтів"),
("Search styles and commands…", "Пошук стилів і команд…"),
("Search: character or U+code", "Пошук: символ або код U+"),
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index d123ec4eb..a0199fbb9 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -2111,7 +2111,7 @@ fn parse_shortcut(sc: &str) -> Option<(egui::Modifiers, egui::Key)> {
pub fn shortcuts(app: &mut DesignApp, ctx: &egui::Context) {
// Only a focused text field takes the keys: the canvas (or a button) having focus after a click
// must not swallow tool shortcuts until Esc clears it.
- if ctx.text_edit_focused() || app.ui.dialog.is_some() || app.ui.palette.is_some() {
+ if ctx.text_edit_focused() || app.ui.dialog.is_some() || app.ui.palette.is_some() || crate::chrome::search_had_focus(ctx) {
return;
}
let typing = app.session.wants_text();
@@ -2234,7 +2234,7 @@ pub struct QuickItem {
/// Quick Apply matches (styles first, as InDesign lists them), at most 14.
pub fn quick_apply_items(session: &designcraft_engine::Session, query: &str) -> Vec<QuickItem> {
let ql = query.to_lowercase();
- let hit = |label: &str, id: &str| ql.is_empty() || label.to_lowercase().contains(&ql) || id.to_lowercase().contains(&ql);
+ let hit = |label: &str, id: &str| quick_match(&ql, &[label, id]);
let mut out = Vec::new();
if let Some(st) = session.active() {
let styles = &st.doc.styles;
@@ -2258,15 +2258,49 @@ pub fn quick_apply_items(session: &designcraft_engine::Session, query: &str) ->
if ql.is_empty() {
out.clear();
}
- let commands = designcraft_engine::command_specs()
+ let commands = quick_commands().filter(|(id, l, _)| hit(l, id)).map(|(id, l, sc)| QuickItem {
+ label: l.to_string(),
+ id: id.to_string(),
+ params: json!({}),
+ shortcut: sc.map(str::to_string),
+ kind: "",
+ });
+ out.extend(commands);
+ out.truncate(14);
+ out
+}
+
+/// The commands Quick Apply and the application bar's search find: (id, label, default shortcut).
+fn quick_commands() -> impl Iterator<Item = (&'static str, &'static str, Option<&'static str>)> {
+ designcraft_engine::command_specs()
.iter()
.filter(|c| !c.menu.is_empty() || c.shortcut.is_some())
.map(|c| (c.id, c.label, c.shortcut))
.chain(UI_COMMANDS.iter().map(|c| (c.0, c.1, c.2)))
- .filter(|(id, l, _)| hit(l, id))
- .map(|(id, l, sc)| QuickItem { label: l.to_string(), id: id.to_string(), params: json!({}), shortcut: sc.map(str::to_string), kind: "" });
- out.extend(commands);
- out.truncate(14);
+}
+
+/// Whether any of `texts` contains the lowercased query (an empty query matches everything).
+fn quick_match(query_lower: &str, texts: &[&str]) -> bool {
+ query_lower.is_empty() || texts.iter().any(|t| t.to_lowercase().contains(query_lower))
+}
+
+/// The application bar's search: the commands whose label in the interface language, English
+/// label or id contains `query`, labelled in the interface language with the shortcut in effect.
+/// Nothing for an empty query; at most 14.
+pub fn command_search(app: &DesignApp, query: &str) -> Vec<QuickItem> {
+ let ql = query.trim().to_lowercase();
+ if ql.is_empty() {
+ return Vec::new();
+ }
+ let lang = app.ui.language.as_str();
+ let mut out: Vec<QuickItem> = Vec::new();
+ for (id, label, _) in quick_commands() {
+ let shown = crate::i18n::tr(lang, label);
+ if out.len() >= 14 || out.iter().any(|it| it.id == id) || !quick_match(&ql, &[shown, label, id]) {
+ continue;
+ }
+ out.push(QuickItem { label: shown.to_string(), id: id.to_string(), params: Value::Null, shortcut: shortcut_of(app, id), kind: "" });
+ }
out
}
@@ -3196,6 +3230,19 @@ mod tests {
assert_eq!(document_items(&app), (2, 2));
}
+ #[test]
+ fn command_search_matches_labels_in_the_interface_language() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.ui.language = "de".into();
+ let items = command_search(&app, "gruppieren");
+ let group = items.iter().find(|i| i.id == "object.group").unwrap();
+ assert_eq!(group.label, "Gruppieren");
+ assert_eq!(group.shortcut.as_deref(), Some("Cmd+G"));
+ assert!(command_search(&app, "group").iter().any(|i| i.id == "object.group"), "English labels match too");
+ assert!(command_search(&app, " ").is_empty());
+ assert!(command_search(&app, "e").len() <= 14);
+ }
+
#[test]
fn quick_apply_lists_styles_then_commands() {
let mut s = designcraft_engine::Session::new();