mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
font-manager: an open glyph menu lets go of a glyph id when the source replaces its faces
The source can replace a face under the same family and style, and says so through its revision. The held menu cell now keeps the revision it was clicked at, and a glyph id is dropped once the revision changes. A character names the same glyph in the read-again face, so it stays held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c2d8c9e2e3
commit
323db9809e
2 files changed
+67
-57
No files matched your search
@@ -327,12 +327,12 @@ struct Marked(Selection<usize>);
|
||||
|
||||
/// The glyph a context menu was opened on, and the face it was opened in. Later passes keep this glyph: the grid
|
||||
/// remembers the row, and another glyph of a same-length list must not take the menu over. A glyph id names a glyph
|
||||
/// only in its own face, so the menu holds the cell only while that face is shown: the same family and style, and for
|
||||
/// a glyph id the same source revision, since the source may replace a face under the same names.
|
||||
/// only in its own face, so the menu holds the cell only while that face is shown: the same family and style, and
|
||||
/// for a glyph id the same source revision, since the source can replace a face under the same names.
|
||||
#[derive(Clone, Default)]
|
||||
struct HeldMenu(Option<MenuCell>);
|
||||
|
||||
/// A held menu's cell, the family and style it was clicked in, and the source's revision then.
|
||||
/// A held menu's cell, the family and style it was clicked in, and the source revision then.
|
||||
#[derive(Clone)]
|
||||
struct MenuCell {
|
||||
cell: GlyphCell,
|
||||
@@ -341,6 +341,14 @@ struct MenuCell {
|
||||
revision: u64,
|
||||
}
|
||||
|
||||
impl MenuCell {
|
||||
/// Whether the cell still names the glyph it was clicked on in `face` at `revision`. A character names the same
|
||||
/// glyph after the source reads its faces again; a glyph id may not.
|
||||
fn holds_in(&self, face: (&str, &str), revision: u64) -> bool {
|
||||
(self.family.as_str(), self.style.as_str()) == face && (matches!(self.cell, GlyphCell::Char(_)) || self.revision == revision)
|
||||
}
|
||||
}
|
||||
|
||||
/// The alternates popup: whether it is open, the cell it was opened for and that cell's alternates, read once when
|
||||
/// it opened.
|
||||
#[derive(Clone, Default)]
|
||||
@@ -651,13 +659,9 @@ impl<'a> GlyphsPanel<'a> {
|
||||
if let Some(cell) = clicked {
|
||||
held = HeldMenu(Some(MenuCell { cell, family: face.0.to_owned(), style: face.1.to_owned(), revision }));
|
||||
}
|
||||
// A closed menu lets go of its cell, and so does another face: the same glyph id there is
|
||||
// another glyph. A new revision may be another face under the same names; a character still
|
||||
// names the same character there, a glyph id does not.
|
||||
let other_face = held.0.as_ref().is_some_and(|held| {
|
||||
(held.family.as_str(), held.style.as_str()) != face
|
||||
|| (matches!(held.cell, GlyphCell::Glyph(_)) && held.revision != revision)
|
||||
});
|
||||
// A closed menu lets go of its cell, and so does another face, or a glyph id after the source
|
||||
// replaced its faces: the same id there is another glyph.
|
||||
let other_face = held.0.as_ref().is_some_and(|held| !held.holds_in(face, revision));
|
||||
if shown_grid.response.menu.is_none() || other_face {
|
||||
held = HeldMenu(None);
|
||||
}
|
||||
|
||||
@@ -203,8 +203,8 @@ fn an_open_menu_keeps_the_clicked_cell_when_that_row_changes() {
|
||||
assert!(menu.iter().all(|cell| *cell == GlyphCell::Char(FIRST)), "the row's new glyph does not take the menu: {menu:?}");
|
||||
}
|
||||
|
||||
/// Unencoded glyphs 1 to 300 in every face: the same id names another glyph in another face. Its revision is the
|
||||
/// shared counter, so a test can replace the faces under the same names.
|
||||
/// Unencoded glyphs 1 to 300 in every face and at every revision: the same id names another glyph in another face,
|
||||
/// or in a face the source replaced under the same names.
|
||||
struct Unencoded(Arc<AtomicU64>);
|
||||
|
||||
impl GlyphSource for Unencoded {
|
||||
@@ -221,67 +221,73 @@ impl GlyphSource for Unencoded {
|
||||
}
|
||||
}
|
||||
|
||||
/// The host's family and the source's revision, which a test changes between passes, and the cells the panel
|
||||
/// reported a menu for.
|
||||
struct OpenMenu {
|
||||
/// A host menu open on glyph 1 of an [`Unencoded`] source, with the family and the revision the test can change.
|
||||
struct GlyphMenu {
|
||||
fx: Fixture,
|
||||
family: Rc<RefCell<String>>,
|
||||
revision: Arc<AtomicU64>,
|
||||
seen: Rc<RefCell<Vec<GlyphCell>>>,
|
||||
}
|
||||
|
||||
/// A panel over [`Unencoded`] whose host hangs a menu from every menu it reports, with that menu opened on glyph 1.
|
||||
fn open_menu_on_glyph_1() -> OpenMenu {
|
||||
let family = Rc::new(RefCell::new("Plain".to_owned()));
|
||||
let revision = Arc::new(AtomicU64::new(0));
|
||||
let seen = Rc::new(RefCell::new(Vec::<GlyphCell>::new()));
|
||||
let (host_family, source, shown) = (Rc::clone(&family), Unencoded(Arc::clone(&revision)), Rc::clone(&seen));
|
||||
let (mut style, mut settings) = ("Regular".to_owned(), GlyphsPanelSettings::default());
|
||||
let mut fx = Fixture::new(move |ui| {
|
||||
let out = {
|
||||
let mut family = host_family.borrow_mut();
|
||||
GlyphsPanel::new("Glyphs", &source, Bind::bound(&mut *family), Bind::bound(&mut style), Bind::bound(&mut settings))
|
||||
.can_insert(true)
|
||||
.show(ui)
|
||||
};
|
||||
if let Some(menu) = out.menu {
|
||||
shown.borrow_mut().push(menu.cell);
|
||||
Menu::context("Glyph", &menu.opener).show(ui, |ui, _| {
|
||||
MenuItem::new("Insert Glyph").show(ui);
|
||||
});
|
||||
}
|
||||
});
|
||||
fx.in_room(Room::Window);
|
||||
fx.settle();
|
||||
let cell = fx.by_label("1, glyph 1").expect("the first glyph");
|
||||
let at = cell.center();
|
||||
fx.frame(vec![pointer::moved(at), pointer::secondary(at, true)]);
|
||||
fx.frame(vec![pointer::secondary(at, false)]);
|
||||
fx.settle();
|
||||
assert!(seen.borrow().contains(&GlyphCell::Glyph(1)), "the click names the glyph");
|
||||
seen.borrow_mut().clear();
|
||||
OpenMenu { fx, family, revision, seen }
|
||||
impl GlyphMenu {
|
||||
fn open() -> Self {
|
||||
let revision = Arc::new(AtomicU64::new(0));
|
||||
let source = Unencoded(Arc::clone(&revision));
|
||||
let family = Rc::new(RefCell::new("Plain".to_owned()));
|
||||
let host_family = Rc::clone(&family);
|
||||
let seen = Rc::new(RefCell::new(Vec::<GlyphCell>::new()));
|
||||
let shown = Rc::clone(&seen);
|
||||
let (mut style, mut settings) = ("Regular".to_owned(), GlyphsPanelSettings::default());
|
||||
let mut fx = Fixture::new(move |ui| {
|
||||
let out = {
|
||||
let mut family = host_family.borrow_mut();
|
||||
GlyphsPanel::new("Glyphs", &source, Bind::bound(&mut *family), Bind::bound(&mut style), Bind::bound(&mut settings))
|
||||
.can_insert(true)
|
||||
.show(ui)
|
||||
};
|
||||
if let Some(menu) = out.menu {
|
||||
shown.borrow_mut().push(menu.cell);
|
||||
Menu::context("Glyph", &menu.opener).show(ui, |ui, _| {
|
||||
MenuItem::new("Insert Glyph").show(ui);
|
||||
});
|
||||
}
|
||||
});
|
||||
fx.in_room(Room::Window);
|
||||
fx.settle();
|
||||
let cell = fx.by_label("1, glyph 1").expect("the first glyph");
|
||||
let at = cell.center();
|
||||
fx.frame(vec![pointer::moved(at), pointer::secondary(at, true)]);
|
||||
fx.frame(vec![pointer::secondary(at, false)]);
|
||||
fx.settle();
|
||||
assert!(seen.borrow().contains(&GlyphCell::Glyph(1)), "the click names the glyph");
|
||||
seen.borrow_mut().clear();
|
||||
GlyphMenu { fx, family, revision, seen }
|
||||
}
|
||||
|
||||
/// The cells the menu reported over the next passes.
|
||||
fn reported(&mut self) -> Vec<GlyphCell> {
|
||||
self.fx.frame(Vec::new());
|
||||
self.fx.settle();
|
||||
self.seen.borrow().clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn an_open_menu_does_not_report_a_glyph_id_from_another_face() {
|
||||
let mut open = open_menu_on_glyph_1();
|
||||
let mut open = GlyphMenu::open();
|
||||
*open.family.borrow_mut() = "Other".to_owned();
|
||||
open.fx.frame(Vec::new());
|
||||
open.fx.settle();
|
||||
let menu = open.seen.borrow().clone();
|
||||
let menu = open.reported();
|
||||
assert!(menu.is_empty(), "the clicked id names another glyph in the new face, so the menu reports none: {menu:?}");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn an_open_menu_does_not_report_a_glyph_id_from_a_replaced_face() {
|
||||
let mut open = open_menu_on_glyph_1();
|
||||
open.revision.fetch_add(1, Ordering::Relaxed);
|
||||
open.fx.frame(Vec::new());
|
||||
open.fx.settle();
|
||||
let menu = open.seen.borrow().clone();
|
||||
assert!(menu.is_empty(), "the same names and ids at a new revision may be another face, so the menu reports none: {menu:?}");
|
||||
fn an_open_menu_does_not_report_a_glyph_id_after_the_source_replaces_the_face() {
|
||||
let mut open = GlyphMenu::open();
|
||||
open.revision.store(1, Ordering::Relaxed);
|
||||
let menu = open.reported();
|
||||
assert!(menu.is_empty(), "the face behind the same names changed, so the clicked id names another glyph: {menu:?}");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn glyphs_panel_alternates() {
|
||||
let (mut fx, _seen) = rig();
|
||||
|
||||
Reference in new issue
Block a user