From f78aa3f04f23f9304019ae571b8fb6cab7dee7bd Mon Sep 17 00:00:00 2001 From: Antony Gelberg Date: Fri, 19 Apr 2024 22:24:07 +0100 Subject: [PATCH] Add reset button --- pyredfruit.py | 77 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/pyredfruit.py b/pyredfruit.py index 3629489..5cd7669 100644 --- a/pyredfruit.py +++ b/pyredfruit.py @@ -14,19 +14,7 @@ class Timer: def __init__(self, _type, minutes, seconds): self.type = _type - # We need to use datetime.datetime, not datetime.time. - # See: - # https://bugs.python.org/issue1118748 - # https://bugs.python.org/issue1487389 - # for more information as to why. So we can't do this: - # self.remaining = datetime.time(0,25,0) - # - # NB This may not be a Bad Thing because we may decide to - # implement time tracking in the future, which would require - # dates. - today = datetime.date.today() - time = datetime.time(0, minutes, seconds) - self.remaining = datetime.datetime.combine(today, time) + self.set_remaining(minutes, seconds) def decrement_second(self): a_second = datetime.timedelta(seconds=1) @@ -42,6 +30,21 @@ def done_text(self): def has_expired(self): return bool(self.remaining.time() == datetime.time(0, 0, 0)) + def set_remaining(self, minutes, seconds): + # We need to use datetime.datetime, not datetime.time. + # See: + # https://bugs.python.org/issue1118748 + # https://bugs.python.org/issue1487389 + # for more information as to why. So we can't do this: + # self.remaining = datetime.time(0,25,0) + # + # NB This may not be a Bad Thing because we may decide to + # implement time tracking in the future, which would require + # dates. + today = datetime.date.today() + time = datetime.time(0, minutes, seconds) + self.remaining = datetime.datetime.combine(today, time) + def __str__(self): return self.remaining.strftime("%M:%S") @@ -63,37 +66,49 @@ def __init__(self): box.pack_start(self.time_label, True, True, 0) self.status = Gtk.Label() - self.sync_state() + self.update_ui_state() box.pack_start(self.status, True, True, 0) - button = Gtk.Button.new_with_label("Start") - button.connect("clicked", self.clicked_start) - box.pack_start(button, True, True, 0) + state_button = Gtk.Button.new_with_label("Start") + state_button.connect("clicked", self.clicked_state) + box.pack_start(state_button, True, True, 0) + + reset_button = Gtk.Button.new_with_label("Reset") + reset_button.connect("clicked", self.clicked_reset) + box.pack_start(reset_button, True, True, 0) # There is probably (hopefully) a better way to do this rather than # calling this function every second when the timer ticks. Ideally # UI components would listen for state changes. But as I don't have # time to read all the docs, especially # https://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html - # which mentions Gio.SimpleAction amongst other things, and there is - # only one label that changes state at the moment, do it like this. - def sync_state(self): + # which mentions Gio.SimpleAction amongst other things, and there + # are only two labels that changes state at the moment, do it like this. + # Or is it + # https://python-gtk-3-tutorial.readthedocs.io/en/latest/objects.html + def update_ui_state(self): self.status.set_label(f"{self.state} {self.timer.type}") + self.time_label.set_markup( + f'{self.timer}' + ) - def clicked_start(self, button): + def clicked_reset(self, reset_button): + self.reset_pomodoro() + + def clicked_state(self, state_button): match self.state: case "stopped": - button.set_label("Pause") + state_button.set_label("Pause") self.start_pomodoro() case "running": - button.set_label("Restart") + state_button.set_label("Restart") self.state = "paused" case "paused": - button.set_label("Pause") + state_button.set_label("Pause") self.state = "running" def time_up(self): - playsound('bell.wav') + playsound("bell.wav") dialog = Gtk.MessageDialog( buttons=Gtk.ButtonsType.OK, text=self.timer.done_text() ) @@ -110,25 +125,25 @@ def start_break(self): self.start_tick() def start_pomodoro(self): - self.timer = Timer("pomodoro", 25, 0) + self.reset_pomodoro() self.start_tick() self.state = "running" + def reset_pomodoro(self): + self.timer = Timer("pomodoro", 25, 0) + def start_tick(self): GLib.timeout_add_seconds(1, self.update_state) - def update_state(self): if self.state == "running": self.timer.decrement_second() - self.time_label.set_markup( - f'{self.timer}' - ) + if self.timer.has_expired(): self.time_up() return False - self.sync_state() + self.update_ui_state() return True # timeout_add_seconds will keep going