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
use gtk::*;
use std::cell::RefCell;
use std::collections::{hash_map, HashMap};
use std::rc::Rc;
pub type RefGtk = Rc<RefCell<SuperGtk>>;
#[derive(Default)]
pub struct SuperGtk {
widgets_map: HashMap<&'static str, HashMap<&'static str, Widget>>,
}
impl SuperGtk {
pub fn new() -> RefGtk {
gtk::init().expect("Error while initilizing gtk");
Default::default()
}
pub fn run() {
gtk::main();
}
}
impl SuperGtk {
pub fn create_win(&mut self, id: &'static str) -> Window {
let win = Window::new(WindowType::Toplevel);
let win_c = win.clone();
self.add_to_map(id, win, "Window");
win_c
}
pub fn create_grid(&mut self, id: &'static str) -> Grid {
let grid = Grid::new();
let grid_c = grid.clone();
self.add_to_map(id, grid, "Grid");
grid_c
}
pub fn create_button(&mut self, id: &'static str) -> Button {
let button = Button::new();
let button_c = button.clone();
self.add_to_map(id, button, "Button");
button_c
}
pub fn create_label(&mut self, id: &'static str) -> Label {
let label = Label::new_with_mnemonic(None);
let label_c = label.clone();
self.add_to_map(id, label, "Label");
label_c
}
pub fn create_entry(&mut self, id: &'static str) -> Entry {
let entry = Entry::new();
let entry_c = entry.clone();
self.add_to_map(id, entry, "Entry");
entry_c
}
pub fn create_box(&mut self, id: &'static str, orientation: Orientation, spacing: i32) -> Box {
let gtk_box = Box::new(orientation, spacing);
let gtk_box_c = gtk_box.clone();
self.add_to_map(id, gtk_box, "Box");
gtk_box_c
}
pub fn create_headerbar(&mut self, id: &'static str) -> HeaderBar {
let header_bar = HeaderBar::new();
let header_bar_c = header_bar.clone();
self.add_to_map(id, header_bar, "HeaderBar");
header_bar_c
}
}
impl SuperGtk {
pub fn get_win(&self, id: &str) -> Window {
self.widgets_map[id]["Window"]
.clone()
.downcast::<Window>()
.expect("Not a win")
}
pub fn get_grid(&self, id: &str) -> Grid {
self.widgets_map[id]["Grid"]
.clone()
.downcast::<Grid>()
.expect("Not a grid")
}
pub fn get_button(&self, id: &str) -> Button {
self.widgets_map[id]["Button"]
.clone()
.downcast::<Button>()
.expect("Not a button")
}
pub fn get_label(&self, id: &str) -> Label {
self.widgets_map[id]["Label"]
.clone()
.downcast::<Label>()
.expect("Not a label")
}
pub fn get_entry(&self, id: &str) -> Entry {
self.widgets_map[id]["Entry"]
.clone()
.downcast::<Entry>()
.expect("Not a entry")
}
pub fn get_box(&self, id: &str) -> Box {
self.widgets_map[id]["Box"]
.clone()
.downcast::<Box>()
.expect("Not a box")
}
pub fn get_headerbar(&self, id: &str) -> HeaderBar {
self.widgets_map[id]["HeaderBar"]
.clone()
.downcast::<HeaderBar>()
.expect("Not a headerbar")
}
}
impl SuperGtk {
pub fn str_to_static(s: &str) -> &'static str {
std::boxed::Box::leak(s.to_string().into_boxed_str())
}
}
impl SuperGtk {
fn add_to_map<T: IsA<Widget>>(&mut self, id: &'static str, widget: T, w_type: &'static str) {
let widgets_map = &mut self.widgets_map;
match widgets_map.entry(id) {
hash_map::Entry::Occupied(widgets_group) => {
widgets_group.into_mut().insert(w_type, widget.upcast());
}
hash_map::Entry::Vacant(_) => {
let mut widget_group = HashMap::new();
widget_group.insert(w_type, widget.upcast());
widgets_map.insert(id, widget_group);
}
};
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
()
}
}