Add a Rename button to Text Style and tighten who has the keys

Rename is now a button beside Delete, so it can be reached by the keyboard, and its field takes the focus when it shows; the focus returns to the styles when the rename ends. A choice made by key in the style filter list and the font style list closes the list. The dialog counts focus on a foreground layer as its own only while a list or menu is open, so an editor over the drawing keeps its Escape; and a click counts only when the dialog is the topmost window under the pointer, so a window over the dialog does not give it the keys.
This commit is contained in:
code-compliant committed 2026-12-02 05:47:15 +08:00
1 parent 206d40bc0d
commit 3371028cdf
2 files changed
+148 -22

No files matched your search

+27 -10
View File
@@ -54,9 +54,8 @@ struct State {
/// The pass that last drew the dialog: a gap means it was closed or replaced and has just
/// opened again.
last_pass: Option<u64>,
/// The dialog's window layer and area, to tell whether the focus or a click is in it.
/// The dialog's window layer, to tell whether the focus or a click is in it.
layer: Option<egui::LayerId>,
rect: Option<egui::Rect>,
/// Whether the keys are the dialog's: the focus is in it, or was and nothing has taken it.
/// egui drops the focus when an Escape arrives, before this code runs, when a control goes
/// away and on a click on blank space; a list or menu holds it for a while. So only the focus
@@ -69,6 +68,8 @@ struct State {
/// The control of the dialog that last had the focus, to give it back when a list or menu
/// opened from the dialog closes.
last_focus: Option<egui::Id>,
/// The rename field takes the focus when it first shows.
rename_focus: bool,
}
/// The page the dialog shows.
@@ -98,14 +99,16 @@ fn track_keys(ctx: &egui::Context, st: &mut State, popup_open: bool) {
let focus_layer = ctx.memory(|memory| memory.focused()).and_then(|id| ctx.read_response(id)).map(|widget| widget.layer_id);
match focus_layer {
Some(layer) if Some(layer) == st.layer => st.has_keys = true,
// A list or menu, from the dialog or from anywhere else: nothing changes.
Some(layer) if layer.order == egui::Order::Foreground => {}
// The drawing, the command line or another window.
// A list or menu: opened from the dialog or from anywhere else, so nothing changes.
Some(layer) if layer.order == egui::Order::Foreground && popup_open => {}
// The drawing, the command line, another window, or an editor over the drawing.
Some(_) => st.has_keys = false,
None => {}
}
if let Some(at) = ctx.input(|i| if i.pointer.any_pressed() { i.pointer.interact_pos() } else { None }) {
if st.rect.is_some_and(|rect| rect.contains(at)) {
// The topmost window under the pointer, so a click on another window that overlaps the
// dialog does not count.
if st.layer.is_some() && ctx.layer_id_at(at) == st.layer {
st.has_keys = true;
} else if !popup_open {
st.has_keys = false;
@@ -222,7 +225,6 @@ pub(super) fn dialog(app: &mut CadApp, ctx: &egui::Context, open: &mut bool) ->
focus_before: st.focus_before,
last_pass: st.last_pass,
layer: st.layer,
rect: st.rect,
has_keys: st.has_keys,
popup_was: st.popup_was,
last_focus: st.last_focus,
@@ -256,7 +258,6 @@ pub(super) fn dialog(app: &mut CadApp, ctx: &egui::Context, open: &mut bool) ->
});
if let Some(window) = &shown {
st.layer = Some(window.response.layer_id);
st.rect = Some(window.response.rect);
}
if !keep {
request(d, &mut st, After::Close, &mut actions, open);
@@ -446,8 +447,12 @@ fn main_page(
let filter = egui::ComboBox::from_id_salt("textstyle_list_filter")
.selected_text(if st.in_use_only { crate::tl!("Styles in use") } else { crate::tl!("All styles") })
.show_ui(ui, |ui| {
ui.selectable_value(&mut st.in_use_only, false, crate::tl!("All styles"));
ui.selectable_value(&mut st.in_use_only, true, crate::tl!("Styles in use"));
// A choice made with Enter or Space closes the list, as a click does.
for (value, label) in [(false, crate::tl!("All styles")), (true, crate::tl!("Styles in use"))] {
if ui.selectable_value(&mut st.in_use_only, value, label).clicked() {
ui.close();
}
}
});
// Named by the heading over it: its own text is the answer it shows.
let filter = filter.response.labelled_by(styles.id);
@@ -480,6 +485,11 @@ fn main_page(
if ui.add(egui::Button::new(crate::tl!("New...")).min_size(w)).clicked() {
pending = Some(After::New);
}
// The list's context menu has Rename for the pointer; this is the way by the keyboard.
if ui.add_enabled(!st.selected.eq_ignore_ascii_case("Standard"), egui::Button::new(crate::tl!("Rename")).min_size(w)).clicked() {
st.renaming = Some((st.selected.clone(), st.selected.clone()));
st.rename_focus = true;
}
if ui.add_enabled(deletable(&st.selected, current, used), egui::Button::new(crate::tl!("Delete")).min_size(w)).clicked() {
actions.push(("style.delete", json!({ "name": st.selected })));
deleted = true;
@@ -552,6 +562,7 @@ fn style_list(
}
if ui.add_enabled(!s.name.eq_ignore_ascii_case("Standard"), egui::Button::new(crate::tl!("Rename"))).clicked() {
st.renaming = Some((s.name.clone(), s.name.clone()));
st.rename_focus = true;
ui.close();
}
if ui.add_enabled(deletable(&s.name, current, used), egui::Button::new(crate::tl!("Delete"))).clicked() {
@@ -568,6 +579,10 @@ fn style_list(
let mut done = false;
ui.horizontal(|ui| {
let r = ui.add(egui::TextEdit::singleline(to).desired_width(120.0)).accessible_name(crate::tl!("Name"));
if st.rename_focus {
r.request_focus();
st.rename_focus = false;
}
let name = to.trim().to_string();
// Only a name the rename takes: a refused one would leave the selection on no style
// (and drop the edits waiting for Apply).
@@ -589,6 +604,7 @@ fn style_list(
});
if done {
st.renaming = None;
st.take_focus = true;
}
}
// Where the dialog puts the focus: the chosen style, or the first one shown when the filter
@@ -657,6 +673,7 @@ fn font_group(ui: &mut egui::Ui, st: &mut State, v: &mut TextStyle, fonts: &[Fon
for e in fonts.iter().filter(|e| e.family == f.family) {
if ui.selectable_label(e.style == f.style, &e.style).clicked() {
v.font = e.file.clone();
ui.close();
}
}
});
+121 -12
View File
@@ -63,6 +63,13 @@ fn click(h: &mut Harness<'static, CadApp>, label: &str) {
h.run_steps(3);
}
/// Clicks the entry `label` of the context menu that is open. The dialog has a button of the same
/// name (Rename), and the menu's entry comes after it.
fn click_menu_item(h: &mut Harness<'static, CadApp>, label: &str) {
h.get_all(by().label(label).role(Role::Button)).last().expect("the menu has the entry").click();
h.run_steps(3);
}
/// Ticks or clears the checkbox `label`.
fn check(h: &mut Harness<'static, CadApp>, label: &str) {
h.get(by().label(label).role(Role::CheckBox)).click();
@@ -236,7 +243,7 @@ fn a_rename_keeps_the_edits_waiting_for_apply() {
check(&mut h, "Backwards");
h.get(by().label("Spare").role(Role::Button)).click_secondary();
h.run_steps(3);
click(&mut h, "Rename");
click_menu_item(&mut h, "Rename");
type_into(&mut h, "Spare", "Extra");
// The edit stays with the style under its new name; undone, nothing waits for Apply.
assert!(enabled(&h, "Apply"));
@@ -269,13 +276,13 @@ fn set_current_rename_and_delete() {
click(&mut h, "Spare");
h.get(by().label("Spare").role(Role::Button)).click_secondary();
h.run_steps(3);
click(&mut h, "Rename");
click_menu_item(&mut h, "Rename");
type_into(&mut h, "Spare", "Extra");
assert!(h.state().session.doc().unwrap().text_style("Extra").is_some());
// A name in use isn't offered: Extra keeps its name and stays chosen.
h.get(by().label("Extra").role(Role::Button)).click_secondary();
h.run_steps(3);
click(&mut h, "Rename");
click_menu_item(&mut h, "Rename");
type_into(&mut h, "Extra", "Used");
assert!(!enabled(&h, "OK"));
assert!(h.state().session.doc().unwrap().text_style("Extra").is_some());
@@ -620,17 +627,17 @@ fn escape_cancels_the_innermost_thing_before_it_closes_the_dialog() {
press(&mut h, Key::Escape);
assert_eq!(h.state().ui.dialog.as_deref(), Some("textstyle"), "Escape leaves only the New page");
assert_eq!(focus_name(&h).as_deref(), Some("Standard"), "the styles page takes the focus again");
// A rename (started from the context menu): Escape drops it and keeps the name.
h.get(by().label("Spare").role(Role::Button)).click_secondary();
h.run_steps(3);
click(&mut h, "Rename");
h.get(by().label("Name").role(Role::TextInput)).focus();
h.run_steps(2);
// A rename, by the keyboard: Escape drops it and keeps the name.
tab_to(&mut h, "Spare");
press(&mut h, Key::Enter);
tab_to(&mut h, "Rename");
press(&mut h, Key::Enter);
assert_eq!(focus_name(&h).as_deref(), Some("Name"), "the rename field takes the focus");
press(&mut h, Key::Escape);
assert_eq!(h.state().ui.dialog.as_deref(), Some("textstyle"), "Escape leaves only the rename");
assert!(h.query_all(by().label("Name").role(Role::TextInput)).next().is_none(), "the rename field is gone");
assert!(h.state().session.doc().unwrap().text_style("Spare").is_some(), "the name is kept");
assert_eq!(focus_name(&h).as_deref(), Some("Standard"), "the focus returns to the styles");
assert_eq!(focus_name(&h).as_deref(), Some("Spare"), "the focus returns to the styles");
// With nothing left to cancel, Escape closes the dialog.
press(&mut h, Key::Escape);
assert_eq!(h.state().ui.dialog, None);
@@ -675,6 +682,108 @@ fn a_second_escape_after_a_closed_font_list_closes_the_dialog() {
assert_eq!(h.state().ui.dialog, None, "the second Escape closes the dialog");
}
#[test]
fn rename_is_reachable_by_the_keyboard_and_returns_the_focus() {
let mut h = dialog(|app| {
app.run("style", json!({ "name": "Spare", "current": false })).unwrap();
});
tab_to(&mut h, "Spare");
press(&mut h, Key::Enter);
tab_to(&mut h, "Rename");
press(&mut h, Key::Enter);
assert_eq!(focus_name(&h).as_deref(), Some("Name"), "the rename field takes the focus");
retype(&mut h, "Renamed");
assert!(h.state().session.doc().unwrap().text_style("Renamed").is_some(), "the style has its new name");
assert!(h.state().session.doc().unwrap().text_style("Spare").is_none(), "and not the old one");
assert_eq!(focus_name(&h).as_deref(), Some("Renamed"), "the focus is back on the styles, on the renamed one");
}
#[test]
fn a_choice_by_the_keyboard_closes_the_style_filter_list_and_keeps_the_focus() {
let mut h = dialog(|_| {});
tab_to(&mut h, "Styles");
press(&mut h, Key::Enter);
tab_to(&mut h, "Styles in use");
press(&mut h, Key::Enter);
assert!(h.query_all(by().label("Styles in use").role(Role::Button)).next().is_none(), "the list closed");
assert_eq!(focus_name(&h).as_deref(), Some("Styles"), "the focus is on the filter box");
assert_eq!(h.get(by().role(Role::ComboBox).label("Styles")).accesskit_node().value().as_deref(), Some("Styles in use"));
// And the dialog still has the keys: the next Escape closes it.
press(&mut h, Key::Escape);
assert_eq!(h.state().ui.dialog, None);
}
#[test]
fn escape_in_an_editor_over_the_drawing_is_not_the_dialogs() {
let mut app = CadApp::new(Session::new(), Services::default());
app.ui.dialog = Some("textstyle".into());
let mut h = Harness::builder().with_size(vec2(1000.0, 560.0)).build_ui_state(
|ui, app: &mut CadApp| {
app.logic(&ui.ctx().clone());
dialogs::show(app, ui.ctx());
// An editor drawn over the drawing, as the in-place text editor is.
egui::Area::new(egui::Id::unique("editor over the drawing")).order(egui::Order::Foreground).fixed_pos(egui::pos2(500.0, 450.0)).show(
ui.ctx(),
|ui| {
let caption = ui.label("Editor field");
let mut text = String::new();
ui.add(egui::TextEdit::singleline(&mut text)).labelled_by(caption.id);
},
);
},
app,
);
h.run_steps(4);
// The focus moves to the editor with no list or menu open (set up through the accessibility
// tree, as a screen reader would).
h.get(by().label("Editor field").role(Role::TextInput)).focus();
h.run_steps(3);
assert_eq!(focus_name(&h).as_deref(), Some("Editor field"));
press(&mut h, Key::Escape);
assert_eq!(h.state().ui.dialog.as_deref(), Some("textstyle"), "the Escape was the editor's, not the dialog's");
}
#[test]
fn a_click_on_a_window_over_the_dialog_takes_the_keys_from_it() {
let at = std::rc::Rc::new(std::cell::Cell::new(None::<egui::Pos2>));
let seen = at.clone();
let mut app = CadApp::new(Session::new(), Services::default());
app.ui.dialog = Some("textstyle".into());
let mut h = Harness::builder().with_size(vec2(1000.0, 560.0)).build_ui_state(
move |ui, app: &mut CadApp| {
app.logic(&ui.ctx().clone());
dialogs::show(app, ui.ctx());
if let Some(pos) = seen.get() {
egui::Window::new("Other window").fixed_pos(pos).show(ui.ctx(), |ui| {
ui.label("Nothing to focus here");
});
}
},
app,
);
h.run_steps(4);
// The other window opens over the middle of the dialog, so a click on it is inside the dialog's area.
let area = h.get(by().label("Text Style").role(Role::Window)).rect();
at.set(Some(area.center() - vec2(60.0, 40.0)));
h.run_steps(4);
let other = h.get(by().label("Other window").role(Role::Window)).rect();
let click = other.left_top() + vec2(40.0, 8.0);
assert!(area.contains(click), "the click lands inside the dialog's area: {click:?} in {area:?}");
h.input_mut().events.push(egui::Event::PointerMoved(click));
h.run_steps(2);
for pressed in [true, false] {
h.input_mut().events.push(egui::Event::PointerButton {
pos: click,
button: egui::PointerButton::Primary,
pressed,
modifiers: egui::Modifiers::NONE,
});
}
h.run_steps(3);
press(&mut h, Key::Escape);
assert_eq!(h.state().ui.dialog.as_deref(), Some("textstyle"), "the click was on the other window: its Escape is not the dialog's");
}
#[test]
fn escape_after_a_closed_context_menu_closes_the_dialog() {
let mut h = dialog(|app| {
@@ -683,9 +792,9 @@ fn escape_after_a_closed_context_menu_closes_the_dialog() {
// The context menu is the pointer's way to Set Current, Rename and Delete.
h.get(by().label("Spare").role(Role::Button)).click_secondary();
h.run_steps(3);
h.get(by().label("Rename").role(Role::Button));
assert_eq!(h.query_all(by().label("Rename").role(Role::Button)).count(), 2, "the menu's entry and the dialog's own button");
press(&mut h, Key::Escape);
assert!(h.query_all(by().label("Rename").role(Role::Button)).next().is_none(), "Escape closes the menu");
assert_eq!(h.query_all(by().label("Rename").role(Role::Button)).count(), 1, "Escape closes the menu: only the dialog's own button is left");
assert_eq!(h.state().ui.dialog.as_deref(), Some("textstyle"), "and leaves the dialog open");
// The focus is nowhere now, and the next Escape still reaches the dialog.
press(&mut h, Key::Escape);