Skip to main content

blockworx/
icons.rs

1//! Embedded monochrome UI icons (SVG), drawn through the canvas image pipeline.
2//!
3//! Like block images, an icon's SVG is registered once with the
4//! [`ImageRegistry`] to mint an
5//! [`ImageHandle`]; the `Painter` then draws it by `uri`. Registration happens
6//! once at startup ([`Icons::register`]); the per-frame `Painter` carries a cheap
7//! clone of the resulting handles.
8//!
9//! Currently unused — the per-pin tag overlays that drew these were retired in
10//! favour of the selection-bar toggle — but kept for the planned conversion of
11//! the toolbar's text buttons to icons.
12#![allow(dead_code)]
13
14use blockworx_doc::block_model::Asset;
15use blockworx_paint::ImageHandle;
16
17use crate::canvas::image::ImageRegistry;
18
19/// Register an embedded SVG icon, returning its handle (or `None` if it fails to
20/// parse/register).
21fn register_svg(
22    ctx: &egui::Context,
23    registry: &mut ImageRegistry,
24    svg: &str,
25) -> Option<ImageHandle> {
26    registry
27        .register(ctx, &Asset::Svg(svg.as_bytes().into()))
28        .ok()
29}
30
31pub const VIEW_VISIBLE: &str = include_str!("../icons/icon-view-visible.svg");
32pub const VIEW_HIDDEN: &str = include_str!("../icons/icon-view-hidden.svg");
33pub const ADD: &str = include_str!("../icons/icon-add.svg");
34
35/// Which UI icon to draw.
36#[derive(Clone, Copy)]
37pub enum Icon {
38    ViewVisible,
39    ViewHidden,
40    Add,
41}
42
43/// The registered handles for the embedded icons. `None` for any that failed to
44/// register (the caller simply draws nothing). Cloning is cheap (a few `Arc`s).
45#[derive(Clone, Default)]
46pub struct Icons {
47    view_visible: Option<ImageHandle>,
48    view_hidden: Option<ImageHandle>,
49    add: Option<ImageHandle>,
50}
51
52impl Icons {
53    /// Register every embedded icon, returning their handles. Call once at
54    /// startup (needs the `egui::Context` for the image loader).
55    pub fn register(ctx: &egui::Context, registry: &mut ImageRegistry) -> Self {
56        Self {
57            view_visible: register_svg(ctx, registry, VIEW_VISIBLE),
58            view_hidden: register_svg(ctx, registry, VIEW_HIDDEN),
59            add: register_svg(ctx, registry, ADD),
60        }
61    }
62
63    pub fn get(&self, icon: Icon) -> Option<&ImageHandle> {
64        match icon {
65            Icon::ViewVisible => self.view_visible.as_ref(),
66            Icon::ViewHidden => self.view_hidden.as_ref(),
67            Icon::Add => self.add.as_ref(),
68        }
69    }
70}