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
use crate::widgets::{Widget, WidgetConfig};
use std::{
cell::RefCell,
rc::Rc,
sync::mpsc::{Receiver, Sender},
};
pub struct Response<R> {
pub channel: R,
pub(crate) _id: WidgetId,
pub(crate) _layer_id: LayerId,
}
pub(crate) type LayerNummerId = u64;
pub(crate) type WidgetNummerId = u64;
pub(crate) type LayerChannelSender = Sender<(LayerNummerId, LayerInstructions)>;
pub(crate) type LayerChannelReceiver = Receiver<(LayerNummerId, LayerInstructions)>;
pub(crate) type WidgetChannelSender = Sender<((LayerNummerId, WidgetNummerId), WidgetInstruction)>;
pub(crate) type WidgetChannelReceiver =
Receiver<((LayerNummerId, WidgetNummerId), WidgetInstruction)>;
pub(crate) enum LayerInstructions {
Drop,
AddWidget(Box<dyn Widget + 'static>, WidgetNummerId),
}
pub(crate) enum WidgetInstruction {
Drop,
}
pub struct SingularLayerId(pub(crate) LayerId);
impl SingularLayerId {
pub fn into_layer_id(self) -> LayerId {
self.0
}
pub fn add_widget<ReturnChannel, W: Widget + 'static>(
&mut self,
widget_config: impl WidgetConfig<ReturnChannel, W>,
) -> Response<ReturnChannel> {
self.0.add_widget(widget_config)
}
pub fn get_active(&self) -> bool {
self.0.get_active()
}
pub(crate) fn set_is_active(&mut self, is_active: bool) {
self.0.set_is_active(is_active)
}
}
#[derive(Clone)]
pub struct LayerId {
layer: Rc<InternalLayerId>,
pub(crate) is_active: Rc<RefCell<bool>>,
widget_id: Rc<RefCell<WidgetNummerId>>,
widget_channel: WidgetChannelSender,
}
impl LayerId {
pub(crate) fn new(
id: LayerNummerId,
channel: LayerChannelSender,
is_active: Rc<RefCell<bool>>,
widget_id: Rc<RefCell<WidgetNummerId>>,
widget_channel: WidgetChannelSender,
) -> Self {
let layer = Rc::new(InternalLayerId::new(id, channel));
Self {
layer,
is_active,
widget_id,
widget_channel,
}
}
pub fn add_widget<ReturnChannel, W: Widget + 'static>(
&mut self,
widget_config: impl WidgetConfig<ReturnChannel, W>,
) -> Response<ReturnChannel> {
let (widget, res) = widget_config.to_widget();
let mut widget_id = self.widget_id.borrow_mut();
*widget_id += 1;
let x = Response {
_layer_id: self.clone(),
channel: res,
_id: WidgetId::new(self.id(), *widget_id, self.widget_channel.clone()),
};
self.layer.as_ref().add_widget(widget, *widget_id);
x
}
pub(crate) fn id(&self) -> LayerNummerId {
self.layer.as_ref().id
}
pub fn set_is_active(&self, is_active: bool) {
self.is_active.replace(is_active);
}
pub fn get_active(&self) -> bool {
*self.is_active.borrow()
}
}
pub(crate) struct InternalLayerId {
pub(crate) id: LayerNummerId,
channel: LayerChannelSender,
}
impl InternalLayerId {
pub(crate) fn new(id: LayerNummerId, channel: LayerChannelSender) -> Self {
Self { id, channel }
}
pub(crate) fn add_widget(&self, widget: impl Widget + 'static, id: WidgetNummerId) {
let _ = self
.channel
.send((self.id, LayerInstructions::AddWidget(Box::new(widget), id)));
}
}
impl Drop for InternalLayerId {
fn drop(&mut self) {
let _ = self.channel.send((self.id, LayerInstructions::Drop));
}
}
pub(crate) struct WidgetId {
pub(crate) id: WidgetNummerId,
pub(crate) layer: LayerNummerId,
channel: WidgetChannelSender,
}
impl Drop for WidgetId {
fn drop(&mut self) {
let _ = self
.channel
.send(((self.layer, self.id), WidgetInstruction::Drop));
}
}
impl WidgetId {
pub(crate) fn new(
layer: LayerNummerId,
id: WidgetNummerId,
channel: WidgetChannelSender,
) -> Self {
Self { layer, id, channel }
}
}