This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
clock.rs
82 lines (66 loc) · 2.09 KB
/
clock.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
//! # Clock Sample
//!
//! This sample demonstrates how to use gtk::timeout_add_seconds to run
//! a periodic task, implementing a clock in this example.
extern crate gio;
extern crate gtk;
extern crate chrono;
use gio::prelude::*;
use gtk::prelude::*;
use std::env::args;
use chrono::Local;
// make moving clones into closures more convenient
macro_rules! clone {
(@param _) => ( _ );
(@param $x:ident) => ( $x );
($($n:ident),+ => move || $body:expr) => (
{
$( let $n = $n.clone(); )+
move || $body
}
);
($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
{
$( let $n = $n.clone(); )+
move |$(clone!(@param $p),)+| $body
}
);
}
fn current_time() -> String {
return format!("{}", Local::now().format("%Y-%m-%d %H:%M:%S"));
}
fn build_ui(application: >k::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title("First GTK+ Clock");
window.set_border_width(10);
window.set_position(gtk::WindowPosition::Center);
window.set_default_size(260, 40);
window.connect_delete_event(clone!(window => move |_, _| {
window.destroy();
Inhibit(false)
}));
let time = current_time();
let label = gtk::Label::new(None);
label.set_text(&time);
window.add(&label);
window.show_all();
// we are using a closure to capture the label (else we could also use a normal function)
let tick = move || {
let time = current_time();
label.set_text(&time);
// we could return gtk::Continue(false) to stop our clock after this tick
gtk::Continue(true)
};
// executes the closure once every second
gtk::timeout_add_seconds(1, tick);
}
fn main() {
let application = gtk::Application::new("com.github.clock",
gio::ApplicationFlags::empty())
.expect("Initialization failed...");
application.connect_startup(|app| {
build_ui(app);
});
application.connect_activate(|_| {});
application.run(&args().collect::<Vec<_>>());
}