// Based on example from https://crates.io/crates/gtk4 use chrono::Local; use gtk4 as gtk; use gtk::prelude::*; use gtk::{glib}; fn main() -> glib::ExitCode { let application = gtk::Application::builder() .application_id("de.privacy1st.pdt") .build(); application.connect_activate(build_ui); application.run() } fn build_ui(app: >k::Application) { let window = gtk::ApplicationWindow::builder() .application(app) .title("PleaseDisturbTimer") .default_width(350) .default_height(70) .build(); let container = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) .margin_top(24) .margin_bottom(24) .margin_start(24) .margin_end(24) .halign(gtk::Align::Center) .valign(gtk::Align::Center) .spacing(24) .build(); let button_start = gtk::Button::with_label("Start"); button_start.connect_clicked(|_| { eprintln!("Timer started."); }); container.append(&button_start); let button_stop = gtk::Button::with_label("Stop"); button_stop.connect_clicked(|_| { eprintln!("Timer stopped."); }); container.append(&button_stop); let time = current_time(); let label = gtk::Label::default(); label.set_text(&time); // label.set_halign(gtk::Align::Center); container.append(&label); window.set_child(Some(&container)); window.present(); // 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 glib::ControlFlow::Break to stop our clock after this tick glib::ControlFlow::Continue }; // executes the closure once every second glib::timeout_add_seconds_local(1, tick); } fn current_time() -> String { format!("{}", Local::now().format("%Y-%m-%d %H:%M:%S")) }