Skip to main content

egui_pop_core/
icon.rs

1//! Icons by role: an [`Icon`] names what the icon means (`Check`,
2//! `Search`, `Warning`), never a codepoint. With the `phosphor` feature
3//! the [Phosphor](https://phosphoricons.com) font (through
4//! `egui-phosphor`, MIT) is added to the context as a family of its own
5//! and icons are drawn from it; without it, or for the marks that are
6//! always painted, the icon is painted with lines and fills. Either way
7//! the icon is sized by its [`Size`] and coloured by its [`Tone`] from
8//! the theme's tokens.
9//!
10//! A [`Source`] is what an [`IconWidget`] draws: an icon by role, a glyph
11//! from any font, or an image.
12//!
13//! Icons are declared by **name** wherever a declaration has no egui (a
14//! command, a panel, a place): one vocabulary of egui-pop's names, the
15//! Phosphor glyphs' (`phosphor:<name>`) and the registered ones
16//! (`<plugin-id>:<name>`), resolved once and checked, the unknown ones
17//! reported ([`registry`]). Widgets taking an icon accept a name too
18//! ([`IconRef`]).
19//!
20//! ```
21//! use egui_pop_core::icon::{Icon, Size, Tone};
22//!
23//! fn toolbar(ui: &mut egui::Ui) {
24//!     let close = Icon::Close.size(Size::Toolbar).label("Close");
25//!     if ui.add(close.sense_click()).clicked() { /* ... */ }
26//!     ui.add(Icon::Warning.tone(Tone::Warning).label("Unsaved changes"));
27//! }
28//! ```
29
30use std::f32::consts::PI;
31
32use egui::{
33    Color32, Context, FontFamily, FontId, ImageSource, Painter, Pos2, Rect, Response, Role, Sense,
34    Stroke, StrokeKind, Ui, Widget, WidgetInfo, pos2, vec2,
35};
36
37use crate::kit::{KitCx, Part, State};
38use crate::theme::Tokens;
39
40pub mod registry;
41
42pub use registry::{IconRef, PLACEHOLDER, PhosphorIcon};
43
44/// The family the icon font is installed under (`phosphor` feature).
45pub const FAMILY: &str = "egui-pop-icons";
46
47/// What an icon means. Each has a glyph in the icon font (with the
48/// `phosphor` feature) and a painted fallback.
49///
50/// Need (icon): an icon by name.
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
52#[non_exhaustive]
53pub enum Icon {
54    /// A tick: a checked menu item, a passed check.
55    Check,
56    /// A submenu, a collapsed section.
57    ChevronRight,
58    /// An open section, a drop-down.
59    ChevronDown,
60    ChevronUp,
61    ChevronLeft,
62    /// Close, dismiss, clear.
63    Close,
64    Plus,
65    Minus,
66    /// A small filled dot: a changed value, a status.
67    Dot,
68    /// A small filled diamond: a value moved by someone else.
69    Diamond,
70    Search,
71    Settings,
72    Folder,
73    File,
74    /// Up one folder.
75    Up,
76    Info,
77    Warning,
78    Error,
79    Success,
80    Undo,
81    Redo,
82    /// Back to the default.
83    Reset,
84    Copy,
85    /// Cut: scissors.
86    Cut,
87    /// Paste: a clipboard.
88    Paste,
89    /// Paste with a choice (format, transform, parts): a clipboard
90    /// holding lines.
91    PasteSpecial,
92    Delete,
93    Keyboard,
94    /// The command palette.
95    Command,
96    /// A menu (a list).
97    Menu,
98    /// More actions.
99    More,
100    Edit,
101    Help,
102    Save,
103    Fullscreen,
104    ExitFullscreen,
105    Notification,
106    /// Light and dark.
107    Theme,
108    Visible,
109    Hidden,
110    /// Recent items.
111    History,
112    Home,
113    /// Locked: not to be changed.
114    Lock,
115    /// Unlocked: free to change.
116    Unlock,
117    /// An eyedropper: pick a value or a colour from the image.
118    Picker,
119    /// A filled triangle pointing to the end: a closed branch (always
120    /// painted, like the dot).
121    TriangleRight,
122    /// A filled triangle pointing down: an open branch, a descending sort.
123    TriangleDown,
124    /// A filled triangle pointing up: an ascending sort.
125    TriangleUp,
126    /// Two dots one above the other: something to grab or to open (a
127    /// folded area's strip); always painted.
128    Grip,
129    /// A window's buttons, drawn by the application: a line, a square,
130    /// two squares, a cross, on the pixel grid; always painted.
131    WindowMinimize,
132    WindowMaximize,
133    WindowRestore,
134    WindowClose,
135    /// Marks filling their square, always painted: what a dot, an LED or
136    /// a flag shows by default (the kit's `led-*` and `flag-*` glyph
137    /// roles). A hollow ring.
138    Circle,
139    /// A filled disc.
140    Disc,
141    /// A filled square.
142    Square,
143    /// A filled triangle, point up.
144    Triangle,
145    /// Three crossed strokes.
146    Asterisk,
147    /// A triangle filling a corner of its square (the top start one when
148    /// drawn alone; a corner flag turns it to its corner).
149    Corner,
150    /// A glyph of the Phosphor font by name (`phosphor:grid-four`,
151    /// [`registry`]); only with the `phosphor` feature.
152    Phosphor(PhosphorIcon),
153    /// The window's layout, as a frame of areas: a button opening the
154    /// layout's choices (VS Code's "Customize layout"); always painted.
155    Layout,
156    /// A frame with its left area filled: that area shows (a toggle on).
157    /// The `-off` icons outline the area instead: it is folded or hidden.
158    /// Always painted, on the pixel grid.
159    LayoutLeft,
160    LayoutLeftOff,
161    /// A frame with its bottom area filled, or outlined (`-off`).
162    LayoutBottom,
163    LayoutBottomOff,
164    /// A frame with its right area filled, or outlined (`-off`).
165    LayoutRight,
166    LayoutRightOff,
167    /// An icon the application registered (`icon_set::register`): an SVG
168    /// file, drawn from textures made when the theme is installed.
169    Custom(crate::icon_set::CustomIcon),
170}
171
172impl Icon {
173    /// Every icon, in declaration order (the gallery shows them).
174    pub const ALL: &'static [Icon] = &[
175        Icon::Check,
176        Icon::ChevronRight,
177        Icon::ChevronDown,
178        Icon::ChevronUp,
179        Icon::ChevronLeft,
180        Icon::Close,
181        Icon::Plus,
182        Icon::Minus,
183        Icon::Dot,
184        Icon::Diamond,
185        Icon::Search,
186        Icon::Settings,
187        Icon::Folder,
188        Icon::File,
189        Icon::Up,
190        Icon::Info,
191        Icon::Warning,
192        Icon::Error,
193        Icon::Success,
194        Icon::Undo,
195        Icon::Redo,
196        Icon::Reset,
197        Icon::Copy,
198        Icon::Cut,
199        Icon::Paste,
200        Icon::PasteSpecial,
201        Icon::Delete,
202        Icon::Keyboard,
203        Icon::Command,
204        Icon::Menu,
205        Icon::More,
206        Icon::Edit,
207        Icon::Help,
208        Icon::Save,
209        Icon::Fullscreen,
210        Icon::ExitFullscreen,
211        Icon::Notification,
212        Icon::Theme,
213        Icon::Visible,
214        Icon::Hidden,
215        Icon::History,
216        Icon::Home,
217        Icon::Lock,
218        Icon::Unlock,
219        Icon::Picker,
220        Icon::TriangleRight,
221        Icon::TriangleDown,
222        Icon::TriangleUp,
223        Icon::Grip,
224        Icon::WindowMinimize,
225        Icon::WindowMaximize,
226        Icon::WindowRestore,
227        Icon::WindowClose,
228        Icon::Circle,
229        Icon::Disc,
230        Icon::Square,
231        Icon::Triangle,
232        Icon::Asterisk,
233        Icon::Corner,
234        Icon::Layout,
235        Icon::LayoutLeft,
236        Icon::LayoutLeftOff,
237        Icon::LayoutBottom,
238        Icon::LayoutBottomOff,
239        Icon::LayoutRight,
240        Icon::LayoutRightOff,
241    ];
242
243    /// The icon of a name (`settings`), as [`Icon::name`] gives it:
244    /// egui-pop's, then the Phosphor glyphs' (`phosphor:<name>`), then
245    /// the registered ones (`icon_set::register`). Resolved once
246    /// ([`registry::lookup`]); [`registry::resolve`] also reports a name
247    /// that does not resolve.
248    pub fn by_name(name: &str) -> Option<Icon> {
249        registry::lookup(name)
250    }
251
252    /// Its name in kebab case (`chevron-right`): for the debug bridge and
253    /// the gallery.
254    pub fn name(self) -> &'static str {
255        match self {
256            Icon::Check => "check",
257            Icon::ChevronRight => "chevron-right",
258            Icon::ChevronDown => "chevron-down",
259            Icon::ChevronUp => "chevron-up",
260            Icon::ChevronLeft => "chevron-left",
261            Icon::Close => "close",
262            Icon::Plus => "plus",
263            Icon::Minus => "minus",
264            Icon::Dot => "dot",
265            Icon::Diamond => "diamond",
266            Icon::Search => "search",
267            Icon::Settings => "settings",
268            Icon::Folder => "folder",
269            Icon::File => "file",
270            Icon::Up => "up",
271            Icon::Info => "info",
272            Icon::Warning => "warning",
273            Icon::Error => "error",
274            Icon::Success => "success",
275            Icon::Undo => "undo",
276            Icon::Redo => "redo",
277            Icon::Reset => "reset",
278            Icon::Copy => "copy",
279            Icon::Cut => "cut",
280            Icon::Paste => "paste",
281            Icon::PasteSpecial => "paste-special",
282            Icon::Delete => "delete",
283            Icon::Keyboard => "keyboard",
284            Icon::Command => "command",
285            Icon::Menu => "menu",
286            Icon::More => "more",
287            Icon::Edit => "edit",
288            Icon::Help => "help",
289            Icon::Save => "save",
290            Icon::Fullscreen => "fullscreen",
291            Icon::ExitFullscreen => "exit-fullscreen",
292            Icon::Notification => "notification",
293            Icon::Theme => "theme",
294            Icon::Visible => "visible",
295            Icon::Hidden => "hidden",
296            Icon::History => "history",
297            Icon::Home => "home",
298            Icon::Lock => "lock",
299            Icon::Unlock => "unlock",
300            Icon::Picker => "picker",
301            Icon::TriangleRight => "triangle-right",
302            Icon::TriangleDown => "triangle-down",
303            Icon::TriangleUp => "triangle-up",
304            Icon::Grip => "grip",
305            Icon::WindowMinimize => "window-minimize",
306            Icon::WindowMaximize => "window-maximize",
307            Icon::WindowRestore => "window-restore",
308            Icon::WindowClose => "window-close",
309            Icon::Circle => "circle",
310            Icon::Disc => "disc",
311            Icon::Square => "square",
312            Icon::Triangle => "triangle",
313            Icon::Asterisk => "asterisk",
314            Icon::Corner => "corner",
315            Icon::Phosphor(p) => p.name(),
316            Icon::Layout => "layout",
317            Icon::LayoutLeft => "layout-left",
318            Icon::LayoutLeftOff => "layout-left-off",
319            Icon::LayoutBottom => "layout-bottom",
320            Icon::LayoutBottomOff => "layout-bottom-off",
321            Icon::LayoutRight => "layout-right",
322            Icon::LayoutRightOff => "layout-right-off",
323            Icon::Custom(c) => crate::icon_set::name(c),
324        }
325    }
326
327    /// Its glyph in the icon font; `None` without the `phosphor` feature,
328    /// and for the marks that are always painted (dot, diamond, the
329    /// triangles).
330    pub fn glyph(self) -> Option<&'static str> {
331        glyph(self)
332    }
333
334    /// Sized for a place.
335    pub fn size(self, size: Size) -> IconWidget {
336        IconWidget::new(self).size(size)
337    }
338
339    /// Coloured by a tone.
340    pub fn tone(self, tone: Tone) -> IconWidget {
341        IconWidget::new(self).tone(tone)
342    }
343
344    /// With an accessible name.
345    pub fn label(self, label: &str) -> IconWidget {
346        IconWidget::new(self).label(label)
347    }
348
349    /// Paints the icon in a square: the font's glyph when the icon font
350    /// is installed in this context, else the painted shape.
351    pub fn paint(self, painter: &Painter, rect: Rect, color: Color32) {
352        if let Icon::Custom(c) = self {
353            crate::icon_set::paint(painter, c, rect, color);
354            return;
355        }
356        match self.glyph() {
357            Some(g) if font_ready(painter.ctx()) => {
358                painter.text(
359                    rect.center(),
360                    egui::Align2::CENTER_CENTER,
361                    g,
362                    FontId::new(rect.height(), FontFamily::Name(FAMILY.into())),
363                    color,
364                );
365            }
366            _ => paint_fallback(painter, self, rect, color),
367        }
368    }
369}
370
371#[cfg(feature = "phosphor")]
372fn glyph(icon: Icon) -> Option<&'static str> {
373    use egui_phosphor::regular as p;
374    Some(match icon {
375        Icon::Check => p::CHECK,
376        Icon::ChevronRight => p::CARET_RIGHT,
377        Icon::ChevronDown => p::CARET_DOWN,
378        Icon::ChevronUp => p::CARET_UP,
379        Icon::ChevronLeft => p::CARET_LEFT,
380        Icon::Close => p::X,
381        Icon::Plus => p::PLUS,
382        Icon::Minus => p::MINUS,
383        Icon::Dot
384        | Icon::Diamond
385        | Icon::TriangleRight
386        | Icon::TriangleDown
387        | Icon::TriangleUp
388        | Icon::Grip
389        | Icon::WindowMinimize
390        | Icon::WindowMaximize
391        | Icon::WindowRestore
392        | Icon::WindowClose
393        | Icon::Circle
394        | Icon::Disc
395        | Icon::Square
396        | Icon::Triangle
397        | Icon::Asterisk
398        | Icon::Corner
399        | Icon::Layout
400        | Icon::LayoutLeft
401        | Icon::LayoutLeftOff
402        | Icon::LayoutBottom
403        | Icon::LayoutBottomOff
404        | Icon::LayoutRight
405        | Icon::LayoutRightOff
406        | Icon::Custom(_) => return None,
407        Icon::Phosphor(g) => return g.glyph(),
408        Icon::Search => p::MAGNIFYING_GLASS,
409        Icon::Settings => p::GEAR_SIX,
410        Icon::Folder => p::FOLDER,
411        Icon::File => p::FILE,
412        Icon::Up => p::ARROW_UP,
413        Icon::Info => p::INFO,
414        Icon::Warning => p::WARNING,
415        Icon::Error => p::X_CIRCLE,
416        Icon::Success => p::CHECK_CIRCLE,
417        Icon::Undo => p::ARROW_U_UP_LEFT,
418        Icon::Redo => p::ARROW_U_UP_RIGHT,
419        Icon::Reset => p::ARROW_COUNTER_CLOCKWISE,
420        Icon::Copy => p::COPY,
421        Icon::Cut => p::SCISSORS,
422        Icon::Paste => p::CLIPBOARD,
423        Icon::PasteSpecial => p::CLIPBOARD_TEXT,
424        Icon::Delete => p::TRASH,
425        Icon::Keyboard => p::KEYBOARD,
426        Icon::Command => p::COMMAND,
427        Icon::Menu => p::LIST,
428        Icon::More => p::DOTS_THREE,
429        Icon::Edit => p::PENCIL_SIMPLE,
430        Icon::Help => p::QUESTION,
431        Icon::Save => p::FLOPPY_DISK,
432        Icon::Fullscreen => p::CORNERS_OUT,
433        Icon::ExitFullscreen => p::CORNERS_IN,
434        Icon::Notification => p::BELL,
435        Icon::Theme => p::CIRCLE_HALF,
436        Icon::Visible => p::EYE,
437        Icon::Hidden => p::EYE_SLASH,
438        Icon::History => p::CLOCK_COUNTER_CLOCKWISE,
439        Icon::Home => p::HOUSE,
440        Icon::Lock => p::LOCK_SIMPLE,
441        Icon::Unlock => p::LOCK_SIMPLE_OPEN,
442        Icon::Picker => p::EYEDROPPER,
443    })
444}
445
446#[cfg(not(feature = "phosphor"))]
447fn glyph(_icon: Icon) -> Option<&'static str> {
448    None
449}
450
451/// Adds the icon font to a context, in a family of its own (the text
452/// fonts are untouched). In use from the next pass; adding it again does
453/// nothing. Called by `theme::install`; does nothing without the
454/// `phosphor` feature.
455pub fn install(ctx: &Context) {
456    #[cfg(feature = "phosphor")]
457    {
458        use egui::epaint::text::{FontInsert, FontPriority, InsertFontFamily};
459        ctx.add_font(FontInsert::new(
460            FAMILY,
461            egui::FontData::from_static(egui_phosphor::Variant::Regular.font_bytes()),
462            vec![InsertFontFamily {
463                family: FontFamily::Name(FAMILY.into()),
464                priority: FontPriority::Highest,
465            }],
466        ));
467    }
468    #[cfg(not(feature = "phosphor"))]
469    let _ = ctx;
470}
471
472/// Puts the icon font, in its family, into font definitions being built
473/// (`fonts::install` replaces egui's definitions and keeps it this way).
474/// Does nothing without the `phosphor` feature.
475pub fn insert(defs: &mut egui::FontDefinitions) {
476    #[cfg(feature = "phosphor")]
477    {
478        defs.font_data.insert(
479            FAMILY.into(),
480            std::sync::Arc::new(egui::FontData::from_static(
481                egui_phosphor::Variant::Regular.font_bytes(),
482            )),
483        );
484        defs.families
485            .insert(FontFamily::Name(FAMILY.into()), vec![FAMILY.into()]);
486    }
487    #[cfg(not(feature = "phosphor"))]
488    let _ = defs;
489}
490
491/// Whether the icon font is in use in this context.
492pub fn font_ready(ctx: &Context) -> bool {
493    cfg!(feature = "phosphor")
494        && ctx.fonts(|f| {
495            f.definitions()
496                .families
497                .contains_key(&FontFamily::Name(FAMILY.into()))
498        })
499}
500
501/// Paints one of the marks that fill their square ([`Icon::Circle`],
502/// `Disc`, `Square`, `Triangle`, `Asterisk`, `Corner`, and the
503/// [`Icon::Diamond`]) at the full size of `rect`, a ring `ring` points
504/// thick; `false` for any other icon, which is left unpainted. Dots, LEDs
505/// and flags draw their default shapes with it.
506pub fn paint_mark(p: &Painter, icon: Icon, rect: Rect, color: Color32, ring: f32) -> bool {
507    let c = rect.center();
508    let s = rect.width().min(rect.height());
509    let r = s / 2.0;
510    match icon {
511        Icon::Circle => {
512            p.circle_stroke(c, r - ring / 2.0, Stroke::new(ring, color));
513        }
514        Icon::Disc => {
515            p.circle_filled(c, r, color);
516        }
517        Icon::Square => {
518            p.rect_filled(
519                Rect::from_center_size(c, egui::Vec2::splat(r * 1.7)),
520                egui::CornerRadius::same(1),
521                color,
522            );
523        }
524        Icon::Triangle => {
525            p.add(egui::Shape::convex_polygon(
526                vec![
527                    pos2(c.x, c.y - r),
528                    pos2(c.x + r, c.y + r * 0.8),
529                    pos2(c.x - r, c.y + r * 0.8),
530                ],
531                color,
532                Stroke::NONE,
533            ));
534        }
535        Icon::Diamond => {
536            p.add(egui::Shape::convex_polygon(
537                vec![
538                    pos2(c.x, c.y - r),
539                    pos2(c.x + r, c.y),
540                    pos2(c.x, c.y + r),
541                    pos2(c.x - r, c.y),
542                ],
543                color,
544                Stroke::NONE,
545            ));
546        }
547        Icon::Asterisk => {
548            let stroke = Stroke::new((s / 5.0).max(1.0), color);
549            for k in 0..3 {
550                let a = std::f32::consts::FRAC_PI_2 + k as f32 * PI / 3.0;
551                let d = vec2(a.cos(), a.sin()) * r;
552                p.line_segment([c - d, c + d], stroke);
553            }
554        }
555        Icon::Corner => {
556            p.add(egui::Shape::convex_polygon(
557                vec![rect.left_top(), rect.right_top(), rect.left_bottom()],
558                color,
559                Stroke::NONE,
560            ));
561        }
562        _ => return false,
563    }
564    true
565}
566
567/// Paints an icon's fallback shape, whatever the fonts. Its strokes are
568/// whole physical pixels at the painter's scale.
569pub fn paint_fallback(p: &Painter, icon: Icon, rect: Rect, color: Color32) {
570    let r = rect.shrink(rect.width() * 0.16);
571    let len = r.width();
572    // The weight shapes are drawn to; strokes snap it to whole physical
573    // pixels at the painter's scale, so lines stay crisp.
574    let w = (rect.width() / 11.0).max(1.25);
575    let ppp = p.pixels_per_point();
576    let stroke = Stroke::new(crate::scale::snap(w, ppp), color);
577    let at = |x: f32, y: f32| -> Pos2 { pos2(r.left() + x * len, r.top() + y * r.height()) };
578    let arc = |from: f32, to: f32, radius: f32, centre: Pos2| -> Vec<Pos2> {
579        (0..=16)
580            .map(|i| {
581                let a = from + (to - from) * i as f32 / 16.0;
582                centre + vec2(a.cos(), a.sin()) * radius
583            })
584            .collect()
585    };
586    let outline = |pts: Vec<Pos2>| {
587        p.add(egui::Shape::closed_line(pts, stroke));
588    };
589    let frame = |a: Pos2, b: Pos2| {
590        p.rect_stroke(Rect::from_min_max(a, b), 1.0, stroke, StrokeKind::Middle);
591    };
592    let ring = || p.circle_stroke(r.center(), len * 0.48, stroke);
593    let point = |x: f32, y: f32| p.circle_filled(at(x, y), w * 0.8, color);
594    match icon {
595        Icon::Check => {
596            p.line(vec![at(0.05, 0.55), at(0.38, 0.85), at(0.95, 0.15)], stroke);
597        }
598        Icon::ChevronRight => {
599            p.line(vec![at(0.35, 0.1), at(0.75, 0.5), at(0.35, 0.9)], stroke);
600        }
601        Icon::ChevronLeft => {
602            p.line(vec![at(0.65, 0.1), at(0.25, 0.5), at(0.65, 0.9)], stroke);
603        }
604        Icon::ChevronDown => {
605            p.line(vec![at(0.1, 0.35), at(0.5, 0.75), at(0.9, 0.35)], stroke);
606        }
607        Icon::ChevronUp => {
608            p.line(vec![at(0.1, 0.65), at(0.5, 0.25), at(0.9, 0.65)], stroke);
609        }
610        Icon::Close => {
611            p.line_segment([at(0.1, 0.1), at(0.9, 0.9)], stroke);
612            p.line_segment([at(0.9, 0.1), at(0.1, 0.9)], stroke);
613        }
614        Icon::Plus => {
615            p.line_segment([at(0.5, 0.05), at(0.5, 0.95)], stroke);
616            p.line_segment([at(0.05, 0.5), at(0.95, 0.5)], stroke);
617        }
618        Icon::Minus => {
619            p.line_segment([at(0.05, 0.5), at(0.95, 0.5)], stroke);
620        }
621        Icon::Dot => {
622            p.circle_filled(rect.center(), len * 0.3, color);
623        }
624        Icon::Diamond => {
625            p.add(egui::Shape::convex_polygon(
626                vec![at(0.5, 0.05), at(0.95, 0.5), at(0.5, 0.95), at(0.05, 0.5)],
627                color,
628                Stroke::NONE,
629            ));
630        }
631        Icon::Search => {
632            p.circle_stroke(at(0.42, 0.42), len * 0.32, stroke);
633            p.line_segment([at(0.66, 0.66), at(0.95, 0.95)], stroke);
634        }
635        Icon::Settings => {
636            p.circle_stroke(r.center(), len * 0.16, stroke);
637            p.circle_stroke(r.center(), len * 0.32, stroke);
638            for i in 0..8 {
639                let a = i as f32 * PI / 4.0;
640                let d = vec2(a.cos(), a.sin());
641                p.line_segment(
642                    [r.center() + d * len * 0.32, r.center() + d * len * 0.48],
643                    stroke,
644                );
645            }
646        }
647        Icon::Folder => outline(vec![
648            at(0.0, 0.18),
649            at(0.38, 0.18),
650            at(0.48, 0.3),
651            at(1.0, 0.3),
652            at(1.0, 0.88),
653            at(0.0, 0.88),
654        ]),
655        Icon::File => {
656            outline(vec![
657                at(0.15, 0.0),
658                at(0.62, 0.0),
659                at(0.88, 0.26),
660                at(0.88, 1.0),
661                at(0.15, 1.0),
662            ]);
663            p.line(vec![at(0.62, 0.0), at(0.62, 0.26), at(0.88, 0.26)], stroke);
664        }
665        Icon::Up => {
666            p.line_segment([at(0.5, 0.05), at(0.5, 0.95)], stroke);
667            p.line(vec![at(0.15, 0.4), at(0.5, 0.05), at(0.85, 0.4)], stroke);
668        }
669        Icon::Info => {
670            ring();
671            p.line_segment([at(0.5, 0.45), at(0.5, 0.75)], stroke);
672            point(0.5, 0.28);
673        }
674        Icon::Help => {
675            ring();
676            p.line(arc(-PI, 0.0, len * 0.16, at(0.5, 0.36)), stroke);
677            p.line(vec![at(0.66, 0.36), at(0.5, 0.52), at(0.5, 0.62)], stroke);
678            point(0.5, 0.78);
679        }
680        Icon::Warning => {
681            outline(vec![at(0.5, 0.05), at(1.0, 0.92), at(0.0, 0.92)]);
682            p.line_segment([at(0.5, 0.38), at(0.5, 0.62)], stroke);
683            point(0.5, 0.77);
684        }
685        Icon::Error => {
686            ring();
687            p.line_segment([at(0.32, 0.32), at(0.68, 0.68)], stroke);
688            p.line_segment([at(0.68, 0.32), at(0.32, 0.68)], stroke);
689        }
690        Icon::Success => {
691            ring();
692            p.line(vec![at(0.28, 0.52), at(0.44, 0.68), at(0.74, 0.36)], stroke);
693        }
694        Icon::Undo | Icon::Redo => {
695            let flip = icon == Icon::Redo;
696            let x = |v: f32| if flip { 1.0 - v } else { v };
697            let (from, to) = if flip {
698                (PI * 0.5, PI * 1.5)
699            } else {
700                (-PI * 0.5, PI * 0.5)
701            };
702            p.line_segment([at(x(0.1), 0.3), at(x(0.65), 0.3)], stroke);
703            p.line(arc(from, to, len * 0.28, at(x(0.65), 0.58)), stroke);
704            p.line_segment([at(x(0.65), 0.86), at(x(0.3), 0.86)], stroke);
705            p.line(
706                vec![at(x(0.3), 0.1), at(x(0.1), 0.3), at(x(0.3), 0.5)],
707                stroke,
708            );
709        }
710        Icon::Reset | Icon::History => {
711            p.line(arc(-PI * 0.75, PI, len * 0.45, r.center()), stroke);
712            let a = -PI * 0.75;
713            let start = r.center() + vec2(a.cos(), a.sin()) * len * 0.45;
714            p.line(
715                vec![
716                    start + vec2(0.0, -len * 0.25),
717                    start,
718                    start + vec2(len * 0.25, 0.0),
719                ],
720                stroke,
721            );
722            if icon == Icon::History {
723                p.line(vec![at(0.5, 0.25), at(0.5, 0.5), at(0.7, 0.62)], stroke);
724            }
725        }
726        Icon::Copy => {
727            frame(at(0.3, 0.3), at(1.0, 1.0));
728            p.line(vec![at(0.0, 0.7), at(0.0, 0.0), at(0.7, 0.0)], stroke);
729        }
730        Icon::Cut => {
731            // Two blades crossing above two rings.
732            p.line_segment([at(0.32, 0.66), at(0.78, 0.0)], stroke);
733            p.line_segment([at(0.68, 0.66), at(0.22, 0.0)], stroke);
734            p.circle_stroke(at(0.22, 0.82), len * 0.17, stroke);
735            p.circle_stroke(at(0.78, 0.82), len * 0.17, stroke);
736        }
737        Icon::Paste | Icon::PasteSpecial => {
738            // A board and its clip.
739            outline(vec![
740                at(0.3, 0.1),
741                at(0.08, 0.1),
742                at(0.08, 1.0),
743                at(0.92, 1.0),
744                at(0.92, 0.1),
745                at(0.7, 0.1),
746            ]);
747            frame(at(0.3, 0.0), at(0.7, 0.2));
748            if icon == Icon::PasteSpecial {
749                p.line_segment([at(0.28, 0.48), at(0.72, 0.48)], stroke);
750                p.line_segment([at(0.28, 0.72), at(0.72, 0.72)], stroke);
751            }
752        }
753        Icon::Delete => {
754            p.line_segment([at(0.0, 0.18), at(1.0, 0.18)], stroke);
755            p.line(
756                vec![
757                    at(0.35, 0.18),
758                    at(0.35, 0.02),
759                    at(0.65, 0.02),
760                    at(0.65, 0.18),
761                ],
762                stroke,
763            );
764            outline(vec![
765                at(0.12, 0.18),
766                at(0.88, 0.18),
767                at(0.8, 1.0),
768                at(0.2, 1.0),
769            ]);
770        }
771        Icon::Keyboard => {
772            frame(at(0.0, 0.2), at(1.0, 0.8));
773            for x in [0.22, 0.42, 0.62, 0.8] {
774                point(x, 0.4);
775            }
776            p.line_segment([at(0.28, 0.62), at(0.72, 0.62)], stroke);
777        }
778        Icon::Command => {
779            frame(at(0.32, 0.32), at(0.68, 0.68));
780            for (x, y) in [(0.2, 0.2), (0.8, 0.2), (0.2, 0.8), (0.8, 0.8)] {
781                p.circle_stroke(at(x, y), len * 0.13, stroke);
782            }
783        }
784        Icon::Menu => {
785            for y in [0.2, 0.5, 0.8] {
786                p.line_segment([at(0.05, y), at(0.95, y)], stroke);
787            }
788        }
789        Icon::More => {
790            for x in [0.15, 0.5, 0.85] {
791                p.circle_filled(at(x, 0.5), w * 1.1, color);
792            }
793        }
794        Icon::Edit => outline(vec![
795            at(0.7, 0.05),
796            at(0.95, 0.3),
797            at(0.3, 0.95),
798            at(0.05, 0.95),
799            at(0.05, 0.7),
800        ]),
801        Icon::Save => {
802            outline(vec![
803                at(0.0, 0.0),
804                at(0.8, 0.0),
805                at(1.0, 0.2),
806                at(1.0, 1.0),
807                at(0.0, 1.0),
808            ]);
809            frame(at(0.25, 0.0), at(0.7, 0.3));
810            frame(at(0.2, 0.6), at(0.8, 1.0));
811        }
812        Icon::Fullscreen | Icon::ExitFullscreen => {
813            let inward = icon == Icon::ExitFullscreen;
814            for (sx, sy) in [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)] {
815                let dx: f32 = if sx == 0.0 { 1.0 } else { -1.0 };
816                let dy: f32 = if sy == 0.0 { 1.0 } else { -1.0 };
817                let points = if inward {
818                    let (cx, cy) = (sx + dx * 0.35, sy + dy * 0.35);
819                    vec![at(sx, cy), at(cx, cy), at(cx, sy)]
820                } else {
821                    vec![at(sx + dx * 0.35, sy), at(sx, sy), at(sx, sy + dy * 0.35)]
822                };
823                p.line(points, stroke);
824            }
825        }
826        Icon::Notification => {
827            p.line(arc(PI, 2.0 * PI, len * 0.32, at(0.5, 0.5)), stroke);
828            p.line(
829                vec![
830                    at(0.18, 0.5),
831                    at(0.18, 0.75),
832                    at(0.05, 0.85),
833                    at(0.95, 0.85),
834                    at(0.82, 0.75),
835                    at(0.82, 0.5),
836                ],
837                stroke,
838            );
839            point(0.5, 0.98);
840        }
841        Icon::Theme => {
842            ring();
843            let mut half = arc(-PI * 0.5, PI * 0.5, len * 0.48, r.center());
844            half.push(r.center());
845            p.add(egui::Shape::convex_polygon(half, color, Stroke::NONE));
846        }
847        Icon::Visible | Icon::Hidden => {
848            let c = r.center();
849            let lid = |up: bool| -> Vec<Pos2> {
850                (0..=12)
851                    .map(|i| {
852                        let t = i as f32 / 12.0;
853                        let y = (t * PI).sin() * len * 0.32;
854                        pos2(r.left() + t * len, if up { c.y - y } else { c.y + y })
855                    })
856                    .collect()
857            };
858            p.line(lid(true), stroke);
859            p.line(lid(false), stroke);
860            p.circle_stroke(c, len * 0.14, stroke);
861            if icon == Icon::Hidden {
862                p.line_segment([at(0.1, 0.05), at(0.9, 0.95)], stroke);
863            }
864        }
865        Icon::Home => {
866            p.line(vec![at(0.0, 0.48), at(0.5, 0.05), at(1.0, 0.48)], stroke);
867            p.line(
868                vec![
869                    at(0.15, 0.36),
870                    at(0.15, 0.95),
871                    at(0.85, 0.95),
872                    at(0.85, 0.36),
873                ],
874                stroke,
875            );
876        }
877        Icon::Lock | Icon::Unlock => {
878            frame(at(0.1, 0.45), at(0.9, 0.98));
879            // The shackle: closed into the body, or lifted and open.
880            let (lift, right) = if icon == Icon::Lock {
881                (0.0, 0.45)
882            } else {
883                (0.12, 0.3)
884            };
885            let top = at(0.5, 0.25 - lift);
886            p.line(arc(PI, 2.0 * PI, len * 0.26, top), stroke);
887            p.line_segment([at(0.24, 0.25 - lift), at(0.24, 0.45 - lift)], stroke);
888            p.line_segment([at(0.76, 0.25 - lift), at(0.76, right - lift)], stroke);
889        }
890        Icon::TriangleRight | Icon::TriangleDown | Icon::TriangleUp => {
891            // Sized on the square, not on the stroke box: a small solid
892            // mark that stays legible in a row's indent.
893            let c = rect.center();
894            let r = (rect.height().min(rect.width()) * 0.18).max(3.0);
895            let points = match icon {
896                Icon::TriangleDown => vec![
897                    pos2(c.x - r, c.y - r * 0.6),
898                    pos2(c.x + r, c.y - r * 0.6),
899                    pos2(c.x, c.y + r * 0.8),
900                ],
901                Icon::TriangleUp => vec![
902                    pos2(c.x - r, c.y + r * 0.6),
903                    pos2(c.x, c.y - r * 0.8),
904                    pos2(c.x + r, c.y + r * 0.6),
905                ],
906                _ => vec![
907                    pos2(c.x - r * 0.6, c.y - r),
908                    pos2(c.x + r * 0.8, c.y),
909                    pos2(c.x - r * 0.6, c.y + r),
910                ],
911            };
912            p.add(egui::Shape::convex_polygon(points, color, Stroke::NONE));
913        }
914        Icon::Grip => {
915            let c = rect.center();
916            let d = (rect.height() * 0.4).max(2.0);
917            let dot = (rect.width() * 0.2).max(1.0);
918            p.circle_filled(pos2(c.x, c.y - d), dot, color);
919            p.circle_filled(pos2(c.x, c.y + d), dot, color);
920        }
921        Icon::WindowMinimize | Icon::WindowMaximize | Icon::WindowRestore | Icon::WindowClose => {
922            paint_window_button(p, icon, rect, color);
923        }
924        // Strokes on whole physical pixels, like every fallback.
925        Icon::Circle => {
926            let ring = stroke.width;
927            p.circle_stroke(
928                rect.center(),
929                rect.width().min(rect.height()) / 2.0 - ring / 2.0,
930                stroke,
931            );
932        }
933        Icon::Asterisk => {
934            let side = rect.width().min(rect.height());
935            let lines = Stroke::new(crate::scale::snap((side / 5.0).max(1.0), ppp), color);
936            for k in 0..3 {
937                let a = std::f32::consts::FRAC_PI_2 + k as f32 * PI / 3.0;
938                let d = vec2(a.cos(), a.sin()) * side / 2.0;
939                p.line_segment([rect.center() - d, rect.center() + d], lines);
940            }
941        }
942        Icon::Disc | Icon::Square | Icon::Triangle | Icon::Corner => {
943            paint_mark(p, icon, rect, color, w);
944        }
945        Icon::Layout
946        | Icon::LayoutLeft
947        | Icon::LayoutLeftOff
948        | Icon::LayoutBottom
949        | Icon::LayoutBottomOff
950        | Icon::LayoutRight
951        | Icon::LayoutRightOff => paint_layout(p, icon, rect, color, stroke.width),
952        // Drawn from its textures (`Icon::paint`): no fallback shape.
953        Icon::Custom(_) => {}
954        // Before the icon font is in use: the placeholder's ring.
955        Icon::Phosphor(_) => paint_fallback(p, PLACEHOLDER, rect, color),
956        Icon::Picker => {
957            // The tube down to its tip, the collar across it, the bulb.
958            p.line_segment([at(0.05, 0.95), at(0.6, 0.4)], stroke);
959            p.line_segment([at(0.42, 0.3), at(0.7, 0.58)], stroke);
960            p.line_segment(
961                [at(0.62, 0.38), at(0.85, 0.15)],
962                Stroke::new(crate::scale::snap(w * 2.6, ppp), color),
963            );
964        }
965    }
966}
967
968/// A layout icon: a frame on the pixel grid, wider than tall, with one
969/// area filled (shown) or outlined (`-off`: folded or hidden), or, for
970/// [`Icon::Layout`], split into a column and two cells. Strokes are
971/// `width` thick (whole physical pixels).
972fn paint_layout(p: &Painter, icon: Icon, rect: Rect, color: Color32, width: f32) {
973    use egui::emath::GuiRounding as _;
974    let ppp = p.pixels_per_point();
975    let stroke = Stroke::new(width, color);
976    let frame = rect
977        .shrink2(vec2(rect.width() * 0.08, rect.height() * 0.18))
978        .round_to_pixels(ppp);
979    p.rect_stroke(frame, 1.0, stroke, StrokeKind::Inside);
980    let inner = frame.shrink(width);
981    let snap = |x: f32| x.round_to_pixels(ppp);
982    // A line along an edge inside the frame, centred on its pixels.
983    let vline = |x: f32| {
984        let x = p.round_to_pixel_center(x);
985        p.line_segment([pos2(x, inner.top()), pos2(x, inner.bottom())], stroke);
986    };
987    let hline = |y: f32, from: f32, to: f32| {
988        let y = p.round_to_pixel_center(y);
989        p.line_segment([pos2(from, y), pos2(to, y)], stroke);
990    };
991    let side = (inner.width() * 0.34).max(width * 2.0);
992    let low = (inner.height() * 0.4).max(width * 2.0);
993    match icon {
994        Icon::LayoutLeft => {
995            p.rect_filled(inner.with_max_x(snap(inner.left() + side)), 0.0, color);
996        }
997        Icon::LayoutLeftOff => vline(inner.left() + side),
998        Icon::LayoutRight => {
999            p.rect_filled(inner.with_min_x(snap(inner.right() - side)), 0.0, color);
1000        }
1001        Icon::LayoutRightOff => vline(inner.right() - side - width),
1002        Icon::LayoutBottom => {
1003            p.rect_filled(inner.with_min_y(snap(inner.bottom() - low)), 0.0, color);
1004        }
1005        Icon::LayoutBottomOff => hline(inner.bottom() - low - width, inner.left(), inner.right()),
1006        _ => {
1007            // A column at the start, two cells after it.
1008            let x = inner.left() + inner.width() * 0.42;
1009            vline(x);
1010            hline(inner.center().y, x, inner.right());
1011        }
1012    }
1013}
1014
1015/// A window button's glyph across the square, centred on a pixel, its
1016/// strokes one physical pixel: the restore glyph's back square shows only
1017/// where the front one does not cover it.
1018fn paint_window_button(p: &Painter, icon: Icon, rect: Rect, color: Color32) {
1019    let c = pos2(
1020        p.round_to_pixel_center(rect.center().x),
1021        p.round_to_pixel_center(rect.center().y),
1022    );
1023    let s = rect.width().min(rect.height()) / 2.0;
1024    let stroke = Stroke::new(crate::scale::stroke(p.ctx(), 1.0), color);
1025    match icon {
1026        Icon::WindowMinimize => {
1027            p.line_segment([pos2(c.x - s, c.y), pos2(c.x + s, c.y)], stroke);
1028        }
1029        Icon::WindowMaximize => {
1030            let r = Rect::from_center_size(c, vec2(2.0 * s, 2.0 * s));
1031            p.rect_stroke(r, 0.0, stroke, StrokeKind::Inside);
1032        }
1033        Icon::WindowRestore => {
1034            let front = Rect::from_min_size(
1035                pos2(c.x - s, c.y - s + 2.0),
1036                vec2(2.0 * s - 2.0, 2.0 * s - 2.0),
1037            );
1038            p.rect_stroke(front, 0.0, stroke, StrokeKind::Inside);
1039            // The back square's top and end edges, offset by 2.
1040            let h = stroke.width / 2.0;
1041            let (l, t, r, b) = (
1042                c.x - s + 2.0 + h,
1043                c.y - s + h,
1044                c.x + s - h,
1045                c.y + s - 2.0 - h,
1046            );
1047            p.line(
1048                vec![
1049                    pos2(l, front.top()),
1050                    pos2(l, t),
1051                    pos2(r, t),
1052                    pos2(r, b),
1053                    pos2(front.right(), b),
1054                ],
1055                stroke,
1056            );
1057        }
1058        _ => {
1059            p.line_segment([pos2(c.x - s, c.y - s), pos2(c.x + s, c.y + s)], stroke);
1060            p.line_segment([pos2(c.x + s, c.y - s), pos2(c.x - s, c.y + s)], stroke);
1061        }
1062    }
1063}
1064
1065impl serde::Serialize for Icon {
1066    /// Its name: what agents read.
1067    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1068        s.serialize_str(self.name())
1069    }
1070}
1071
1072/// What an icon widget draws.
1073#[derive(Clone, Debug)]
1074pub enum Source {
1075    /// An icon by role.
1076    Icon(Icon),
1077    /// Text in a font family (an icon font the application installed, or
1078    /// any font).
1079    Glyph {
1080        text: String,
1081        family: FontFamily,
1082    },
1083    Image(ImageSource<'static>),
1084}
1085
1086impl From<Icon> for Source {
1087    fn from(icon: Icon) -> Self {
1088        Source::Icon(icon)
1089    }
1090}
1091
1092impl Source {
1093    /// An icon by name ([`registry`]): a name that does not resolve is
1094    /// reported (`surface`) and drawn as the [`PLACEHOLDER`].
1095    pub fn named(name: &str, surface: &str) -> Source {
1096        Source::Icon(registry::resolve(name, surface).unwrap_or(PLACEHOLDER))
1097    }
1098
1099    /// The name of the icon it draws; `None` for a glyph or an image.
1100    pub fn icon_name(&self) -> Option<&'static str> {
1101        match self {
1102            Source::Icon(i) => Some(i.name()),
1103            _ => None,
1104        }
1105    }
1106
1107    /// A glyph in the proportional family.
1108    pub fn glyph(text: &str) -> Source {
1109        Source::Glyph {
1110            text: text.into(),
1111            family: FontFamily::Proportional,
1112        }
1113    }
1114
1115    /// Paints it in a square.
1116    pub fn paint(&self, ui: &Ui, rect: Rect, color: Color32) {
1117        match self {
1118            Source::Icon(i) => i.paint(ui.painter(), rect, color),
1119            Source::Glyph { text, family } => {
1120                ui.painter().text(
1121                    rect.center(),
1122                    egui::Align2::CENTER_CENTER,
1123                    text,
1124                    FontId::new(rect.height(), family.clone()),
1125                    color,
1126                );
1127            }
1128            Source::Image(source) => {
1129                egui::Image::new(source.clone())
1130                    .tint(color)
1131                    .paint_at(ui, rect);
1132            }
1133        }
1134    }
1135}
1136
1137/// Where an icon is used, which decides its size.
1138#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
1139pub enum Size {
1140    /// In a line of text.
1141    #[default]
1142    Inline,
1143    Toolbar,
1144    /// In a panel strip.
1145    Panel,
1146}
1147
1148impl Size {
1149    /// Its side in points, from the tokens.
1150    pub fn points(self, t: &Tokens) -> f32 {
1151        match self {
1152            Size::Inline => t.size.icon_inline,
1153            Size::Toolbar => t.size.icon_toolbar,
1154            Size::Panel => t.size.icon_panel,
1155        }
1156    }
1157}
1158
1159/// Its colour, by meaning.
1160#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
1161pub enum Tone {
1162    #[default]
1163    Default,
1164    Muted,
1165    Strong,
1166    Accent,
1167    OnAccent,
1168    Danger,
1169    Warning,
1170    Success,
1171    Info,
1172}
1173
1174impl Tone {
1175    /// The kit's tone it is drawn in on a button; `Default`, `Strong`
1176    /// and `OnAccent` are the button's own box.
1177    ///
1178    /// <div class="warning">Experimental: may change in any release
1179    /// (docs/stability.md).</div>
1180    pub fn kit_tone(self) -> crate::kit::Tone {
1181        use crate::kit::Tone as K;
1182        match self {
1183            Tone::Default | Tone::Strong | Tone::OnAccent => K::Neutral,
1184            Tone::Muted => K::Muted,
1185            Tone::Accent => K::Accent,
1186            Tone::Danger => K::Danger,
1187            Tone::Warning => K::Warning,
1188            Tone::Success => K::Success,
1189            Tone::Info => K::Info,
1190        }
1191    }
1192
1193    pub fn color(self, t: &Tokens) -> Color32 {
1194        match self {
1195            Tone::Default => t.text.default,
1196            Tone::Muted => t.text.muted,
1197            Tone::Strong => t.text.strong,
1198            Tone::Accent => t.accent.default,
1199            Tone::OnAccent => t.text.on_accent,
1200            Tone::Danger => t.status.danger,
1201            Tone::Warning => t.status.warning,
1202            Tone::Success => t.status.success,
1203            Tone::Info => t.status.info,
1204        }
1205    }
1206}
1207
1208/// An icon as a widget.
1209///
1210/// Need (icon): an icon as a widget (sized, toned, clickable).
1211/// Need (action): an icon button.
1212#[derive(Clone, Debug)]
1213#[must_use]
1214pub struct IconWidget {
1215    source: Source,
1216    size: Size,
1217    tone: Tone,
1218    label: Option<String>,
1219    sense: Sense,
1220    selected: Option<bool>,
1221}
1222
1223impl IconWidget {
1224    pub fn new(source: impl Into<Source>) -> Self {
1225        Self {
1226            source: source.into(),
1227            size: Size::Inline,
1228            tone: Tone::Default,
1229            label: None,
1230            sense: Sense::hover(),
1231            selected: None,
1232        }
1233    }
1234
1235    pub fn size(mut self, size: Size) -> Self {
1236        self.size = size;
1237        self
1238    }
1239
1240    pub fn tone(mut self, tone: Tone) -> Self {
1241        self.tone = tone;
1242        self
1243    }
1244
1245    /// Its accessible name (and tooltip); an icon alone says nothing to a
1246    /// screen reader without it.
1247    pub fn label(mut self, label: &str) -> Self {
1248        self.label = Some(label.into());
1249        self
1250    }
1251
1252    /// Clickable (a button made of the icon alone).
1253    pub fn sense_click(mut self) -> Self {
1254        self.sense = Sense::click();
1255        self
1256    }
1257
1258    /// Clickable and on or off (a toggle made of the icon alone): drawn
1259    /// in the kit's `icon-button.selected` while on, announced as
1260    /// toggled.
1261    pub fn selected(mut self, selected: bool) -> Self {
1262        self.sense = Sense::click();
1263        self.selected = Some(selected);
1264        self
1265    }
1266}
1267
1268impl Widget for IconWidget {
1269    fn ui(self, ui: &mut Ui) -> Response {
1270        let t = Tokens::of(ui.ctx());
1271        let side = self.size.points(&t);
1272        let (rect, response) = ui.allocate_exact_size(vec2(side, side), self.sense);
1273        if ui.is_rect_visible(rect) {
1274            if self.sense.senses_click() {
1275                // A button: drawn by the kit's `icon-button`.
1276                let kit = crate::kit::Kit::of(ui.ctx());
1277                let button = &kit.icon_button;
1278                let state = State::from_response(&response)
1279                    .with(State::SELECTED, self.selected == Some(true));
1280                let part = Part {
1281                    rect,
1282                    room: rect,
1283                    state: state.toned(self.tone.kit_tone()),
1284                    label: None,
1285                };
1286                let cx = KitCx {
1287                    painter: ui.painter(),
1288                    ppp: ui.ctx().pixels_per_point(),
1289                };
1290                button.paint(&cx, &part);
1291                // Strong and on-accent are no tones: their colour holds
1292                // while nothing else (hover, selected, disabled) does.
1293                let plain = !state.contains(State::HOVER)
1294                    && !state.contains(State::SELECTED)
1295                    && !state.contains(State::DISABLED);
1296                let color = match self.tone {
1297                    Tone::Strong | Tone::OnAccent if plain => self.tone.color(&t),
1298                    _ => button.style(part.state).text,
1299                };
1300                self.source.paint(ui, rect, color);
1301            } else {
1302                let color = if ui.is_enabled() {
1303                    self.tone.color(&t)
1304                } else {
1305                    t.text.muted
1306                };
1307                self.source.paint(ui, rect, color);
1308            }
1309        }
1310        let kind = if self.sense.senses_click() {
1311            Role::Button
1312        } else {
1313            Role::Image
1314        };
1315        let enabled = ui.is_enabled();
1316        let label = self.label.clone().unwrap_or_default();
1317        match self.selected {
1318            Some(on) => response.widget_info(|| WidgetInfo::selected(kind, enabled, on, &label)),
1319            None => response.widget_info(|| WidgetInfo::labeled(kind, enabled, &label)),
1320        }
1321        match self.label {
1322            Some(l) if self.sense.senses_click() => response.on_hover_text(l),
1323            _ => response,
1324        }
1325    }
1326}
1327
1328/// Allocates a square of `side` points and paints an icon in it, with no
1329/// interaction: a mark inside another widget (a menu tick, a toast's
1330/// level).
1331pub fn mark(ui: &mut Ui, icon: Icon, side: f32, color: Color32) -> Response {
1332    let (rect, response) = ui.allocate_exact_size(vec2(side, side), Sense::hover());
1333    if ui.is_rect_visible(rect) {
1334        icon.paint(ui.painter(), rect, color);
1335    }
1336    response
1337}