mirui 0.45.0

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use alloc::borrow::Cow;
use core::cell::RefCell;

use crate::core::resource::{ResourceHandle, ResourceManager};
use crate::ecs::{Entity, World};
use crate::render::command::{CompositeMode, DrawCommand};
use crate::render::path::Path;
use crate::render::renderer::Renderer;
use crate::render::texture::Texture;
use crate::types::{Color, Fixed, Point, Rect, Transform};
use crate::ui::theme::{ColorToken, ThemedColor};
use crate::ui::view::{View, ViewCtx};

#[derive(Clone, Debug)]
pub enum ImageSource {
    Texture(Cow<'static, str>),
    Vector(Path),
}

impl Default for ImageSource {
    fn default() -> Self {
        Self::Texture(Cow::Borrowed(""))
    }
}

impl From<Cow<'static, str>> for ImageSource {
    fn from(value: Cow<'static, str>) -> Self {
        Self::Texture(value)
    }
}

impl From<&'static str> for ImageSource {
    fn from(value: &'static str) -> Self {
        Self::Texture(Cow::Borrowed(value))
    }
}

impl From<alloc::string::String> for ImageSource {
    fn from(value: alloc::string::String) -> Self {
        Self::Texture(Cow::Owned(value))
    }
}

impl From<Path> for ImageSource {
    fn from(value: Path) -> Self {
        Self::Vector(value)
    }
}

impl From<crate::ui::icons::IconAsset> for ImageSource {
    fn from(value: crate::ui::icons::IconAsset) -> Self {
        Self::Vector(value.into())
    }
}

#[derive(crate::Component)]
pub struct Image {
    pub src: ImageSource,
    pub composite: CompositeMode,
    pub radius: Fixed,
    pub color: ThemedColor,
    pub viewbox: Fixed,
    pub scale: Fixed,
}

impl Default for Image {
    fn default() -> Self {
        Self {
            src: ImageSource::default(),
            composite: CompositeMode::SourceOver,
            radius: Fixed::ZERO,
            color: ThemedColor::Token(ColorToken::OnSurface),
            viewbox: Fixed::from_int(24),
            scale: Fixed::ONE,
        }
    }
}

impl Image {
    pub fn new(src: impl Into<ImageSource>) -> Self {
        Self {
            src: src.into(),
            ..Self::default()
        }
    }

    pub fn with_composite(mut self, mode: CompositeMode) -> Self {
        self.composite = mode;
        self
    }

    pub fn with_radius(mut self, radius: impl Into<Fixed>) -> Self {
        self.radius = radius.into();
        self
    }

    pub fn with_color(mut self, color: impl Into<ThemedColor>) -> Self {
        self.color = color.into();
        self
    }

    pub fn with_viewbox(mut self, viewbox: impl Into<Fixed>) -> Self {
        self.viewbox = viewbox.into();
        self
    }

    pub fn with_scale(mut self, scale: impl Into<Fixed>) -> Self {
        self.scale = scale.into();
        self
    }

    pub fn build(src: impl Into<ImageSource>) -> ImageBuilder {
        ImageBuilder {
            image: Image::new(src),
            style: None,
        }
    }
}

pub struct ImageBuilder {
    image: Image,
    style: Option<crate::ui::Style>,
}

struct ImageResource(RefCell<Option<ResourceHandle<Texture<'static>>>>);

pub(crate) fn attach_image_resource(world: &mut World, entity: Entity) {
    if !world.has::<Image>(entity) || world.has::<ImageResource>(entity) {
        return;
    }
    let handle = world
        .get::<Image>(entity)
        .and_then(|image| match &image.src {
            ImageSource::Texture(token) => world
                .resource::<ResourceManager<Texture<'static>>>()
                .map(|manager| manager.load(token.clone())),
            ImageSource::Vector(_) => None,
        });
    world.insert(entity, ImageResource(RefCell::new(handle)));
}

fn resolve_image(
    world: &World,
    entity: Entity,
    image: &Image,
) -> Option<alloc::rc::Rc<Texture<'static>>> {
    let ImageSource::Texture(token) = &image.src else {
        return None;
    };
    if let Some(cache) = world.get::<ImageResource>(entity) {
        let mut handle = cache.0.borrow_mut();
        if let Some(current) = handle.as_ref()
            && current.token() == token.as_ref()
            && let Some(texture) = current.get_cached()
        {
            return Some(texture);
        }
        let manager = world.resource::<ResourceManager<Texture<'static>>>()?;
        if handle
            .as_ref()
            .is_none_or(|current| current.token() != token.as_ref())
        {
            *handle = Some(manager.load(token.clone()));
        }
        return handle.as_ref().map(ResourceHandle::get);
    }
    world
        .resource::<ResourceManager<Texture<'static>>>()
        .map(|manager| manager.resolve(token))
}

