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
use crate::drawing::{Drawable, Plan};
use crate::interacting::{Event, Interactive};
use crate::layouts::{HasPlanner, Layout};
use skia_safe::{Canvas, Rect};
use std::any::Any;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum BoxArrangement {
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
    Center,
}

#[derive(Clone, Debug)]
pub struct BoxPadding {
    pub left: f32,
    pub right: f32,
    pub top: f32,
    pub bottom: f32,
}

impl BoxPadding {
    pub fn new(left: f32, right: f32, top: f32, bottom: f32) -> Self {
        Self {
            left,
            right,
            top,
            bottom,
        }
    }

    pub fn zero() -> Self {
        Self {
            left: 0.0,
            right: 0.0,
            top: 0.0,
            bottom: 0.0,
        }
    }
}

#[derive(Clone)]
pub struct BoxLayoutElement {
    pub drawable: Rc<dyn Drawable>,
    pub arrangement: BoxArrangement,
    pub padding: BoxPadding,
}

pub struct BoxLayout {
    pub childs: RefCell<BTreeMap<String, BoxLayoutElement>>,
    pub planner: RefCell<Option<Rc<dyn Layout>>>,

    // Caches the calculated absolute bounds for each child during the pre-draw phase.
    // Keyed by the raw address of the drawable trait object.
    calculated_rects: RefCell<BTreeMap<usize, Rect>>,
    enabled: RefCell<bool>,

    // Caches the dimensional plans to avoid recalculating every frame.
    cached_h_plan: RefCell<Option<Plan>>,
    cached_v_plan: RefCell<Option<Plan>>,
}

impl BoxLayout {
    pub fn new(is_enabled: bool) -> Rc<Self> {
        Rc::new(Self {
            childs: RefCell::new(BTreeMap::new()),
            planner: RefCell::new(None),
            calculated_rects: RefCell::new(BTreeMap::new()),
            enabled: RefCell::new(is_enabled),
            cached_h_plan: RefCell::new(None),
            cached_v_plan: RefCell::new(None),
        })
    }

    pub fn add_child(self: Rc<Self>, id: &str, e: BoxLayoutElement) -> Rc<Self> {
        e.drawable.set_planner(Some(self.clone()));
        self.childs.borrow_mut().insert(id.to_string(), e);
        self.replan();

        self
    }

    pub fn add_child_inplace(self: &Rc<Self>, id: &str, e: BoxLayoutElement) {
        e.drawable.set_planner(Some(self.clone()));
        self.childs.borrow_mut().insert(id.to_string(), e);
    }

    pub fn remove_child(&self, id: &str) {
        if let Some(c) = self.lookup_child(id) {
            c.drawable.set_planner(None);
            self.childs.borrow_mut().remove(id);
            self.replan();
        }
    }

    pub fn lookup_child(&self, id: &str) -> Option<BoxLayoutElement> {
        self.childs.borrow().get(id).cloned()
    }

    #[allow(clippy::too_many_arguments)]
    pub fn rewrite_child(&self, id: &str, r: BoxArrangement, p: BoxPadding) -> anyhow::Result<()> {
        if let Some(v) = self.childs.borrow_mut().get_mut(id) {
            v.arrangement = r;
            v.padding = p;
            self.replan();
            return Ok(());
        }
        anyhow::bail!("Id not found")
    }
}

impl HasPlanner for BoxLayout {
    fn planner(&self) -> &RefCell<Option<Rc<dyn Layout>>> {
        &self.planner
    }

    fn set_planner(&self, p: Option<Rc<dyn Layout>>) {
        *self.planner.borrow_mut() = p;
    }
}

