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
|
extern crate gtk;
use gtk::*;
use std::process;
use std::sync::atomic::{ AtomicUsize, Ordering };
use std::sync::Arc;
// TODO listen for connect_delete_event
pub struct App {
pub window: Window,
pub header: Header,
pub content: Content,
}
pub struct Header {
pub container: HeaderBar,
pub reset: Button,
}
pub struct Content {
pub container: Box,
pub add: Button,
pub counter: Label,
}
pub struct Counter(AtomicUsize);
impl App {
fn new(counter: &Counter) -> App {
let window = Window::new(WindowType::Toplevel);
let header = Header::new();
let content = Content::new(counter);
// Define windows stuff
window.set_titlebar(&header.container);
window.set_title("Hello world!");
window.set_wmclass("test-app", "test app");
Window::set_default_icon_name("iconname");
window.add(&content.container);
window.connect_delete_event(move |_, _| {
main_quit();
Inhibit(false)
});
App { window, header, content}
}
}
impl Content {
fn new(counter: &Counter) -> Content {
let container = Box::new(Orientation::Horizontal, 0);
let counter = Label::new(counter.get_count().to_string().as_str());
let add = Button::new_with_label("Add");
add.get_style_context().map(|c| c.add_class("suggested-action"));
container.pack_start(&counter, true, true, 0);
container.pack_start(&add, false, false, 0);
Content { container, counter, add}
}
}
impl Header {
fn new() -> Header {
let container = HeaderBar::new();
let reset = Button::new_with_label("Reset");
container.set_title("Hello world!");
container.set_show_close_button(true);
reset.get_style_context().map(|c| c.add_class("destructive-action"));
container.pack_start(&reset);
Header { container, reset }
}
}
impl Counter {
fn new() -> Counter {
Counter(AtomicUsize::new(0))
}
fn get_count(&self) -> usize { self.0.load(Ordering::SeqCst) }
fn reset(&self) {
self.0.store(0, Ordering::SeqCst);
}
fn count(&self) -> usize {
let before = self.0.fetch_add(1, Ordering::SeqCst);
before + 1
}
}
fn main() {
println!("Hello, world!");
let c = Arc::new(Counter::new());
if gtk::init().is_err() {
eprintln!("failed to init GTK");
process::exit(1);
}
let app = App::new(&c);
{
let counter = c.clone();
let info = app.content.counter.clone();
app.header.reset.clone().connect_clicked(move |_| {
counter.reset();
info.set_label("0");
});
}
{
let counter = c.clone();
let info = app.content.counter.clone();
app.content.add.clone().connect_clicked(move |_| {
let new = counter.count();
info.set_label(new.to_string().as_str());
});
}
app.window.show_all();
gtk::main();
}
|