pub(crate) fn draw_vector_transformed(
    renderer: &mut dyn Renderer,
    ctx: &mut ViewCtx,
    path: &Path,
    color: Color,
    transform: Transform,
) {
    let paint = crate::render::canvas::Paint::Color(color.into());
    ctx.draw(
        renderer,
        &DrawCommand::FillPath {
            path,
            transform,
            paint: &paint,
            opa: 255,
            fill_rule: crate::render::raster::FillRule::EvenOdd,
        },
        ctx.clip,
    );
}

pub(crate) fn vector_transform(
    rect: &Rect,
    viewbox: Fixed,
    rendered_size: Fixed,
    parent: Transform,
) -> Option<Transform> {
    if viewbox <= Fixed::ZERO || rendered_size <= Fixed::ZERO {
        return None;
    }
    let x = rect.x + (rect.w - rendered_size) / Fixed::from_int(2);
    let y = rect.y + (rect.h - rendered_size) / Fixed::from_int(2);
    Some(
        parent
            .compose(&Transform::translate(x, y))
            .compose(&Transform::scale(
                rendered_size / viewbox,
                rendered_size / viewbox,
            )),
    )
}

impl ImageBuilder {
    pub fn style(mut self, style: crate::ui::Style) -> Self {
        self.style = Some(style);
        self
    }

    pub fn composite(mut self, mode: CompositeMode) -> Self {
        self.image.composite = mode;
        self
    }

    pub fn radius(mut self, radius: impl Into<Fixed>) -> Self {
        self.image.radius = radius.into();
        self
    }

    pub fn spawn(self, world: &mut World) -> Entity {
        world.spawn(self)
    }
}

impl crate::ecs::IntoBundle for ImageBuilder {
    fn spawn_into(self, world: &mut World, entity: Entity) {
        world.insert(entity, self.image);
        attach_image_resource(world, entity);
        if let Some(style) = self.style {
            world.insert(entity, style);
        }
    }
}

fn image_render(
    renderer: &mut dyn Renderer,
    world: &World,
    entity: Entity,
    rect: &Rect,
    ctx: &mut ViewCtx,
) {
    crate::trace_span!("image.render", {
        let img = crate::trace_span!("image.world_get", { world.get::<Image>(entity) });
        let Some(img) = img else { return };
        if let ImageSource::Vector(path) = &img.src {
            let size = rect.w.min(rect.h) * img.scale;
            let Some(transform) = vector_transform(rect, img.viewbox, size, ctx.transform) else {
                return;
            };
            draw_vector_transformed(
                renderer,
                ctx,
                path,
                img.color.resolve_in(ctx.theme(world), ctx.state),
                transform,
            );
            return;
        }
        let rc = crate::trace_span!("image.resolve", { resolve_image(world, entity, img) });
        let Some(rc) = rc else { return };
        crate::trace_span!("image.blit", {
            ctx.draw(
                renderer,
                &DrawCommand::Blit {
                    pos: Point {
                        x: rect.x,
                        y: rect.y,
                    },
                    size: Point {
                        x: rect.w,
                        y: rect.h,
                    },
                    transform: ctx.transform,
                    quad: ctx.quad,
                    texture: &rc,
                    opa: 255,
                    radius: img.radius,
                    composite: img.composite,
                },
                ctx.clip,
            );
        });
        crate::trace_span!("image.rc_drop", { drop(rc) });
    });
}

