Ribbon: bind command IDs for Table Styles, Borders, Delete, and AutoFit menus (#7324) (#7328)

* Ribbon: compact tile sizing for Home › Bullets library dropdown (#7323)

In the Home › Bullets dropdown popup (bullets_menu), the Bullet Library
grid tiles were rendered with .content_cells() without an explicit
thumbnail sizing constraint, causing each bullet tile to expand to default
card dimensions (~132×132 pt).

Replace .content_cells() with .thumb_size(ThumbSize::Small) so the
Bullet Library renders compact square bullet previews (~22×22 pt), matching
pre-refactor ribbon dropdown parity. Add a regression test verifying the
compact tile dimensions.

* Ribbon: bind command IDs for Table Styles, Borders, Delete, and AutoFit menus (#7324)

In crates/ui-egui/src/ribbon/table.rs, bind command IDs for four table
ribbon menu buttons (Styles -> table.style, Borders -> table.borders,
Delete -> table.deleteTable, AutoFit -> table.autofit) instead of None.

In crates/ui-egui/src/ribbon/common.rs, update menu_button to apply the
bound command's hint/tooltip when the command is disabled.

This ensures that menu button availability, disabled states, and disabled
tooltips accurately reflect engine command state and document protection rules.

---------

Co-authored-by: Martin Ball <martin.ball@ucscaregroup.co.uk>
This commit is contained in:
batistalucasv
2026-11-22 17:40:08 +00:00
committed by GitHub
co-authored by Martin Ball
parent 9cfe513477
commit d7085aabde
3 changed files with 110 additions and 24 deletions
+7 -8
View File
@@ -3,7 +3,7 @@ use std::ops::RangeInclusive;
use craft::keymap::CommandRef;
use craft::menu::ribbon::{RibbonGroup, RibbonRow};
use craft::ui::buttons::{Arrangement, Button};
use craft::ui::contract::{Bind, BodyCx, Gap, Props as _, Show as _, ShowContainer as _, Trigger};
use craft::ui::contract::{Bind, BodyCx, Gap, Props as _, Show as _, ShowContainer as _, TooltipSpec, Trigger};
use craft::ui::controls::disclosure::DisclosureGlyph;
use craft::ui::display::Divider;
use craft::ui::layout_components::{Row, Stack};
@@ -18,11 +18,6 @@ use super::{Ui, WordApp, json};
use crate::icons;
use crate::undo_marks::apply_edits;
/// Whether the command a menu is about can run now; a menu about no command is always open.
pub(super) fn about_enabled(ctx: &egui::Context, about: &str) -> bool {
about.is_empty() || craft::ui::runtime::commands::resolve(ctx, CommandRef::new(about)).enabled
}
/// The arrow after the label of a ribbon menu button: the small filled triangle of the old ribbon.
pub(crate) static MENU_ARROW: DisclosureGlyph = DisclosureGlyph::new(1.0);
@@ -30,12 +25,16 @@ pub(crate) static MENU_ARROW: DisclosureGlyph = DisclosureGlyph::new(1.0);
/// `face` is the caller's button; `about` is that command's id, `None` for a menu about no command.
pub(crate) fn menu_button(
ui: &mut Ui,
face: Button<'_>,
mut face: Button<'_>,
name: impl Into<Label>,
about: Option<&str>,
body: impl FnMut(&mut Ui, &mut BodyCx<'_>),
) -> MenuOutput {
let enabled = about_enabled(ui.ctx(), about.unwrap_or(""));
let resolved = about.filter(|id| !id.is_empty()).map(|id| craft::ui::runtime::commands::resolve(ui.ctx(), CommandRef::new(id)));
let enabled = resolved.as_ref().is_none_or(|cmd| cmd.enabled);
if !enabled && let Some(hint) = resolved.as_ref().and_then(|cmd| cmd.hint.clone()) {
face = face.tooltip(TooltipSpec::new(hint.clone()).disabled_text(hint));
}
let opener = face.trailing_icon(IconSpec::glyph(&MENU_ARROW)).enabled(enabled).show(ui);
Menu::new(name).opened_by(&opener.response, Trigger::Click).show(ui, body).output
}
+22 -16
View File
@@ -57,25 +57,31 @@ pub(super) fn table_design(row: &mut RibbonRow<'_>, app: &mut WordApp) {
row.group(RibbonGroup::new("tableStyles", tl!("Table Styles").to_string(), IconId::new("tableStyles").into()), |ui| {
super::galleries::table_styles(ui, app);
Stack::new().gap(Gap::Tight).show(ui, |ui, _| {
let _ = menu_button(ui, Button::new(tl!("Styles").to_string()).icon(IconId::new("styles")), tl!("Styles").to_string(), None, |ui, _| {
MenuItem::command(CommandRef::with_params("ui.dialog", json!({"name": "newTableStyle"})))
.label(tl!("New Table Style…").to_string())
.show(ui);
MenuItem::command(CommandRef::with_params("ui.dialog", json!({"name": "modifyTableStyle"})))
.label(tl!("Modify Table Style…").to_string())
.enabled(can_modify)
.show(ui);
MenuItem::command(CommandRef::with_params("table.deleteStyle", json!({})))
.label(tl!("Delete Table Style").to_string())
.enabled(can_delete)
.show(ui);
});
let _ = menu_button(
ui,
Button::new(tl!("Styles").to_string()).icon(IconId::new("styles")),
tl!("Styles").to_string(),
Some("table.style"),
|ui, _| {
MenuItem::command(CommandRef::with_params("ui.dialog", json!({"name": "newTableStyle"})))
.label(tl!("New Table Style…").to_string())
.show(ui);
MenuItem::command(CommandRef::with_params("ui.dialog", json!({"name": "modifyTableStyle"})))
.label(tl!("Modify Table Style…").to_string())
.enabled(can_modify)
.show(ui);
MenuItem::command(CommandRef::with_params("table.deleteStyle", json!({})))
.label(tl!("Delete Table Style").to_string())
.enabled(can_delete)
.show(ui);
},
);
let _ = crate::colors::shading_split(ui, app, tl!("Shading").to_string(), Target::cmd("table.shading").remember(Remember::Shading));
});
});
row.group(RibbonGroup::new("borders", tl!("Borders").to_string(), IconId::new("borders").into()), |ui| {
let face = Button::new(tl!("Borders").to_string()).icon(IconId::new("borders")).arrangement(Arrangement::Stacked);
let _ = menu_button(ui, face, tl!("Borders").to_string(), None, |ui, _| {
let _ = menu_button(ui, face, tl!("Borders").to_string(), Some("table.borders"), |ui, _| {
for (label, kind) in [
("All Borders", "all"),
("Outside Borders", "outside"),
@@ -181,7 +187,7 @@ pub(super) fn table_layout(row: &mut RibbonRow<'_>, app: &mut WordApp) {
});
row.group(RibbonGroup::new("rowsColumns", tl!("Rows & Columns").to_string(), IconId::new("insertAbove").into()), |ui| {
let face = Button::new(tl!("Delete").to_string()).icon(IconId::new("deleteTable")).arrangement(Arrangement::Stacked);
let _ = menu_button(ui, face, tl!("Delete").to_string(), None, |ui, _| {
let _ = menu_button(ui, face, tl!("Delete").to_string(), Some("table.deleteTable"), |ui, _| {
for (label, id) in [
("Delete Cells", "table.deleteCells"),
("Delete Columns", "table.deleteColumn"),
@@ -214,7 +220,7 @@ pub(super) fn table_layout(row: &mut RibbonRow<'_>, app: &mut WordApp) {
});
row.group(RibbonGroup::new("cellSize", tl!("Cell Size").to_string(), IconId::new("autofit").into()), |ui| {
let face = Button::new(tl!("AutoFit").to_string()).icon(IconId::new("autofit")).arrangement(Arrangement::Stacked);
let _ = menu_button(ui, face, tl!("AutoFit").to_string(), None, |ui, _| {
let _ = menu_button(ui, face, tl!("AutoFit").to_string(), Some("table.autofit"), |ui, _| {
for (label, mode) in [("AutoFit Contents", "contents"), ("AutoFit Window", "window"), ("Fixed Column Width", "fixed")] {
MenuItem::command(CommandRef::with_params("table.autofit", json!({"mode": mode}))).label(tl!(label).to_string()).show(ui);
}
+81
View File
@@ -829,3 +829,84 @@ fn unimplemented_stub_ribbon_buttons_are_disabled() {
assert!(button.accesskit_node().is_disabled(), "{tab}: stub button {:?} ({id}) must be disabled", button.accesskit_node().label());
}
}
/// #7324: Table ribbon menu buttons (Styles, Borders, Delete, AutoFit) bind their command IDs
/// so they disable when editing is protected or when no table is active.
#[test]
fn table_ribbon_menu_buttons_disable_when_unselected_or_protected() {
use egui::accesskit::Role;
let is_disabled = |h: &Harness, name: &str| {
h.query_all(By::new().role(Role::Button).label(name))
.next()
.unwrap_or_else(|| panic!("button {name} not found"))
.accesskit_node()
.is_disabled()
};
let description = |h: &Harness, name: &str| {
h.query_all(By::new().role(Role::Button).label(name))
.next()
.unwrap_or_else(|| panic!("button {name} not found"))
.accesskit_node()
.description()
.map(|s| s.to_string())
};
// Configure Table Design & Table Layout as explicit ribbon tabs so they can be
// displayed even when the cursor is outside a table.
let make_table_tabs_app = |mut app: WordApp| {
app.session.ribbon.tabs = vec![
wordcraft_engine::ribbon::TabEntry { name: "Table Design".to_string(), custom: false, hidden: false, groups: Vec::new() },
wordcraft_engine::ribbon::TabEntry { name: "Table Layout".to_string(), custom: false, hidden: false, groups: Vec::new() },
];
app
};
// 1. Without a table active:
let app_no_table = make_table_tabs_app(WordApp::new(Session::new(wordcraft_doc::Document::from_text("Hello")), crate::Services::default()));
let h_design_empty = tab_harness(app_no_table, "Table Design");
assert!(is_disabled(&h_design_empty, "Styles"), "Styles button should be disabled when not in table");
assert!(is_disabled(&h_design_empty, "Borders"), "Borders button should be disabled when not in table");
let app_no_table2 = make_table_tabs_app(WordApp::new(Session::new(wordcraft_doc::Document::from_text("Hello")), crate::Services::default()));
let h_layout_empty = tab_harness(app_no_table2, "Table Layout");
assert!(is_disabled(&h_layout_empty, "Delete"), "Delete button should be disabled when not in table");
assert!(is_disabled(&h_layout_empty, "AutoFit"), "AutoFit button should be disabled when not in table");
// 2. With an active table and no protection: buttons are enabled:
let mut app_table = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
app_table.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
let h_design = tab_harness(app_table, "Table Design");
assert!(!is_disabled(&h_design, "Styles"), "Styles button should be enabled in table");
assert!(!is_disabled(&h_design, "Borders"), "Borders button should be enabled in table");
let mut app_table2 = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
app_table2.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
let h_layout = tab_harness(app_table2, "Table Layout");
assert!(!is_disabled(&h_layout, "Delete"), "Delete button should be enabled in table");
assert!(!is_disabled(&h_layout, "AutoFit"), "AutoFit button should be enabled in table");
// 3. With an active table under document protection (read-only): buttons are disabled:
let mut app_protected_design = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
app_protected_design.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
app_protected_design.session.doc.settings.protection = Some("readOnly".into());
let h_prot_design = tab_harness(app_protected_design, "Table Design");
assert!(is_disabled(&h_prot_design, "Styles"), "Styles button should be disabled under protection");
assert!(is_disabled(&h_prot_design, "Borders"), "Borders button should be disabled under protection");
assert!(
description(&h_prot_design, "Styles").as_deref().unwrap_or("").contains("protected"),
"Styles should indicate protection in disabled hint"
);
let mut app_protected_layout = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default());
app_protected_layout.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
app_protected_layout.session.doc.settings.protection = Some("readOnly".into());
let h_prot_layout = tab_harness(app_protected_layout, "Table Layout");
assert!(is_disabled(&h_prot_layout, "Delete"), "Delete button should be disabled under protection");
assert!(is_disabled(&h_prot_layout, "AutoFit"), "AutoFit button should be disabled under protection");
assert!(
description(&h_prot_layout, "Delete").as_deref().unwrap_or("").contains("protected"),
"Delete should indicate protection in disabled hint"
);
}