yuno 0.2.0

Multimedia UI layout and rendering framework powered by Skia.
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
pub mod linechart;
pub mod ringchart;

use crate::drawing::sprites::{HorizontalAlign, Text};
use crate::drawing::{Drawable, Plan, RoundedRectangle};
use crate::interacting::{Event, Interactive};
use crate::layouts::{EdgeLayout, EdgeLayoutElement, Edges, HasPlanner, Layout};
use crate::story_board::{StoryBoard, easing};
use crate::sys_monitor::HwStats;
use linechart::LineChart;
use ringchart::RingProgressChart;
use skia_safe::{Canvas, Color, Color4f, Font, Paint, PaintStyle, Point, Rect};
use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;

/// Represents basic video information decoupled from the media pipeline.
pub struct VideoMetadata {
    pub video_filename: String,
    pub codec_name: String,
    pub frame_rate: f64,
    pub sv_sz: (i32, i32),
}

pub struct TopBar {
    planner: RefCell<Option<Rc<dyn Layout>>>,
    sh_state: RefCell<StoryBoard>,

    // The internal layout engine
    internal_layout: Rc<EdgeLayout>,

    // Child Components kept for dynamic updates
    video_title: Rc<Text>,
    video_subtitle: Rc<Text>,

    cpu_chart: Rc<LineChart>,
    gpu_chart: Rc<LineChart>,
    vram_ring: Rc<RingProgressChart>,
    ram_ring: Rc<RingProgressChart>,

    enabled: RefCell<bool>,
}

impl HasPlanner for TopBar {
    fn planner(&self) -> &RefCell<Option<Rc<dyn Layout>>> {
        &self.planner
    }
    fn set_planner(&self, p: Option<Rc<dyn Layout>>) {
        *self.planner.borrow_mut() = p;
    }
}

// Enable TopBar to act as a Planner for its internal EdgeLayout
impl Layout for TopBar {
    fn measure_child(&self, _c: &dyn Drawable, canvas: &Canvas) -> Rect {
        // Just forward the exact planned area TopBar was assigned to its internal layout
        self.get_planned_drawing_area(canvas)
    }

    fn as_layout(&self) -> &dyn Layout {
        self
    }

    fn foreach_child(&self, f: &mut dyn FnMut(&dyn Drawable)) {
        f(self.internal_layout.as_ref());
        f(self.video_title.as_ref());
        f(self.video_subtitle.as_ref());

        f(self.cpu_chart.as_ref());
        f(self.gpu_chart.as_ref());

        f(self.vram_ring.as_ref());
        f(self.ram_ring.as_ref());
    }
}

impl Interactive for TopBar {
    fn handle_and_route(&self, e: &Event, u: &dyn Any) {
        self.foreach_child(&mut |x| {
            // Check if the child is enabled before attempting to route events
            if x.is_enabled()
                && let Some(i) = x.try_to_interactive()
            {
                i.handle_and_route(e, u);
            }
        });
    }
}

impl Drawable for TopBar {
    fn draw(&self, canvas: &Canvas) -> anyhow::Result<()> {
        let anim_offset_y = -(f64::from(&*self.sh_state.borrow()) as f32);

        canvas.save();
        canvas.translate(Point::new(0.0, anim_offset_y));

        self.internal_layout.draw(canvas)?;

        canvas.restore();
        Ok(())
    }

    fn get_horizontal_plan(&self) -> Plan {
        Plan::FillPositive
    }
    fn get_vertical_plan(&self) -> Plan {
        Plan::Fit(55.0)
    }

    fn replan(&self) {
        self.foreach_child(&mut |x| x.replan());
    }

    fn try_to_interactive(&self) -> Option<&dyn Interactive> {
        Some(self)
    }

    fn is_enabled(&self) -> bool {
        *self.enabled.borrow()
    }

    fn set_enabled(&self, e: bool) {
        *self.enabled.borrow_mut() = e;
    }

    fn as_drawable(&self) -> &dyn Drawable {
        self
    }
}

