-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rs
465 lines (395 loc) · 15.4 KB
/
app.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use gdk;
use gio::{self, prelude::*};
use glib;
use gtk::{self, prelude::*};
use crate::about_dialog::show_about_dialog;
use crate::header_bar::HeaderBar;
use crate::overlay::Overlay;
use crate::pipeline::Pipeline;
use crate::settings::show_settings_dialog;
use crate::utils;
use std::cell::RefCell;
use std::error;
use std::ops;
use std::rc::{Rc, Weak};
// Here we specify our custom, application specific CSS styles for various widgets
const STYLE: &str = "
#countdown-label {
background-color: rgba(192, 192, 192, 0.8);
color: black;
font-size: 42pt;
font-weight: bold;
}";
// Our refcounted application struct for containing all the state we have to carry around.
//
// This represents our main application window.
#[derive(Clone)]
pub struct App(Rc<AppInner>);
// Deref into the contained struct to make usage a bit more ergonomic
impl ops::Deref for App {
type Target = AppInner;
fn deref(&self) -> &AppInner {
&*self.0
}
}
// Weak reference to our application struct
//
// Weak references are important to prevent reference cycles. Reference cycles are cases where
// struct A references directly or indirectly struct B, and struct B references struct A again
// while both are using reference counting.
pub struct AppWeak(Weak<AppInner>);
impl AppWeak {
// Upgrade to a strong reference if it still exists
pub fn upgrade(&self) -> Option<App> {
self.0.upgrade().map(App)
}
}
pub struct AppInner {
main_window: gtk::ApplicationWindow,
header_bar: HeaderBar,
overlay: Overlay,
pipeline: Pipeline,
timer: RefCell<Option<SnapshotTimer>>,
}
// Helper struct for the snapshot timer
//
// Allows counting down and removes the timeout source on Drop
struct SnapshotTimer {
remaining: u32,
// This needs to be Option because we need to be able to take
// the value out in Drop::drop() removing the timeout id
timeout_id: Option<glib::source::SourceId>,
}
impl SnapshotTimer {
fn new(remaining: u32, timeout_id: glib::SourceId) -> Self {
Self {
remaining,
timeout_id: Some(timeout_id),
}
}
fn tick(&mut self) -> u32 {
assert!(self.remaining > 0);
self.remaining -= 1;
self.remaining
}
}
impl Drop for SnapshotTimer {
fn drop(&mut self) {
glib::source::source_remove(self.timeout_id.take().expect("No timeout id"));
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SnapshotState {
Idle,
TimerRunning,
}
impl<'a> From<&'a glib::Variant> for SnapshotState {
fn from(v: &glib::Variant) -> SnapshotState {
v.get::<bool>().expect("Invalid snapshot state type").into()
}
}
impl From<bool> for SnapshotState {
fn from(v: bool) -> SnapshotState {
match v {
false => SnapshotState::Idle,
true => SnapshotState::TimerRunning,
}
}
}
impl From<SnapshotState> for glib::Variant {
fn from(v: SnapshotState) -> glib::Variant {
match v {
SnapshotState::Idle => false.to_variant(),
SnapshotState::TimerRunning => true.to_variant(),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RecordState {
Idle,
Recording,
}
impl<'a> From<&'a glib::Variant> for RecordState {
fn from(v: &glib::Variant) -> RecordState {
v.get::<bool>().expect("Invalid record state type").into()
}
}
impl From<bool> for RecordState {
fn from(v: bool) -> RecordState {
match v {
false => RecordState::Idle,
true => RecordState::Recording,
}
}
}
impl From<RecordState> for glib::Variant {
fn from(v: RecordState) -> glib::Variant {
match v {
RecordState::Idle => false.to_variant(),
RecordState::Recording => true.to_variant(),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Action {
Quit,
Settings,
About,
Snapshot(SnapshotState),
Record(RecordState),
}
impl App {
fn new(application: >k::Application) -> Result<App, Box<dyn error::Error>> {
// Here build the UI but don't show it yet
let window = gtk::ApplicationWindow::new(application);
window.set_title("WebCam Viewer");
window.set_border_width(5);
window.set_position(gtk::WindowPosition::Center);
window.set_default_size(840, 480);
// Create headerbar for the application window
let header_bar = HeaderBar::new(&window);
// Create the pipeline and if that fail return
let pipeline =
Pipeline::new().map_err(|err| format!("Error creating pipeline: {:?}", err))?;
// Create an overlay for showing the seconds until a snapshot
// This is hidden while we're not doing a countdown
let overlay = Overlay::new(&window, &pipeline.get_widget());
let app = App(Rc::new(AppInner {
main_window: window,
header_bar,
overlay,
pipeline,
timer: RefCell::new(None),
}));
// Create the application actions
Action::create(&app, &application);
Ok(app)
}
// Downgrade to a weak reference
pub fn downgrade(&self) -> AppWeak {
AppWeak(Rc::downgrade(&self.0))
}
pub fn on_startup(application: >k::Application) {
// Load our custom CSS style-sheet and set it as the application specific style-sheet for
// this whole application
let provider = gtk::CssProvider::new();
provider
.load_from_data(STYLE.as_bytes())
.expect("Failed to load CSS");
gtk::StyleContext::add_provider_for_screen(
&gdk::Screen::get_default().expect("Error initializing gtk css provider."),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
// Create application and error out if that fails for whatever reason
let app = match App::new(application) {
Ok(app) => app,
Err(err) => {
utils::show_error_dialog(
true,
format!("Error creating application: {}", err).as_str(),
);
return;
}
};
// When the application is activated show the UI. This happens when the first process is
// started, and in the first process whenever a second process is started
let app_weak = app.downgrade();
application.connect_activate(move |_| {
let app = upgrade_weak!(app_weak);
app.on_activate();
});
// When the application is shut down we drop our app struct
//
// It has to be stored in a RefCell<Option<T>> to be able to pass it to a Fn closure. With
// FnOnce this wouldn't be needed and the closure will only be called once, but the
// bindings define all signal handlers as Fn.
let app_container = RefCell::new(Some(app));
application.connect_shutdown(move |_| {
let app = app_container
.borrow_mut()
.take()
.expect("Shutdown called multiple times");
app.on_shutdown();
});
}
// Called on the first application instance whenever the first application instance is started,
// or any future second application instance
fn on_activate(&self) {
// Show our window and bring it to the foreground
self.main_window.show_all();
// Have to call this instead of present() because of
// https://gitlab.gnome.org/GNOME/gtk/issues/624
self.main_window
.present_with_time((glib::get_monotonic_time() / 1000) as u32);
// Once the UI is shown, start the GStreamer pipeline. If
// an error happens, we immediately shut down
if let Err(err) = self.pipeline.start() {
utils::show_error_dialog(
true,
format!("Failed to set pipeline to playing: {}", err).as_str(),
);
}
}
// Called when the application shuts down. We drop our app struct here
fn on_shutdown(self) {
// This might fail but as we shut down right now anyway this doesn't matter
// TODO: If a recording is currently running we would like to finish that first
// before quitting the pipeline and shutting down the pipeline.
let _ = self.pipeline.stop();
}
// When the snapshot button is clicked it triggers the snapshot action, which calls this
// function here. We have to stop an existing timer here, start a new timer or immediately
// snapshot.
fn on_snapshot_state_changed(&self, new_state: SnapshotState) {
let settings = utils::load_settings();
// Stop snapshot timer, if any, and return
if new_state == SnapshotState::Idle {
let _ = self.timer.borrow_mut().take();
self.overlay.set_label_visible(false);
return;
}
if settings.timer_length == 0 {
// Take a snapshot immediately if there's no timer length or start the timer
// Set the togglebutton unchecked again immediately
self.header_bar.set_snapshot_active(false);
if let Err(err) = self.pipeline.take_snapshot() {
utils::show_error_dialog(
false,
format!("Failed to take snapshot: {}", err).as_str(),
);
}
} else {
// Start a snapshot timer
// Make the overlay visible, remember how much we have to count down and start our
// timeout for the timer
self.overlay.set_label_visible(true);
self.overlay
.set_label_text(&settings.timer_length.to_string());
let app_weak = self.downgrade();
// The closure is called every 1000ms
let timeout_id = gtk::timeout_add(1000, move || {
let app = upgrade_weak!(app_weak, glib::Continue(false));
let remaining = app
.timer
.borrow_mut()
.as_mut()
.map(|t| t.tick())
.unwrap_or(0);
if remaining == 0 {
// Set the togglebutton unchecked again and make the overlay text invisible
app.overlay.set_label_visible(false);
// Remove timer
let _ = app.timer.borrow_mut().take();
// This directly calls the surrounding function again and then removes the
// timer
app.header_bar.set_snapshot_active(false);
if let Err(err) = app.pipeline.take_snapshot() {
utils::show_error_dialog(
false,
format!("Failed to take snapshot: {}", err).as_str(),
);
}
glib::Continue(false)
} else {
app.overlay.set_label_text(&remaining.to_string());
glib::Continue(true)
}
});
*self.timer.borrow_mut() = Some(SnapshotTimer::new(settings.timer_length, timeout_id));
}
}
// When the record button is clicked it triggers the record action, which will call this.
// We have to start or stop recording here
fn on_record_state_changed(&self, new_state: RecordState) {
// Start/stop recording based on button active'ness
match new_state {
RecordState::Recording => {
if let Err(err) = self.pipeline.start_recording() {
utils::show_error_dialog(
false,
format!("Failed to start recording: {}", err).as_str(),
);
self.header_bar.set_record_active(false);
}
}
RecordState::Idle => self.pipeline.stop_recording(),
}
}
}
impl Action {
// The full action name as is used in e.g. menu models
pub fn full_name(self) -> &'static str {
match self {
Action::Quit => "app.quit",
Action::Settings => "app.settings",
Action::About => "app.about",
Action::Snapshot(_) => "app.snapshot",
Action::Record(_) => "app.record",
}
}
// Create our application actions here
//
// These are connected to our buttons and can be triggered by the buttons, as well as remotely
fn create(app: &App, application: >k::Application) {
// When activated, show a settings dialog
let settings = gio::SimpleAction::new("settings", None);
let weak_application = application.downgrade();
settings.connect_activate(move |_action, _parameter| {
let application = upgrade_weak!(weak_application);
show_settings_dialog(&application);
});
application.add_action(&settings);
// about action: when activated it will show an about dialog
let about = gio::SimpleAction::new("about", None);
let weak_application = application.downgrade();
about.connect_activate(move |_action, _parameter| {
let application = upgrade_weak!(weak_application);
show_about_dialog(&application);
});
application.add_action(&about);
// When activated, shuts down the application
let quit = gio::SimpleAction::new("quit", None);
let weak_application = application.downgrade();
quit.connect_activate(move |_action, _parameter| {
let application = upgrade_weak!(weak_application);
application.quit();
});
application.add_action(&quit);
// And add an accelerator for triggering the action on ctrl+q
application.set_accels_for_action(Action::Quit.full_name(), &["<Primary>Q"]);
// snapshot action: changes state between true/false
let snapshot =
gio::SimpleAction::new_stateful("snapshot", None, &SnapshotState::Idle.into());
let weak_app = app.downgrade();
snapshot.connect_change_state(move |action, state| {
let app = upgrade_weak!(weak_app);
let state = state.expect("No state provided");
app.on_snapshot_state_changed(state.into());
// Let the action store the new state
action.set_state(state);
});
application.add_action(&snapshot);
// record action: changes state between true/false
let record = gio::SimpleAction::new_stateful("record", None, &RecordState::Idle.into());
let weak_app = app.downgrade();
record.connect_change_state(move |action, state| {
let app = upgrade_weak!(weak_app);
let state = state.expect("No state provided");
app.on_record_state_changed(state.into());
// Let the action store the new state
action.set_state(state);
});
application.add_action(&record);
}
// Triggers the provided action on the application
pub fn trigger<A: IsA<gio::Application> + IsA<gio::ActionGroup>>(self, app: &A) {
match self {
Action::Quit => app.activate_action("quit", None),
Action::Settings => app.activate_action("settings", None),
Action::About => app.activate_action("about", None),
Action::Snapshot(new_state) => app.change_action_state("snapshot", &new_state.into()),
Action::Record(new_state) => app.change_action_state("record", &new_state.into()),
}
}
}