This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.rs
214 lines (178 loc) · 5.98 KB
/
main.rs
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
use gtk::prelude::*;
use gtk::{gio, glib};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::thread;
use std::time::Duration;
pub fn main() {
glib::set_program_name(Some("Progress Tracker"));
let application = gtk::Application::new(
Some("com.github.progress-tracker"),
gio::ApplicationFlags::empty(),
);
application.connect_startup(|app| {
let application = Application::new(app);
let application_container = RefCell::new(Some(application));
app.connect_shutdown(move |_| {
let application = application_container
.borrow_mut()
.take()
.expect("Shutdown called multiple times");
// Here we could do whatever we need to do for shutdown now
drop(application);
});
});
application.connect_activate(|_| {});
application.run();
}
pub struct Application {
pub widgets: Rc<Widgets>,
}
impl Application {
pub fn new(app: >k::Application) -> Self {
let app = Application {
widgets: Rc::new(Widgets::new(app)),
};
app.connect_progress();
app
}
fn connect_progress(&self) {
let active = Rc::new(Cell::new(false));
self.widgets.main_view.button.connect_clicked(
glib::clone!(@weak self.widgets as widgets => move |_| {
if active.get() {
return;
}
active.set(true);
let (tx, rx) = async_channel::bounded(10);
thread::spawn(move || {
for v in 1..=10 {
let _ = tx.send_blocking(Some(v));
thread::sleep(Duration::from_millis(500));
}
let _ = tx.send_blocking(None);
});
let active = active.clone();
glib::MainContext::default().spawn_local(async move {
while let Ok(Some(value)) = rx.recv().await {
widgets
.main_view
.progress
.set_fraction(f64::from(value) / 10.0);
if value == 10 {
widgets
.view_stack
.set_visible_child(&widgets.complete_view.container);
glib::timeout_add_local(Duration::from_millis(1500), glib::clone!(@weak widgets => @default-return glib::ControlFlow::Break, move || {
widgets.main_view.progress.set_fraction(0.0);
widgets
.view_stack
.set_visible_child(&widgets.main_view.container);
glib::ControlFlow::Break
}));
}
}
active.set(false);
});
}),
);
}
}
pub struct Widgets {
pub window: gtk::ApplicationWindow,
pub header: Header,
pub view_stack: gtk::Stack,
pub main_view: MainView,
pub complete_view: CompleteView,
}
impl Widgets {
pub fn new(application: >k::Application) -> Self {
let complete_view = CompleteView::new();
let main_view = MainView::new();
let view_stack = gtk::Stack::new();
view_stack.set_border_width(6);
view_stack.set_vexpand(true);
view_stack.set_hexpand(true);
view_stack.add(&main_view.container);
view_stack.add(&complete_view.container);
let header = Header::new();
let window = gtk::ApplicationWindow::new(application);
window.set_icon_name(Some("package-x-generic"));
window.set_window_position(gtk::WindowPosition::Center);
window.set_titlebar(Some(&header.container));
window.add(&view_stack);
window.show_all();
window.set_default_size(500, 250);
window.connect_delete_event(move |window, _| {
window.close();
glib::Propagation::Proceed
});
Self {
window,
header,
view_stack,
main_view,
complete_view,
}
}
}
pub struct Header {
container: gtk::HeaderBar,
}
impl Header {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let container = gtk::HeaderBar::new();
container.set_title(Some("Progress Tracker"));
container.set_show_close_button(true);
Self { container }
}
}
pub struct CompleteView {
pub container: gtk::Grid,
}
impl CompleteView {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let label = gtk::Label::new(None);
label.set_markup("Task complete");
label.set_halign(gtk::Align::Center);
label.set_valign(gtk::Align::Center);
label.set_vexpand(true);
label.set_hexpand(true);
let container = gtk::Grid::new();
container.set_vexpand(true);
container.set_hexpand(true);
container.add(&label);
Self { container }
}
}
pub struct MainView {
pub container: gtk::Grid,
pub progress: gtk::ProgressBar,
pub button: gtk::Button,
}
impl MainView {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let progress = gtk::ProgressBar::new();
progress.set_text(Some("Progress Bar"));
progress.set_show_text(true);
progress.set_hexpand(true);
let button = gtk::Button::new();
button.set_label("start");
button.set_halign(gtk::Align::Center);
let container = gtk::Grid::new();
container.attach(&progress, 0, 0, 1, 1);
container.attach(&button, 0, 1, 1, 1);
container.set_row_spacing(12);
container.set_border_width(6);
container.set_vexpand(true);
container.set_hexpand(true);
Self {
container,
progress,
button,
}
}
}