impl TopBar {
    pub fn new(font: &Font, meta: &VideoMetadata, hw: &HwStats) -> Rc<Self> {
        // 1. Setup Paints
        let mut bg_paint = Paint::default();
        bg_paint
            .set_anti_alias(true)
            .set_color4f(Color4f::new(0.08, 0.08, 0.1, 0.85), None);

        let mut border_paint = Paint::default();
        border_paint
            .set_anti_alias(true)
            .set_style(PaintStyle::Stroke)
            .set_stroke_width(1.0)
            .set_color4f(Color4f::new(1.0, 1.0, 1.0, 0.15), None);

        let mut text_paint = Paint::default();
        text_paint.set_anti_alias(true).set_color(Color::WHITE);

        let mut subtext_paint = Paint::default();
        subtext_paint
            .set_anti_alias(true)
            .set_color4f(Color4f::new(0.7, 0.7, 0.75, 1.0), None);

        let mut sub_font = font.clone();
        sub_font.set_size(14.0);

        // 2. Initialize Sub-Components
        let bg =
            RoundedRectangle::new(Plan::FillPositive, Plan::FillPositive, 12.0, 12.0, bg_paint);
        let border = RoundedRectangle::new(
            Plan::FillPositive,
            Plan::FillPositive,
            12.0,
            12.0,
            border_paint,
        );

        let video_title = Text::new(
            &meta.video_filename,
            font.clone(),
            text_paint.clone(),
            HorizontalAlign::Left,
        );

        let sub_info = format!(
            "Codec: {}  |  FPS: {:.2}  |  Size: {}x{}",
            meta.codec_name, meta.frame_rate, meta.sv_sz.0, meta.sv_sz.1
        );
        let video_subtitle = Text::new(
            &sub_info,
            sub_font.clone(),
            subtext_paint,
            HorizontalAlign::Left,
        );

        let gpu_colors = [
            Color::from_argb(255, 255, 42, 133),
            Color::from_argb(255, 138, 43, 226),
        ];
        let gpu_chart = LineChart::new(hw.gpu_history.clone(), gpu_colors);

        let cpu_colors = [
            Color::from_argb(255, 0, 212, 255),
            Color::from_argb(255, 41, 121, 255),
        ];
        let cpu_chart = LineChart::new(hw.cpu_history.clone(), cpu_colors);

        let vram_ring = RingProgressChart::new(
            hw.vram_usage,
            "VR",
            Color::from_rgb(138, 43, 226),
            font.clone(),
        );
        let ram_ring = RingProgressChart::new(
            hw.mem_usage,
            "RAM",
            Color::from_rgb(0, 212, 255),
            font.clone(),
        );

        let gpu_label = Text::new(
            "GPU",
            sub_font.clone(),
            text_paint.clone(),
            HorizontalAlign::Left,
        );
        let cpu_label = Text::new(
            "CPU",
            sub_font.clone(),
            text_paint.clone(),
            HorizontalAlign::Left,
        );

        // 3. Assemble Layout with Chain Calls
        let chart_y_margin = (55.0 - 32.0) / 2.0;

        // Create the initial layout instance as Rc to start the chain
        let internal_layout = EdgeLayout::new(true);

        let internal_layout = internal_layout
            .add_child(
                "bg",
                EdgeLayoutElement {
                    drawable: bg,
                    l_edge: Edges::Left,
                    l_margin: 0.0,
                    t_edge: Edges::Top,
                    t_margin: 0.0,
                    r_edge: Edges::Right,
                    r_margin: 0.0,
                    b_edge: Edges::Bottom,
                    b_margin: 0.0,
                },
            )
            .add_child(
                "border",
                EdgeLayoutElement {
                    drawable: border,
                    l_edge: Edges::Left,
                    l_margin: 0.0,
                    t_edge: Edges::Top,
                    t_margin: 0.0,
                    r_edge: Edges::Right,
                    r_margin: 0.0,
                    b_edge: Edges::Bottom,
                    b_margin: 0.0,
                },
            )
            .add_child(
                "title",
                EdgeLayoutElement {
                    drawable: video_title.clone(),
                    l_edge: Edges::Left,
                    l_margin: 24.0,
                    t_edge: Edges::Top,
                    t_margin: 14.0,
                    r_edge: Edges::Right,
                    r_margin: 300.0,
                    b_edge: Edges::Bottom,
                    b_margin: 20.0,
                },
            )
            .add_child(
                "subtitle",
                EdgeLayoutElement {
                    drawable: video_subtitle.clone(),
                    l_edge: Edges::Left,
                    l_margin: 24.0,
                    t_edge: Edges::Top,
                    t_margin: 34.0,
                    r_edge: Edges::Right,
                    r_margin: 300.0,
                    b_edge: Edges::Bottom,
                    b_margin: 0.0,
                },
            )
            .add_child(
                "gpu_chart",
                EdgeLayoutElement {
                    drawable: gpu_chart.clone(),
                    l_edge: Edges::Right,
                    l_margin: 114.0,
                    t_edge: Edges::Top,
                    t_margin: chart_y_margin,
                    r_edge: Edges::Right,
                    r_margin: 24.0,
                    b_edge: Edges::Bottom,
                    b_margin: chart_y_margin,
                },
            )
            .add_child(
                "gpu_label",
                EdgeLayoutElement {
                    drawable: gpu_label,
                    l_edge: Edges::Right,
                    l_margin: 149.0,
                    t_edge: Edges::Top,
                    t_margin: chart_y_margin,
                    r_edge: Edges::Right,
                    r_margin: 119.0,
                    b_edge: Edges::Bottom,
                    b_margin: chart_y_margin,
                },
            )
            .add_child(
                "cpu_chart",
                EdgeLayoutElement {
                    drawable: cpu_chart.clone(),
                    l_edge: Edges::Right,
                    l_margin: 254.0,
                    t_edge: Edges::Top,
                    t_margin: chart_y_margin,
                    r_edge: Edges::Right,
                    r_margin: 164.0,
                    b_edge: Edges::Bottom,
                    b_margin: chart_y_margin,
                },
            )
            .add_child(
                "cpu_label",
                EdgeLayoutElement {
                    drawable: cpu_label,
                    l_edge: Edges::Right,
                    l_margin: 289.0,
                    t_edge: Edges::Top,
                    t_margin: chart_y_margin,
                    r_edge: Edges::Right,
                    r_margin: 259.0,
                    b_edge: Edges::Bottom,
                    b_margin: chart_y_margin,
                },
            )
            .add_child(
                "vram_ring",
                EdgeLayoutElement {
                    drawable: vram_ring.clone(),
                    l_edge: Edges::Right,
                    l_margin: 326.0,
                    t_edge: Edges::Top,
                    t_margin: chart_y_margin,
                    r_edge: Edges::Right,
                    r_margin: 294.0,
                    b_edge: Edges::Bottom,
                    b_margin: chart_y_margin,
                },
            )
            .add_child(
                "ram_ring",
                EdgeLayoutElement {
                    drawable: ram_ring.clone(),
                    l_edge: Edges::Right,
                    l_margin: 368.0,
                    t_edge: Edges::Top,
                    t_margin: chart_y_margin,
                    r_edge: Edges::Right,
                    r_margin: 336.0,
                    b_edge: Edges::Bottom,
                    b_margin: chart_y_margin,
                },
            );

        // 4. Construct Parent Instance
        let instance = Rc::new(Self {
            planner: RefCell::new(None),
            sh_state: RefCell::new(StoryBoard::init(
                100f64,
                -20f64,
                500f64,
                false,
                easing::cubic_in_out,
            )),
            internal_layout: internal_layout.clone(),
            video_title,
            video_subtitle,
            cpu_chart,
            gpu_chart,
            vram_ring,
            ram_ring,
            enabled: RefCell::new(true),
        });

        internal_layout.set_planner(Some(instance.clone() as Rc<dyn Layout>));

        instance
    }

