diff --git a/cozy/ui/widgets/sleep_timer.py b/cozy/ui/widgets/sleep_timer.py index 637bd4e9..afe2868a 100644 --- a/cozy/ui/widgets/sleep_timer.py +++ b/cozy/ui/widgets/sleep_timer.py @@ -117,6 +117,8 @@ def close(self, *_): @Gtk.Template.Callback() def set_timer(self, *_): + super().close() + value = self.sleep_timer_action.get_state().unpack() if value == -1: self._view_model.remaining_seconds = self.custom_adjustment.get_value() * 60 @@ -125,20 +127,19 @@ def set_timer(self, *_): else: self._view_model.remaining_seconds = value * 60 - super().close() - @Gtk.Template.Callback() def plus_5_minutes(self, *_): - self._view_model.remaining_seconds += 300 - super().close() + if self._view_model.stop_after_chapter: + self._view_model.remaining_seconds = self._view_model.get_remaining_from_chapter() + 300 + else: + self._view_model.remaining_seconds += 300 @Gtk.Template.Callback() def till_end_of_chapter(self, *_): self._view_model.stop_after_chapter = True - super().close() @Gtk.Template.Callback() def cancel_timer(self, *_): + super().close() self._view_model.remaining_seconds = 0 self._view_model.stop_after_chapter = False - super().close() diff --git a/cozy/view_model/sleep_timer_view_model.py b/cozy/view_model/sleep_timer_view_model.py index b092fe09..c5f9ce90 100644 --- a/cozy/view_model/sleep_timer_view_model.py +++ b/cozy/view_model/sleep_timer_view_model.py @@ -4,6 +4,8 @@ from enum import Enum, auto from typing import Optional +from gi.repository import Gst + import inject from cozy.architecture.observable import Observable @@ -80,6 +82,14 @@ def stop_after_chapter(self, new_value: bool): self._notify("stop_after_chapter") self._notify("timer_enabled") + def get_remaining_from_chapter(self) -> float | None: + book = self._player.loaded_book + if not book: + return 0 + + position = book.current_chapter.length - (book.current_chapter.position - book.current_chapter.start_position) + return int(position / Gst.SECOND / book.playback_speed) + def destroy(self): self._stop_timer() @@ -127,7 +137,7 @@ def _on_player_changed(self, event, _): self._stop_timer() def _handle_system_power_event(self): - # TODO: This doesn't work in Flatpak. Either remove it completely, or make it conditional' + # TODO: This doesn't work in Flatpak. Either remove it completely, or make it conditional command = None if self.system_power_control == SystemPowerControl.SHUTDOWN: diff --git a/data/ui/sleep_timer_dialog.blp b/data/ui/sleep_timer_dialog.blp index ca40e870..54e120b2 100644 --- a/data/ui/sleep_timer_dialog.blp +++ b/data/ui/sleep_timer_dialog.blp @@ -14,7 +14,7 @@ Adjustment timer_value { template $SleepTimer: Adw.Dialog { width-request: 180; - title: _("Set Sleep Timer"); + title: _("Sleep Timer"); child: Adw.ToolbarView toolbarview{ [top]