From 7304929aa468e56cdf0844e28c39608dfd566d20 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sun, 23 Feb 2025 22:12:24 +0100 Subject: [PATCH] add restart button to notification --- src/dndbuster/app.py | 47 +++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/dndbuster/app.py b/src/dndbuster/app.py index ad67341..522449a 100644 --- a/src/dndbuster/app.py +++ b/src/dndbuster/app.py @@ -18,13 +18,21 @@ class Application(Adw.Application): self.window = AppWindow(application=self) self.window.present() + action = Gio.SimpleAction.new('restart-timer', None) + action.connect('activate', self.on_notification_button_restart) + self.add_action(action) + def send_notification_timeout(self) -> None: notification = Gio.Notification() notification.set_title('Timeout') notification.set_body('The time is over and your do-not-disturb mode just got busted!') notification.set_priority(Gio.NotificationPriority.URGENT) + notification.add_button(label='Restart', detailed_action='app.restart-timer') self.send_notification(id=f'{self.get_application_id()}.{Timer.current_time()}', notification=notification) + def on_notification_button_restart(self, _action: Gio.SimpleAction, _other: None): + self.window.restart() + class AppWindow(Gtk.ApplicationWindow): def __init__(self, application: Application) -> None: @@ -56,42 +64,45 @@ class AppWindow(Gtk.ApplicationWindow): def on_button_start_restart_clicked(self, _widget) -> None: if not self.timer.is_started(): - self.reset_start_timer() - - self.button_start_restart.set_label("Restart") - self.button_pause_resume.set_visible(True) + self.start() else: - self.reset_start_timer() - - self.update_label() - - self.add_timeout() + self.restart() def on_button_pause_resume_clicked(self, _widget) -> None: if self.timer.is_paused(): self.timer.resume() - self.add_timeout() + self.regularly_update_progress_bar() else: self.timer.pause() - self.update_label() + self.update_pause_resume_label() - def update_label(self) -> None: + def start(self): + self.reset_start_timer() + self.button_start_restart.set_label('Restart') + self.button_pause_resume.set_visible(True) + + def restart(self): + self.reset_start_timer() + self.update_pause_resume_label() + + def update_pause_resume_label(self) -> None: if self.timer.is_paused(): - self.button_pause_resume.set_label("Resume") + self.button_pause_resume.set_label('Resume') else: - self.button_pause_resume.set_label("Pause") + self.button_pause_resume.set_label('Pause') def reset_start_timer(self) -> None: self.timer.reset() self.timer.start() self.progress_bar.set_visible(True) + self.regularly_update_progress_bar() - def add_timeout(self) -> None: + def regularly_update_progress_bar(self) -> None: # Start regularly calling update_progress() - GLib.timeout_add_seconds(1, self.update_progress) + GLib.timeout_add_seconds(1, self.update_progress_bar) - def update_progress(self) -> bool: - total_min = 0.1 + def update_progress_bar(self) -> bool: + total_min = 25 total_sec = total_min * 60 delta_sec = self.timer.read() progress = delta_sec / total_sec