impl Drawable for BoxLayout {
    fn draw(&self, canvas: &Canvas) -> anyhow::Result<()> {
        let parent_area = self.get_planned_drawing_area(canvas);
        let parent_w = parent_area.width();
        let parent_h = parent_area.height();

        if parent_w <= 0.0 || parent_h <= 0.0 {
            return Ok(());
        }

        let childs_borrow = self.childs.borrow();

        let mut min_x: Option<f32> = None;
        let mut max_x: Option<f32> = None;
        let mut min_y: Option<f32> = None;
        let mut max_y: Option<f32> = None;

        let mut update_x = |min: f32, max: f32| {
            min_x = Some(min_x.map_or(min, |v| v.min(min)));
            max_x = Some(max_x.map_or(max, |v| v.max(max)));
        };
        let mut update_y = |min: f32, max: f32| {
            min_y = Some(min_y.map_or(min, |v| v.min(min)));
            max_y = Some(max_y.map_or(max, |v| v.max(max)));
        };

        // Phase 1: Calculate the union boundary based on elements that provide explicit sizing.
        for el in childs_borrow.values() {
            // Ignore disabled children in metrics calculation
            if !el.drawable.is_enabled() {
                continue;
            }

            let h_plan = el.drawable.get_horizontal_plan();
            let v_plan = el.drawable.get_vertical_plan();

            let pad = &el.padding;
            let max_aw = (parent_w - pad.left - pad.right).max(0.0);
            let max_ah = (parent_h - pad.top - pad.bottom).max(0.0);

            let cw = match h_plan {
                Plan::Fit(w) => w,
                _ => max_aw,
            };
            let ch = match v_plan {
                Plan::Fit(h) => h,
                _ => max_ah,
            };

            let (x, y) = match el.arrangement {
                BoxArrangement::TopLeft => (parent_area.left + pad.left, parent_area.top + pad.top),
                BoxArrangement::TopRight => (
                    parent_area.right - pad.right - cw,
                    parent_area.top + pad.top,
                ),
                BoxArrangement::BottomLeft => (
                    parent_area.left + pad.left,
                    parent_area.bottom - pad.bottom - ch,
                ),
                BoxArrangement::BottomRight => (
                    parent_area.right - pad.right - cw,
                    parent_area.bottom - pad.bottom - ch,
                ),
                BoxArrangement::Center => {
                    let cx = parent_area.left + pad.left + (max_aw - cw) / 2.0;
                    let cy = parent_area.top + pad.top + (max_ah - ch) / 2.0;
                    (cx, cy)
                }
            };

            if h_plan != Plan::FillNegative {
                update_x(x - pad.left, x + cw + pad.right);
            }
            if v_plan != Plan::FillNegative {
                update_y(y - pad.top, y + ch + pad.bottom);
            }
        }

        let union_left = min_x.unwrap_or(parent_area.left);
        let union_right = max_x.unwrap_or(parent_area.right);
        let union_top = min_y.unwrap_or(parent_area.top);
        let union_bottom = max_y.unwrap_or(parent_area.bottom);

        let union_w = (union_right - union_left).max(0.0);
        let union_h = (union_bottom - union_top).max(0.0);

        // Phase 2: Compute absolute bounds and store them into the cache
        {
            let mut rects = self.calculated_rects.borrow_mut();
            rects.clear();

            for el in childs_borrow.values() {
                // Skip bound calculation for disabled children
                if !el.drawable.is_enabled() {
                    continue;
                }

                let target_ptr = el.drawable.as_ref() as *const _ as *const () as usize;

                let h_plan = el.drawable.get_horizontal_plan();
                let v_plan = el.drawable.get_vertical_plan();

                let pad = &el.padding;
                let max_aw = (parent_w - pad.left - pad.right).max(0.0);
                let max_ah = (parent_h - pad.top - pad.bottom).max(0.0);

                let cw = match h_plan {
                    Plan::Fit(w) => w,
                    Plan::FillPositive => max_aw,
                    Plan::FillNegative => union_w,
                };
                let ch = match v_plan {
                    Plan::Fit(h) => h,
                    Plan::FillPositive => max_ah,
                    Plan::FillNegative => union_h,
                };

                let (mut x, mut y) = match el.arrangement {
                    BoxArrangement::TopLeft => {
                        (parent_area.left + pad.left, parent_area.top + pad.top)
                    }
                    BoxArrangement::TopRight => (
                        parent_area.right - pad.right - cw,
                        parent_area.top + pad.top,
                    ),
                    BoxArrangement::BottomLeft => (
                        parent_area.left + pad.left,
                        parent_area.bottom - pad.bottom - ch,
                    ),
                    BoxArrangement::BottomRight => (
                        parent_area.right - pad.right - cw,
                        parent_area.bottom - pad.bottom - ch,
                    ),
                    BoxArrangement::Center => {
                        let cx = parent_area.left + pad.left + (max_aw - cw) / 2.0;
                        let cy = parent_area.top + pad.top + (max_ah - ch) / 2.0;
                        (cx, cy)
                    }
                };

                if h_plan == Plan::FillNegative {
                    x = union_left;
                }
                if v_plan == Plan::FillNegative {
                    y = union_top;
                }

                rects.insert(target_ptr, Rect::from_xywh(x, y, cw, ch));
            }
        }

        // Phase 3: Execute rendering based on BTreeMap key order
        for (key, el) in childs_borrow.iter() {
            // Ignore disabled children during the rendering pass
            if !el.drawable.is_enabled() {
                continue;
            }

            if let Err(e) = el.drawable.draw(canvas) {
                println!("BoxLayout draw error at child {}: {:?}", key, e);
            }
        }

        Ok(())
    }

