mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 08:52:45 +01:00
Command Palette: "setting" finds Preferences, and a Recent Commands pick gives the focus back
The stem forms of a group's English words join the group, so "setting" finds Preferences through settings. A pick from Recent Commands with the keyboard, which leaves the focus on its row, now gives the focus back to the widget that had it when the palette opened.
This commit is contained in:
1 parent
eeec4e69e2
commit
8cd51bf95a
2 files changed
+70
-10
No files matched your search
@@ -569,16 +569,21 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
// closed it focused another widget, such as a field in a window above the backdrop.
|
||||
let focus_id = egui::Id::unique("palette-focus-before");
|
||||
let field_id = egui::Id::unique("palette-field");
|
||||
// The Recent Commands rows drawn in the last frame, so a pick with the keyboard, which leaves the
|
||||
// focus on its row, gives the focus back too.
|
||||
let rows_id = egui::Id::unique("palette-recent-rows");
|
||||
if !app.ui.palette_open {
|
||||
let field = ctx.data_mut(|d| {
|
||||
let (field, rows) = ctx.data_mut(|d| {
|
||||
let field = d.get_temp::<egui::Id>(field_id);
|
||||
d.remove::<egui::Id>(field_id);
|
||||
field
|
||||
let rows = d.get_temp::<Vec<egui::Id>>(rows_id).unwrap_or_default();
|
||||
d.remove::<Vec<egui::Id>>(rows_id);
|
||||
(field, rows)
|
||||
});
|
||||
let now = ctx.memory(|m| m.focused());
|
||||
if let Some(Some(before)) = ctx.data_mut(|d| d.remove_temp::<Option<egui::Id>>(focus_id))
|
||||
&& app.ui.dialog.is_none()
|
||||
&& (now.is_none() || now == field)
|
||||
&& (now.is_none() || now == field || now.is_some_and(|n| rows.contains(&n)))
|
||||
{
|
||||
ctx.memory_mut(|m| m.request_focus(before));
|
||||
}
|
||||
@@ -701,12 +706,14 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
if !list_open && !history_focused {
|
||||
r.request_focus();
|
||||
}
|
||||
let mut rows = Vec::new();
|
||||
egui::Popup::menu(&button).id(recent_list_id()).show(|ui| {
|
||||
if recent.is_empty() {
|
||||
ui.add_enabled(false, egui::Button::new(tl!("No recent commands")));
|
||||
}
|
||||
for (i, e) in recent.iter().enumerate() {
|
||||
let row = ui.add(egui::Button::new(e.shown.as_str()).shortcut_text(menus::shown_shortcut(&e.shortcut)));
|
||||
rows.push(row.id);
|
||||
if i == rsel {
|
||||
row.request_focus();
|
||||
}
|
||||
@@ -715,6 +722,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.ctx().data_mut(|d| d.insert_temp(rows_id, rows));
|
||||
r
|
||||
})
|
||||
.inner;
|
||||
@@ -2334,6 +2342,44 @@ mod tests {
|
||||
assert_eq!(ctx.memory(|m| m.focused()), Some(above), "the clicked field keeps the focus");
|
||||
}
|
||||
|
||||
/// A pick with the keyboard from Recent Commands that opens no dialog gives the focus back to
|
||||
/// the widget that had it when the palette opened, though the picked row had the keyboard.
|
||||
#[test]
|
||||
fn a_pick_from_recent_commands_gives_the_focus_back() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default());
|
||||
app.ui.recent_commands = vec!["paint.swap".into()];
|
||||
let mut text = String::new();
|
||||
let field = egui::Id::unique("other field");
|
||||
let mut frame = |app: &mut VectorcraftApp, focus: bool, events: Vec<egui::Event>| {
|
||||
ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| {
|
||||
let r = ui.add(egui::TextEdit::singleline(&mut text).id(field));
|
||||
if focus {
|
||||
r.request_focus();
|
||||
}
|
||||
show(app, ui.ctx());
|
||||
})
|
||||
.textures_delta
|
||||
.clear();
|
||||
};
|
||||
frame(&mut app, true, vec![]);
|
||||
frame(&mut app, false, vec![]);
|
||||
app.ui.palette_open = true;
|
||||
frame(&mut app, false, vec![]);
|
||||
egui::Popup::open_id(&ctx, recent_list_id());
|
||||
frame(&mut app, false, vec![]);
|
||||
frame(&mut app, false, vec![]);
|
||||
assert!(recent_list_open(&ctx), "the list is open");
|
||||
assert_ne!(ctx.memory(|m| m.focused()), Some(field), "the row has the keyboard");
|
||||
let enter = egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE };
|
||||
frame(&mut app, false, vec![enter]);
|
||||
assert!(!app.ui.palette_open, "the pick closes the palette");
|
||||
assert!(app.ui.dialog.is_none(), "{}", app.ui.status);
|
||||
frame(&mut app, false, vec![]);
|
||||
frame(&mut app, false, vec![]);
|
||||
assert_eq!(ctx.memory(|m| m.focused()), Some(field));
|
||||
}
|
||||
|
||||
/// The whole window, with a document whose rectangle is selected, drawn a frame at a time in
|
||||
/// English with screen readers on.
|
||||
struct Window {
|
||||
|
||||
@@ -72,8 +72,9 @@ pub(crate) struct Index {
|
||||
initials_text: String,
|
||||
/// Each result's offset and length in `initials_text`.
|
||||
initials: Vec<(u32, u16)>,
|
||||
/// Each word of a group of words with the same meaning ([`super::words::GROUPS`], and the
|
||||
/// words the UI language's catalog adds), with the group's place in the table.
|
||||
/// Each word of a group of words with the same meaning ([`super::words::GROUPS`], the words the
|
||||
/// UI language's catalog adds, and the English words' stem forms), with the group's place in the
|
||||
/// table.
|
||||
synonyms: HashMap<Box<str>, u16>,
|
||||
}
|
||||
|
||||
@@ -84,8 +85,10 @@ pub(crate) fn is_one_word(w: &str) -> bool {
|
||||
}
|
||||
|
||||
/// The words of each group of [`super::words::GROUPS`], with the words `search_row` gives for the
|
||||
/// group's catalog row ([`super::words::row_key`]) in the UI language. A word the catalog gives
|
||||
/// that is not a single word, or that is already a word of a group, is left out.
|
||||
/// group's catalog row ([`super::words::row_key`]) in the UI language, and the English stem forms
|
||||
/// ([`stems`]) of the group's English words (`setting` of settings). A word the catalog gives that
|
||||
/// is not a single word, or that is already a word of a group, is left out, and so is a stem form
|
||||
/// that is already a word of a group.
|
||||
fn synonym_groups(search_row: impl Fn(&str) -> Option<String>) -> HashMap<Box<str>, u16> {
|
||||
let mut out: HashMap<Box<str>, u16> = HashMap::new();
|
||||
let groups = super::words::GROUPS.iter().enumerate().filter_map(|(g, words)| u16::try_from(g).ok().map(|g| (g, words)));
|
||||
@@ -94,7 +97,7 @@ fn synonym_groups(search_row: impl Fn(&str) -> Option<String>) -> HashMap<Box<st
|
||||
out.insert(w.into(), g);
|
||||
}
|
||||
}
|
||||
for (g, words) in groups {
|
||||
for (g, words) in groups.clone() {
|
||||
let Some(text) = search_row(&super::words::row_key(words)) else { continue };
|
||||
for (w, single) in super::words::row_words(&text) {
|
||||
if single {
|
||||
@@ -102,6 +105,11 @@ fn synonym_groups(search_row: impl Fn(&str) -> Option<String>) -> HashMap<Box<st
|
||||
}
|
||||
}
|
||||
}
|
||||
for (g, words) in groups {
|
||||
for form in words.iter().flat_map(|w| stems(w)) {
|
||||
out.entry(form.into()).or_insert(g);
|
||||
}
|
||||
}
|
||||
out.shrink_to_fit();
|
||||
out
|
||||
}
|
||||
@@ -1104,8 +1112,9 @@ mod tests {
|
||||
|
||||
/// A query word finds a result whose label or menu path holds another word of its group of words
|
||||
/// with the same meaning: below a result whose label holds the word itself, above one whose menu
|
||||
/// path holds the word, and with a menu path word of the group last. A stem form of the query word
|
||||
/// and of a result's word meet the group too; ids, word starts and words of no group do not.
|
||||
/// path holds the word, and with a menu path word of the group last. A stem form of the query word,
|
||||
/// of a result's word and of a group's word meet the group too; ids, word starts and words of no
|
||||
/// group do not.
|
||||
#[test]
|
||||
fn a_word_with_the_same_meaning_finds_the_result() {
|
||||
let c = synthetic(vec![
|
||||
@@ -1121,6 +1130,10 @@ mod tests {
|
||||
let c = synthetic(vec![entry("Zz Unite", "a.one"), entry("Zz Merge", "b.two")]);
|
||||
assert_eq!(ranked(&c, "merge"), ["b.two"], "merge is in no group");
|
||||
assert_eq!(ranked(&c, "combine"), ["a.one"]);
|
||||
// The stem form of a plural word of a group is in the group too: "setting" meets settings.
|
||||
let c = synthetic(vec![entry("Zz Preferences", "a.one"), entry("Zz Other", "b.two")]);
|
||||
assert_eq!(ranked(&c, "setting zz"), ["a.one"], "a stem form of a group's word");
|
||||
assert_eq!(ranked(&c, "settings zz"), ["a.one"]);
|
||||
// Among results that match equally well, the whole query read with the word of the same
|
||||
// meaning ("colour" as "color") puts the label Color first, but never above a label that
|
||||
// holds the query word itself.
|
||||
@@ -1164,6 +1177,7 @@ mod tests {
|
||||
assert!(place(&c, "unite", "object.slice.combine").is_some(), "{:?}", ranked(&c, "unite"));
|
||||
assert!(place(&c, "delete", "view.guides.clear").is_some(), "{:?}", ranked(&c, "delete"));
|
||||
assert!(place(&c, "settings", "edit.preferences").is_some(), "{:?}", ranked(&c, "settings"));
|
||||
assert!(place(&c, "setting", "edit.preferences").is_some(), "{:?}", ranked(&c, "setting"));
|
||||
let merge = ranked(&c, "merge");
|
||||
let unite = merge.iter().position(|id| id == "object.pathfinder.unite");
|
||||
let named = |id: &String| c.list.iter().any(|e| &e.id == id && split_name(&e.shown).0 == "Merge");
|
||||
|
||||
Reference in new issue
Block a user