perf(ui-egui): wire AppShell slot versions for MemoView chrome replay (#8115) (#8119)

* perf(ui-egui): wire AppShell slot versions for MemoView chrome replay (#8115)

* test(chrome): fix E0502 borrow order in ribbon_slot_version invalidation check

* fix(ui-egui): hash missing non-rev state and keep ribbon context menu live in slot versions

* fix(ui-egui): version the status bar on layout page count and expired status messages (#8115)

* fix(ui-egui): drop needless mut on status bar test closure (#8115)

* perf(chrome): use cached_state, hash remembered split-button colours, and trim slot tests (#8115)
This commit is contained in:
Lucas Batista Vargas
2026-12-06 08:08:56 +00:00
committed by GitHub
parent 56f29ad63b
commit c28b01dc27
5 changed files with 748 additions and 97 deletions
+58 -22
View File
@@ -76,6 +76,19 @@ impl WordApp {
/// shell's regions; a window another command swapped in mid-draw returns none.
pub(super) fn paint_window(&mut self, ui: &mut egui::Ui) -> ShellRegions {
crate::announce::expose(self, ui.ctx());
if self.chrome.ribbon_context_menu {
self.chrome.ribbon_context_menu_tick = self.chrome.ribbon_context_menu_tick.wrapping_add(1);
}
// Keep chrome slots mounted in MemoView (preserving egui::Id) while forcing live runs
// when focus sits outside the canvas or an AccessKit action arrives so Tab traversal,
// programmatic focus requests, and keyboard-focus tooltips work without stale replay.
let chrome_focus_live = (!self.canvas.focused(ui.ctx()) && ui.ctx().memory(|m| m.focused()).is_some())
|| ui.ctx().input(|i| i.events.iter().any(|e| matches!(e, egui::Event::AccessKitActionRequest(_))));
if chrome_focus_live {
self.chrome.focus_tick = self.chrome.focus_tick.wrapping_add(1);
}
// The status bar replays on this count; the layout is cached, and the bar reads it anyway.
self.chrome.page_total = if self.has_document() { self.session.layout().pages.len() } else { 0 };
let window = self.windows.shown();
let stale = |app: &WordApp| app.windows.shown() != window;
let mode = self.screen_mode();
@@ -88,33 +101,25 @@ impl WordApp {
.dropped(Bind::given(dropped))
.window_title(self.window_title())
.page(self.ui.backstage, crate::backstage::scope(), |_| None, crate::backstage::show)
.title_bar(|_| None, chrome::title_bar::show)
.top(Slot::new("screenBar").screen_bar(), |_| None, chrome::screen_bar::show)
.title_bar(WordApp::title_bar_slot_version, chrome::title_bar::show)
.top(Slot::new("screenBar").screen_bar(), WordApp::screen_bar_slot_version, chrome::screen_bar::show)
// The ribbon is a tab strip over a group row: its slots are as tall as it is and stack it. A slot of the
// default size is one options bar high and lays its content in one line, which put the groups beside the
// tabs and collapsed every one of them.
.top(
Slot::new("ribbonDrop").drops().size(SlotSize::Content),
|_| None,
|ui, _cx, app: &mut WordApp| {
if !stale(app) {
*ribbon_out.borrow_mut() = Some(ribbon::show(app, ui));
}
},
)
.top(
Slot::new("ribbon").size(SlotSize::Content),
|_| None,
|ui, _cx, app: &mut WordApp| {
if !stale(app) {
*ribbon_out.borrow_mut() = Some(ribbon::show(app, ui));
}
},
)
.top(Slot::new("ribbonDrop").drops().size(SlotSize::Content), WordApp::ribbon_slot_version, |ui, _cx, app: &mut WordApp| {
if !stale(app) {
*ribbon_out.borrow_mut() = Some(ribbon::show(app, ui));
}
})
.top(Slot::new("ribbon").size(SlotSize::Content), WordApp::ribbon_slot_version, |ui, _cx, app: &mut WordApp| {
if !stale(app) {
*ribbon_out.borrow_mut() = Some(ribbon::show(app, ui));
}
})
// As tall as the bar: a failure's line under the message puts Install below the first line, and a slot of
// the default size (one options bar high) clips it, so a press on it reached nothing.
.top(Slot::new("languagePack").size(SlotSize::Content).visible(language_packs::bar_offer(self)), |_| None, language_packs::pack_bar)
.status(true, |_| None, chrome::status_bar::show)
.status(true, WordApp::status_bar_slot_version, chrome::status_bar::show)
.body(|ui, _cx, app: &mut WordApp| {
if stale(app) {
return;
@@ -150,7 +155,38 @@ impl WordApp {
self.ribbon_dropped = out.dropped;
// Keytips run outside the ribbon slots so legacy sequences and their hint still work when
// no ribbon shows (Focus, Full Screen View) and anchor under the slim screen bar.
let ribbon = ribbon_out.into_inner().unwrap_or_else(ribbon::no_ribbon);
let ribbon_visible =
!self.ui.backstage && (out.screen_mode == craft::ui::shell::ScreenMode::Standard || self.ribbon_dropped) && !out.panels_hidden;
let ribbon = match ribbon_out.into_inner() {
Some(r) => {
self.chrome.last_ribbon = Some(craft::menu::RibbonOutput {
shown: r.shown.clone(),
picked: None,
tab_ids: r.tab_ids.clone(),
controls: r.controls.clone(),
tabs: r.tabs,
});
r
}
None if ribbon_visible => {
self.chrome.ribbon_context_menu = false;
match &self.chrome.last_ribbon {
Some(r) => craft::menu::RibbonOutput {
shown: r.shown.clone(),
picked: None,
tab_ids: r.tab_ids.clone(),
controls: r.controls.clone(),
tabs: r.tabs,
},
None => ribbon::no_ribbon(),
}
}
None => {
self.chrome.ribbon_context_menu = false;
self.chrome.last_ribbon = None;
ribbon::no_ribbon()
}
};
let screen_bar = out.regions.slots.iter().find(|(slot, _)| *slot == "screenBar").map(|(_, rect)| *rect);
crate::keytips::show(ui, self, &ribbon, screen_bar);
out.regions
+328
View File
@@ -30,6 +30,20 @@ pub(crate) struct ChromeOpen {
/// Cached word count `(document_id, count_key, words)` for [`StatusCount::Words`] so typing
/// avoids full-document text extraction and character scans.
pub(crate) status_words: Option<(u64, u64, usize)>,
/// Cached ribbon output for `keytips::show` when the ribbon slot replays via `MemoView`.
pub(crate) last_ribbon: Option<craft::menu::RibbonOutput<String>>,
/// True while a ribbon context menu is held open so `ribbon_slot_version` advances each frame
/// (keeping `MemoView` mounted with the same `Id` while bypassing replay so `HeldMenu.frame` stays fresh).
pub(crate) ribbon_context_menu: bool,
/// Per-frame counter bumped in `paint_window` while [`ChromeOpen::ribbon_context_menu`] is true.
pub(crate) ribbon_context_menu_tick: u64,
/// Per-frame counter bumped in `paint_window` while keyboard focus sits outside the document canvas,
/// keeping `MemoView` mounted with the same `Id` while preventing stale focus/tooltip replay.
pub(crate) focus_tick: u64,
/// The laid-out page count, read in `paint_window` before the slots are versioned. Every layout input
/// that does not bump `rev` (markup display, Read Mode sizing, hidden text) changes it, so the status
/// bar's "Page x of y" replays only while it is current.
pub(crate) page_total: usize,
}
/// The steps a bar's group of controls takes in a narrow window, from the fullest to the most compact.
@@ -287,6 +301,320 @@ impl WordApp {
}
self.status_counts.1
}
/// Version stamp for `AppShell`'s `title_bar` slot (`None` while popups/keytips are active so they run live).
pub(crate) fn title_bar_slot_version(&self) -> Option<u64> {
use std::hash::{Hash as _, Hasher as _};
if self.chrome.undo || self.chrome.undo_closing.is_some() || self.chrome.search || self.keytips_active {
return None;
}
let mut h = std::collections::hash_map::DefaultHasher::new();
self.has_document().hash(&mut h);
self.windows.shown().hash(&mut h);
self.session.document_id().hash(&mut h);
self.session.doc.core.title.hash(&mut h);
self.session.dirty.hash(&mut h);
self.session.path.hash(&mut h);
self.session.file_name.hash(&mut h);
self.session.md_source.is_some().hash(&mut h);
self.autosaves().hash(&mut h);
self.autosave_block().map(|b| std::mem::discriminant(&b)).hash(&mut h);
self.session.undo_label().hash(&mut h);
self.session.author.hash(&mut h);
self.session.user_initials().hash(&mut h);
self.session.ribbon.qat().hash(&mut h);
std::sync::Arc::as_ptr(&self.session.registry).hash(&mut h);
self.ui.language.hash(&mut h);
self.ui.quick_access_toolbar.hash(&mut h);
self.ui.show_discord.hash(&mut h);
self.ui.backstage.hash(&mut h);
self.chrome.title_fit.step().hash(&mut h);
self.chrome.focus_tick.hash(&mut h);
self.command_states.revision().hash(&mut h);
Some(h.finish())
}
/// Version stamp for `AppShell`'s `screenBar` slot.
pub(crate) fn screen_bar_slot_version(&self) -> Option<u64> {
use std::hash::{Hash as _, Hasher as _};
if self.chrome.search || self.keytips_active {
return None;
}
let mut h = std::collections::hash_map::DefaultHasher::new();
std::mem::discriminant(&Screen::of(&self.session.view)).hash(&mut h);
self.windows.shown().hash(&mut h);
self.ui.language.hash(&mut h);
std::sync::Arc::as_ptr(&self.session.registry).hash(&mut h);
self.chrome.focus_tick.hash(&mut h);
self.command_states.revision().hash(&mut h);
Some(h.finish())
}
/// Version stamp for `AppShell`'s `ribbon` / `ribbonDrop` slots (`None` when dropped down, Backstage, or Keytips are active).
pub(crate) fn ribbon_slot_version(&self) -> Option<u64> {
use std::hash::{Hash as _, Hasher as _};
if self.ribbon_dropped || self.ui.backstage || self.keytips_active || !self.dialog_host.is_empty() {
return None;
}
let mut h = std::collections::hash_map::DefaultHasher::new();
self.has_document().hash(&mut h);
self.windows.shown().hash(&mut h);
self.session.document_id().hash(&mut h);
let home_collapsed = self.ui.tab == "Home"
&& self.session.sel.is_collapsed()
&& self.session.painter.is_none()
&& self.session.column.is_none()
&& !self.session.selects_whole_story();
home_collapsed.hash(&mut h);
if home_collapsed {
self.session.sel.focus.story.hash(&mut h);
self.session.sel.focus.path.hash(&mut h);
self.session.can_undo().hash(&mut h);
self.session.can_redo().hash(&mut h);
crate::ribbon::contextual_tabs(&self.session).hash(&mut h);
std::sync::Arc::as_ptr(&self.session.doc.styles).hash(&mut h);
self.session.doc.styles.gallery().len().hash(&mut h);
std::sync::Arc::as_ptr(&self.session.doc.numbering).hash(&mut h);
for c in &self.session.doc.settings.theme_colors {
(c.0, c.1, c.2).hash(&mut h);
}
let st = wordcraft_engine::cmd::format::cached_state(&self.session);
hash_json_value(&st, &mut h);
if let Some(p) = self.session.doc.para_at(&self.session.sel.focus) {
p.props.align.hash(&mut h);
p.props.bidi.hash(&mut h);
p.props.numbering.is_some().hash(&mut h);
if let Some(n) = &p.props.numbering {
(n.num, n.level).hash(&mut h);
}
}
} else {
self.session.rev().hash(&mut h);
self.session.sel.anchor.hash(&mut h);
self.session.sel.focus.hash(&mut h);
}
self.session.pending.is_some().hash(&mut h);
if let Some(p) = &self.session.pending {
p.style.hash(&mut h);
p.font.hash(&mut h);
p.size.map(f32::to_bits).hash(&mut h);
p.bold.hash(&mut h);
p.italic.hash(&mut h);
p.strike.hash(&mut h);
p.double_strike.hash(&mut h);
p.underline.is_some().hash(&mut h);
p.color.is_some().hash(&mut h);
p.highlight.is_some().hash(&mut h);
}
self.session.copied_format.is_some().hash(&mut h);
self.session.painter.as_ref().map(|(_, _, sticky)| *sticky).hash(&mut h);
self.session.math.is_some().hash(&mut h);
self.session.md_source.is_some().hash(&mut h);
self.session.member.is_some().hash(&mut h);
self.session.also_selected.len().hash(&mut h);
self.session.column_mode.hash(&mut h);
self.session.column.is_some().hash(&mut h);
self.canvas.last_font_color.hash(&mut h);
self.canvas.last_shading.hash(&mut h);
self.canvas.last_highlight.hash(&mut h);
self.session.doc.settings.track_changes.hash(&mut h);
let v = &self.session.view;
std::mem::discriminant(&v.mode).hash(&mut h);
v.read_mode.hash(&mut h);
v.focus_mode.hash(&mut h);
v.full_screen_view.hash(&mut h);
v.marks.hash(&mut h);
v.field_codes.hash(&mut h);
self.session.prefs.marks.hidden.hash(&mut h);
v.ruler.hash(&mut h);
v.gridlines.hash(&mut h);
v.table_gridlines.hash(&mut h);
v.nav_pane.hash(&mut h);
v.styles_pane.hash(&mut h);
v.style_inspector.hash(&mut h);
v.comments_pane.hash(&mut h);
v.clipboard_pane.hash(&mut h);
v.smart_art_pane.hash(&mut h);
v.accessibility_pane.hash(&mut h);
v.alt_text_pane.hash(&mut h);
v.format_shape_pane.hash(&mut h);
v.multi_page.hash(&mut h);
v.single_column.hash(&mut h);
v.fit.hash(&mut h);
v.zoom.to_bits().hash(&mut h);
v.dark_mode.hash(&mut h);
v.show_markup.hash(&mut h);
v.track_changes_pane.hash(&mut h);
v.proofing.hash(&mut h);
v.hide_white_space.hash(&mut h);
v.full_screen.hash(&mut h);
v.hide_ink.hash(&mut h);
v.design_mode.hash(&mut h);
v.outline.show_level.hash(&mut h);
v.outline.first_line_only.hash(&mut h);
v.outline.hide_formatting.hash(&mut h);
v.read_column_width.map(f32::to_bits).hash(&mut h);
v.read_zoom.to_bits().hash(&mut h);
v.read_line_spacing.map(f32::to_bits).hash(&mut h);
std::mem::discriminant(&v.read_page_color).hash(&mut h);
std::mem::discriminant(&v.draw.mode).hash(&mut h);
for pen in [v.draw.pen, v.draw.pencil, v.draw.highlighter] {
pen.color.hash(&mut h);
pen.width.to_bits().hash(&mut h);
}
v.draw.ink_to_shape.hash(&mut h);
v.draw.replay.hash(&mut h);
v.draw.pens.len().hash(&mut h);
for p in &v.draw.pens {
p.kind.hash(&mut h);
p.color.hash(&mut h);
p.width.to_bits().hash(&mut h);
}
self.session.ribbon.tabs.len().hash(&mut h);
for t in &self.session.ribbon.tabs {
t.name.hash(&mut h);
t.hidden.hash(&mut h);
t.custom.hash(&mut h);
t.groups.len().hash(&mut h);
for g in &t.groups {
g.commands.hash(&mut h);
}
}
self.session.ribbon.qat().hash(&mut h);
std::sync::Arc::as_ptr(&self.session.registry).hash(&mut h);
std::mem::discriminant(&self.session.units()).hash(&mut h);
self.session.prefs.region.hash(&mut h);
self.session.merge.kind.hash(&mut h);
self.session.merge.headers.hash(&mut h);
self.session.merge.rows.len().hash(&mut h);
self.session.merge.preview.hash(&mut h);
self.session.merge.record.hash(&mut h);
self.session.merge.highlight.hash(&mut h);
self.session.bib_style.hash(&mut h);
self.session.doc.sources.len().hash(&mut h);
for s in &self.session.doc.sources {
s.tag.hash(&mut h);
s.author.hash(&mut h);
s.title.hash(&mut h);
}
self.session.windows.available.hash(&mut h);
self.session.windows.current.hash(&mut h);
self.session.windows.side_by_side.hash(&mut h);
self.session.windows.sync_scroll.hash(&mut h);
self.session.windows.others.len().hash(&mut h);
for o in &self.session.windows.others {
o.id.hash(&mut h);
o.title.hash(&mut h);
}
self.ui.tab.hash(&mut h);
self.ui.nav_tab.hash(&mut h);
std::mem::discriminant(&self.ui.theme).hash(&mut h);
self.ui.ribbon_collapsed.hash(&mut h);
self.ui.quick_access_toolbar.hash(&mut h);
self.ui.language.hash(&mut h);
self.ui.recent_colors.hash(&mut h);
self.ui.recent_symbols.len().hash(&mut h);
for sym in &self.ui.recent_symbols {
sym.ch.hash(&mut h);
sym.font.hash(&mut h);
}
self.ui.font_picker.recent.hash(&mut h);
self.ui.font_picker.favorites.hash(&mut h);
self.ui.dark_page.hash(&mut h);
self.font_generation.hash(&mut h);
if self.ui.tab == "Review" {
wordcraft_engine::proof::dictionary_generation().hash(&mut h);
}
self.equation_symbol_set.hash(&mut h);
self.smart_art_item.hash(&mut h);
self.canvas.object_gesture().is_some().hash(&mut h);
self.canvas.table_tool.hash(&mut h);
self.canvas.link_from.is_some().hash(&mut h);
self.command_states.revision().hash(&mut h);
self.chrome.focus_tick.hash(&mut h);
if self.chrome.ribbon_context_menu {
(true, self.chrome.ribbon_context_menu_tick).hash(&mut h);
}
Some(h.finish())
}
/// Version stamp for `AppShell`'s `status` slot (`None` while a transient status message is shown).
pub(crate) fn status_bar_slot_version(&self) -> Option<u64> {
use std::hash::{Hash as _, Hasher as _};
if self.status_message().is_some() || self.keytips_active {
return None;
}
let mut h = std::collections::hash_map::DefaultHasher::new();
self.has_document().hash(&mut h);
self.windows.shown().hash(&mut h);
self.session.document_id().hash(&mut h);
self.session.rev().hash(&mut h);
self.session.sel.anchor.hash(&mut h);
self.session.sel.focus.hash(&mut h);
self.canvas.viewed_page.hash(&mut h);
self.session.page_hint.hash(&mut h);
self.status_counts.0.hash(&mut h);
std::mem::discriminant(&self.ui.status_count).hash(&mut h);
self.ui.language.hash(&mut h);
self.session.prefs.default_language.hash(&mut h);
self.session.prefs.overtype.hash(&mut h);
self.session.prefs.count_notes.hash(&mut h);
self.session.prefs.marks.hidden.hash(&mut h);
std::mem::discriminant(&self.session.view.mode).hash(&mut h);
self.session.view.read_mode.hash(&mut h);
self.session.view.read_zoom.to_bits().hash(&mut h);
self.session.view.zoom.to_bits().hash(&mut h);
self.session.view.dark_mode.hash(&mut h);
self.session.view.marks.hash(&mut h);
self.session.view.field_codes.hash(&mut h);
self.session.view.show_markup.hash(&mut h);
self.session.view.read_width.to_bits().hash(&mut h);
self.session.view.read_height.to_bits().hash(&mut h);
self.session.view.read_line_spacing.map(f32::to_bits).hash(&mut h);
self.session.view.read_column_width.map(f32::to_bits).hash(&mut h);
self.chrome.page_total.hash(&mut h);
self.session.doc.settings.track_changes.hash(&mut h);
self.session.read_aloud.open.hash(&mut h);
self.chrome.status_fit.step().hash(&mut h);
self.font_generation.hash(&mut h);
wordcraft_engine::proof::dictionary_generation().hash(&mut h);
self.chrome.focus_tick.hash(&mut h);
self.command_states.revision().hash(&mut h);
Some(h.finish())
}
}
/// Hashes a cached `format::state` JSON value without allocating.
fn hash_json_value(v: &serde_json::Value, h: &mut impl std::hash::Hasher) {
use std::hash::Hash as _;
std::mem::discriminant(v).hash(h);
match v {
serde_json::Value::Null => {}
serde_json::Value::Bool(b) => b.hash(h),
serde_json::Value::Number(n) => {
if let Some(u) = n.as_u64() {
(0u8, u).hash(h);
} else if let Some(i) = n.as_i64() {
(1u8, i).hash(h);
} else if let Some(f) = n.as_f64() {
(2u8, f.to_bits()).hash(h);
}
}
serde_json::Value::String(s) => s.hash(h),
serde_json::Value::Array(arr) => {
arr.len().hash(h);
for item in arr {
hash_json_value(item, h);
}
}
serde_json::Value::Object(map) => {
map.len().hash(h);
for (k, val) in map {
k.hash(h);
hash_json_value(val, h);
}
}
}
}
/// The status bar's count line (#2797): the whole document's `total`, or "selected of total" when
+350 -73
View File
@@ -570,19 +570,42 @@ fn title_bar_controls_never_overlap_in_a_narrow_window() {
}
}
/// Flattens nested `Shape::Vec` and replayed `Shape::PaintChunk` groups into `out`.
fn collect_shapes<'a>(shapes: &'a [egui::epaint::ClippedShape], out: &mut Vec<&'a egui::Shape>) {
fn walk<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::Shape>) {
match shape {
egui::Shape::Vec(children) => children.iter().for_each(|c| walk(c, out)),
egui::Shape::PaintChunk(chunk) => chunk.iter().for_each(|c| walk(&c.shape, out)),
other => out.push(other),
}
}
for clipped in shapes {
walk(&clipped.shape, out);
}
}
/// The texts the last frame painted on the row of `anchor` (its vertical middle inside theirs), each with the part of
/// it its clip lets show.
fn painted_on_row_of(h: &egui_kittest::Harness<'static, WordApp>, anchor: egui::Rect) -> Vec<(String, egui::Rect)> {
fn walk(shape: &egui::Shape, clip: egui::Rect, y: f32, out: &mut Vec<(String, egui::Rect)>) {
match shape {
egui::Shape::Vec(children) => children.iter().for_each(|c| walk(c, clip, y, out)),
egui::Shape::PaintChunk(chunk) => chunk.iter().for_each(|c| walk(&c.shape, clip.intersect(c.clip_rect), y, out)),
egui::Shape::Text(t) => {
let r = shape.visual_bounding_rect().intersect(clip);
if r.is_positive() && r.min.y <= y && y <= r.max.y {
out.push((t.galley.text().to_string(), r));
}
}
_ => {}
}
}
let y = anchor.center().y;
h.output()
.shapes
.iter()
.filter_map(|c| match &c.shape {
egui::Shape::Text(t) => Some((t.galley.text().to_string(), c.shape.visual_bounding_rect().intersect(c.clip_rect))),
_ => None,
})
.filter(|(_, r)| r.is_positive() && r.min.y <= y && y <= r.max.y)
.collect()
let mut out = Vec::new();
for c in &h.output().shapes {
walk(&c.shape, c.clip_rect, y, &mut out);
}
out
}
/// #5886: the status bar's texts stop before its controls on the right; in a narrow window the ones that don't fit
@@ -771,6 +794,7 @@ fn switch_track_and_knob(shapes: &[egui::epaint::ClippedShape], switch_rect: egu
fn filled_rects(shape: &egui::Shape, inside: egui::Rect, out: &mut Vec<egui::Rect>) {
match shape {
egui::Shape::Vec(children) => children.iter().for_each(|c| filled_rects(c, inside, out)),
egui::Shape::PaintChunk(chunk) => chunk.iter().for_each(|c| filled_rects(&c.shape, inside, out)),
egui::Shape::Rect(r) if r.blur_width == 0.0 && r.fill != egui::Color32::TRANSPARENT && inside.contains_rect(r.rect) => {
out.push(r.rect);
}
@@ -868,22 +892,6 @@ fn ribbon_and_chrome_buttons_have_responsive_hover_lighting_and_contrast() {
egui::Color32::from(craft::theme::Srgba::from(rgba))
}
fn collect_shapes<'a>(shapes: &'a [egui::epaint::ClippedShape], out: &mut Vec<&'a egui::Shape>) {
for clipped in shapes {
fn walk<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::Shape>) {
match shape {
egui::Shape::Vec(children) => {
for child in children {
walk(child, out);
}
}
other => out.push(other),
}
}
walk(&clipped.shape, out);
}
}
fn find_button_fill(shapes: &[egui::epaint::ClippedShape], rect: egui::Rect) -> Option<(egui::Color32, egui::CornerRadius)> {
let mut flattened = Vec::new();
collect_shapes(shapes, &mut flattened);
@@ -961,22 +969,6 @@ fn title_bar_discord_button_keeps_accent_fill_and_icon_contrast_in_wide_and_narr
egui::Color32::from(craft::theme::Srgba::from(rgba))
}
fn collect_shapes<'a>(shapes: &'a [egui::epaint::ClippedShape], out: &mut Vec<&'a egui::Shape>) {
for clipped in shapes {
fn walk<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::Shape>) {
match shape {
egui::Shape::Vec(children) => {
for child in children {
walk(child, out);
}
}
other => out.push(other),
}
}
walk(&clipped.shape, out);
}
}
for theme in ["light", "dark"] {
for (width, expect_compact) in [(1440.0_f32, false), (520.0_f32, true)] {
let mut a = new_app();
@@ -1075,22 +1067,6 @@ fn discord_icons_distinguish_title_bar_pill_and_ribbon_accent() {
egui::Color32::from(craft::theme::Srgba::from(rgba))
}
fn collect_shapes<'a>(shapes: &'a [egui::epaint::ClippedShape], out: &mut Vec<&'a egui::Shape>) {
for clipped in shapes {
fn walk<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::Shape>) {
match shape {
egui::Shape::Vec(children) => {
for child in children {
walk(child, out);
}
}
other => out.push(other),
}
}
walk(&clipped.shape, out);
}
}
for theme in ["light", "dark"] {
let mut a = new_app();
a.ui.show_discord = true;
@@ -1204,22 +1180,6 @@ fn status_bar_view_mode_toggles_and_zoom_slider_proportions() {
// 2. Zoom slider thumb proportions:
// Slider thumb is rendered as a round knob sized at glyph_md (14 pt) for ControlSize::Md.
fn collect_shapes<'a>(shapes: &'a [egui::epaint::ClippedShape], out: &mut Vec<&'a egui::Shape>) {
for clipped in shapes {
fn walk<'a>(shape: &'a egui::Shape, out: &mut Vec<&'a egui::Shape>) {
match shape {
egui::Shape::Vec(children) => {
for child in children {
walk(child, out);
}
}
other => out.push(other),
}
}
walk(&clipped.shape, out);
}
}
let mut shapes = Vec::new();
collect_shapes(&h.output().shapes, &mut shapes);
@@ -1267,3 +1227,320 @@ fn the_title_bar_search_keeps_its_index_until_the_commands_or_the_language_chang
assert!(!std::sync::Arc::ptr_eq(&rebuilt, &index(&h)), "another interface language builds it again");
}
}
#[test]
fn slot_versions_stay_stable_while_idle_or_scrolling_and_invalidate_on_changes() {
let mut app = new_app();
let tb0 = app.title_bar_slot_version().expect("title bar memoizes while closed");
let sb0 = app.status_bar_slot_version().expect("status bar memoizes without toast");
let rb0 = app.ribbon_slot_version().expect("ribbon memoizes when settled");
let sc0 = app.screen_bar_slot_version().expect("screen bar memoizes when closed");
assert_eq!(app.title_bar_slot_version(), Some(tb0));
assert_eq!(app.status_bar_slot_version(), Some(sb0));
assert_eq!(app.ribbon_slot_version(), Some(rb0));
assert_eq!(app.screen_bar_slot_version(), Some(sc0));
// Canvas scrolling leaves title bar and ribbon slot versions unchanged while updating status bar.
app.canvas.viewed_page = Some(4);
assert_eq!(app.title_bar_slot_version(), Some(tb0));
assert_eq!(app.ribbon_slot_version(), Some(rb0));
assert_ne!(app.status_bar_slot_version(), Some(sb0));
// Switching ribbon tab invalidates ribbon only; opening popups/keytips bypasses memoization.
app.run("ui.tab", json!({"tab": "Insert"})).unwrap();
assert_ne!(app.ribbon_slot_version(), Some(rb0));
app.chrome.search = true;
assert_eq!(app.title_bar_slot_version(), None);
assert_eq!(app.screen_bar_slot_version(), None);
app.chrome.search = false;
app.chrome.undo_closing = Some((vec!["Typing".into()], Default::default()));
assert_eq!(app.title_bar_slot_version(), None);
app.chrome.undo_closing = None;
app.keytips_active = true;
assert_eq!(app.ribbon_slot_version(), None);
assert_eq!(app.title_bar_slot_version(), None);
// Mid-word typing on the Home tab leaves title_bar and ribbon slot versions stable after the
// first edit makes the document dirty, while formatting changes still invalidate ribbon_slot_version.
app.keytips_active = false;
app.run("ui.tab", json!({"tab": "Home"})).unwrap();
app.run("text.insert", json!({"text": "a"})).unwrap();
let tb_dirty = app.title_bar_slot_version().expect("title bar memoizes after edit");
let rb_typed = app.ribbon_slot_version().expect("ribbon memoizes during Home typing");
app.run("text.insert", json!({"text": "b"})).unwrap();
assert_eq!(app.title_bar_slot_version(), Some(tb_dirty));
assert_eq!(app.ribbon_slot_version(), Some(rb_typed));
app.run("format.bold", json!({"value": true})).unwrap();
let rb_bold = app.ribbon_slot_version().expect("ribbon memoizes after bold");
assert_ne!(rb_bold, rb_typed);
// Editing a gallery pen or custom ribbon group command list invalidates ribbon_slot_version.
if let Some(pen) = app.session.view.draw.pens.first_mut() {
pen.width += 1.0;
let rb_pen = app.ribbon_slot_version().expect("ribbon memoizes after pen edit");
assert_ne!(rb_pen, rb_bold);
}
let rb_before_cmd = app.ribbon_slot_version();
if let Some(group) = app.session.ribbon.tabs.first_mut().and_then(|t| t.groups.first_mut()) {
group.commands.push("edit.paste".into());
assert_ne!(app.ribbon_slot_version(), rb_before_cmd);
}
}
#[test]
fn status_bar_reruns_for_marks_field_codes_and_dictionary_generation_at_same_rev() {
use egui::accesskit::Role;
use egui_kittest::kittest::By;
struct RemoveWord(&'static str);
impl Drop for RemoveWord {
fn drop(&mut self) {
wordcraft_engine::proof::remove_word(self.0);
}
}
let hidden = wordcraft_doc::CharProps { hidden: Some(true), ..Default::default() };
let mut doc = wordcraft_doc::Document::from_text("Visible");
// A short page keeps layout cheap while letting a few hidden lines or expanded fields spill onto page 2.
doc.last_section.page_h = 120.0;
doc.last_section.margin_top = 18.0;
doc.last_section.margin_bottom = 18.0;
for i in 0..8 {
doc.body.push(wordcraft_doc::para_block(wordcraft_doc::Paragraph::with_text(&format!("Hidden line {i}"), hidden.clone())));
}
let mut app = WordApp::new(wordcraft_engine::Session::new(doc), crate::Services::default());
app.ui.language = "en".into();
for _ in 0..10 {
app.run("insert.field", json!({"instr": "AUTHOR \\* UPPER \\* MERGEFORMAT", "result": "x"})).unwrap();
}
let mut h = crate::test_harness::builder().with_size(egui::vec2(1000.0, 400.0)).build_ui_state(app_frame, app);
crate::test_harness::frames(&mut h, 2);
let page_button = |h: &egui_kittest::Harness<'static, WordApp>| -> String {
h.query_all(By::new().role(Role::Button).label_contains("Page "))
.next()
.expect("status bar page button")
.accesskit_node()
.label()
.expect("page button label")
};
assert_eq!(page_button(&h), "Page 1 of 1");
let rev = h.state().session.rev();
// 1. view.marks changes page count via relayout() at the same rev.
let sb0 = h.state().status_bar_slot_version().unwrap();
h.state_mut().run("view.marks", json!({"value": true})).unwrap();
assert_eq!(h.state().session.rev(), rev);
assert_ne!(h.state().status_bar_slot_version(), Some(sb0));
crate::test_harness::frames(&mut h, 2);
assert_ne!(page_button(&h), "Page 1 of 1", "status bar must rerun when view.marks changes page count");
h.state_mut().run("view.marks", json!({"value": false})).unwrap();
crate::test_harness::frames(&mut h, 2);
assert_eq!(page_button(&h), "Page 1 of 1");
// 2. prefs.marks.hidden changes page count at the same rev.
let sb1 = h.state().status_bar_slot_version().unwrap();
h.state_mut().session.prefs.marks.hidden = true;
assert_ne!(h.state().status_bar_slot_version(), Some(sb1));
crate::test_harness::frames(&mut h, 2);
assert_ne!(page_button(&h), "Page 1 of 1", "status bar must rerun when prefs.marks.hidden changes page count");
h.state_mut().session.prefs.marks.hidden = false;
crate::test_harness::frames(&mut h, 2);
assert_eq!(page_button(&h), "Page 1 of 1");
// 3. view.fieldCodes changes page count via relayout() at the same rev.
let sb2 = h.state().status_bar_slot_version().unwrap();
h.state_mut().run("view.fieldCodes", json!({"value": true})).unwrap();
assert_eq!(h.state().session.rev(), rev);
assert_ne!(h.state().status_bar_slot_version(), Some(sb2));
crate::test_harness::frames(&mut h, 2);
assert_ne!(page_button(&h), "Page 1 of 1", "status bar must rerun when view.field_codes changes page count");
// 4. dictionary_generation() invalidates both status_bar and Review-tab ribbon versions at the same rev.
h.state_mut().run("ui.tab", json!({"tab": "Review"})).unwrap();
let sb_dict = h.state().status_bar_slot_version().unwrap();
let rb_dict = h.state().ribbon_slot_version().unwrap();
let _guard = RemoveWord("slotversionprobe42");
wordcraft_engine::proof::add_word("slotversionprobe42");
assert_eq!(h.state().session.rev(), rev);
assert_ne!(h.state().status_bar_slot_version(), Some(sb_dict));
assert_ne!(h.state().ribbon_slot_version(), Some(rb_dict));
}
#[test]
fn ribbon_reruns_for_non_rev_state() {
use egui::accesskit::{Role, Toggled};
use egui_kittest::kittest::By;
let mut app = new_app();
app.ui.language = "en".into();
app.run("insert.table", json!({"rows": 2, "cols": 2})).unwrap();
app.run("ui.tab", json!({"tab": "Table Layout"})).unwrap();
let mut h = crate::test_harness::builder().with_size(egui::vec2(2000.0, 360.0)).build_ui_state(app_frame, app);
crate::test_harness::frames(&mut h, 2);
// 1. table_tool toggles Draw Table selected state and clears on Escape at the same rev.
let draw_toggled = |h: &egui_kittest::Harness<'static, WordApp>| -> Option<Toggled> {
h.query_all(By::new().role(Role::Button).label_contains("Draw"))
.find(|n| n.accesskit_node().label().is_some_and(|l| l.contains("Table")))
.expect("Draw Table ribbon button")
.accesskit_node()
.toggled()
};
assert_eq!(draw_toggled(&h), Some(Toggled::False));
let rev = h.state().session.rev();
let rb_table = h.state().ribbon_slot_version().unwrap();
h.state_mut().run("table.draw", json!({"on": true})).unwrap();
assert_eq!(h.state().session.rev(), rev);
assert_ne!(h.state().ribbon_slot_version(), Some(rb_table));
crate::test_harness::frames(&mut h, 2);
assert_eq!(draw_toggled(&h), Some(Toggled::True), "Draw Table button must show selected");
h.key_press(egui::Key::Escape);
crate::test_harness::frames(&mut h, 2);
assert_eq!(h.state().session.rev(), rev);
assert_eq!(draw_toggled(&h), Some(Toggled::False), "Draw Table button must clear after Escape");
// 2. bib_style and doc.sources on References tab at the same rev.
h.state_mut().run("ui.tab", json!({"tab": "References"})).unwrap();
crate::test_harness::frames(&mut h, 2);
assert!(h.query_by_label("Style: APA").is_some(), "expected initial APA style button");
let rb_bib = h.state().ribbon_slot_version().unwrap();
h.state_mut().session.bib_style = "MLA".into();
assert_ne!(h.state().ribbon_slot_version(), Some(rb_bib));
crate::test_harness::frames(&mut h, 2);
assert!(h.query_by_label("Style: MLA").is_some(), "ribbon must rerun to show Style: MLA");
let rb_src = h.state().ribbon_slot_version().unwrap();
h.state_mut().session.doc.sources.push(wordcraft_doc::Source {
tag: "S1".into(),
author: "Doe".into(),
title: "Book".into(),
..Default::default()
});
assert_ne!(h.state().ribbon_slot_version(), Some(rb_src));
// 3. session.merge on Mailings tab at the same rev.
h.state_mut().run("ui.tab", json!({"tab": "Mailings"})).unwrap();
crate::test_harness::frames(&mut h, 2);
let rb_merge = h.state().ribbon_slot_version().unwrap();
h.state_mut().session.merge.rows = vec![vec!["Alice".into()], vec!["Bob".into()]];
h.state_mut().session.merge.record = 1;
assert_ne!(h.state().ribbon_slot_version(), Some(rb_merge));
crate::test_harness::frames(&mut h, 2);
let record_val = h.get_by_role_and_label(Role::SpinButton, "Record").accesskit_node().numeric_value();
assert_eq!(record_val, Some(2.0), "Mailings tab must rerun to show updated record number");
// 4. session.units(), canvas.link_from, ui.nav_tab, and remembered split-button colours at the same rev.
let rb_units = h.state().ribbon_slot_version().unwrap();
h.state_mut().session.prefs.units = Some(wordcraft_geom::Unit::Centimeters);
assert_ne!(h.state().ribbon_slot_version(), Some(rb_units));
let rb_link = h.state().ribbon_slot_version().unwrap();
h.state_mut().canvas.link_from = Some(wordcraft_doc::Pos::body(0, 0));
assert_ne!(h.state().ribbon_slot_version(), Some(rb_link));
let rb_nav = h.state().ribbon_slot_version().unwrap();
h.state_mut().ui.nav_tab = "results".into();
assert_ne!(h.state().ribbon_slot_version(), Some(rb_nav));
let rb_fc = h.state().ribbon_slot_version().unwrap();
h.state_mut().canvas.last_font_color = "0070C0".into();
assert_ne!(h.state().ribbon_slot_version(), Some(rb_fc));
let rb_sh = h.state().ribbon_slot_version().unwrap();
h.state_mut().canvas.last_shading = "E97132".into();
assert_ne!(h.state().ribbon_slot_version(), Some(rb_sh));
let rb_hl = h.state().ribbon_slot_version().unwrap();
h.state_mut().canvas.last_highlight = "cyan".into();
assert_ne!(h.state().ribbon_slot_version(), Some(rb_hl));
assert_eq!(h.state().session.rev(), rev);
}
#[test]
fn ribbon_view_tab_and_context_menu_rerun_without_closing() {
use egui::accesskit::{Role, Toggled};
// 1. session.windows on View tab at the same rev (using window_ui so arrange::poll doesn't overwrite test state).
let mut win_app = new_app();
win_app.ui.language = "en".into();
win_app.run("ui.tab", json!({"tab": "View"})).unwrap();
let mut wh =
crate::test_harness::builder().with_size(egui::vec2(2000.0, 360.0)).build_ui_state(|ui, app: &mut WordApp| app.window_ui(ui), win_app);
crate::test_harness::frames(&mut wh, 2);
let sync_toggled = |h: &egui_kittest::Harness<'static, WordApp>| -> Option<Toggled> {
h.get_by_role_and_label(Role::Button, "Synchronous Scrolling").accesskit_node().toggled()
};
assert_eq!(sync_toggled(&wh), Some(Toggled::False));
let win_rev = wh.state().session.rev();
let rb_win = wh.state().ribbon_slot_version().unwrap();
wh.state_mut().session.windows.sync_scroll = true;
assert_eq!(wh.state().session.rev(), win_rev);
assert_ne!(wh.state().ribbon_slot_version(), Some(rb_win));
crate::test_harness::frames(&mut wh, 2);
assert_eq!(sync_toggled(&wh), Some(Toggled::True), "View tab must rerun when session.windows changes");
// 2. Ribbon context menu advances ribbon_slot_version each frame (keeping MemoView's Id) and stays open across idle frames.
wh.state_mut().run("ui.tab", json!({"tab": "Home"})).unwrap();
crate::test_harness::frames(&mut wh, 2);
let bold_pos = wh.get_by_role_and_label(Role::Button, "Bold").rect().center();
click_at(&mut wh, bold_pos, egui::PointerButton::Secondary);
assert!(wh.query_by_label("Customize the Ribbon…").is_some(), "ribbon context menu must open");
assert!(wh.state().chrome.ribbon_context_menu);
let v_open = wh.state().ribbon_slot_version().expect("keeps MemoView mounted so Ribbon Id stays stable");
crate::test_harness::frames(&mut wh, 3);
assert!(wh.query_by_label("Customize the Ribbon…").is_some(), "ribbon context menu must stay open across idle frames");
assert_ne!(wh.state().ribbon_slot_version(), Some(v_open), "version advances per frame while context menu is open");
wh.key_press(egui::Key::Escape);
crate::test_harness::frames(&mut wh, 2);
assert!(wh.query_by_label("Customize the Ribbon…").is_none(), "Escape closes the ribbon context menu");
assert!(!wh.state().chrome.ribbon_context_menu);
let v_closed = wh.state().ribbon_slot_version().expect("ribbon memoizes again once context menu closes");
crate::test_harness::frames(&mut wh, 2);
assert_eq!(wh.state().ribbon_slot_version(), Some(v_closed));
}
#[test]
fn status_bar_version_tracks_layout_inputs_outside_rev_and_ignores_expired_messages() {
let mut app = new_app();
app.ui.language = "en".into();
let rev = app.session.rev();
// An expired message no longer holds the bar live (the field is only cleared on a document switch).
app.status = Some(crate::Status { text: "done".into(), until: 0.0, tone: craft::theme::variant::Tone::Neutral });
assert!(app.status_message().is_none());
let sb0 = app.status_bar_slot_version().expect("an expired status message must not keep the bar live");
app.status("shown now");
assert_eq!(app.status_bar_slot_version(), None, "a message still showing runs the bar live");
app.status = None;
// Layout inputs that relayout() without bumping rev: Review > Display for Review, and Read Mode sizing.
let changed = |app: &mut WordApp, f: &dyn Fn(&mut WordApp), what: &str| {
let before = app.status_bar_slot_version().unwrap();
f(app);
assert_eq!(app.session.rev(), rev);
assert_ne!(app.status_bar_slot_version(), Some(before), "{what}");
};
changed(&mut app, &|a| a.session.view.show_markup = !a.session.view.show_markup, "show_markup");
changed(&mut app, &|a| a.session.view.read_width += 10.0, "read_width");
changed(&mut app, &|a| a.session.view.read_height += 10.0, "read_height");
changed(&mut app, &|a| a.session.view.read_line_spacing = Some(1.5), "read_line_spacing");
changed(&mut app, &|a| a.session.view.read_column_width = Some(480.0), "read_column_width");
changed(&mut app, &|a| a.chrome.page_total += 1, "page_total");
assert_ne!(app.status_bar_slot_version(), Some(sb0));
// The ribbon shows these toggles too.
let rb = app.ribbon_slot_version().unwrap();
app.session.view.field_codes = !app.session.view.field_codes;
let rb1 = app.ribbon_slot_version().unwrap();
assert_ne!(rb1, rb, "field_codes");
app.session.prefs.marks.hidden = !app.session.prefs.marks.hidden;
assert_ne!(app.ribbon_slot_version(), Some(rb1), "prefs.marks.hidden");
}
+11 -1
View File
@@ -87,6 +87,9 @@ pub(crate) const RIBBON_LOOK: RibbonLook = RibbonLook::new(36, 17).arrow(10).row
/// controls for the keytips.
pub fn show(app: &mut WordApp, ui: &mut Ui) -> RibbonOutput<String> {
if app.session.view.read_mode {
if !ui.is_sizing_pass() {
app.chrome.ribbon_context_menu = false;
}
reading_toolbar(app, ui);
return no_ribbon();
}
@@ -169,10 +172,14 @@ pub fn show(app: &mut WordApp, ui: &mut Ui) -> RibbonOutput<String> {
let qat_shown = app.ui.quick_access_toolbar;
// The collapse state is written through `ui.collapseRibbon`, the one write path, when the strip toggles it.
let mut collapsed = app.ui.ribbon_collapsed;
let context_open = std::cell::Cell::new(false);
let out = Ribbon::new(tl!("Ribbon").to_string(), &tabs, Bind::given(app.ui.tab.clone()))
.trailing(&ends)
.minimised(Bind::bound(&mut collapsed))
.context_menu(&mut |ui, request, _| context_menu::rows(ui, request, &qat, qat_shown))
.context_menu(&mut |ui, request, _| {
context_open.set(true);
context_menu::rows(ui, request, &qat, qat_shown);
})
// Large icons 36 points square and every other icon 17 (`RIBBON_LOOK`), and the dialog
// launcher as a small corner arrow, as the old ribbon drew it.
.look(RIBBON_LOOK)
@@ -190,6 +197,9 @@ pub fn show(app: &mut WordApp, ui: &mut Ui) -> RibbonOutput<String> {
custom_groups(row, app, &e.groups);
}
});
if !ui.is_sizing_pass() {
app.chrome.ribbon_context_menu = context_open.get();
}
if collapsed != app.ui.ribbon_collapsed {
let _ = app.run("ui.collapseRibbon", json!({}));
}
+1 -1
View File
@@ -18,7 +18,7 @@ use crate::WordApp;
use crate::ink::{page_under, page_xy, placed};
/// Which table tool is on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TableTool {
Draw,
Erase,