Skip to content

Commit

Permalink
Add Ctrl+f as search action accelerator
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende committed Dec 10, 2023
1 parent 86d27e8 commit 657e159
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
54 changes: 24 additions & 30 deletions cozy/ui/main_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", ["<Control>q", "<Control>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", ["<Control>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, ["<Control>q", "<Control>w"])
self.create_action("prefs", self.show_prefs, ["<Control>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)
Expand Down
10 changes: 7 additions & 3 deletions cozy/ui/search_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ["<primary>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()
Expand Down

0 comments on commit 657e159

Please sign in to comment.