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
use gtk::{
prelude::{Cast, StaticType, WidgetExt},
traits::StyleContextExt,
};
pub trait RelmWidgetExt {
fn set_size_group(&self, size_group: >k::SizeGroup);
fn toplevel_window(&self) -> Option<gtk::Window>;
fn set_margin_all(&self, margin: i32);
fn set_class_active(&self, class: &str, active: bool);
fn inline_css(&self, style: &str);
}
impl<T: gtk::glib::IsA<gtk::Widget>> RelmWidgetExt for T {
fn set_size_group(&self, size_group: >k::SizeGroup) {
size_group.add_widget(self);
}
fn toplevel_window(&self) -> Option<gtk::Window> {
self.ancestor(gtk::Window::static_type())
.and_then(|widget| widget.dynamic_cast::<gtk::Window>().ok())
}
fn set_margin_all(&self, margin: i32) {
self.set_margin_start(margin);
self.set_margin_end(margin);
self.set_margin_top(margin);
self.set_margin_bottom(margin);
}
fn set_class_active(&self, class: &str, active: bool) {
if active {
self.add_css_class(class);
} else {
self.remove_css_class(class);
}
}
fn inline_css(&self, style: &str) {
let context = self.style_context();
let provider = gtk::CssProvider::new();
let data = if style.ends_with(';') {
[b"*{", style.as_bytes(), b"}"].concat()
} else {
[b"*{", style.as_bytes(), b";}"].concat()
};
provider.load_from_data(&data);
context.add_provider(&provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION + 1);
}
}