From 63e92490f03c1c1829a748b133c824b2e30582df Mon Sep 17 00:00:00 2001 From: rdbende Date: Sun, 14 Jul 2024 18:27:45 +0200 Subject: [PATCH] Use a menu to toggle book downloading instead of the switch --- cozy/ui/book_detail_view.py | 47 ++++++++++++++++++------------------ data/ui/book_detail.blp | 48 ++++++++++++++----------------------- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/cozy/ui/book_detail_view.py b/cozy/ui/book_detail_view.py index f59dd71f..b502aa18 100644 --- a/cozy/ui/book_detail_view.py +++ b/cozy/ui/book_detail_view.py @@ -4,7 +4,7 @@ from typing import Callable, Final from math import pi as PI -from gi.repository import Adw, GLib, Gtk, GObject, Gdk, Graphene, Gsk +from gi.repository import Adw, GLib, Gtk, GObject, Gdk, Graphene, Gsk, Gio import cairo from cozy.control.artwork_cache import ArtworkCache from cozy.ext import inject @@ -13,6 +13,7 @@ from cozy.report import reporter from cozy.ui.chapter_element import ChapterElement from cozy.view_model.book_detail_view_model import BookDetailViewModel +from cozy.ui.toaster import ToastNotifier log = logging.getLogger(__name__) @@ -95,11 +96,6 @@ class BookDetailView(Adw.NavigationPage): book_progress_ring: ProgressRing = Gtk.Template.Child() - download_box: Gtk.Box = Gtk.Template.Child() - download_label: Gtk.Label = Gtk.Template.Child() - download_image: Gtk.Image = Gtk.Template.Child() - download_switch: Gtk.Switch = Gtk.Template.Child() - album_art: Gtk.Picture = Gtk.Template.Child() album_art_container: Gtk.Box = Gtk.Template.Child() @@ -112,6 +108,7 @@ class BookDetailView(Adw.NavigationPage): _view_model: BookDetailViewModel = inject.attr(BookDetailViewModel) _artwork_cache: ArtworkCache = inject.attr(ArtworkCache) + _toaster: ToastNotifier = inject.attr(ToastNotifier) _current_selected_chapter: ChapterElement | None = None @@ -128,6 +125,15 @@ def __init__(self): self._connect_view_model() self._connect_widgets() + menu_action_group = Gio.SimpleActionGroup() + self.insert_action_group("book_overview", menu_action_group) + + self.available_offline_action = Gio.SimpleAction.new_stateful( + "download", None, GLib.Variant.new_boolean(False) + ) + self.available_offline_action.connect("change-state", self._download_switch_changed) + menu_action_group.add_action(self.available_offline_action) + def _connect_view_model(self): self._view_model.bind_to("book", self._on_book_changed) self._view_model.bind_to("playing", self._on_play_changed) @@ -141,7 +147,6 @@ def _connect_view_model(self): def _connect_widgets(self): self.play_button.connect("clicked", self._play_book_clicked) - self.download_switch.connect("state-set", self._download_switch_changed) def _on_book_changed(self): book = self._view_model.book @@ -216,7 +221,7 @@ def _on_progress_changed(self): self.book_progress_ring.progress = self._view_model.progress_percent def _on_lock_ui_changed(self): - self.download_switch.set_sensitive(not self._view_model.lock_ui) + self.available_offline_action.set_enabled(not self._view_model.lock_ui) def _on_chapters_displayed(self): self.total_label.set_text(self._view_model.total_text) @@ -294,13 +299,12 @@ def _add_chapter(self, chapter: Chapter): def _display_external_section(self): external = self._view_model.is_book_external - self.download_box.set_visible(external) - self.download_switch.set_visible(external) + self.available_offline_action.set_enabled(external) if external: - self.download_switch.handler_block_by_func(self._download_switch_changed) - self.download_switch.set_active(self._view_model.book.offline) - self.download_switch.handler_unblock_by_func(self._download_switch_changed) + self.available_offline_action.handler_block_by_func(self._download_switch_changed) + self.available_offline_action.set_state(GLib.Variant.new_boolean(self._view_model.book.offline)) + self.available_offline_action.handler_unblock_by_func(self._download_switch_changed) def _set_cover_image(self, book: Book): self.album_art_container.set_visible(False) @@ -324,18 +328,13 @@ def _set_book_download_status(self): if not self._view_model.is_book_external: return - if self._view_model.book.downloaded: - icon_name = "downloaded-symbolic" - text = _("Downloaded") - else: - icon_name = "download-symbolic" - text = _("Download") - - self.download_image.set_from_icon_name(icon_name) - self.download_label.set_text(text) + # TODO: show this only after download + # if self._view_model.book.downloaded: + # self._toaster.show(_("{book_title} is now available offline").format(book_title=self._view_model.book.name)) - def _download_switch_changed(self, _, state: bool): - self._view_model.download_book(state) + def _download_switch_changed(self, action, value): + action.set_state(value) + self._view_model.download_book(value.get_boolean()) self._set_book_download_status() def _play_chapter_clicked(self, _, chapter: Chapter): diff --git a/data/ui/book_detail.blp b/data/ui/book_detail.blp index 661bcacd..edca2288 100644 --- a/data/ui/book_detail.blp +++ b/data/ui/book_detail.blp @@ -7,7 +7,14 @@ template $BookDetail: Adw.NavigationPage { Adw.ToolbarView { [top] - Adw.HeaderBar {} + Adw.HeaderBar { + [end] + MenuButton book_menu { + menu-model: book_menu_model; + icon-name: "view-more-symbolic"; + tooltip-text: _("Open Book Menu"); + } + } content: Adw.BreakpointBin { width-request: 350; @@ -194,35 +201,6 @@ template $BookDetail: Adw.NavigationPage { } } } - - Box { - halign: center; - valign: center; - margin-bottom: 12; - spacing: 5; - visible: false; - - Box download_box { - halign: end; - valign: center; - - Image download_image { - icon-name: 'download-symbolic'; - } - - Label download_label { - margin-start: 5; - margin-end: 4; - label: _("Download"); - } - } - - Switch download_switch { - focusable: true; - halign: start; - valign: center; - } - } } } @@ -261,3 +239,13 @@ template $BookDetail: Adw.NavigationPage { }; } } + +menu book_menu_model { + section { + item { + action: 'book_overview.download'; + label: _("_Available Offline"); + hidden-when: "action-disabled"; + } + } +}