Skip to content

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

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

aiongg6b205302d8d0
diff --cc crates/ui-egui/src/chrome.rs
index 9de56d2e8,4100f7586..451c933d1
--- a/crates/ui-egui/src/chrome.rs
+++ b/crates/ui-egui/src/chrome.rs
@@@ -10,47 -10,158 +10,190 @@@ use crate::theme::{Tokens, semibold}
use crate::widgets::{caption, measure, number};
use crate::{DesignApp, icons};
+/// Height of the application bar (InDesign 2026), then a 7 pt lower band and a 1 pt border. The
+/// band covers the panel's 1 pt separator line, so the frame's bottom margin is 1 pt short of it.
+const APP_BAR: f32 = 36.0;
+const APP_BAR_BAND: f32 = 7.0;
+const SEPARATOR: f32 = 1.0;
+/// Height of the application bar on macOS, where it is the window's title bar (#333). AppKit draws
+/// the traffic lights itself, centred in the standard 28 pt title-bar band at the top of the
+/// window; moving them would take AppKit calls (`unsafe`, which the workspace forbids). So the bar
+/// is that band: the lights sit vertically centred in it, with the bar's controls beside them.
+pub const MAC_APP_BAR: f32 = 28.0;
+/// The traffic lights as AppKit lays them out: button diameter, centre-to-centre pitch and the
+/// first button's left edge (8–10 pt depending on the macOS version; the larger value keeps the
+/// gap after them from shrinking).
+const MAC_LIGHT: f32 = 12.0;
+const MAC_LIGHT_PITCH: f32 = 20.0;
+const MAC_LIGHTS_LEFT: f32 = 10.0;
+/// Where the bar's content starts to the right of the traffic lights: the end of the three lights
+/// plus the same gap the lights keep from the top of the window.
+pub const MAC_CONTENT_LEFT: f32 = MAC_LIGHTS_LEFT + 2.0 * MAC_LIGHT_PITCH + MAC_LIGHT + (MAC_APP_BAR - MAC_LIGHT) / 2.0;
+
+ 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 };
- // 36 pt bar + 7 pt lower band (InDesign 2026), then a 1 pt border.
+ let mac = app.integrated_titlebar;
+ let left = if mac { MAC_CONTENT_LEFT as i8 } else { 8 };
+ let bar = if mac { MAC_APP_BAR } else { APP_BAR };
+ // The tallest control keeps a margin above and below it in the shorter macOS bar.
+ let home = if mac { 22.0 } else { 24.0 };
let resp = egui::Panel::top("app_bar")
- .exact_size(43.0)
- .frame(egui::Frame::NONE.fill(t.app_bar).inner_margin(egui::Margin { left, right: 10, top: 0, bottom: 7 }))
+ .exact_size(bar + APP_BAR_BAND)
+ .frame(egui::Frame::NONE.fill(t.app_bar).inner_margin(egui::Margin { left, right: 10, top: 0, bottom: (APP_BAR_BAND - SEPARATOR) as i8 }))
.show(ui, |ui| {
let full = ui.max_rect();
+ if mac {
+ // The whole title-bar band, margins included; the controls drawn below take
+ // their own clicks.
+ let band = egui::Rect::from_min_max(
+ egui::pos2(full.min.x - f32::from(left), full.min.y),
+ egui::pos2(full.max.x + 10.0, full.min.y + MAC_APP_BAR),
+ );
+ crate::title_bar::empty_area(ui, band);
+ }
let mut menus_end = full.min.x;
let mut tools_start = full.max.x;
// The right-hand controls' width, measured last frame: in a window too narrow for them
diff --cc crates/ui-egui/src/menus.rs
index fff62d7d2,a0199fbb9..bd8fb299f
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@@ -2469,14 -2111,7 +2469,15 @@@ fn parse_shortcut(sc: &str) -> Option<(
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() || crate::chrome::search_had_focus(ctx) {
+ // While an IME composes at the text caret, the keyboard is its own. An open canvas context menu
+ // takes the keys (Escape closes it), as a native menu would.
+ if ctx.text_edit_focused()
+ || app.ui.dialog.is_some()
+ || app.ui.palette.is_some()
+ || app.session.tool_composing()
+ || crate::canvas::context_menu_open(ctx)
++ || crate::chrome::search_had_focus(ctx)
+ {
return;
}
let typing = app.session.wants_text();
@@@ -2597,9 -2232,9 +2598,9 @@@ 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> {
+pub fn quick_apply_items(session: &designcraft_engine::Session, query: &str, cjk: bool) -> 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;
@@@ -2623,7 -2258,21 +2624,21 @@@
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 {
++ let commands = quick_commands().filter(|(id, l, _)| hit(l, id) && (cjk || !CJK_ONLY.contains(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))