logo
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
use std::{cell::Cell, rc::Rc};

use le::{
    layout::{Rect, Vec2},
    Layout,
};

use crate::{Buffer, Widget, WidgetType};

/// Records the layout of a widget
///
/// This is mostly useful if you wanted to do mouse-input, as you
/// can detect where a widget is as to detect if the mouse is over
/// said widget.
pub struct LayoutRecord<'a> {
    /// The widget to record.
    pub widget: WidgetType<'a>,
    /// The place to store the record.
    pub record: Rc<Cell<Rect>>,
}

impl<'a> LayoutRecord<'a> {
    /// Creates a new [`LayoutRecord`]
    pub fn new(widget: WidgetType<'a>, record: Rc<Cell<Rect>>) -> Self {
        Self { widget, record }
    }
}

impl Layout for LayoutRecord<'_> {
    fn width_for_height(&self, height: usize) -> usize {
        self.widget.width_for_height(height)
    }

    fn height_for_width(&self, width: usize) -> usize {
        self.widget.height_for_width(width)
    }

    fn prefered_size(&self) -> Vec2 {
        self.widget.prefered_size()
    }
}

impl Widget for LayoutRecord<'_> {
    fn render(&self, rect: Rect, buffer: &mut Buffer) {
        self.record.set(rect);
        self.widget.render(rect, buffer);
    }
}