framework: FacePaint and the preset browser's host closures key through the MemoProps and PlainData derives (covered_by, every_pass) instead of hand impls; the Font Height Reference glyphs are consts, and their segment helper no longer shares egui's stroke-taking name line

This commit is contained in:
bflatastic
2026-11-22 20:16:41 +07:00
parent 35535b7720
commit bd7e55c424
3 changed files with 23 additions and 36 deletions
+7 -7
View File
@@ -391,10 +391,10 @@ const STROKE_SHARE: f32 = 0.2;
#[derive(craft_ui::contract::MemoProps)]
struct FontHeightGlyph(FontHeightReference);
static EM_BOX: FontHeightGlyph = FontHeightGlyph(FontHeightReference::EmBox);
static CAP_HEIGHT: FontHeightGlyph = FontHeightGlyph(FontHeightReference::CapHeight);
static X_HEIGHT: FontHeightGlyph = FontHeightGlyph(FontHeightReference::XHeight);
static ICF_BOX: FontHeightGlyph = FontHeightGlyph(FontHeightReference::IcfBox);
const EM_BOX: FontHeightGlyph = FontHeightGlyph(FontHeightReference::EmBox);
const CAP_HEIGHT: FontHeightGlyph = FontHeightGlyph(FontHeightReference::CapHeight);
const X_HEIGHT: FontHeightGlyph = FontHeightGlyph(FontHeightReference::XHeight);
const ICF_BOX: FontHeightGlyph = FontHeightGlyph(FontHeightReference::IcfBox);
impl Glyph for FontHeightGlyph {
fn paint(&self, painter: &Painter, tokens: &UiTokens, rect: Rect, color: Color32, _mirrored: bool) {
@@ -408,11 +408,11 @@ impl Glyph for FontHeightGlyph {
let boxed = |left: f32, top: f32, right: f32, bottom: f32| {
Shape::closed_line([(left, top), (right, top), (right, bottom), (left, bottom)].map(at).to_vec(), stroke)
};
let line = |from: (f32, f32), to: (f32, f32)| Shape::line_segment([at(from), at(to)], stroke);
let segment = |from: (f32, f32), to: (f32, f32)| Shape::line_segment([at(from), at(to)], stroke);
// A letter between a top guide and the baseline: the guide's height is what the value measures.
let guided = |top: f32| vec![line((0.1, top), (0.9, top)), line((0.1, 0.8), (0.9, 0.8)), line((0.5, top), (0.5, 0.8))];
let guided = |top: f32| vec![segment((0.1, top), (0.9, top)), segment((0.1, 0.8), (0.9, 0.8)), segment((0.5, top), (0.5, 0.8))];
let shapes = match self.0 {
FontHeightReference::EmBox => vec![boxed(0.1, 0.1, 0.9, 0.9), line((0.1, 0.75), (0.9, 0.75))],
FontHeightReference::EmBox => vec![boxed(0.1, 0.1, 0.9, 0.9), segment((0.1, 0.75), (0.9, 0.75))],
FontHeightReference::CapHeight => guided(0.2),
FontHeightReference::XHeight => guided(0.45),
FontHeightReference::IcfBox => vec![boxed(0.2, 0.2, 0.8, 0.8)],
+3 -11
View File
@@ -7,9 +7,11 @@ use std::hash::{Hash, Hasher};
use std::sync::Arc;
/// The one host painter a value carries: two faces with one key are the same value.
#[derive(Clone)]
#[derive(Clone, crate::contract::MemoProps, craft_ui_macros::PlainData)]
pub struct FacePaint {
key: u64,
#[memo_props(covered_by = "key", reason = "the key names what the painter paints")]
#[plain_data(assume = "a face is replaced, never written: its painter is shared")]
paint: Arc<dyn Fn(&ContentPainterCx<'_>, Rect) + Send + Sync>,
}
@@ -44,16 +46,6 @@ impl Hash for FacePaint {
}
}
/// By its key: two faces with one key are the same value.
impl crate::contract::MemoProps for FacePaint {
fn key(&self, key: &mut crate::contract::MemoKey) {
crate::contract::MemoProps::key(&self.key, key);
}
}
/// A face is replaced, never written: its painter is shared and its key names what it paints.
impl crate::contract::PlainData for FacePaint {}
impl fmt::Debug for FacePaint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("FacePaint").field(&self.key).finish()
@@ -21,9 +21,9 @@ use super::words::PresetWords;
use crate::bars::{Bar, BarKind};
use crate::choice::Segmented;
use crate::contract::{
Bind, BodyCx, CollectionEvent, DropAt, DropKind, DropMark, DropPayload, DropPlace, DropTarget, DropZone, Entry, ListDrag, MemoKey, MemoProps,
MemoView, OpenChange, Outbox, Pattern, Props, Rename, ReorderMove, ReorderRule, RowHeight, SelectMode, Show as _, ShowCollection as _,
ShowContainer as _, TooltipSpec, request_rename,
Bind, BodyCx, CollectionEvent, DropAt, DropKind, DropMark, DropPayload, DropPlace, DropTarget, DropZone, Entry, ListDrag, MemoProps, MemoView,
OpenChange, Outbox, Pattern, Props, Rename, ReorderMove, ReorderRule, RowHeight, SelectMode, Show as _, ShowCollection as _, ShowContainer as _,
TooltipSpec, request_rename,
};
use crate::display::Divider;
use crate::layout_components::Stack;
@@ -800,7 +800,11 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
row_menu,
drags,
group_heads,
host: HostFns { tip: tip_painter.as_ref(), row_items: row_items.as_ref(), drops: &drops },
host: (tip_painter.is_some() || row_items.is_some() || !drops.is_empty()).then(|| HostFns {
tip: tip_painter.as_ref(),
row_items: row_items.as_ref(),
drops: &drops,
}),
empty: empty_text.clone(),
sent: Outbox::new(&mut sent),
};
@@ -861,24 +865,14 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
}
/// The host's closures a run of the presets view calls: the tip painter, the row menu's rows and the drop targets.
#[derive(crate::contract::MemoProps)]
#[memo_props(every_pass = "a closure reads what it captured, which no key can feed")]
struct HostFns<'p, 'a, K> {
tip: Option<&'p TipFn<'a, K>>,
row_items: Option<&'p RowItems<'a, K>>,
drops: &'p [PresetDrop<'a, K>],
}
/// A closure reads what it captured, which no key can feed: a view given one runs in every pass. Without them the view
/// keys on its plain props alone.
impl<K> MemoProps for HostFns<'_, '_, K> {
fn key(&self, key: &mut MemoKey) {
let any = self.tip.is_some() || self.row_items.is_some() || !self.drops.is_empty();
key.write(&[u8::from(any)]);
if any {
key.changes_every_pass();
}
}
}
/// Every input of the browser's presets view: the source, the state the toolbar left (the open groups, the search,
/// the view and the zoom), the host's selection, the row menu's target, the browser's plain settings and the host's
/// closures, and an outbox for what a run reports.
@@ -906,7 +900,8 @@ struct ListProps<'p, 'a, K: 'static> {
row_menu: PresetRowMenu,
drags: bool,
group_heads: GroupHeads,
host: HostFns<'p, 'a, K>,
/// The host's closures; `None` without any, so the view keys on its plain props alone.
host: Option<HostFns<'p, 'a, K>>,
empty: Option<Label>,
sent: Outbox<'p, ListRun<K>>,
}
@@ -951,7 +946,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
(*list_id, *view, *zoom, *cell_shape, *tile_size, *columns, *max_rows, *reorder, *drop_zone, *row_menu, *select, *drags, *group_heads);
let label = label.clone();
let actions = props.actions;
let (tip_painter, row_items, drops) = (props.host.tip, props.host.row_items, props.host.drops);
let (tip_painter, row_items, drops) = props.host.as_ref().map_or((None, None, &[][..]), |host| (host.tip, host.row_items, host.drops));
let reveal = props.reveal.clone();
let mut held = props.selection.clone();
let mut menu_target = props.menu_target.clone();