Add keyboard shortcuts!

This commit is contained in:
heliguy4599
2024-10-12 22:43:23 -04:00
parent c81ca758e3
commit 40be21887d
10 changed files with 312 additions and 197 deletions

View File

@@ -32,10 +32,10 @@ from .const import Config
class WarehouseApplication(Adw.Application):
"""The main application singleton class."""
troubleshooting = "OS: {os}\nWarehouse version: {wv}\nGTK: {gtk}\nlibadwaita: {adw}\nApp ID: {app_id}\nProfile: {profile}\nLanguage: {lang}"
version = Config.VERSION
def __init__(self):
super().__init__(
application_id="io.github.flattool.Warehouse",
@@ -43,9 +43,6 @@ class WarehouseApplication(Adw.Application):
)
self.create_action("about", self.on_about_action)
self.create_action("preferences", self.on_preferences_action)
self.create_action("quit", lambda *_: self.quit(), ["<primary>q"])
self.create_action("refresh", self.on_refresh_shortcut, ["<primary>r", "F5"])
self.create_action("open-menu", lambda *_: self.props.active_window.main_menu.popup(), ["F10"])
self.create_action("show-packages-page", lambda *_: self.props.active_window.switch_page_shortcut_handler("p"), ["<primary>p"])
self.create_action("show-remotes-page", lambda *_: self.props.active_window.switch_page_shortcut_handler("m"), ["<primary>m"])
@@ -53,8 +50,20 @@ class WarehouseApplication(Adw.Application):
self.create_action("show-snapshots-page", lambda *_: self.props.active_window.switch_page_shortcut_handler("s"), ["<primary>s"])
self.create_action("show-install-page", lambda *_: self.props.active_window.switch_page_shortcut_handler("i"), ["<primary>i"])
self.create_action("quit", lambda *_: self.quit(), ["<primary>q"])
self.create_action("refresh", lambda *_: self.props.active_window.refresh_handler(), ["<primary>r", "F5"])
self.create_action("open-menu", lambda *_: self.props.active_window.main_menu.popup(), ["F10"])
self.create_action("open-files", self.on_open_files_shortcut, ["<primary>o"])
self.create_action("toggle-select-mode", self.on_toggle_select_mode_shortcut, ["<primary>b", "<primary>Return", "<primary>KP_Enter"])
self.create_action("toggle-search-mode", self.on_toggle_search_mode_shortcut, ["<primary>f"])
self.create_action("filter", self.on_filter_shortcut, ["<primary>t"])
self.create_action("new", self.on_new_shortcut, ["<primary>n"])
self.create_action("delete", self.on_delete_shortcut, ["BackSpace", "Delete"])
self.create_action("active-data-view", lambda *_: self.on_data_view_shortcut(True), ["<Alt>1"])
self.create_action("leftover-data-view", lambda *_: self.on_data_view_shortcut(False), ["<Alt>2"])
self.is_dialog_open = False
gtk_version = (
str(Gtk.MAJOR_VERSION)
+ "."
@@ -71,7 +80,7 @@ class WarehouseApplication(Adw.Application):
)
os_string = GLib.get_os_info("NAME") + " " + GLib.get_os_info("VERSION")
lang = GLib.environ_getenv(GLib.get_environ(), "LANG")
self.troubleshooting = self.troubleshooting.format(
os=os_string,
wv=self.version,
@@ -82,33 +91,108 @@ class WarehouseApplication(Adw.Application):
lang=lang,
)
def on_refresh_shortcut(self, *args):
self.props.active_window.refresh_handler()
# def file_callback(self, object, result):
# window = self.props.active_window
# try:
# file = object.open_finish(result)
# window.install_file(file.get_path())
# except GLib.GError:
# pass
# def install_from_file(self, widget, _a):
# window = self.props.active_window
# filter = Gtk.FileFilter(name=_("Flatpaks"))
# filter.add_suffix("flatpak")
# filter.add_suffix("flatpakref")
# filters = Gio.ListStore.new(Gtk.FileFilter)
# filters.append(filter)
# file_chooser = Gtk.FileDialog()
# file_chooser.set_filters(filters)
# file_chooser.set_default_filter(filter)
# file_chooser.open(window, None, self.file_callback)
def on_open_files_shortcut(self, *args):
window = self.props.active_window
def file_choose_callback(object, result):
files = object.open_multiple_finish(result)
window.on_file_drop(None, files, None, None)
file_filter = Gtk.FileFilter(name=_("Flatpaks & Remotes"))
file_filter.add_suffix("flatpak")
file_filter.add_suffix("flatpakref")
file_filter.add_suffix("flatpakrepo")
filters = Gio.ListStore.new(Gtk.FileFilter)
filters.append(file_filter)
file_chooser = Gtk.FileDialog()
file_chooser.set_filters(filters)
file_chooser.set_default_filter(file_filter)
file_chooser.open_multiple(window, None, file_choose_callback)
def on_toggle_select_mode_shortcut(self, *args):
try:
button = self.props.active_window.stack.get_visible_child().select_button
button.set_active(not button.get_active())
except AttributeError:
pass
def on_toggle_search_mode_shortcut(self, *args):
try:
button = self.props.active_window.stack.get_visible_child().search_button
button.set_active(not button.get_active())
except AttributeError:
pass
def on_filter_shortcut(self, *args):
try:
button = self.props.active_window.stack.get_visible_child().filter_button
button.set_active(not button.get_active())
except AttributeError:
pass
try:
button = self.props.active_window.stack.get_visible_child().sort_button
button.set_active(True)
except AttributeError:
pass
try:
button = self.props.active_window.stack.get_visible_child().show_disabled_button
if button.get_visible():
button.set_active(not button.get_active())
except AttributeError:
pass
def on_new_shortcut(self, *args):
page = self.props.active_window.stack.get_visible_child()
try:
page.new_custom_handler()
except AttributeError:
pass
try:
page.on_new()
except AttributeError:
pass
def on_delete_shortcut(self, *args):
page = self.props.active_window.stack.get_visible_child()
try:
if not page.select_button.get_active():
return
page.selection_uninstall()
except AttributeError:
pass
try:
if not page.select_button.get_active():
return
page.trash_handler()
except AttributeError:
pass
try:
if not page.select_button.get_active():
return
page.select_trash_handler()
except AttributeError:
pass
def on_data_view_shortcut(self, is_active):
page = self.props.active_window.stack.get_visible_child()
try:
adp = page.adp
ldp = page.ldp
page.stack.set_visible_child(adp if is_active else ldp)
except AttributeError:
pass
def do_activate(self):
"""Called when the application is activated.
We raise the application's main window, creating it if
necessary.
"""
@@ -116,7 +200,7 @@ class WarehouseApplication(Adw.Application):
if not win:
win = WarehouseWindow(application=self)
win.present()
def on_about_action(self, widget, _a):
"""Callback for the app.about action."""
about = Adw.AboutDialog(
@@ -156,18 +240,17 @@ class WarehouseApplication(Adw.Application):
],
)
about.present(self.props.active_window)
def on_preferences_action(self, widget, _):
"""Callback for the app.preferences action."""
print("app.preferences action activated")
def create_action(self, name, callback, shortcuts=None):
"""Add an application action.
Args:
name: the name of the action
callback: the function to be called when the action is
activated
callback: the function to be called when the action is activated
shortcuts: an optional list of accelerators
"""
action = Gio.SimpleAction.new(name, None)
@@ -175,7 +258,7 @@ class WarehouseApplication(Adw.Application):
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
def main(version):
"""The application's entry point."""
app = WarehouseApplication()