add restart button to notification

This commit is contained in:
Daniel Langbein 2025-02-23 22:12:24 +01:00
parent 150a5dd83b
commit 7304929aa4
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002

View File

@ -18,13 +18,21 @@ class Application(Adw.Application):
self.window = AppWindow(application=self) self.window = AppWindow(application=self)
self.window.present() 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: def send_notification_timeout(self) -> None:
notification = Gio.Notification() notification = Gio.Notification()
notification.set_title('Timeout') notification.set_title('Timeout')
notification.set_body('The time is over and your do-not-disturb mode just got busted!') notification.set_body('The time is over and your do-not-disturb mode just got busted!')
notification.set_priority(Gio.NotificationPriority.URGENT) 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) 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): class AppWindow(Gtk.ApplicationWindow):
def __init__(self, application: Application) -> None: def __init__(self, application: Application) -> None:
@ -56,42 +64,45 @@ class AppWindow(Gtk.ApplicationWindow):
def on_button_start_restart_clicked(self, _widget) -> None: def on_button_start_restart_clicked(self, _widget) -> None:
if not self.timer.is_started(): if not self.timer.is_started():
self.reset_start_timer() self.start()
self.button_start_restart.set_label("Restart")
self.button_pause_resume.set_visible(True)
else: else:
self.reset_start_timer() self.restart()
self.update_label()
self.add_timeout()
def on_button_pause_resume_clicked(self, _widget) -> None: def on_button_pause_resume_clicked(self, _widget) -> None:
if self.timer.is_paused(): if self.timer.is_paused():
self.timer.resume() self.timer.resume()
self.add_timeout() self.regularly_update_progress_bar()
else: else:
self.timer.pause() 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(): if self.timer.is_paused():
self.button_pause_resume.set_label("Resume") self.button_pause_resume.set_label('Resume')
else: else:
self.button_pause_resume.set_label("Pause") self.button_pause_resume.set_label('Pause')
def reset_start_timer(self) -> None: def reset_start_timer(self) -> None:
self.timer.reset() self.timer.reset()
self.timer.start() self.timer.start()
self.progress_bar.set_visible(True) 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() # 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: def update_progress_bar(self) -> bool:
total_min = 0.1 total_min = 25
total_sec = total_min * 60 total_sec = total_min * 60
delta_sec = self.timer.read() delta_sec = self.timer.read()
progress = delta_sec / total_sec progress = delta_sec / total_sec