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
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
use cgmath::Point2;
use std::{
    cell::RefCell,
    default::default,
    fmt::{Debug, Formatter, Result},
    rc::Rc,
};
use stretch::{node::Node, style};

use crate::prelude::Singleton;

use crate::{
    foundation::{properties::LabelProperties, Signal},
    rendering::backend::{WidgetRenderFactory, WidgetRenderHolder},
    services::LayoutSystem,
};

use super::{Element, WidgetComponent};

#[derive(Default, Debug, Clone)]
struct LabelState {
    /// The text displayed by the label
    text: String,
}

/// A simple label control
/// Additional Signals: onchange
pub struct LabelElement {
    component: Rc<RefCell<WidgetComponent>>,
    state: RefCell<LabelState>,
    /// Emitted whenever text is changed.
    /// `fn(text:String)`
    pub onchange: Signal<String>,

    // The concrete renderer for this control instance
    pub renderer: Option<Rc<WidgetRenderHolder<Self>>>,
    // The node in layout system
    pub node: Node,
}

impl Debug for LabelElement {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.debug_struct("LabelElement").finish()
    }
}

impl LabelElement {
    pub fn new(widget: &LabelProperties) -> Self {
        let node = LayoutSystem::new_node(style::Style { ..default() }, vec![]).unwrap();

        let component = WidgetComponent::get(widget.key.id());

        // if let Some(ref onclick) = widget.onclick {
        //     component.mouse_input = true;
        //     // let handler = onclick.clone();
        //     // component.onmouseup.listen(box move |e| handler.emit(e));
        // }

        let text = widget.text.clone();

        Self {
            component,
            state: RefCell::new(LabelState { text }),
            onchange: Signal::new(),
            renderer: WidgetRenderFactory::global().get::<Self>(),
            node,
        }
    }

    //Internal

    /// The text displayed by the label
    pub fn text(&self) -> String {
        self.state.borrow().text.clone()
    }

    /// The text displayed by the label
    pub fn set_text(&self, str: String) {
        self.state.borrow_mut().text = str.clone();

        self.onchange.emit(&str);
    }
}

impl AsRef<RefCell<WidgetComponent>> for LabelElement {
    fn as_ref(&self) -> &RefCell<WidgetComponent> {
        self.component.as_ref()
    }
}

impl Element for LabelElement {
    fn destroy(&self) {
        // self.base.destroy();

        self.onchange.clear();
    }

    fn node(&self) -> Option<Node> {
        Some(self.node)
    }

    fn relayout(&self, origin: Point2<f32>) {
        let update_childs = match LayoutSystem::layout(self.node) {
            Ok(layout) => {
                let mut comp = self.as_ref().borrow_mut();
                comp.x = layout.location.x + origin.x;
                comp.y = layout.location.y + origin.y;
                comp.w = layout.size.width;
                comp.h = layout.size.height;

                true
            }
            Err(e) => {
                log::error!("{}", e);
                false
            }
        };

        if update_childs {
            // self.leading.relayout();
            // self.title.relayout();
            // self.flexible_space.relayout();
        }
    }
}