    pub fn set_state(&self, meta: &VideoMetadata, hw: &HwStats, fps: f32) {
        // Direct mutations via RefCells held in the components
        self.video_title.set_text(&meta.video_filename);
        let sub_info = format!(
            "Codec: {}  |  FPS: {:.2}  |  Size: {}x{}  |  Rendering FPS: {:.2}",
            meta.codec_name, meta.frame_rate, meta.sv_sz.0, meta.sv_sz.1, fps
        );
        self.video_subtitle.set_text(&sub_info);

        *self.cpu_chart.history.borrow_mut() = hw.cpu_history.clone();
        *self.gpu_chart.history.borrow_mut() = hw.gpu_history.clone();
        *self.vram_ring.pct.borrow_mut() = hw.vram_usage;
        *self.ram_ring.pct.borrow_mut() = hw.mem_usage;
    }

    pub fn to_layout_element(self: &Rc<Self>) -> EdgeLayoutElement {
        EdgeLayoutElement {
            drawable: self.clone(),
            l_edge: Edges::Left,
            l_margin: 40.0,
            r_edge: Edges::Right,
            r_margin: 40.0,
            t_edge: Edges::Top,
            t_margin: 0.0,
            b_edge: Edges::Top,
            b_margin: 55.0,
        }
    }

    pub fn toggle_sh(&self) {
        let mut sh = self.sh_state.borrow_mut();
        sh.reverse();
        sh.restart();
    }
}