Skip to content
Open
12 changes: 6 additions & 6 deletions cozy/ui/widgets/book_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BookCard(Gtk.FlowBoxChild):
artwork: Gtk.Picture = Gtk.Template.Child()
fallback_icon: Gtk.Image = Gtk.Template.Child()
stack: Gtk.Stack = Gtk.Template.Child()
button: Gtk.Stack = Gtk.Template.Child()
button: Gtk.Button = Gtk.Template.Child()
menu_button: Gtk.MenuButton = Gtk.Template.Child()
play_revealer: Gtk.Revealer = Gtk.Template.Child()
menu_revealer: Gtk.Revealer = Gtk.Template.Child()
Expand Down Expand Up @@ -143,9 +143,10 @@ def _install_event_controllers(self):

long_press_gesture = Gtk.GestureLongPress()
long_press_gesture.connect("pressed", self._on_long_tap)
long_press_gesture.set_propagation_phase(Gtk.PropagationPhase.CAPTURE)

self.add_controller(hover_controller)
self.add_controller(long_press_gesture)
self.button.add_controller(long_press_gesture)

def set_playing(self, is_playing):
self.play_button.set_playing(is_playing)
Expand All @@ -169,10 +170,9 @@ def _on_leave(self, *_) -> None:

def _on_long_tap(self, gesture: Gtk.Gesture, *_):
gesture.set_state(Gtk.EventSequenceState.CLAIMED)

device = gesture.get_device()
if device and device.get_source() == Gdk.InputSource.TOUCHSCREEN:
self.menu_button.emit("activate")
self.menu_button.emit("activate")
self.play_revealer.set_reveal_child(True)
self.menu_revealer.set_reveal_child(True)

def update_progress(self):
if self.book.duration:
Expand Down
28 changes: 22 additions & 6 deletions cozy/view_model/sleep_timer_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self):
self._system_power_action = SystemPowerAction.NONE
self._timer_running = False
self._fadeout_running = False
self._glib_timer_id = None

self._player.add_listener(self._on_player_changed)

Expand Down Expand Up @@ -92,12 +93,28 @@ def _start_timer(self):
log.info("Start Sleep Timer")
self._notify("timer_enabled")

# Start a GLib timeout to tick every second
if self._glib_timer_id is None:
self._glib_timer_id = GLib.timeout_add_seconds(1, self._glib_timer_tick)

def _stop_timer(self):
self._timer_running = False

log.info("Stop Sleep Timer")
self._notify("timer_enabled")

# Remove the GLib timeout if running
if self._glib_timer_id is not None:
GLib.source_remove(self._glib_timer_id)
self._glib_timer_id = None

def _glib_timer_tick(self):
if not self._timer_running:
return False

self._on_timer_tick()
return self._timer_running and self._remaining_seconds > 0

def _on_timer_tick(self):
self._remaining_seconds -= 1
self._notify("remaining_seconds")
Expand All @@ -112,10 +129,7 @@ def _on_timer_tick(self):
self._handle_system_power_event()

def _on_player_changed(self, event, _):
if event == "position":
if self._timer_running:
self._on_timer_tick()
elif event == "chapter-changed":
if event == "chapter-changed":
self.stop_after_chapter = False
elif event == "fadeout-finished":
self._handle_system_power_event()
Expand All @@ -129,7 +143,8 @@ def _handle_system_power_event(self):
case _:
return

def _shutdown(self):
@staticmethod
def _shutdown():
inject.instance("MainWindow").quit() # Exit gracefully
if os.getenv("XDG_CURRENT_DESKTOP") == "GNOME":
Gio.bus_get_sync(Gio.BusType.SESSION, None).call_sync(
Expand All @@ -156,7 +171,8 @@ def _shutdown(self):
None,
)

def _suspend(self):
@staticmethod
def _suspend():
Gio.bus_get_sync(Gio.BusType.SYSTEM, None).call_sync(
"org.freedesktop.login1",
"/org/freedesktop/login1",
Expand Down