Compare commits

...
Author SHA1 Message Date
autocraft-bot[bot] 89ba3016a7 Merge main (1580f46c) into the pull request
I resolved the conflicts in context_menu.rs and tests.rs against the new

I resolved the conflicts in context_menu.rs and tests.rs against the new main (1580f46c), and no conflict markers remain. In context_menu.rs, `item` keeps the pull request's `app` parameter and canvas refocus, and builds its row with main's `MenuItem::command(..).label(..)`. The synonym pick uses main's `thesaurus_word` lookup, followed by the pull request's `refocus_canvas` call. In tests.rs I kept main's tests and put the pull request's two context-menu focus tests after them, unchanged. `cargo fmt --check` and `cargo clippy --offline -p wordcraft-ui-egui --tests` pass. I did not run the tests, because the test build is too slow to finish within the shell time limit.
2026-12-03 16:43:41 +00:00
jayzed80 eff8661a58 Canvas: port context menu focus restoration onto craft-libs Menu (#6715) 2026-11-26 21:28:39 +00:00
jayzed80 acf8e9950d Merge remote-tracking branch 'upstream/main' into HEAD
# Conflicts:
#	crates/ui-egui/src/canvas/context_menu.rs
2026-11-26 18:46:48 +00:00
jayzed80 0a29a6d73a Canvas: keep the keyboard in the frame right after a context menu action (#6715)
A menu item's click takes focus from the page, and the page is drawn before the
menu, so `canvas.focused` (which gates the keys) was only recomputed on the next
pass: a character typed in the frame right after the click reached no widget and
was lost. The spelling, synonym and generic item paths now claim the keyboard in
the same pass, as a click on the chrome already does, and keep `want_focus` for
the later frames.

The test types into the very next pass and checks the character lands.
2026-11-19 07:29:55 +00:00
jayzed80 a29663bd95 Canvas: request a repaint when a context menu action hands focus back (#6715) 2026-11-19 03:59:15 +00:00
jayzed80 a7e02482fe Merge remote-tracking branch 'upstream/main' into fix/context-menu-focus-6715 2026-11-19 03:45:54 +00:00
Martin Ball 6f6bf92576 Merge branch 'main' into fix/context-menu-focus-6715 2026-11-17 13:42:57 +01:00
Martin Ball b66f579e7b Merge branch 'main' into fix/context-menu-focus-6715 2026-11-17 08:26:46 +01:00
jayzed80 7df90b72cf Return focus to canvas after context menu actions (#6715) 2026-11-15 02:12:03 +00:00
2 changed files with 162 additions and 45 deletions
+56 -45
View File
@@ -27,6 +27,13 @@ pub(super) fn context_suggestions(app: &mut WordApp) {
app.canvas.context_synonyms = app.session.run("review.thesaurus", &json!({})).ok().and_then(|v| v.get("synonyms").cloned());
}
/// Hands the keyboard back to the page from a menu action, in this same pass, and asks for a
/// repaint so the page redraws with the focused caret (#6715).
fn refocus_canvas(app: &mut WordApp, ui: &Ui) {
app.canvas.focus_page(ui.ctx());
ui.ctx().request_repaint();
}
/// The heading of a proofing issue's context menu, in the interface language like the rest of
/// the menu: a spelling issue's own message is in the text's language (craft-proof's, "Faute
/// d'orthographe possible" for French text), so it says Possible spelling mistake instead.
@@ -80,7 +87,7 @@ pub(crate) fn show(app: &mut WordApp, ui: &mut Ui, opener: &Response, ask: Optio
let pick = menu.pick(ui, &rows);
if let Some(act) = pick.picked {
super::equation_frame::run_equation_act(app, &act);
app.canvas.focus_page(ui.ctx());
refocus_canvas(app, ui);
}
} else {
// The companion bar and the rows both need the app; they run one after the other, never nested.
@@ -106,9 +113,11 @@ pub(crate) fn show(app: &mut WordApp, ui: &mut Ui, opener: &Response, ask: Optio
/// A row labelled `label` that runs command `id` with `params`; its chord, enabled state and
/// action come from the command, its wording from the menu (one command id serves several rows).
fn item(ui: &mut Ui, label: &str, id: &str, params: Value) {
fn item(app: &mut WordApp, ui: &mut Ui, label: &str, id: &str, params: Value) {
// `MenuItem::command`, not `new(..).command(..)`: only the row's own binding shows the command's chord.
let _ = MenuItem::command(CommandRef::with_params(id, params)).label(tl!(label).to_string()).show(ui);
if MenuItem::command(CommandRef::with_params(id, params)).label(tl!(label).to_string()).show(ui).ran && !id.starts_with("ui.") {
refocus_canvas(app, ui);
}
}
fn rule(ui: &mut Ui) {
@@ -130,22 +139,23 @@ fn rows(app: &mut WordApp, ui: &mut Ui) {
for sgt in sugg {
if MenuItem::new(sgt.clone()).show(ui).ran {
let _ = app.run("review.applySuggestion", json!({"text": sgt}));
refocus_canvas(app, ui);
}
}
if issue.get("kind").and_then(|k| k.as_str()) == Some("spelling") {
item(ui, "Ignore All", "review.ignoreAll", json!({}));
item(ui, "Add to Dictionary", "review.addToDictionary", json!({}));
item(app, ui, "Ignore All", "review.ignoreAll", json!({}));
item(app, ui, "Add to Dictionary", "review.addToDictionary", json!({}));
}
rule(ui);
}
item(ui, "Cut", "edit.cut", json!({}));
item(ui, "Copy", "edit.copy", json!({}));
item(app, ui, "Cut", "edit.cut", json!({}));
item(app, ui, "Copy", "edit.copy", json!({}));
// How to paste, chosen before pasting (#2583): the ribbon's Paste menu, item for item. Paste
// keeps the clipboard's formatting (Word's Keep Source Formatting).
item(ui, "Paste", "edit.paste", json!({}));
item(ui, "Keep Text Only", "edit.pasteText", json!({}));
item(ui, "Merge Formatting", "edit.pasteMerge", json!({}));
item(ui, "Paste Special…", "edit.pasteSpecial", json!({}));
item(app, ui, "Paste", "edit.paste", json!({}));
item(app, ui, "Keep Text Only", "edit.pasteText", json!({}));
item(app, ui, "Merge Formatting", "edit.pasteMerge", json!({}));
item(app, ui, "Paste Special…", "edit.pasteSpecial", json!({}));
rule(ui);
if let Some(syn) = app.canvas.context_synonyms.clone().and_then(|v| v.as_array().cloned()).filter(|a| !a.is_empty()) {
let _ = Menu::submenu_row(ui, tl!("Synonyms").to_string(), |ui, _cx| {
@@ -156,28 +166,29 @@ fn rows(app: &mut WordApp, ui: &mut Ui) {
app.session.set_selection(wordcraft_engine::Selection { anchor: a, focus: b });
let _ = app.run("text.insert", json!({"text": w, "raw": true}));
}
refocus_canvas(app, ui);
}
}
});
}
item(ui, "Font…", "ui.dialog", json!({"name": "font"}));
item(ui, "Paragraph…", "ui.dialog", json!({"name": "paragraph"}));
item(ui, "Link…", "ui.dialog", json!({"name": "link"}));
item(ui, "New Comment", "review.newComment", json!({}));
item(app, ui, "Font…", "ui.dialog", json!({"name": "font"}));
item(app, ui, "Paragraph…", "ui.dialog", json!({"name": "paragraph"}));
item(app, ui, "Link…", "ui.dialog", json!({"name": "link"}));
item(app, ui, "New Comment", "review.newComment", json!({}));
if !wordcraft_engine::cmd::fields::selected(&app.session).is_empty() {
rule(ui);
item(ui, "Update Field", "references.updateFields", json!({}));
item(ui, "Edit Field…", "fields.setCode", json!({}));
item(ui, "Toggle Field Codes", "fields.toggleCode", json!({}));
item(app, ui, "Update Field", "references.updateFields", json!({}));
item(app, ui, "Edit Field…", "fields.setCode", json!({}));
item(app, ui, "Toggle Field Codes", "fields.toggleCode", json!({}));
} else if wordcraft_engine::cmd::references::caret_in_toc(&app.session) {
// The TOC field sits in the table's heading; its entries are plain paragraphs.
rule(ui);
item(ui, "Update Field", "references.updateToc", json!({}));
item(ui, "Remove Table of Contents", "references.removeToc", json!({}));
item(app, ui, "Update Field", "references.updateToc", json!({}));
item(app, ui, "Remove Table of Contents", "references.removeToc", json!({}));
}
let selected_object = wordcraft_engine::cmd::objects::selected(&app.session).is_some();
if selected_object || app.session.sel.focus.path.cell().is_some() {
item(ui, "View Alt Text…", "view.altTextPane", json!({"value": true}));
item(app, ui, "View Alt Text…", "view.altTextPane", json!({"value": true}));
}
// A selected picture, shape, text box or chart: its own items, then Wrap Text (#513).
let picture = crate::ribbon::has_picture_selected(&app.session);
@@ -186,61 +197,61 @@ fn rows(app: &mut WordApp, ui: &mut Ui) {
rule(ui);
}
if picture {
item(ui, "Change Picture…", "ui.changePicture", json!({}));
item(ui, "Reset Picture", "picture.reset", json!({}));
item(ui, "Size and Crop…", "ui.tab", json!({"tab": "Picture Format"}));
item(ui, "Crop", "ui.crop", json!({}));
item(app, ui, "Change Picture…", "ui.changePicture", json!({}));
item(app, ui, "Reset Picture", "picture.reset", json!({}));
item(app, ui, "Size and Crop…", "ui.tab", json!({"tab": "Picture Format"}));
item(app, ui, "Crop", "ui.crop", json!({}));
}
if picture || object {
// A numbered caption under (or over) the picture, shape, text box or chart (#1230).
item(ui, "Insert Caption…", "references.caption", json!({}));
item(app, ui, "Insert Caption…", "references.caption", json!({}));
}
if object {
let _ = Menu::submenu_row(ui, tl!("Wrap Text").to_string(), |ui, _cx| crate::ribbon::wrap_text_items(ui, app));
}
if craft::ui::runtime::commands::resolve(ui.ctx(), CommandRef::new("shape.fill")).enabled {
item(ui, "Format Shape…", "view.formatShapePane", json!({"value": true}));
item(app, ui, "Format Shape…", "view.formatShapePane", json!({"value": true}));
}
if app.session.sel.focus.path.cell().is_some() {
// The table's items follow what is selected, as in Word (2019, Windows).
let pick = table_pick(&app.session);
rule(ui);
let _ = Menu::submenu_row(ui, tl!("Insert").to_string(), |ui, _cx| {
item(ui, "Insert Columns to the Left", "table.insertColumnLeft", json!({}));
item(ui, "Insert Columns to the Right", "table.insertColumnRight", json!({}));
item(ui, "Insert Rows Above", "table.insertRowAbove", json!({}));
item(ui, "Insert Rows Below", "table.insertRowBelow", json!({}));
item(app, ui, "Insert Columns to the Left", "table.insertColumnLeft", json!({}));
item(app, ui, "Insert Columns to the Right", "table.insertColumnRight", json!({}));
item(app, ui, "Insert Rows Above", "table.insertRowAbove", json!({}));
item(app, ui, "Insert Rows Below", "table.insertRowBelow", json!({}));
});
// Every Delete is there whatever is selected, as on Word's Delete button: with the caret
// in a cell, Delete Table is how the table goes (#1198).
let _ = Menu::submenu_row(ui, tl!("Delete").to_string(), |ui, _cx| {
item(ui, "Delete Cells", "table.deleteCells", json!({}));
item(ui, "Delete Columns", "table.deleteColumn", json!({}));
item(ui, "Delete Rows", "table.deleteRow", json!({}));
item(ui, "Delete Table", "table.deleteTable", json!({}));
item(app, ui, "Delete Cells", "table.deleteCells", json!({}));
item(app, ui, "Delete Columns", "table.deleteColumn", json!({}));
item(app, ui, "Delete Rows", "table.deleteRow", json!({}));
item(app, ui, "Delete Table", "table.deleteTable", json!({}));
});
if pick == TablePick::Cell {
item(ui, "Split Cells", "table.split", json!({}));
item(app, ui, "Split Cells", "table.split", json!({}));
}
if matches!(pick, TablePick::Cells | TablePick::Rows | TablePick::Columns) {
item(ui, "Merge Cells", "table.merge", json!({}));
item(app, ui, "Merge Cells", "table.merge", json!({}));
}
if matches!(pick, TablePick::Rows | TablePick::Columns | TablePick::Table) {
item(ui, "Distribute Rows", "table.distributeRows", json!({}));
item(ui, "Distribute Columns", "table.distributeColumns", json!({}));
item(app, ui, "Distribute Rows", "table.distributeRows", json!({}));
item(app, ui, "Distribute Columns", "table.distributeColumns", json!({}));
}
if pick == TablePick::Table {
let _ = Menu::submenu_row(ui, tl!("AutoFit").to_string(), |ui, _cx| {
item(ui, "AutoFit Contents", "table.autofit", json!({"mode": "contents"}));
item(ui, "AutoFit Window", "table.autofit", json!({"mode": "window"}));
item(ui, "Fixed Column Width", "table.autofit", json!({"mode": "fixed"}));
item(app, ui, "AutoFit Contents", "table.autofit", json!({"mode": "contents"}));
item(app, ui, "AutoFit Window", "table.autofit", json!({"mode": "window"}));
item(app, ui, "Fixed Column Width", "table.autofit", json!({"mode": "fixed"}));
});
}
item(ui, "Text Direction", "table.textDirection", json!({}));
item(app, ui, "Text Direction", "table.textDirection", json!({}));
if pick == TablePick::Table {
item(ui, "Insert Caption…", "references.caption", json!({}));
item(app, ui, "Insert Caption…", "references.caption", json!({}));
}
item(ui, "Table Properties…", "table.properties", json!({}));
item(app, ui, "Table Properties…", "table.properties", json!({}));
}
}
+106
View File
@@ -2797,3 +2797,109 @@ fn shift_clicking_a_picture_does_not_scroll_the_view() {
assert!(picked.contains(&first) && picked.contains(&second), "both pictures are selected: {picked:?} {:?} at {at:?}", h.state().session.sel);
assert_eq!(h.state().canvas.view.offset, before, "the view stayed where it was");
}
#[test]
fn context_menu_spelling_suggestion_opens_with_shift_f10_and_keeps_focus() {
use egui_kittest::kittest::Queryable;
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("teh")), Default::default());
a.session.sel = wordcraft_engine::Selection::caret(Pos::body(0, 1));
let mut h = crate::test_harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
|ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
a,
);
steps(&mut h, 6);
assert!(h.state().canvas.focused(&h.ctx), "canvas starts focused");
// Shift+F10 opens the context menu with spelling suggestions.
h.event(egui::Event::Key { key: egui::Key::F10, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::SHIFT });
steps(&mut h, 3);
assert!(h.state().canvas.context_menu_open, "Shift+F10 opens the context menu");
let suggestion = h
.state()
.canvas
.context_issue
.as_ref()
.and_then(|v| v.get("suggestions"))
.and_then(|v| v.as_array())
.and_then(|a| a.first())
.and_then(|v| v.as_str())
.expect("spelling suggestion for 'teh'")
.to_string();
let btn = h.get_by_label(&suggestion).rect().center();
h.event(egui::Event::PointerMoved(btn));
steps(&mut h, 2);
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: btn, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
h.app_frame();
}
// The very next pass is the one that has to keep the keyboard: the page was drawn before the
// menu, so it only takes focus back then, and a character typed before that used to be lost.
h.event(egui::Event::Text("x".into()));
h.app_frame();
assert!(!h.state().canvas.context_menu_open, "applying a suggestion closes the context menu");
assert_eq!(
h.state().session.doc.plain_text(wordcraft_doc::StoryRef::Body),
format!("{suggestion}x"),
"a character typed in the frame after the click lands in the document"
);
assert!(h.state().canvas.focused(&h.ctx), "canvas keeps focus after applying a spelling suggestion so the caret remains visible");
}
#[test]
fn context_menu_spelling_suggestion_returns_focus_when_opened_unfocused() {
use egui::accesskit::Role;
use egui_kittest::kittest::Queryable;
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("teh")), Default::default());
a.session.sel = wordcraft_engine::Selection::caret(Pos::body(0, 1));
let mut h = crate::test_harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(
|ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
a,
);
steps(&mut h, 6);
let size_box =
h.get_all_by_role(Role::TextInput).find(|n| n.value().is_some_and(|v| v.trim().parse::<f64>().is_ok())).expect("the font size box").rect();
h.event(egui::Event::PointerMoved(size_box.center()));
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: size_box.center(), button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
h.app_frame();
}
h.app_frame();
assert!(!h.state().canvas.focused(&h.ctx), "canvas starts unfocused before opening the menu");
h.state_mut().canvas.context_request = Some(super::context_menu::ContextAsk::AtCaret);
steps(&mut h, 3);
assert!(h.state().canvas.context_menu_open, "context menu opens");
let suggestion = h
.state()
.canvas
.context_issue
.as_ref()
.and_then(|v| v.get("suggestions"))
.and_then(|v| v.as_array())
.and_then(|a| a.first())
.and_then(|v| v.as_str())
.expect("spelling suggestion for 'teh'")
.to_string();
let btn = h.get_by_label(&suggestion).rect().center();
h.event(egui::Event::PointerMoved(btn));
steps(&mut h, 2);
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: btn, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
h.app_frame();
}
h.event(egui::Event::Text("x".into()));
h.app_frame();
assert!(!h.state().canvas.context_menu_open, "applying a suggestion closes the context menu");
assert_eq!(
h.state().session.doc.plain_text(wordcraft_doc::StoryRef::Body),
format!("{suggestion}x"),
"a character typed in the frame after the click lands in the document"
);
assert!(h.state().canvas.focused(&h.ctx), "canvas takes focus after applying a spelling suggestion even when opened unfocused");
}