diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index dc9f9fa848..cb97e43170 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -151,8 +151,7 @@ pub fn app_bar(app: &mut VectorcraftApp, ui: &mut Ui) { }) .inner; if clicked && enabled { - app.ui.palette_open = !app.ui.palette_open; - app.ui.palette_query.clear(); + crate::palette::toggle(app); } if learn { let shown = crate::panels::learn::visible(&app.ui); diff --git a/crates/ui-egui/src/menus/run.rs b/crates/ui-egui/src/menus/run.rs index b4e1c668ac..5e31c39b2e 100644 --- a/crates/ui-egui/src/menus/run.rs +++ b/crates/ui-egui/src/menus/run.rs @@ -729,8 +729,7 @@ fn run_ui_part7(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option crate::dialogs::safe_mode::open(app), "help.copyTroubleshootingInfo" => crate::troubleshooting::copy(app), "help.commandPalette" => { - app.ui.palette_open = !app.ui.palette_open; - app.ui.palette_query.clear(); + crate::palette::toggle(app); Ok(Value::Null) } // Closing asks Save / Don't Save / Cancel for modified documents first (`unsaved`). diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index 52e42dcc28..f834d66196 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -635,6 +635,13 @@ pub(crate) fn recent_list_open(ctx: &egui::Context) -> bool { egui::Popup::is_id_open(ctx, recent_list_id()) } +/// Opens the palette with an empty search when it is closed, and closes it when it is open. +pub(crate) fn toggle(app: &mut VectorcraftApp) { + app.ui.palette_open = !app.ui.palette_open; + app.ui.palette_query.clear(); + app.ui.palette_toggled = true; +} + pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { // The widget that had the keyboard focus when the palette opened takes it back when the palette // closes, unless a pick opened a dialog, which focuses its own first field, or the press that @@ -642,6 +649,11 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { let focus_id = egui::Id::unique("palette-focus-before"); let field_id = egui::Id::unique("palette-field"); let sel_id = egui::Id::unique("palette-selected"); + // A toggle can close the palette and open it again before it is drawn closed, as two runs of its + // command in one step do, so the palette forgets its highlight after any toggle. + if std::mem::take(&mut app.ui.palette_toggled) { + ctx.data_mut(|d| d.remove::(sel_id)); + } if !app.ui.palette_open { // The palette opens again with its first row highlighted and its list scrolled to the top // (a list height of 0 scrolls it to the highlighted row). @@ -1624,6 +1636,30 @@ mod tests { assert_eq!(open.app.ui.recent_commands, ["paint.swap", "object.group"]); } + /// A palette that its command closes and opens again before it is drawn closed starts at its + /// first row, so Enter on an empty search runs the last pick. + #[test] + fn the_palette_toggled_closed_and_open_between_frames_starts_at_its_first_row() { + let mut open = Open::new("", WINDOW); + open.app = VectorcraftApp::new(vectorcraft_testkit::fixtures::Fixture::Multi.session(), crate::Services::default()); + open.app.ui.recent_commands = vec!["paint.swap".into(), "object.group".into()]; + open.app.ui.palette_open = true; + open.frame(vec![]); + open.press(egui::Key::ArrowDown, 1); + assert_eq!(open.highlighted(), 1); + open.app.run("help.commandPalette", json!({})).unwrap(); + assert!(!open.app.ui.palette_open, "the command closes the palette"); + open.app.run("help.commandPalette", json!({})).unwrap(); + assert!(open.app.ui.palette_open, "and opens it again"); + open.frame(vec![]); + assert_eq!(open.highlighted(), 0); + let before = open.app.session.active().unwrap().history.undo.len(); + open.press(egui::Key::Enter, 1); + assert!(!open.app.ui.palette_open, "the palette closes"); + assert_eq!(open.app.session.active().unwrap().history.undo.len(), before + 1, "{}", open.app.ui.status); + assert_eq!(open.app.ui.recent_commands, ["paint.swap", "object.group"]); + } + /// A pick of the palette's own toggle is not recorded, and one saved earlier is not listed, so /// Enter on an empty search runs the last other pick. #[test] diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d3698aaeef..b6bf1793d5 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -594,6 +594,11 @@ pub struct UiState { pub status_notes: Option<(String, Vec)>, pub palette_open: bool, pub palette_query: String, + /// Set when the palette's shortcut, its command or the search icon opens or closes the palette, + /// so that the palette starts at its first row the next time it is drawn, even when it closed + /// and opened again before then. Not saved. + #[serde(skip)] + pub palette_toggled: bool, /// Screen mode: 0 normal, 1 full screen with menu, 2 full screen, 3 presentation. Not saved: /// the app always starts in Normal Screen Mode, with its menus and panels (#472: a saved /// Presentation Mode came back on restart with no way out). @@ -1205,6 +1210,7 @@ impl Default for UiState { status_notes: None, palette_open: false, palette_query: String::new(), + palette_toggled: false, screen_mode: 0, tiled: false, floating: Vec::new(),