6ef13b2cc8a6
context_menu.rs
use super::{Ui, WordApp, json};
/// The spelling suggestions and synonyms the context menu offers for the selection or the word
/// at the caret.
pub(super) fn context_suggestions(app: &mut WordApp) {
app.canvas.context_issue = app.session.run("review.suggestions", &json!({})).ok().filter(|v| !v.is_null());
app.canvas.context_synonyms = app.session.run("review.thesaurus", &json!({})).ok().and_then(|v| v.get("synonyms").cloned());
}
/// Right-click inside an equation: its structures' actions, display and conversion.
fn equation_menu(app: &mut WordApp, ui: &mut Ui) {
ui.set_min_width(260.0);
let acts = app.session.run("equation.structureActions", &json!({})).ok().and_then(|v| v.get("actions").cloned()).unwrap_or_default();
let mut last_level = None;
for a in acts.as_array().into_iter().flatten() {
let (Some(action), Some(label), Some(level)) =
(a.get("action").and_then(|v| v.as_str()), a.get("label").and_then(|v| v.as_str()), a.get("level").and_then(|v| v.as_u64()))
else {
continue;
};
if last_level.is_some_and(|l| l != level) {
ui.separator();
}
last_level = Some(level);
if ui.button(tl!(label)).clicked() {
let _ = app.run("equation.structure", json!({"action": action, "level": level}));
app.canvas.want_focus = true;
ui.close();
}
}
if last_level.is_some() {
ui.separator();
}
let display = app.session.run("equation.get", &json!({})).ok().and_then(|v| v.get("display").and_then(|d| d.as_bool())).unwrap_or(false);
let items: [(&str, &str, serde_json::Value); 5] = [
(if display { "Change to Inline" } else { "Change to Display" }, "equation.display", json!({"value": !display})),
("Professional", "equation.convert", json!({"to": "professional"})),
("Linear", "equation.convert", json!({"to": "linear"})),
("Equation Number", "equation.number", json!({})),
("Close Equation", "equation.exit", json!({})),
];
for (label, id, params) in items {
if ui.button(tl!(label)).clicked() {
let _ = app.run(id, params);
app.canvas.want_focus = true;
ui.close();
}
}
}
/// 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.
/// English grammar rules' messages are catalog keys; other languages' rules explain in their own.
pub(crate) fn issue_heading(issue: &serde_json::Value) -> String {
if issue.get("kind").and_then(|k| k.as_str()) == Some("spelling") {
return tl!("Possible spelling mistake").to_string();
}
tl!(issue.get("message").and_then(|m| m.as_str()).unwrap_or("")).to_string()
}
pub(super) fn context_menu(app: &mut WordApp, ui: &mut Ui) {
if app.session.math.is_some() {
return equation_menu(app, ui);
}
ui.set_min_width(220.0);
let item = |ui: &mut Ui, app: &mut WordApp, label: &str, id: &str, params: serde_json::Value| {
let sc = crate::widgets::shortcut_text(app, id);
let on = crate::widgets::enabled(app, id);
if ui.add_enabled(on, crate::rtl::menu_item(ui, tl!(label), &sc, 0.0)).clicked() {
let _ = app.run(id, params);
ui.close();
}
};
if let Some(issue) = app.canvas.context_issue.clone() {
let sugg: Vec<String> = issue
.get("suggestions")
.and_then(|v| v.as_array())
.map(|a| a.iter().filter_map(|x| x.as_str().map(str::to_string)).collect())
.unwrap_or_default();
ui.label(egui::RichText::new(issue_heading(&issue)).small().weak());
if sugg.is_empty() {
ui.label(egui::RichText::new(tl!("(no suggestions)")).italics());
}
for sgt in sugg {
if ui.button(egui::RichText::new(&sgt).strong()).clicked() {
let _ = app.run("review.applySuggestion", json!({"text": sgt}));
ui.close();
}
}
if issue.get("kind").and_then(|k| k.as_str()) == Some("spelling") {
item(ui, app, "Ignore All", "review.ignoreAll", json!({}));
item(ui, app, "Add to Dictionary", "review.addToDictionary", json!({}));
}
ui.separator();
}
item(ui, app, "Cut", "edit.cut", json!({}));
item(ui, app, "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, app, "Paste", "edit.paste", json!({}));
item(ui, app, "Keep Text Only", "edit.pasteText", json!({}));
item(ui, app, "Merge Formatting", "edit.pasteMerge", json!({}));
item(ui, app, "Paste Special…", "edit.pasteSpecial", json!({}));
ui.separator();
if let Some(syn) = app.canvas.context_synonyms.clone().and_then(|v| v.as_array().cloned()).filter(|a| !a.is_empty()) {
ui.menu_button(tl!("Synonyms"), |ui| {
crate::rtl::root(ui, |ui| {
for w in syn.iter().filter_map(|x| x.as_str()) {
if ui.button(w).clicked() {
let _ = app.run("select.word", json!({}));
let (a, b) = app.session.sel.ordered();
let trimmed = app.session.doc.para_at(&a).and_then(|p| p.text.get(a.off..b.off)).map(|t| t.trim_end().len()).unwrap_or(0);
let end = wordcraft_doc::Pos { off: a.off + trimmed, ..b };
app.session.sel = wordcraft_engine::Selection { anchor: a, focus: end };
let _ = app.run("text.insert", json!({"text": w, "raw": true}));
ui.close();
}
}
})
});
}
item(ui, app, "Font…", "ui.dialog", json!({"name": "font"}));
item(ui, app, "Paragraph…", "ui.dialog", json!({"name": "paragraph"}));
item(ui, app, "Link…", "ui.dialog", json!({"name": "link"}));
item(ui, app, "New Comment", "review.newComment", json!({}));
if !wordcraft_engine::cmd::fields::selected(&app.session).is_empty() {
ui.separator();
item(ui, app, tl!("Update Field"), "references.updateFields", json!({}));
item(ui, app, tl!("Edit Field…"), "fields.setCode", json!({}));
item(ui, app, tl!("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.
ui.separator();
item(ui, app, tl!("Update Field"), "references.updateToc", json!({}));
item(ui, app, tl!("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, app, "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);
let object = wordcraft_engine::cmd::objects::object_selection(&app.session).is_some();
if picture || object {
ui.separator();
}
if picture {
item(ui, app, "Change Picture…", "ui.changePicture", json!({}));
item(ui, app, "Reset Picture", "picture.reset", json!({}));
item(ui, app, "Size and Crop…", "ui.tab", json!({"tab": "Picture Format"}));
item(ui, app, "Crop", "ui.crop", json!({}));
}
if picture || object {
// A numbered caption under (or over) the picture, shape, text box or chart (#1230).
item(ui, app, "Insert Caption…", "references.caption", json!({}));
}
if object {
ui.menu_button(tl!("Wrap Text"), |ui| crate::ribbon::wrap_text_items(ui, app));
}
if crate::widgets::enabled(app, "shape.fill") {
item(ui, app, "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);
ui.separator();
ui.menu_button(tl!("Insert"), |ui| {
crate::rtl::root(ui, |ui| {
item(ui, app, "Insert Columns to the Left", "table.insertColumnLeft", json!({}));
item(ui, app, "Insert Columns to the Right", "table.insertColumnRight", json!({}));
item(ui, app, "Insert Rows Above", "table.insertRowAbove", json!({}));
item(ui, app, "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).
ui.menu_button(tl!("Delete"), |ui| {
item(ui, app, "Delete Cells", "table.deleteCells", json!({}));
item(ui, app, "Delete Columns", "table.deleteColumn", json!({}));
item(ui, app, "Delete Rows", "table.deleteRow", json!({}));
item(ui, app, "Delete Table", "table.deleteTable", json!({}));
});
if pick == TablePick::Cell {
item(ui, app, "Split Cells", "table.split", json!({}));
}
if matches!(pick, TablePick::Cells | TablePick::Rows | TablePick::Columns) {
item(ui, app, "Merge Cells", "table.merge", json!({}));
}
if matches!(pick, TablePick::Rows | TablePick::Columns | TablePick::Table) {
item(ui, app, "Distribute Rows", "table.distributeRows", json!({}));
item(ui, app, "Distribute Columns", "table.distributeColumns", json!({}));
}
if pick == TablePick::Table {
ui.menu_button(tl!("AutoFit"), |ui| {
crate::rtl::root(ui, |ui| {
item(ui, app, "AutoFit Contents", "table.autofit", json!({"mode": "contents"}));
item(ui, app, "AutoFit Window", "table.autofit", json!({"mode": "window"}));
item(ui, app, "Fixed Column Width", "table.autofit", json!({"mode": "fixed"}));
})
});
}
item(ui, app, "Text Direction", "table.textDirection", json!({}));
if pick == TablePick::Table {
item(ui, app, "Insert Caption…", "references.caption", json!({}));
}
item(ui, app, "Table Properties…", "table.properties", json!({}));
}
}
/// What of a table the selection is: Word's context menu offers different table items for each.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum TablePick {
/// The caret or text in one cell, or that one cell.
Cell,
/// Cells of more than one row or column, but not whole rows or columns.
Cells,
Rows,
Columns,
Table,
}
fn table_pick(s: &wordcraft_engine::Session) -> TablePick {
if let Some(r) = s.cell_range()
&& let Some(t) = s.doc.table(r.story, &r.table)
{
let all_rows = r.rows == (0, t.rows.len().saturating_sub(1));
let all_cols = r.cols == (0, t.cols().saturating_sub(1));
return match (all_rows, all_cols) {
(true, true) => TablePick::Table,
(true, false) => TablePick::Columns,
(false, true) => TablePick::Rows,
_ if r.cells(t).len() > 1 => TablePick::Cells,
_ => TablePick::Cell,
};
}
// Text running from one cell into another of the same table.
match (s.sel.anchor.path.cell(), s.sel.focus.path.cell()) {
(Some((ta, ra, ca)), Some((tb, rb, cb))) if ta == tb && (ra, ca) != (rb, cb) => TablePick::Cells,
_ => TablePick::Cell,
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.