From 657e159643e176ba5c58c442366c1958b5930d62 Mon Sep 17 00:00:00 2001 From: rdbende Date: Sun, 10 Dec 2023 20:34:19 +0100 Subject: [PATCH] Add Ctrl+f as search action accelerator --- cozy/ui/main_view.py | 54 +++++++++++++++++++----------------------- cozy/ui/search_view.py | 10 +++++--- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/cozy/ui/main_view.py b/cozy/ui/main_view.py index e0b2e20a..98ce1bbf 100644 --- a/cozy/ui/main_view.py +++ b/cozy/ui/main_view.py @@ -130,39 +130,33 @@ def __init_actions(self): about_action.connect("activate", self.about) self.app.add_action(about_action) - quit_action = Gio.SimpleAction.new("quit", None) - quit_action.connect("activate", self.quit) - self.app.add_action(quit_action) - self.app.set_accels_for_action( - "app.quit", ["q", "w"]) - - pref_action = Gio.SimpleAction.new("prefs", None) - pref_action.connect("activate", self.show_prefs) - self.app.add_action(pref_action) - self.app.set_accels_for_action("app.prefs", ["comma"]) - - self.scan_action = Gio.SimpleAction.new("scan", None) - self.scan_action.connect("activate", self.scan) - self.app.add_action(self.scan_action) - - self.play_pause_action = Gio.SimpleAction.new("play_pause", None) - self.play_pause_action.connect("activate", self.play_pause) - self.app.add_action(self.play_pause_action) - self.app.set_accels_for_action("app.play_pause", ["space"]) - - # NavigationView.pop-on-escape doesn't work in some cases, so this is a hack - back_action = Gio.SimpleAction.new("back", None) - back_action.connect("activate", lambda *_: self.navigation_view.pop()) - self.app.add_action(back_action) - self.app.set_accels_for_action("app.back", ["Escape"]) - - self.hide_offline_action = Gio.SimpleAction.new_stateful("hide_offline", - None, - GLib.Variant.new_boolean( - self.application_settings.hide_offline)) + self.create_action("about", self.about) + self.create_action("quit", self.quit, ["q", "w"]) + self.create_action("prefs", self.show_prefs, ["comma"]) + self.create_action("scan", self.scan) + self.play_pause_action = self.create_action("play_pause", self.play_pause, ["space"]) + + self.hide_offline_action = Gio.SimpleAction.new_stateful( + "hide_offline", None, GLib.Variant.new_boolean(self.application_settings.hide_offline) + ) self.hide_offline_action.connect("change-state", self.__on_hide_offline) self.app.add_action(self.hide_offline_action) + def create_action( + self, + name: str, + callback: Callable[[Gio.SimpleAction, None], None], + shortcuts: list[str] | None = None, + ) -> Gio.SimpleAction: + action = Gio.SimpleAction.new(name, None) + action.connect("activate", callback) + self.app.add_action(action) + + if shortcuts: + self.app.set_accels_for_action(f"app.{name}", shortcuts) + + return action + def __init_components(self): if not self._player.loaded_book: self.block_ui_buttons(True) diff --git a/cozy/ui/search_view.py b/cozy/ui/search_view.py index 2f6c3f69..495c9c1e 100644 --- a/cozy/ui/search_view.py +++ b/cozy/ui/search_view.py @@ -50,18 +50,22 @@ def __init__(self, main_window_builder: Gtk.Builder, headerbar: Headerbar) -> No self.search_thread_stop = threading.Event() self.view_model.bind_to("close", self.close) + self.main_window.create_action("search", self.open, ["f"]) + + def open(self) -> None: + self.library_stack.set_visible_child(self) + self.main_view.play_pause_action.set_enabled(False) def close(self) -> None: self.library_stack.set_visible_child(self.split_view) self.search_bar.set_search_mode(False) + self.main_view.play_pause_action.set_enabled(True) def on_state_changed(self, widget: Gtk.Widget, param) -> None: if widget.get_property(param.name): - self.library_stack.set_visible_child(self) - self.main_view.play_pause_action.set_enabled(False) + self.open() else: self.close() - self.main_view.play_pause_action.set_enabled(True) def _on_search_changed(self, _) -> None: self.search_thread_stop.set()