DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #279: Menus: menus taller than the window scroll
aionggf3c32bcb90df
diff --cc crates/ui-egui/src/chrome.rsindex 2a52134b7,1aeb879f9..09a578e10--- a/crates/ui-egui/src/chrome.rs+++ b/crates/ui-egui/src/chrome.rs@@@ -21,105 -21,96 +21,120 @@@ pub fn app_bar(app: &mut DesignApp, uilet full = ui.max_rect();let mut menus_end = full.min.x;let mut tools_start = full.max.x;- ui.horizontal_centered(|ui| {- if icons::button(ui, "home", 24.0, app.session.active().is_none(), crate::i18n::tr(&app.ui.language, "Home")).clicked() {- app.session_home();- }- ui.add_space(6.0);- if !app.native_menu {- ui.style_mut().visuals.widgets.inactive.weak_bg_fill = Color32::TRANSPARENT;- ui.style_mut().visuals.widgets.inactive.bg_stroke = Stroke::NONE;- ui.style_mut().visuals.widgets.hovered.bg_stroke = Stroke::NONE;- ui.style_mut().spacing.button_padding = vec2(7.0, 3.0);- crate::menus::menu_bar(app, ui);- }- menus_end = ui.min_rect().max.x;- ui.with_layout(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,- );- ui.add_space(8.0);- let current = app.ui.workspace.clone();- let shown = crate::i18n::workspace_name(&app.ui.language, ¤t);- crate::menus::menu_button(- ui,- crate::rtl::widget(ui, egui::RichText::new(format!("{shown} ▾")).font(semibold(11.5)).color(t.text)),- |ui| {- let customs: Vec<String> = app.ui.custom_workspaces.iter().map(|w| w.name.clone()).collect();- for w in- ["Essentials", "Advanced", "Book", "Digital Publishing", "Interactive for PDF", "Printing and Proofing", "Typography"]- .into_iter()- .map(str::to_string)- .chain(customs.iter().cloned())- {+ // The right-hand controls' width, measured last frame: in a window too narrow for them+ // and the menus they follow the menus, and the bar scrolls sideways.+ let tools_w_id = ui.id().with("tools_width");+ let tools_w: f32 = ui.data(|d| d.get_temp(tools_w_id)).unwrap_or(0.0);+ crate::widgets::overflow_scrolling(ui);+ egui::ScrollArea::horizontal().id_salt("app_bar_scroll").auto_shrink([false, false]).show(ui, |ui| {+ ui.horizontal_centered(|ui| {+ if icons::button(ui, "home", 24.0, app.session.active().is_none(), crate::i18n::tr(&app.ui.language, "Home")).clicked() {+ app.session_home();+ }+ ui.add_space(6.0);+ if !app.native_menu {+ ui.style_mut().visuals.widgets.inactive.weak_bg_fill = Color32::TRANSPARENT;+ ui.style_mut().visuals.widgets.inactive.bg_stroke = Stroke::NONE;+ ui.style_mut().visuals.widgets.hovered.bg_stroke = Stroke::NONE;+ ui.style_mut().spacing.button_padding = vec2(7.0, 3.0);+ 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 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,+ );+ ui.add_space(8.0);+ let current = app.ui.workspace.clone();+ let shown = crate::i18n::workspace_name(&app.ui.language, ¤t);- ui.menu_button(crate::rtl::widget(ui, egui::RichText::new(format!("{shown} ▾")).font(semibold(11.5)).color(t.text)), |ui| {- let customs: Vec<String> = app.ui.custom_workspaces.iter().map(|w| w.name.clone()).collect();- for w in- ["Essentials", "Advanced", "Book", "Digital Publishing", "Interactive for PDF", "Printing and Proofing", "Typography"]- .into_iter()- .map(str::to_string)- .chain(customs.iter().cloned())- {++ crate::menus::menu_button(++ ui,++ crate::rtl::widget(ui, egui::RichText::new(format!("{shown} ▾")).font(semibold(11.5)).color(t.text)),++ |ui| {++ let customs: Vec<String> = app.ui.custom_workspaces.iter().map(|w| w.name.clone()).collect();++ for w in [++ "Essentials",++ "Advanced",++ "Book",++ "Digital Publishing",++ "Interactive for PDF",++ "Printing and Proofing",++ "Typography",++ ]++ .into_iter()++ .map(str::to_string)++ .chain(customs.iter().cloned())++ {++ if ui++ .selectable_label(w == current, crate::rtl::widget(ui, crate::i18n::workspace_name(&app.ui.language, &w)))++ .clicked()++ {++ let _ = app.run("window.workspace", json!({"name": w}));++ ui.close();++ }++ }++ ui.separator();if ui-- .selectable_label(w == current, crate::rtl::widget(ui, crate::i18n::workspace_name(&app.ui.language, &w)))++ .button(crate::rtl::widget(++ ui,++ format!(++ "{} {}",++ crate::i18n::tr(&app.ui.language, "Reset"),++ crate::i18n::workspace_name(&app.ui.language, ¤t)++ ),++ )).clicked(){-- let _ = app.run("window.workspace", json!({"name": w}));++ let _ = app.run("window.resetWorkspace", json!({}));ui.close();}-- }-- ui.separator();-- if ui-- .button(crate::rtl::widget(-- ui,-- format!(-- "{} {}",-- crate::i18n::tr(&app.ui.language, "Reset"),-- crate::i18n::workspace_name(&app.ui.language, ¤t)-- ),-- ))-- .clicked()-- {-- let _ = app.run("window.resetWorkspace", json!({}));-- ui.close();-- }-- if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "New Workspace…"))).clicked() {-- let _ = app.run("window.newWorkspace", json!({}));-- ui.close();-- }-- if !customs.is_empty() {- ui.menu_button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Workspace")), |ui| {- crate::menus::menu_button(ui, crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Workspace")), |ui| {-- for w in &customs {-- if ui.button(w).clicked() {-- let _ = app.run("window.deleteWorkspace", json!({"name": w}));-- ui.close();-- }-- }-- });-- }- });- },- );- ui.add_space(6.0);- if icons::button(ui, "share", 22.0, false, crate::i18n::tr(&app.ui.language, "Share")).clicked() {- app.status("Export a PDF, IDML or package to share — no cloud account needed.");- }- ui.add_space(8.0);- // Always one click away: the ArtCraft community Discord.- if crate::about::discord_button(ui, "Discord", vec2(78.0, 22.0)) {- let _ = app.run("help.discord", json!({}));++ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "New Workspace…"))).clicked() {++ let _ = app.run("window.newWorkspace", json!({}));++ ui.close();++ }++ if !customs.is_empty() {++ crate::menus::menu_button(++ ui,++ crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Workspace")),++ |ui| {++ for w in &customs {++ if ui.button(w).clicked() {++ let _ = app.run("window.deleteWorkspace", json!({"name": w}));++ ui.close();++ }++ }++ },++ );++ }++ },++ );+ ui.add_space(6.0);+ if icons::button(ui, "share", 22.0, false, crate::i18n::tr(&app.ui.language, "Share")).clicked() {+ app.status("Export a PDF, IDML or package to share — no cloud account needed.");+ }+ ui.add_space(8.0);+ // Always one click away: the ArtCraft community Discord.+ if crate::about::discord_button(ui, "Discord", vec2(78.0, 22.0)) {+ let _ = app.run("help.discord", json!({}));+ }+ tools_start = ui.min_rect().min.x;+ ui.min_rect().width()+ });+ if (tools.inner - tools_w).abs() > 0.5 {+ ui.data_mut(|d| d.insert_temp(tools_w_id, tools.inner));+ ui.ctx().request_discard("app bar: the right-hand controls changed width");}- tools_start = ui.min_rect().min.x;});});// Centred title.diff --cc crates/ui-egui/src/menus.rsindex 296588e3d,f548b19af..ef7996ed5--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@@ -1844,44 -1743,42 +1844,80 @@@ pub fn activate(app: &mut DesignApp, idlet _ = app.run(id, p);}+/// A native menu bar item was chosen (macOS): like [`activate`], except that the menu bar takes+/// ⌘A, ⌘C, ⌘X, ⌘V, ⌘Z and ⇧⌘Z before the window sees them, so while a text field has keyboard+/// focus, Select All, Copy, Cut, Paste, Undo and Redo act on that field, as in any Mac app.+pub fn activate_native(app: &mut DesignApp, ctx: &egui::Context, id: &str, params: &Value) {+ if !(ctx.text_edit_focused() && params.is_null() && forward_to_text_field(app, ctx, id)) {+ activate(app, id, params);+ }+}++/// Whether a native menu bar item can be chosen: as in the in-window menus, but the editing+/// commands a focused text field takes are always available to it.+pub fn native_menu_enabled(app: &DesignApp, ctx: &egui::Context, id: &str) -> bool {+ (ctx.text_edit_focused() && TEXT_FIELD_COMMANDS.contains(&id)) || menu_enabled(app, id)+}++/// The editing commands a focused text field takes from the native menu bar.+const TEXT_FIELD_COMMANDS: [&str; 6] = ["edit.selectAll", "edit.copy", "edit.cut", "edit.paste", "edit.undo", "edit.redo"];++/// Hand a standard editing command to the focused text field as next frame's input; false when+/// `id` isn't one of them.+fn forward_to_text_field(app: &mut DesignApp, ctx: &egui::Context, id: &str) -> bool {+ let key = |key: egui::Key, modifiers: egui::Modifiers| {+ [true, false].map(|pressed| egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers })+ };+ match id {+ "edit.selectAll" => app.synthetic.extend(key(egui::Key::A, egui::Modifiers::COMMAND)),+ "edit.undo" => app.synthetic.extend(key(egui::Key::Z, egui::Modifiers::COMMAND)),+ "edit.redo" => app.synthetic.extend(key(egui::Key::Z, egui::Modifiers::COMMAND | egui::Modifiers::SHIFT)),+ "edit.copy" => app.synthetic.push(egui::Event::Copy),+ "edit.cut" => app.synthetic.push(egui::Event::Cut),+ // Only the integration reads the system clipboard: it answers with an `Event::Paste`.+ "edit.paste" => ctx.send_viewport_cmd(egui::ViewportCommand::RequestPaste),+ _ => return false,+ }+ ctx.request_repaint();+ true+}++ /// [`egui::Ui::menu_button`] for every DesignCraft menu: a menu taller than the window scrolls+ /// with the mouse wheel inside the larger of the spaces below and above its button, as native+ /// menus do. Shorter menus are unchanged.+ pub fn menu_button<'a, R>(+ ui: &mut egui::Ui,+ atoms: impl egui::IntoAtoms<'a>,+ add_contents: impl FnOnce(&mut egui::Ui) -> R,+ ) -> egui::InnerResponse<Option<R>> {+ // The button's rect from the last frame: a menu only opens on a button that was shown.+ let button = ui.ctx().read_response(ui.next_auto_id()).map(|r| r.rect);+ let max_height = menu_max_height(ui, button);+ ui.menu_button(atoms, |ui| {+ let mut scroll = egui::ScrollArea::vertical().max_height(max_height).auto_shrink([true, true]);+ if ui.is_sizing_pass() {+ // A menu that just opened starts at its top.+ scroll = scroll.vertical_scroll_offset(0.0);+ }+ scroll.show(ui, add_contents).inner+ })+ }++ /// The tallest a menu's contents may be. It depends only on the button and the window (never on+ /// where the popup ended up), so the popup's placement cannot oscillate between frames.+ fn menu_max_height(ui: &egui::Ui, button: Option<egui::Rect>) -> f32 {+ let screen = ui.ctx().content_rect();+ let space = match button {+ // Submenus open beside their item, going down from its top or up from its bottom.+ Some(r) if egui::containers::menu::is_in_menu(ui) => (screen.bottom() - r.top()).max(r.bottom() - screen.top()),+ Some(r) => (screen.bottom() - r.bottom()).max(r.top() - screen.top()),+ None => screen.height(),+ };+ // The popup's frame and a small gap to the window edge.+ let chrome = egui::Frame::menu(ui.style()).total_margin().sum().y + 8.0;+ (space - chrome).max(ui.spacing().interact_size.y)+ }+/// The menu bar contents (inside the app bar; macOS uses the native menu instead).pub fn menu_bar(app: &mut DesignApp, ui: &mut egui::Ui) {let lang = app.ui.language.clone();