From 397c693aef28d21b6fb3d2a1d0741e582b10cee8 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 8 Jun 2023 14:18:13 -0400 Subject: [PATCH] window: Add a keyboard accelerator I use the "Escape" key as a shortcut for resetting the currently focused widget. Signed-off-by: Anna Schumaker --- emmental/__init__.py | 7 +++++++ emmental/window.py | 6 ++++++ tests/test_emmental.py | 3 +++ tests/test_window.py | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/emmental/__init__.py b/emmental/__init__.py index bd62781..2e570d7 100644 --- a/emmental/__init__.py +++ b/emmental/__init__.py @@ -3,6 +3,7 @@ import musicbrainzngs import pathlib from . import gsetup +from . import action from . import audio from . import db from . import header @@ -44,6 +45,11 @@ class Application(Adw.Application): flags=Gio.ApplicationFlags.HANDLES_OPEN) self.add_main_option_entries([options.Version]) + def __add_accelerators(self, accels: list[action.ActionEntry]) -> None: + for entry in accels: + self.add_action(entry.action) + self.set_accels_for_action(f"app.{entry.name}", entry.accels) + def __load_file(self, file: pathlib.Path, *, gapless: bool = False) -> None: self.__stop_current_track() @@ -200,6 +206,7 @@ class Application(Adw.Application): ("sidebar.size", "sidebar-size")]: self.db.settings.bind_setting(setting, win, property) + self.__add_accelerators(win.accelerators) return win def connect_mpris2(self) -> None: diff --git a/emmental/window.py b/emmental/window.py index 2a368d7..ee19502 100644 --- a/emmental/window.py +++ b/emmental/window.py @@ -3,6 +3,7 @@ from gi.repository import GObject from gi.repository import Gtk from gi.repository import Adw +from .action import ActionEntry def _make_pane(orientation: Gtk.Orientation, position: int = 0, @@ -86,3 +87,8 @@ class Window(Adw.Window): def present(self, *args) -> None: """Present the window.""" super().present() + + @property + def accelerators(self) -> list[ActionEntry]: + """Get a list of accelerators for the Window.""" + return [ActionEntry("reset-focus", self.set_focus, "Escape")] diff --git a/tests/test_emmental.py b/tests/test_emmental.py index bbe7cf4..5806093 100644 --- a/tests/test_emmental.py +++ b/tests/test_emmental.py @@ -115,6 +115,9 @@ class TestEmmental(unittest.TestCase): self.assertEqual(win.header.title, emmental.VERSION_STRING) + self.assertEqual(self.application.get_accels_for_action( + "app.reset-focus"), ["Escape"]) + @unittest.mock.patch("emmental.audio.Player.pause") @unittest.mock.patch("emmental.audio.Player.play") @unittest.mock.patch("sys.stdout", new_callable=io.StringIO) diff --git a/tests/test_window.py b/tests/test_window.py index 65dbc01..7565711 100644 --- a/tests/test_window.py +++ b/tests/test_window.py @@ -160,3 +160,13 @@ class TestWindow(unittest.TestCase): with unittest.mock.patch.object(Gtk.Window, "present") as mock_present: self.window.present(1, 2, 3, 4, 5) mock_present.assert_called() + + def test_accelerators(self): + """Test that the Window accelerators are set up properly.""" + accels = self.window.accelerators + self.assertIsInstance(accels, list) + self.assertEqual(len(accels), 1) + + self.assertEqual(accels[0].name, "reset-focus") + self.assertEqual(accels[0].func, self.window.set_focus) + self.assertListEqual(accels[0].accels, ["Escape"])