wip: add notification

This commit is contained in:
Daniel Langbein 2025-02-11 21:39:15 +01:00
parent 2201e57ffb
commit 3f4d41958e
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002

View File

@ -8,6 +8,7 @@ use chrono::Local;
use std::cell::Cell;
use std::rc::Rc;
use gtk::gio::{Notification, NotificationPriority};
use gtk::glib;
use gtk::glib::clone;
use gtk::prelude::*;
@ -19,9 +20,9 @@ const DUR_MIN: f64 = 1.0;
const INTERVAL_SEC: u32 = 1;
fn main() -> glib::ExitCode {
let application = gtk::Application::builder().application_id(APP_ID).build();
application.connect_activate(build_ui);
application.run()
let app = gtk::Application::builder().application_id(APP_ID).build();
app.connect_activate(move |app| build_ui(app));
app.run()
}
fn build_ui(app: &gtk::Application) {
@ -74,22 +75,27 @@ fn build_ui(app: &gtk::Application) {
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 tick = clone!(@strong app => move || {
if running.get() {
progress += ((INTERVAL_SEC as f64) / 60.0) / DUR_MIN;
progress_bar.set_fraction(progress);
}
if progress > 1.0 {
// todo
eprintln!("Time over.");
// TODO: Notification does not show up
let notification = Notification::new("Alert");
notification.set_body(Some("Time is over"));
notification.set_priority(NotificationPriority::Urgent);
app.send_notification(Some(APP_ID), &notification);
}
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 `INTERVAL_SEC` seconds
glib::timeout_add_seconds_local(INTERVAL_SEC, tick);