    fn get_horizontal_plan(&self) -> Plan {
        if let Some(plan) = *self.cached_h_plan.borrow() {
            return plan;
        }

        let mut max_w = 0.0f32;
        let mut has_fill_positive = false;

        for el in self.childs.borrow().values() {
            if !el.drawable.is_enabled() {
                continue;
            }

            match el.drawable.get_horizontal_plan() {
                Plan::FillPositive => {
                    has_fill_positive = true;
                    break;
                }
                Plan::Fit(w) => {
                    max_w = max_w.max(w + el.padding.left + el.padding.right);
                }
                Plan::FillNegative => {
                    max_w = max_w.max(el.padding.left + el.padding.right);
                }
            }
        }

        let plan = if has_fill_positive {
            Plan::FillPositive
        } else {
            Plan::Fit(max_w)
        };

        *self.cached_h_plan.borrow_mut() = Some(plan);
        plan
    }

    fn get_vertical_plan(&self) -> Plan {
        if let Some(plan) = *self.cached_v_plan.borrow() {
            return plan;
        }

        let mut max_h = 0.0f32;
        let mut has_fill_positive = false;

        for el in self.childs.borrow().values() {
            if !el.drawable.is_enabled() {
                continue;
            }

            match el.drawable.get_vertical_plan() {
                Plan::FillPositive => {
                    has_fill_positive = true;
                    break;
                }
                Plan::Fit(h) => {
                    max_h = max_h.max(h + el.padding.top + el.padding.bottom);
                }
                Plan::FillNegative => {
                    max_h = max_h.max(el.padding.top + el.padding.bottom);
                }
            }
        }

        let plan = if has_fill_positive {
            Plan::FillPositive
        } else {
            Plan::Fit(max_h)
        };

        *self.cached_v_plan.borrow_mut() = Some(plan);
        plan
    }

    fn replan(&self) {
        // Invalidate self cache
        *self.cached_h_plan.borrow_mut() = None;
        *self.cached_v_plan.borrow_mut() = None;

        // Cascade replan signal to all children
        for el in self.childs.borrow().values() {
            el.drawable.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 Layout for BoxLayout {
    fn measure_child(&self, c: &dyn Drawable, _canvas: &Canvas) -> Rect {
        let target_ptr = c as *const _ as *const () as usize;
        self.calculated_rects
            .borrow()
            .get(&target_ptr)
            .cloned()
            .unwrap_or_else(|| Rect::new(0.0, 0.0, 0.0, 0.0))
    }

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

    fn foreach_child(&self, f: &mut dyn FnMut(&dyn Drawable)) {
        let ch = self.childs.borrow();
        for i in ch.iter() {
            f(i.1.drawable.as_ref());
        }
    }
}

impl Interactive for BoxLayout {
    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);
            }
        });
    }
}