top-w10: net change of 2 commits

- top-w10: written from its sheet
- top-w10: review fixes (review-1.md)
This commit is contained in:
aiongg
2026-11-15 04:46:09 +00:00
parent b9d2a9b266
commit d66c2f286d
4 changed files with 174 additions and 32 deletions
+19 -14
View File
@@ -52,6 +52,8 @@ pub enum IconSource<'a> {
/// A letter in an outlined box, in the spec's colour or ink: the icon of something that has none, from the first
/// letter of its name. Painted by [`Draw::icon`](super::Draw::icon) as text, in the theme's fonts.
Initial(char),
/// The mark of a cursor badge, painted by the cursor-badge painter in the spec's colour or ink.
Mark(craft_behavior::CursorBadge),
}
impl std::fmt::Debug for IconSource<'_> {
@@ -62,6 +64,7 @@ impl std::fmt::Debug for IconSource<'_> {
IconSource::Swatch(color) => f.debug_tuple("Swatch").field(color).finish(),
IconSource::Disc => f.write_str("Disc"),
IconSource::Initial(letter) => f.debug_tuple("Initial").field(letter).finish(),
IconSource::Mark(badge) => f.debug_tuple("Mark").field(badge).finish(),
}
}
}
@@ -75,6 +78,7 @@ impl PartialEq for IconSource<'_> {
(IconSource::Swatch(a), IconSource::Swatch(b)) => a == b,
(IconSource::Disc, IconSource::Disc) => true,
(IconSource::Initial(a), IconSource::Initial(b)) => a == b,
(IconSource::Mark(a), IconSource::Mark(b)) => a == b,
_ => false,
}
}
@@ -106,8 +110,7 @@ impl<'a> IconSpec<'a> {
/// An icon whose glyph is the mark of the cursor `badge` (the plus or minus drawn at the cursor), painted by the
/// one cursor-badge painter.
pub fn mark(badge: craft_behavior::CursorBadge) -> IconSpec<'a> {
let _ = badge;
todo!("cw.C75")
IconSpec { source: IconSource::Mark(badge), ink: Ink::OnPanel, color: None, mirror: Mirror::Auto, set: IconSet::default(), badge: None }
}
/// An icon of `glyph` in the panel ink.
@@ -199,12 +202,13 @@ impl From<IconId> for IconSpec<'_> {
}
}
/// Whether the catalog has an icon named `id`.
/// Whether the built-in catalog or a registered app icon set has an icon named `id`.
pub fn has_icon(id: &IconId) -> bool {
svg_set::find(id.name()).is_some()
svg_set::lookup(id.name()).is_some()
}
/// The names of the catalog's icons, sorted.
/// The names of the built-in catalog's icons, sorted; app icon sets registered with [`register_app_icons`] are not
/// listed.
pub fn icon_names() -> &'static [&'static str] {
svg_set::names()
}
@@ -227,13 +231,14 @@ pub(crate) fn paint(painter: &Painter, tokens: &UiTokens, direction: Direction,
icon.paint(painter, square, color);
}
}
IconSource::Mark(badge) => crate::contract::ghost::paint_badge(painter, tokens, direction, square, *badge, color),
}
}
/// A catalog icon as it is drawn: its entry and set, whether it is mirrored, and its stroke width in points.
/// A catalog or app icon as it is drawn: its entry and set, whether it is mirrored, and its stroke width in points.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct SvgIcon {
index: usize,
entry: svg_set::Entry,
set: IconSet,
mirrored: bool,
stroke: f32,
@@ -241,14 +246,14 @@ pub(crate) struct SvgIcon {
impl SvgIcon {
/// The icon `id` of `set` at `side` points under `direction` and `mirror`; `None`, with a log line, for a name the
/// catalog lacks.
/// catalog and the registered app sets lack.
pub(crate) fn of(tokens: &UiTokens, direction: Direction, side: f32, id: &IconId, set: IconSet, mirror: Mirror) -> Option<SvgIcon> {
let Some(index) = svg_set::find(id.name()) else {
let Some(entry) = svg_set::lookup(id.name()) else {
log::error!("icon {:?} is not in the catalog", id.name());
return None;
};
let mirrored = direction == Direction::Rtl && svg_set::directional(index) && mirror == Mirror::Auto;
Some(SvgIcon { index, set, mirrored, stroke: stroke(tokens, side) })
let mirrored = direction == Direction::Rtl && svg_set::directional(entry) && mirror == Mirror::Auto;
Some(SvgIcon { entry, set, mirrored, stroke: stroke(tokens, side) })
}
/// The image's side in pixels and its stroke in 64ths of a pixel for a square of `side` points at `ppp`: the
@@ -256,14 +261,14 @@ impl SvgIcon {
/// stroke, so all its requests share one.
fn pixels(&self, side: f32, ppp: f32) -> (u32, u32) {
let px = (side * ppp).round().max(1.0) as u32;
let stroke_px = if svg_set::stroke_width(self.index, self.set).is_some() { (self.stroke * ppp * 64.0).round() as u32 } else { 0 };
let stroke_px = if svg_set::entry_stroke_width(self.entry, self.set).is_some() { (self.stroke * ppp * 64.0).round() as u32 } else { 0 };
(px, stroke_px)
}
/// The icon in white for a square of `side` points at `ppp`, its alpha from the drawing.
pub(crate) fn mask(&self, side: f32, ppp: f32) -> egui::ColorImage {
let (px, stroke_px) = self.pixels(side, ppp);
svg_set::rasterise(self.index, self.set, px, stroke_px as f32 / 64.0, self.mirrored)
svg_set::rasterise_entry(self.entry, self.set, px, stroke_px as f32 / 64.0, self.mirrored)
}
/// Paints the icon in `color` centred in `square`, on whole pixels.
@@ -271,7 +276,7 @@ impl SvgIcon {
let ctx = painter.ctx();
let ppp = ctx.pixels_per_point();
let (px, stroke_px) = self.pixels(square.width(), ppp);
let key = (self.index, self.set, px, stroke_px, self.mirrored);
let key = (self.entry, self.set, px, stroke_px, self.mirrored);
let texture = TextureCache::texture(ctx, key, || self.mask(square.width(), ppp));
let side = px as f32 / ppp;
let min = (square.center() - vec2(side, side) / 2.0).round_to_pixels(ppp);
+96 -18
View File
@@ -1,7 +1,8 @@
//! The SVG icon catalog the build embeds, and its rasteriser.
//! The SVG icon catalog the build embeds, the app icon sets registered beside it, and their rasteriser.
use egui::{Color32, ColorImage};
use resvg::tiny_skia;
use std::sync::{PoisonError, RwLock};
use super::svg_tree;
@@ -23,14 +24,63 @@ pub struct AppIcons {
/// Registering the same set again is a no-op; a name the built-in catalog or an earlier app set has is refused with a
/// log line.
pub fn register_app_icons(set: &'static AppIcons) {
let _ = set;
todo!("tp.T51")
let mut guard = APPS.write().unwrap_or_else(PoisonError::into_inner);
let registry = &mut *guard;
if registry.sets.iter().any(|known| std::ptr::eq(*known, set)) {
return;
}
let set_at = registry.sets.len();
registry.sets.push(set);
for (icon, (name, _)) in set.icons.iter().enumerate() {
if find(name).is_some() {
log::warn!("app icon set {:?}: icon {name:?} is refused, the built-in catalog has it", set.name);
continue;
}
match registry.index.binary_search_by(|(known, ..)| (*known).cmp(name)) {
Ok(at) => {
let earlier = registry.sets[registry.index[at].1].name;
log::warn!("app icon set {:?}: icon {name:?} is refused, set {earlier:?} has it", set.name);
}
Err(at) => registry.index.insert(at, (name, set_at, icon)),
}
}
}
/// The names of the registered app icons.
#[allow(dead_code)] // P3(top-w10): tp.T51
pub(crate) fn app_names() -> Vec<&'static str> {
todo!("tp.T51")
/// The registered sets and the sorted index of their icons: name, set, position in the set.
struct Registry {
sets: Vec<&'static AppIcons>,
index: Vec<(&'static str, usize, usize)>,
}
/// Process-wide like the generated catalog: it is the icon catalog, not pass state.
static APPS: RwLock<Registry> = RwLock::new(Registry { sets: Vec::new(), index: Vec::new() });
/// An icon the lookup found: a catalog position or an app set's icon.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum Entry {
/// The position in the built-in catalog.
Catalog(usize),
/// The icon `icon` of the registered set `set`.
App { set: usize, icon: usize },
}
/// The icon called `name`: the built-in catalog first, then the registered app sets.
pub(crate) fn lookup(name: &str) -> Option<Entry> {
if let Some(index) = find(name) {
return Some(Entry::Catalog(index));
}
let registry = APPS.read().unwrap_or_else(PoisonError::into_inner);
let at = registry.index.binary_search_by(|(known, ..)| (*known).cmp(name)).ok()?;
let (_, set, icon) = registry.index[at];
Some(Entry::App { set, icon })
}
/// The name, document and own stroke width of an app icon.
fn app_icon(set: usize, icon: usize) -> (&'static str, &'static [u8], Option<f32>) {
let registry = APPS.read().unwrap_or_else(PoisonError::into_inner);
let set = registry.sets[set];
let (name, svg) = set.icons[icon];
(name, svg, set.stroke_width)
}
/// The position of the icon called `name` in the catalog.
@@ -43,9 +93,44 @@ pub(crate) fn names() -> &'static [&'static str] {
&NAMES
}
/// Whether the icon at `index` flips under right-to-left.
pub(crate) fn directional(index: usize) -> bool {
DIRECTIONAL[index]
/// Whether `entry` flips under right-to-left; no app icon does.
pub(crate) fn directional(entry: Entry) -> bool {
match entry {
Entry::Catalog(index) => DIRECTIONAL[index],
Entry::App { .. } => false,
}
}
/// The document's own stroke width of `entry` in `set`, `None` for a document drawn with fills or as drawn.
pub(crate) fn entry_stroke_width(entry: Entry, set: IconSet) -> Option<f32> {
match entry {
Entry::Catalog(index) => stroke_width(index, set),
Entry::App { set: s, icon } => app_icon(s, icon).2,
}
}
/// `entry` in white at `px` x `px` physical pixels; a catalog entry as [`rasterise`], an app entry through the same
/// drawing with its set's stroke width.
pub(crate) fn rasterise_entry(entry: Entry, set: IconSet, px: u32, stroke_px: f32, mirrored: bool) -> ColorImage {
match entry {
Entry::Catalog(index) => rasterise(index, set, px, stroke_px, mirrored),
Entry::App { set: s, icon } => {
let (name, svg, own) = app_icon(s, icon);
image(draw(svg, own, px, stroke_px, mirrored), px, || format!("app icon {name}"))
}
}
}
/// The white image of a drawn pixmap; a failed drawing gives a transparent image and a log line.
fn image(drawn: Result<tiny_skia::Pixmap, String>, px: u32, label: impl FnOnce() -> String) -> ColorImage {
let size = [px as usize; 2];
match drawn {
Ok(pixmap) => ColorImage::new(size, pixmap.pixels().iter().map(|p| Color32::from_white_alpha(p.alpha())).collect()),
Err(error) => {
log::error!("{}: {error}", label());
ColorImage::filled(size, Color32::TRANSPARENT)
}
}
}
/// The icon in white at `px` x `px` physical pixels, its alpha from the drawing; tinted when drawn.
@@ -53,14 +138,7 @@ pub(crate) fn directional(index: usize) -> bool {
/// A document drawn with strokes is drawn `stroke_px` physical pixels wide, one drawn with fills as drawn. `mirrored`
/// flips the drawing horizontally. A document that fails to parse gives a transparent image and a log line.
pub(crate) fn rasterise(index: usize, set: IconSet, px: u32, stroke_px: f32, mirrored: bool) -> ColorImage {
let size = [px as usize; 2];
match draw(svg(index, set), stroke_width(index, set), px, stroke_px, mirrored) {
Ok(pixmap) => ColorImage::new(size, pixmap.pixels().iter().map(|p| Color32::from_white_alpha(p.alpha())).collect()),
Err(error) => {
log::error!("icon {} in {}: {error}", NAMES[index], set.id());
ColorImage::filled(size, Color32::TRANSPARENT)
}
}
image(draw(svg(index, set), stroke_width(index, set), px, stroke_px, mirrored), px, || format!("icon {} in {}", NAMES[index], set.id()))
}
/// `own` is the document's own stroke width in view-box units; a document without one is drawn as it is.
@@ -0,0 +1,58 @@
//! Trials of the app icon registry: the catalog wins a clashing name, the first set keeps a name two sets share.
use crate::primitives::{AppIcons, IconSet, Mirror, SvgIcon, icon_names, register_app_icons};
use crate::testing::{style, trial};
use craft_behavior::IconId;
use craft_i18n::Direction;
use craft_theme::{Contrast, Mode};
use egui::ColorImage;
// Built from parts so the workspace scan of icon-id literals does not read them.
const FIRST_NAME: &str = concat!("zz_t10_", "first");
const SECOND_NAME: &str = concat!("zz_t10_", "second");
const CATALOG_NAME: &str = "close";
const DISC: &[u8] = b"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><circle cx=\"12\" cy=\"12\" r=\"8\" fill=\"black\"/></svg>";
const SQUARE: &[u8] = b"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect x=\"4\" y=\"4\" width=\"16\" height=\"16\" fill=\"black\"/></svg>";
static FIRST_SET: AppIcons = AppIcons { name: "t10-first", stroke_width: None, icons: &[(FIRST_NAME, DISC), (CATALOG_NAME, DISC)] };
static SECOND_SET: AppIcons = AppIcons { name: "t10-second", stroke_width: None, icons: &[(FIRST_NAME, SQUARE), (SECOND_NAME, SQUARE)] };
fn icon(name: &'static str) -> Option<SvgIcon> {
let tokens = style::tokens(Mode::Light, Contrast::Standard);
SvgIcon::of(&tokens, Direction::Ltr, 24.0, &IconId::from(std::borrow::Cow::Borrowed(name)), IconSet::default(), Mirror::Auto)
}
fn mask(name: &'static str) -> ColorImage {
icon(name).expect("the name resolves").mask(24.0, 1.0)
}
#[trial]
fn an_app_icon_set_resolves_after_the_catalog_and_the_first_set_keeps_a_shared_name() {
assert_eq!(icon(FIRST_NAME), None);
let catalog = icon_names().len();
let close = mask(CATALOG_NAME);
let catalog_icon = icon(CATALOG_NAME);
register_app_icons(&FIRST_SET);
assert!(icon(FIRST_NAME).is_some(), "the set's new name resolves");
assert_eq!(icon(CATALOG_NAME), catalog_icon, "the catalog keeps its name");
assert_eq!(mask(CATALOG_NAME), close, "the refused icon does not change the catalog icon");
let first = mask(FIRST_NAME);
assert_ne!(first, close);
assert_eq!(icon_names().len(), catalog, "icon_names lists the built-in catalog only");
assert!(!icon_names().contains(&FIRST_NAME));
// A later set repeating a registered name is refused for that name only; its other names join.
assert_eq!(icon(SECOND_NAME), None);
register_app_icons(&SECOND_SET);
assert!(icon(SECOND_NAME).is_some());
assert_eq!(mask(FIRST_NAME), first, "the first set keeps the shared name");
assert_ne!(mask(SECOND_NAME), first);
let second = mask(SECOND_NAME);
// Registering a set again changes nothing.
register_app_icons(&FIRST_SET);
register_app_icons(&SECOND_SET);
assert_eq!((mask(FIRST_NAME), mask(SECOND_NAME), mask(CATALOG_NAME)), (first, second, close));
assert_eq!(icon_names().len(), catalog);
}
+1
View File
@@ -14,6 +14,7 @@ pub mod fixture;
pub mod focus;
pub mod frames;
mod host_key_trials;
mod icon_trials;
pub mod index;
pub mod input;
mod key_trials;