mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 14:34:34 +00:00
Command Palette: a palette toggled closed and open before it is drawn starts at its first row
The palette's command, its shortcut and the search icon now go through one toggle that marks the palette as toggled, and the palette forgets its highlight the next time it is drawn. Two runs of the command in one step (closed, then open again before a frame drew it closed) kept the old row, and Enter on an empty search ran that row instead of the last pick.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -729,8 +729,7 @@ fn run_ui_part7(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option<Result<
|
||||
"help.safeMode" => 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`).
|
||||
|
||||
@@ -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::<Highlight>(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]
|
||||
|
||||
@@ -594,6 +594,11 @@ pub struct UiState {
|
||||
pub status_notes: Option<(String, Vec<String>)>,
|
||||
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(),
|
||||
|
||||
Reference in New Issue
Block a user