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
use gtk::glib::{self, Sender};
use gtk::prelude::{IsA, WidgetExt};
use std::{
cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut},
fmt::{Debug, Display},
rc::Rc,
};
use crate::WidgetPlus;
#[derive(Debug)]
pub enum MicroComponentError {
Borrow(BorrowError),
BorrowMut(BorrowMutError),
}
impl From<BorrowError> for MicroComponentError {
fn from(err: BorrowError) -> Self {
MicroComponentError::Borrow(err)
}
}
impl From<BorrowMutError> for MicroComponentError {
fn from(err: BorrowMutError) -> Self {
MicroComponentError::BorrowMut(err)
}
}
impl Display for MicroComponentError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MicroComponentError::Borrow(err) => f.write_fmt(format_args!("{}", err)),
MicroComponentError::BorrowMut(err) => f.write_fmt(format_args!("{}", err)),
}
}
}
#[derive(Debug)]
pub struct MicroComponent<Model: MicroModel> {
model: Rc<RefCell<Model>>,
widgets: Rc<RefCell<Model::Widgets>>,
root_widget: <Model::Widgets as MicroWidgets<Model>>::Root,
sender: Sender<Model::Msg>,
}
impl<Model> MicroComponent<Model>
where
Model::Widgets: MicroWidgets<Model> + 'static,
Model::Msg: 'static,
Model::Data: 'static,
Model: MicroModel + 'static,
{
pub fn new(model: Model, data: Model::Data) -> Self {
gtk::init().unwrap();
let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
let widgets = Model::Widgets::init_view(&model, sender.clone());
let root_widget = widgets.root_widget();
let cloned_sender = sender.clone();
let shared_widgets = Rc::new(RefCell::new(widgets));
let handler_widgets = shared_widgets.clone();
let shared_model = Rc::new(RefCell::new(model));
let handler_model = shared_model.clone();
{
let context = glib::MainContext::default();
let _guard = context
.acquire()
.expect("Couldn't acquire glib main context");
receiver.attach(Some(&context), move |msg: Model::Msg| {
if let Ok(ref mut model) = handler_model.try_borrow_mut() {
model.update(msg, &data, sender.clone());
if let Ok(ref mut widgets) = handler_widgets.try_borrow_mut() {
widgets.view(model, sender.clone());
} else {
log::warn!("Could not mutably borrow the widgets. Make sure you drop all references to widgets after use");
}
} else {
log::warn!("Could not mutably borrow the model. Make sure you drop all references to model after use")
}
glib::Continue(true)
});
}
MicroComponent {
model: shared_model,
widgets: shared_widgets,
root_widget,
sender: cloned_sender,
}
}
pub fn update_view(&self) -> Result<(), MicroComponentError> {
let mut widgets = self.widgets()?;
let model = self.model()?;
widgets.view(&model, self.sender());
Result::Ok(())
}
pub fn model(&self) -> Result<Ref<'_, Model>, BorrowError> {
self.model.as_ref().try_borrow()
}
pub fn model_mut(&self) -> Result<RefMut<'_, Model>, BorrowMutError> {
self.model.as_ref().try_borrow_mut()
}
pub fn widgets(&self) -> Result<RefMut<'_, Model::Widgets>, BorrowMutError> {
self.widgets.as_ref().try_borrow_mut()
}
pub fn send(&self, msg: Model::Msg) -> Result<(), std::sync::mpsc::SendError<Model::Msg>> {
self.sender.send(msg)
}
pub fn sender(&self) -> Sender<Model::Msg> {
self.sender.clone()
}
pub fn root_widget(&self) -> &<Model::Widgets as MicroWidgets<Model>>::Root {
&self.root_widget
}
}
impl<Model> MicroComponent<Model>
where
Model::Widgets: MicroWidgets<Model> + 'static,
Model::Msg: 'static,
Model::Data: 'static,
Model: MicroModel + 'static,
<Model::Widgets as MicroWidgets<Model>>::Root: IsA<gtk::Widget>,
{
pub fn is_connected(&self) -> bool {
self.root_widget.parent().is_some()
}
pub fn try_diconnect_root(&self) -> bool {
if let Some(parent) = &self.root_widget.parent() {
parent.try_remove(&self.root_widget)
} else {
false
}
}
}
pub trait MicroModel {
type Msg: 'static;
type Widgets: MicroWidgets<Self> + Debug;
type Data;
fn update(&mut self, msg: Self::Msg, data: &Self::Data, sender: Sender<Self::Msg>);
}
pub trait MicroWidgets<Model: MicroModel + ?Sized> {
type Root: std::fmt::Debug;
fn init_view(model: &Model, sender: Sender<Model::Msg>) -> Self;
fn view(&mut self, model: &Model, sender: Sender<Model::Msg>);
fn root_widget(&self) -> Self::Root;
}