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
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::Signal,
rendering::backend::{WidgetRenderFactory, WidgetRenderHolder},
services::LayoutSystem,
widgets::Icon,
};
use super::{Element, WidgetComponent};
#[derive(Default, Debug, Clone)]
struct IconState {
path: String,
}
pub struct IconElement {
component: Rc<RefCell<WidgetComponent>>,
state: RefCell<IconState>,
pub onchange: Signal<String>,
pub renderer: Option<Rc<WidgetRenderHolder<Self>>>,
pub node: Node,
}
impl Debug for IconElement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("IconElement").finish()
}
}
impl IconElement {
pub fn new(widget: &Icon) -> Self {
let node = LayoutSystem::new_node(style::Style { ..default() }, vec![]).unwrap();
let component = WidgetComponent::get(widget.key.id());
let path = String::new();
Self {
component,
state: RefCell::new(IconState { path }),
onchange: Signal::new(),
renderer: WidgetRenderFactory::global().get::<Self>(),
node,
}
}
pub fn path(&self) -> String {
self.state.borrow().path.clone()
}
pub fn set_path(&self, path: String) {
self.state.borrow_mut().path = path.clone();
self.onchange.emit(&path);
}
}
impl AsRef<RefCell<WidgetComponent>> for IconElement {
fn as_ref(&self) -> &RefCell<WidgetComponent> {
self.component.as_ref()
}
}
impl Element for IconElement {
fn destroy(&self) {
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 {
}
}
}