window: Add a keyboard accelerator
I use the "Escape" key as a shortcut for resetting the currently focused widget. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
eb162154b5
commit
397c693aef
|
@ -3,6 +3,7 @@
|
||||||
import musicbrainzngs
|
import musicbrainzngs
|
||||||
import pathlib
|
import pathlib
|
||||||
from . import gsetup
|
from . import gsetup
|
||||||
|
from . import action
|
||||||
from . import audio
|
from . import audio
|
||||||
from . import db
|
from . import db
|
||||||
from . import header
|
from . import header
|
||||||
|
@ -44,6 +45,11 @@ class Application(Adw.Application):
|
||||||
flags=Gio.ApplicationFlags.HANDLES_OPEN)
|
flags=Gio.ApplicationFlags.HANDLES_OPEN)
|
||||||
self.add_main_option_entries([options.Version])
|
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,
|
def __load_file(self, file: pathlib.Path,
|
||||||
*, gapless: bool = False) -> None:
|
*, gapless: bool = False) -> None:
|
||||||
self.__stop_current_track()
|
self.__stop_current_track()
|
||||||
|
@ -200,6 +206,7 @@ class Application(Adw.Application):
|
||||||
("sidebar.size", "sidebar-size")]:
|
("sidebar.size", "sidebar-size")]:
|
||||||
self.db.settings.bind_setting(setting, win, property)
|
self.db.settings.bind_setting(setting, win, property)
|
||||||
|
|
||||||
|
self.__add_accelerators(win.accelerators)
|
||||||
return win
|
return win
|
||||||
|
|
||||||
def connect_mpris2(self) -> None:
|
def connect_mpris2(self) -> None:
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
from gi.repository import GObject
|
from gi.repository import GObject
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
from gi.repository import Adw
|
from gi.repository import Adw
|
||||||
|
from .action import ActionEntry
|
||||||
|
|
||||||
|
|
||||||
def _make_pane(orientation: Gtk.Orientation, position: int = 0,
|
def _make_pane(orientation: Gtk.Orientation, position: int = 0,
|
||||||
|
@ -86,3 +87,8 @@ class Window(Adw.Window):
|
||||||
def present(self, *args) -> None:
|
def present(self, *args) -> None:
|
||||||
"""Present the window."""
|
"""Present the window."""
|
||||||
super().present()
|
super().present()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def accelerators(self) -> list[ActionEntry]:
|
||||||
|
"""Get a list of accelerators for the Window."""
|
||||||
|
return [ActionEntry("reset-focus", self.set_focus, "Escape")]
|
||||||
|
|
|
@ -115,6 +115,9 @@ class TestEmmental(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(win.header.title, emmental.VERSION_STRING)
|
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.pause")
|
||||||
@unittest.mock.patch("emmental.audio.Player.play")
|
@unittest.mock.patch("emmental.audio.Player.play")
|
||||||
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
||||||
|
|
|
@ -160,3 +160,13 @@ class TestWindow(unittest.TestCase):
|
||||||
with unittest.mock.patch.object(Gtk.Window, "present") as mock_present:
|
with unittest.mock.patch.object(Gtk.Window, "present") as mock_present:
|
||||||
self.window.present(1, 2, 3, 4, 5)
|
self.window.present(1, 2, 3, 4, 5)
|
||||||
mock_present.assert_called()
|
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"])
|
||||||
|
|
Loading…
Reference in New Issue