|
| 1 | +//! Track progress with a background thread and a channel. |
| 2 | +
|
| 3 | +#[macro_use] |
| 4 | +extern crate cascade; |
| 5 | +extern crate gio; |
| 6 | +extern crate gtk; |
| 7 | + |
| 8 | +use gio::prelude::*; |
| 9 | +use gtk::prelude::*; |
| 10 | + |
| 11 | +use std::env::args; |
| 12 | +use std::rc::Rc; |
| 13 | +use std::thread; |
| 14 | +use std::time::Duration; |
| 15 | +use std::sync::mpsc::{self, TryRecvError}; |
| 16 | + |
| 17 | +pub fn main() { |
| 18 | + let application = gtk::Application::new( |
| 19 | + "com.github.progress-tracker", |
| 20 | + gio::ApplicationFlags::empty() |
| 21 | + ).expect("initialization failed"); |
| 22 | + |
| 23 | + application.connect_startup(|app| { |
| 24 | + Application::new(app); |
| 25 | + }); |
| 26 | + |
| 27 | + application.connect_activate(|_| {}); |
| 28 | + application.run(&args().collect::<Vec<_>>()); |
| 29 | +} |
| 30 | + |
| 31 | +pub struct Application { |
| 32 | + pub widgets: Rc<Widgets>, |
| 33 | +} |
| 34 | + |
| 35 | +impl Application { |
| 36 | + pub fn new(app: >k::Application) -> Self { |
| 37 | + let app = Application { |
| 38 | + widgets: Rc::new(Widgets::new(app)) |
| 39 | + }; |
| 40 | + |
| 41 | + app |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +pub struct Widgets { |
| 46 | + pub window: gtk::ApplicationWindow, |
| 47 | + pub header: Header, |
| 48 | + pub main_view: MainView |
| 49 | +} |
| 50 | + |
| 51 | +impl Widgets { |
| 52 | + pub fn new(application: >k::Application) -> Self { |
| 53 | + let main_view = MainView::new(); |
| 54 | + let header = Header::new(); |
| 55 | + |
| 56 | + let window = cascade! { |
| 57 | + gtk::ApplicationWindow::new(application); |
| 58 | + ..set_icon_name("package-x-generic"); |
| 59 | + ..set_property_window_position(gtk::WindowPosition::Center); |
| 60 | + ..set_titlebar(&header.container); |
| 61 | + ..add(&main_view.container); |
| 62 | + ..show_all(); |
| 63 | + ..set_default_size(500, 250); |
| 64 | + ..connect_delete_event(move |window, _| { |
| 65 | + window.destroy(); |
| 66 | + Inhibit(false) |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + Widgets { window, header, main_view } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub struct Header { |
| 75 | + container: gtk::HeaderBar |
| 76 | +} |
| 77 | + |
| 78 | +impl Header { |
| 79 | + pub fn new() -> Self { |
| 80 | + let container = cascade! { |
| 81 | + gtk::HeaderBar::new(); |
| 82 | + ..set_title("Progress Tracker"); |
| 83 | + ..set_show_close_button(true); |
| 84 | + }; |
| 85 | + |
| 86 | + Header { container } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +pub struct MainView { |
| 91 | + pub container: gtk::Grid, |
| 92 | + pub progress: gtk::ProgressBar, |
| 93 | + pub button: gtk::Button |
| 94 | +} |
| 95 | + |
| 96 | +impl MainView { |
| 97 | + pub fn new() -> Self { |
| 98 | + let progress = cascade! { |
| 99 | + gtk::ProgressBar::new(); |
| 100 | + ..set_text("Progress Bar"); |
| 101 | + ..set_show_text(true); |
| 102 | + ..set_hexpand(true); |
| 103 | + }; |
| 104 | + |
| 105 | + let progress_ = progress.clone(); |
| 106 | + |
| 107 | + let button = cascade! { |
| 108 | + gtk::Button::new(); |
| 109 | + ..set_label("start"); |
| 110 | + ..set_halign(gtk::Align::Center); |
| 111 | + ..connect_clicked(move |_| { |
| 112 | + let (tx, rx) = mpsc::channel(); |
| 113 | + |
| 114 | + thread::spawn(move || { |
| 115 | + for v in 1..=10 { |
| 116 | + tx.send(v); |
| 117 | + thread::sleep(Duration::from_secs(1)); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + let progress_ = progress_.clone(); |
| 122 | + gtk::timeout_add(16, move || { |
| 123 | + match rx.try_recv() { |
| 124 | + Ok(v) => { |
| 125 | + progress_.set_fraction(f64::from(v) / 10.0); |
| 126 | + gtk::Continue(true) |
| 127 | + } |
| 128 | + Err(TryRecvError::Empty) => gtk::Continue(true), |
| 129 | + Err(TryRecvError::Disconnected) => gtk::Continue(false), |
| 130 | + } |
| 131 | + }); |
| 132 | + }); |
| 133 | + }; |
| 134 | + |
| 135 | + let container = cascade! { |
| 136 | + gtk::Grid::new(); |
| 137 | + ..attach(&progress, 0, 0, 1, 1); |
| 138 | + ..attach(&button, 0, 1, 1, 1); |
| 139 | + ..set_row_spacing(6); |
| 140 | + ..set_border_width(6); |
| 141 | + ..set_vexpand(true); |
| 142 | + ..set_hexpand(true); |
| 143 | + }; |
| 144 | + |
| 145 | + MainView { container, progress, button } |
| 146 | + } |
| 147 | +} |
0 commit comments