pub fn view() -> View {
    View::new("Image", 70, image_render)
        .with_filter::<Image>()
        .with_attach(attach_image_resource)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::cache::MaxSize;
    use crate::render::texture::ColorFormat;

    static RED: [u8; 4] = [255, 0, 0, 255];
    static BLUE: [u8; 4] = [0, 0, 255, 255];

    fn image_manager() -> ResourceManager<Texture<'static>> {
        ResourceManager::new(
            MaxSize::Bytes(64),
            Texture::from_static(&RED, 1, 1, ColorFormat::RGBA8888),
        )
        .with_static(
            "blue",
            Texture::from_static(&BLUE, 1, 1, ColorFormat::RGBA8888),
        )
    }

    #[test]
    fn build_spawns_image_with_style() {
        let mut world = World::new();
        let e = Image::build("thumbs_up")
            .style(crate::ui::Style::default())
            .spawn(&mut world);
        assert!(world.has::<Image>(e));
        assert!(world.has::<ImageResource>(e));
        assert!(world.has::<crate::ui::Style>(e));
        assert!(world.has::<crate::ui::Widget>(e));
    }

    #[test]
    fn build_without_style_omits_it() {
        let mut world = World::new();
        let e = Image::build("thumbs_up").spawn(&mut world);
        assert!(world.has::<Image>(e));
        assert!(world.has::<ImageResource>(e));
        assert!(!world.has::<crate::ui::Style>(e));
    }

    #[test]
    fn new_accepts_str_and_string() {
        let a = Image::new("static");
        assert!(matches!(a.src, ImageSource::Texture(ref token) if token == "static"));
        let owned: alloc::string::String = "owned".into();
        let b = Image::new(owned);
        assert!(matches!(b.src, ImageSource::Texture(ref token) if token == "owned"));
    }

    #[test]
    fn attached_image_reuses_lease_and_tracks_token_changes() {
        let mut world = World::new();
        world.insert_resource(image_manager());
        let entity = world.spawn(Image::new("blue"));
        attach_image_resource(&mut world, entity);

        let image = world.get::<Image>(entity).unwrap();
        let first = resolve_image(&world, entity, image).unwrap();
        let second = resolve_image(&world, entity, image).unwrap();
        assert!(alloc::rc::Rc::ptr_eq(&first, &second));
        assert_eq!(first.buf.as_slice(), &BLUE);

        world.get_mut::<Image>(entity).unwrap().src = ImageSource::from("missing");
        let image = world.get::<Image>(entity).unwrap();
        let fallback = resolve_image(&world, entity, image).unwrap();
        assert_eq!(fallback.buf.as_slice(), &RED);
        assert_eq!(
            world
                .get::<ImageResource>(entity)
                .unwrap()
                .0
                .borrow()
                .as_ref()
                .unwrap()
                .token(),
            "missing"
        );
    }

    #[test]
    fn new_accepts_borrowed_vector_paths_without_allocating_commands() {
        use crate::render::path::PathCmd;

        static COMMANDS: &[PathCmd] = &[PathCmd::Close];
        let image = Image::new(Path::from_static(COMMANDS));
        assert!(matches!(image.src, ImageSource::Vector(ref path) if path.is_borrowed()));
    }

    #[test]
    fn new_accepts_typed_icon_assets_without_allocating_commands() {
        let image = Image::new(crate::ui::icons::ICON_HOME);
        assert!(matches!(image.src, ImageSource::Vector(ref path) if path.is_borrowed()));
    }

    #[test]
    fn unresolved_component_keeps_the_direct_manager_path() {
        let mut world = World::new();
        world.insert_resource(image_manager());
        let entity = world.spawn_empty();
        world.insert(entity, Image::new("blue"));

        let image = world.get::<Image>(entity).unwrap();
        assert_eq!(
            resolve_image(&world, entity, image).unwrap().buf.as_slice(),
            &BLUE
        );
    }
}