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
use glib::Cast;
use gtk;
use gtk::{ContainerExt, IsA, Object, WidgetExt};
use relm_state::EventStream;
use super::{Component, DisplayVariant, Relm, create_widget, init_component};
use widget::Widget;
pub struct ContainerComponent<WIDGET: Container + Widget> {
component: Component<WIDGET>,
pub container: WIDGET::Container,
pub containers: WIDGET::Containers,
}
impl<WIDGET: Container + Widget> ContainerComponent<WIDGET> {
#[doc(hidden)]
pub fn new(component: Component<WIDGET>, container: WIDGET::Container, containers: WIDGET::Containers) -> Self {
ContainerComponent {
component,
container,
containers,
}
}
pub fn add<CHILDWIDGET: IsA<gtk::Widget>>(&self, widget: &CHILDWIDGET) {
self.container.add(widget);
}
pub fn add_widget<CHILDWIDGET, PARENTWIDGET>(&self, relm: &Relm<PARENTWIDGET>, model_param: CHILDWIDGET::ModelParam)
-> Component<CHILDWIDGET>
where CHILDWIDGET: Widget + 'static,
PARENTWIDGET: Widget,
WIDGET::Container: ContainerExt + IsA<gtk::Widget> + IsA<Object>,
{
let (widget, component, child_relm) = create_widget::<CHILDWIDGET>(relm.executor(), model_param);
let container = WIDGET::add_widget(self, &widget);
component.on_add(container);
init_component::<CHILDWIDGET>(widget.stream(), component, relm.executor(), &child_relm);
widget
}
pub fn emit(&self, msg: WIDGET::Msg) {
self.stream().emit(msg);
}
pub fn stream(&self) -> &EventStream<WIDGET::Msg> {
self.component.stream()
}
pub fn widget(&self) -> &WIDGET::Root {
self.component.widget()
}
}
pub trait Container: Widget {
type Container: Clone + IsA<gtk::Container> + IsA<Object> + IsA<gtk::Widget>;
type Containers;
fn add_widget<WIDGET: Widget>(container: &ContainerComponent<Self>, component: &Component<WIDGET>)
-> gtk::Container
{
container.container.add(component.widget());
container.container.clone().upcast()
}
fn container(&self) -> &Self::Container;
fn other_containers(&self) -> Self::Containers;
}
pub trait ContainerWidget {
fn add_container<CHILDWIDGET, WIDGET>(&self, relm: &Relm<WIDGET>, model_param: CHILDWIDGET::ModelParam)
-> ContainerComponent<CHILDWIDGET>
where CHILDWIDGET: Container + Widget + 'static,
CHILDWIDGET::Msg: DisplayVariant + 'static,
CHILDWIDGET::Root: IsA<gtk::Widget> + IsA<Object> + WidgetExt,
WIDGET: Widget;
fn add_widget<CHILDWIDGET, WIDGET>(&self, relm: &Relm<WIDGET>, model_param: CHILDWIDGET::ModelParam)
-> Component<CHILDWIDGET>
where CHILDWIDGET: Widget + 'static,
CHILDWIDGET::Msg: DisplayVariant + 'static,
CHILDWIDGET::Root: IsA<gtk::Widget> + IsA<Object> + WidgetExt,
WIDGET: Widget;
fn remove_widget<CHILDWIDGET>(&self, component: Component<CHILDWIDGET>)
where CHILDWIDGET: Widget,
CHILDWIDGET::Root: IsA<gtk::Widget>;
}
impl<W: Clone + ContainerExt + IsA<gtk::Widget> + IsA<Object>> ContainerWidget for W {
fn add_container<CHILDWIDGET, WIDGET>(&self, relm: &Relm<WIDGET>, model_param: CHILDWIDGET::ModelParam)
-> ContainerComponent<CHILDWIDGET>
where CHILDWIDGET: Container + Widget + 'static,
CHILDWIDGET::Msg: DisplayVariant + 'static,
CHILDWIDGET::Root: IsA<gtk::Widget> + IsA<Object> + WidgetExt,
WIDGET: Widget,
{
let (widget, component, child_relm) = create_widget::<CHILDWIDGET>(relm.executor(), model_param);
let container = component.container().clone();
let containers = component.other_containers();
let root = component.root().clone();
self.add(&root);
component.on_add(self.clone());
init_component::<CHILDWIDGET>(widget.stream(), component, relm.executor(), &child_relm);
ContainerComponent::new(widget, container, containers)
}
fn add_widget<CHILDWIDGET, WIDGET>(&self, relm: &Relm<WIDGET>, model_param: CHILDWIDGET::ModelParam)
-> Component<CHILDWIDGET>
where CHILDWIDGET: Widget + 'static,
CHILDWIDGET::Msg: DisplayVariant + 'static,
CHILDWIDGET::Root: IsA<gtk::Widget> + IsA<Object> + WidgetExt,
WIDGET: Widget,
{
let (widget, component, child_relm) = create_widget::<CHILDWIDGET>(relm.executor(), model_param);
self.add(widget.widget());
component.on_add(self.clone());
init_component::<CHILDWIDGET>(widget.stream(), component, relm.executor(), &child_relm);
widget
}
fn remove_widget<WIDGET>(&self, component: Component<WIDGET>)
where WIDGET: Widget,
WIDGET::Root: IsA<gtk::Widget>,
{
self.remove(component.widget());
}
}