DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #488: Fix needless horizontal scrollbar in the app bar at 115% scale (#481)
aionggc2124b77b8d2
diff --cc crates/ui-egui/src/chrome.rsindex 4ef83ad05,fe5e3ff63..3ebd2db6b--- a/crates/ui-egui/src/chrome.rs+++ b/crates/ui-egui/src/chrome.rs@@@ -10,190 -10,22 +10,197 @@@ 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);+ }+}++ /// Width of the app bar's right-hand slot: what is left of the row, minus a pixel of slack so+ /// that rounding at fractional scales (115%) never makes the row a hair wider than the scroll+ /// area and shows a needless scroll bar. Never narrower than the controls themselves.+ fn tools_slot_width(available: f32, tools_w: f32) -> f32 {+ (available - 1.0).max(tools_w)+ }+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@@@ -215,10 -47,21 +222,10 @@@crate::menus::menu_bar(app, ui);}menus_end = ui.min_rect().max.x;- let size = vec2(ui.available_width().max(tools_w), ui.available_height());+ let size = vec2(tools_slot_width(ui.available_width(), 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, ¤t);