diff --git a/crates/ui-egui/src/menus/menubar.rs b/crates/ui-egui/src/menus/menubar.rs index 941a8c336b..000c3a1521 100644 --- a/crates/ui-egui/src/menus/menubar.rs +++ b/crates/ui-egui/src/menus/menubar.rs @@ -1043,8 +1043,9 @@ pub fn shortcut_text(sc: &str, mac: bool) -> String { /// caption on both sides, and [`bar_width`] keeps the top bar's search field clear of the titles. const TITLE_GAP: f32 = 24.0; -/// The least room the in-window bar is given: enough for its collapsed "Menu" title. -const MENU_MIN: f32 = 64.0; +/// The room the in-window bar is given when its titles do not fit: enough for its collapsed "Menu" title and too +/// little for a title beside the "…" one, as the old bar showed one "Menu" button whatever the room. +const MENU_MIN: f32 = 48.0; /// Width of the in-window menu bar's titles. pub fn bar_width(ui: &egui::Ui) -> f32 { @@ -1082,7 +1083,7 @@ pub fn show_in_window(app: &mut LightcraftApp, ui: &mut egui::Ui, max_width: f32 let start = ui.cursor().left(); // A dialog or the shortcut recorder owns the keys: no F10, Alt or Alt+letter for the bar then. let keys = app.ui.dialog.is_none() && app.recording_shortcut.is_none(); - let room = egui::Rect::from_min_size(ui.cursor().min, egui::vec2(max_width.max(MENU_MIN), ui.available_height())); + let room = egui::Rect::from_min_size(ui.cursor().min, egui::vec2(if max_width >= bar_width(ui) { max_width } else { MENU_MIN }, ui.available_height())); let (shown, record) = craft::ui::runtime::inspect::record(ui, egui::Id::unique("lc-menubar-inspect"), |ui| { ui.scope_builder(egui::UiBuilder::new().max_rect(room), |ui| { let titles: Vec> = bar diff --git a/crates/ui-egui/src/menus/menubar/in_window_title_flow_tests.rs b/crates/ui-egui/src/menus/menubar/in_window_title_flow_tests.rs index 72c0e4a137..52dd2f16c8 100644 --- a/crates/ui-egui/src/menus/menubar/in_window_title_flow_tests.rs +++ b/crates/ui-egui/src/menus/menubar/in_window_title_flow_tests.rs @@ -344,6 +344,12 @@ fn in_window_titles_disabled_sizing_and_outside_release_do_not_open_or_execute() ], 0, ); + // The framework bar opens a menu on the press, so a release elsewhere is not what decides it: the menu + // may stay up. Either way nothing runs, and Escape leaves no menu. + f.settle(); + assert_eq!(f.document(), before); + assert_eq!(f.app.session.journal, journal); + f.key(Key::Escape); assert!(!f.is_open()); assert_eq!(f.document(), before); assert_eq!(f.app.session.journal, journal); diff --git a/crates/ui-egui/src/panels/bottombar.rs b/crates/ui-egui/src/panels/bottombar.rs index 8d4d98b5db..377b2e870d 100644 --- a/crates/ui-egui/src/panels/bottombar.rs +++ b/crates/ui-egui/src/panels/bottombar.rs @@ -650,15 +650,16 @@ mod visual_regressions { harness.key_press(egui::Key::Space); harness.run_steps(3); let mut people_focused = false; + // the menu's rows are one roving group: the arrow keys move through them for _ in 0..20 { - harness.key_press(egui::Key::Tab); + harness.key_press(egui::Key::ArrowDown); harness.run_steps(3); people_focused = harness.get_by_label("People").is_focused(); if people_focused { break; } } - assert!(people_focused, "Tab reaches People at {scale}x"); + assert!(people_focused, "ArrowDown reaches People at {scale}x"); harness.key_press(egui::Key::Space); harness.run_steps(3); assert_eq!(harness.state().ui.view, ViewMode::People, "keyboard activation dispatches the view command"); diff --git a/crates/ui-egui/src/panels/filterbar.rs b/crates/ui-egui/src/panels/filterbar.rs index 4160ee035f..375f7ffd58 100644 --- a/crates/ui-egui/src/panels/filterbar.rs +++ b/crates/ui-egui/src/panels/filterbar.rs @@ -104,30 +104,33 @@ fn dropdown_face(ui: &mut egui::Ui, id: &str, text: &str, active: bool) -> egui: shown.response } -/// Before a dropdown's menu is shown: remember whether Enter or Space (rather than a press) is what opens it. +/// Before a dropdown's menu is shown: remember that a press on its opener (rather than a key) opens it. fn note_opening(ui: &egui::Ui, opener: &egui::Response) { - let key = opener.id.with("lc-menu-by-key"); - let focused = craft::ui::runtime::focus::focused_rec(ui.ctx()).is_some_and(|rec| rec.stop.id == opener.id); - let (typed, pressed) = ui.input(|i| (i.key_pressed(egui::Key::Enter) || i.key_pressed(egui::Key::Space), i.pointer.any_pressed())); - if focused && typed { - ui.data_mut(|d| d.insert_temp(key, true)); - } else if pressed && opener.contains_pointer() { - ui.data_mut(|d| d.insert_temp(key, false)); + if ui.input(|i| i.pointer.any_pressed()) && opener.contains_pointer() { + ui.data_mut(|d| d.insert_temp(opener.id.with("lc-menu-by-pointer"), true)); } } /// When a dropdown's menu closes: one the pointer opened gives the keyboard back to the grid, so its shortcuts -/// keep working; one Enter or Space opened leaves the focus on its opener, where the framework returns it. +/// keep working, even when the framework returns the focus to the opener a few frames after the close; one a key +/// opened leaves the focus on its opener, where the framework returns it. fn menu_closed(ui: &egui::Ui, opener: &egui::Response, closed: bool) { - if !closed { - return; + let by_pointer = opener.id.with("lc-menu-by-pointer"); + let release = opener.id.with("lc-menu-release"); + let pass = ui.ctx().cumulative_pass_nr(); + if closed { + if ui.data(|d| d.get_temp::(by_pointer)).unwrap_or(false) { + ui.data_mut(|d| d.insert_temp(release, pass)); + } + ui.data_mut(|d| d.remove::(by_pointer)); } - let key = opener.id.with("lc-menu-by-key"); - let by_key = ui.data(|d| d.get_temp::(key)).unwrap_or(false); - if !by_key && let Some(rec) = craft::ui::runtime::focus::focused_rec(ui.ctx()).filter(|rec| rec.stop.id == opener.id) { + let Some(since) = ui.data(|d| d.get_temp::(release)) else { return }; + if let Some(rec) = craft::ui::runtime::focus::focused_rec(ui.ctx()).filter(|rec| rec.stop.id == opener.id) { ui.memory_mut(|m| m.surrender_focus(rec.stop.root)); + ui.data_mut(|d| d.remove::(release)); + } else if pass.saturating_sub(since) > 60 { + ui.data_mut(|d| d.remove::(release)); } - ui.data_mut(|d| d.remove::(key)); } /// One row of a value menu: radio-marked while `checked`, the lead when the menu opens. `caption` is diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs index b88a540b2d..8313418aa4 100644 --- a/crates/ui-egui/src/shortcuts.rs +++ b/crates/ui-egui/src/shortcuts.rs @@ -431,7 +431,8 @@ fn pressed_commands(app: &LightcraftApp, ctx: &egui::Context) -> (Vec, V let taken: Vec<(Modifiers, Key)> = saved_keys(keymap).map(|(_, k)| k).collect(); // an open popup (a menu, a date picker's calendar) closes on Esc itself: Esc's command (Back, // which also closes dialogs) waits until nothing is open - let popup_open = egui::Popup::is_any_open(ctx); + // (the in-window menu bar's menus are the framework's, not egui popups) + let popup_open = egui::Popup::is_any_open(ctx) || craft::ui::menu::bar::is_open(ctx); // Esc that just took a text field's focus away (it gives back the field's text) does nothing else let popup_open = popup_open || app.text_focus; ctx.input(|i| { diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index a6fbb13332..820dd07dca 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -676,7 +676,7 @@ mod issue1771 { let mut h = grid(view); assert_eq!(h.request("ui.clickWidget", json!({"id":"menu:File"}), std::time::Duration::from_secs(20))["ok"], true); h.step(); - assert!(egui::Popup::is_any_open(&h.view.ctx), "File menu open before Enter"); + assert!(craft::ui::menu::bar::is_open(&h.view.ctx), "File menu open before Enter"); key(&mut h, "Enter", false); assert_eq!(h.app.ui.view, view); assert_eq!(h.app.session.active(), Some(PhotoId(2))); diff --git a/crates/ui-egui/src/tests/carried_text_field_menu.rs b/crates/ui-egui/src/tests/carried_text_field_menu.rs index 2951deb433..7e8c246b03 100644 --- a/crates/ui-egui/src/tests/carried_text_field_menu.rs +++ b/crates/ui-egui/src/tests/carried_text_field_menu.rs @@ -69,7 +69,7 @@ fn right_click_while_carrying(h: &mut Headless, field: &str) { fn right_click_on_a_text_field_while_carrying_opens_no_menu() { let (mut h, root) = app("carry"); right_click_while_carrying(&mut h, FIELD); - assert!(!egui::Popup::is_any_open(&h.view.ctx), "{FIELD}: no Cut / Copy / Paste menu while photos are carried"); + assert!(!h.app.widgets.iter().any(|(w, _)| w == &format!("{FIELD}:copy")), "{FIELD}: no Cut / Copy / Paste menu while photos are carried"); let _ = std::fs::remove_dir_all(&root); } @@ -82,7 +82,8 @@ fn right_click_on_a_text_field_still_opens_the_menu_without_a_drag() { for _ in 0..4 { h.step(); } - assert!(egui::Popup::is_any_open(&h.view.ctx), "{FIELD}: its menu"); + // (the field's menu is the framework's: its rows are published while it is open) + assert!(h.app.widgets.iter().any(|(w, _)| w == &format!("{FIELD}:copy")), "{FIELD}: its menu"); assert!(h.app.widgets.iter().any(|(w, _)| w == &format!("{FIELD}:copy")), "{FIELD}: Copy in its menu"); let _ = std::fs::remove_dir_all(&root); } @@ -108,6 +109,6 @@ fn a_drag_left_over_without_its_button_does_not_hold_back_the_menu() { } h.step(); h.step(); - assert!(egui::Popup::is_any_open(&h.view.ctx), "the search field's menu opens"); + assert!(h.app.widgets.iter().any(|(w, _)| w == &format!("{FIELD}:copy")), "the search field's menu opens"); let _ = std::fs::remove_dir_all(&root); }