Font menu: a font preview leaves the Document filter's families as they were

While a font is previewed from the menu, the Document filter reads the
document as it was before the preview, so the family the previewed text
had stays listed and the rows do not move under the pointer. A pick or
an undo still lists the families again.
This commit is contained in:
dotCooCoo
2026-11-16 08:46:28 -08:00
parent 14cf026bcc
commit 9a562bd0ba
2 changed files with 71 additions and 13 deletions
+40 -13
View File
@@ -129,10 +129,34 @@ pub struct MenuLook<'a> {
/// The current document as the Document filter reads it.
#[derive(Clone, Copy, Debug)]
pub struct MenuDocument<'a> {
/// The document's identity and revision: its families are listed again when either changes.
/// The document's identity.
pub uid: u64,
pub revision: u64,
pub doc: &'a vectorcraft_doc::Document,
/// Its content: while an interaction is open (a font previewed from the menu), the content it
/// started from, so a preview does not change the families listed. Its families are listed
/// again when this is another version of the document.
pub doc: &'a Arc<vectorcraft_doc::Document>,
}
impl MenuDocument<'_> {
/// Which document and which version of it the families were read from.
fn key(&self) -> DocumentKey {
DocumentKey { uid: self.uid, doc: Arc::downgrade(self.doc) }
}
}
/// A document and a version of its content, compared by identity. The weak reference keeps the
/// version's allocation, so a later version is never placed at the same address while the key is
/// kept.
#[derive(Clone, Debug)]
struct DocumentKey {
uid: u64,
doc: std::sync::Weak<vectorcraft_doc::Document>,
}
impl PartialEq for DocumentKey {
fn eq(&self, other: &Self) -> bool {
self.uid == other.uid && std::sync::Weak::ptr_eq(&self.doc, &other.doc)
}
}
impl Default for MenuLook<'static> {
@@ -171,7 +195,10 @@ impl<'a> MenuLook<'a> {
wheel: false,
wheel_owner: None,
name: None,
document: app.session.active().map(|state| MenuDocument { uid: state.uid, revision: state.revision, doc: &state.doc }),
document: app
.session
.active()
.map(|state| MenuDocument { uid: state.uid, doc: state.interaction.as_ref().map_or(&state.doc, |interaction| &interaction.doc) }),
}
}
}
@@ -327,13 +354,13 @@ fn remap_highlight(before: &[Row], selected: Option<usize>, after: &[Row]) -> Op
/// What the rows were listed for: the query, the script, kind, font technology (variable only) and
/// favourites filters, Show Font Names in English, the expanded families, favourites, recent
/// history, font list generation, the family Show Similar Fonts orders by, and the document (its
/// uid and revision) while the Document filter lists its families.
type RowsKey = (String, usize, Option<FontClass>, bool, bool, bool, Vec<String>, Vec<String>, Vec<String>, u64, Option<String>, Option<(u64, u64)>);
/// history, font list generation, the family Show Similar Fonts orders by, and the document version
/// while the Document filter lists its families.
type RowsKey = (String, usize, Option<FontClass>, bool, bool, bool, Vec<String>, Vec<String>, Vec<String>, u64, Option<String>, Option<DocumentKey>);
/// The families a [`MenuState`] found in a document (lowercase), and the document's uid, revision
/// and font list generation they were found for.
type DocumentFonts = ((u64, u64, u64), Arc<HashSet<String>>);
/// The families a [`MenuState`] found in a document (lowercase), and the document version and font
/// list generation they were found for.
type DocumentFonts = ((DocumentKey, u64), Arc<HashSet<String>>);
/// The menu's state between frames.
#[derive(Clone, Default)]
@@ -444,10 +471,10 @@ fn document_families(db: &vectorcraft_text::FontDb, doc: &vectorcraft_doc::Docum
}
/// The families the Document filter lists, when it is on and a document is open: those of
/// `look`'s document, found again only when the document, its revision or the font list changed.
/// `look`'s document, found again only when the document, its version or the font list changed.
fn used_families(db: &vectorcraft_text::FontDb, st: &mut MenuState, look: MenuLook) -> Option<Arc<HashSet<String>>> {
let document = look.document.filter(|_| st.document_only)?;
let key = (document.uid, document.revision, db.generation());
let key = (document.key(), db.generation());
if let Some((k, families)) = &st.document_fonts
&& *k == key
{
@@ -1176,7 +1203,7 @@ fn header<'s>(ui: &mut Ui, db: &'s vectorcraft_text::FontDb, state_id: egui::Id,
look.recent.to_vec(),
db.generation(),
st.similar.clone(),
look.document.filter(|_| st.document_only).map(|d| (d.uid, d.revision)),
look.document.filter(|_| st.document_only).map(|d| d.key()),
);
if st.rows_key.as_ref() != Some(&key) {
let used = used_families(db, st, look);
+31
View File
@@ -1252,6 +1252,37 @@ fn the_document_filter_combines_with_the_others_and_follows_edits() {
assert_eq!(super::wheel_order(&mut st, look), ["Source Serif 4", "Inter"], "the wheel steps through the same families");
}
/// A font previewed from the menu does not change what the Document filter lists: the family the
/// previewed text had stays listed, though no other type uses it. A pick lists the families again.
#[test]
fn a_preview_keeps_the_document_filters_families() {
let mut app = app_with_type(&["Source Serif 4", "Inter"]);
let ctx = egui::Context::default();
let mut st = super::MenuState { document_only: true, ..Default::default() };
let frames = |app: &crate::VectorcraftApp, st: &mut super::MenuState| {
let look = super::MenuLook { samples: false, recent: &[], ..super::MenuLook::of(app) };
for _ in 0..2 {
menu_frame(&ctx, st, look, vec![]);
}
};
frames(&app, &mut st);
assert_eq!(listed_families(&st), ["Inter", "Source Serif 4"]);
// The selected text, the only type in Inter, previewed in Source Serif 4.
super::apply(&mut app, &ctx, super::FontPick::Preview("Source Serif 4".into(), None));
assert!(app.session.active().unwrap().interaction.is_some(), "the preview is open");
frames(&app, &mut st);
assert_eq!(listed_families(&st), ["Inter", "Source Serif 4"], "the preview leaves the list as it was");
super::apply(&mut app, &ctx, super::FontPick::Preview("Inter".into(), None));
frames(&app, &mut st);
assert_eq!(listed_families(&st), ["Inter", "Source Serif 4"], "previewing back changes nothing");
super::apply(&mut app, &ctx, super::FontPick::Chosen("Source Serif 4".into(), None));
frames(&app, &mut st);
assert_eq!(listed_families(&st), ["Source Serif 4"], "the pick is an edit: Inter is no longer used");
app.run("edit.undo", serde_json::json!({})).unwrap();
frames(&app, &mut st);
assert_eq!(listed_families(&st), ["Inter", "Source Serif 4"], "undo lists it again");
}
/// The Document filter on a document without type lists nothing and says why; without a document
/// it is unavailable and lists every family; the filter is shared like the others.
#[test]