home · contact · privacy
Browser: replace movement buttons with vi keybindings.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 27 Aug 2024 09:42:21 +0000 (11:42 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 27 Aug 2024 09:42:21 +0000 (11:42 +0200)
browser.py

index 97684430a9be3fdef15f865e6b86495af77195d0..081829700bdb1fcda26bee37defe0de139932a0e 100755 (executable)
@@ -75,14 +75,10 @@ class Window(Gtk.ApplicationWindow):
             btn.connect('clicked', on_click)
             parent_box.append(btn)
 
-        def add_nav_button(label_, inc_, pos_):
-            add_button(label_, lambda _: self.move_selection(inc_, pos_),
-                       box_nav_buttons)
-
-        box_nav_buttons = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
-        for label, inc, pos in (('first', None, 0), ('prev', -1, None),
-                                ('next', 1, None), ('last', None, -1)):
-            add_nav_button(label, inc, pos)
+        keyboard_control = Gtk.EventControllerKey()
+        keyboard_control.connect('key-pressed', self.handle_keypress, self)
+        self.prev_key = ['']
+        self.add_controller(keyboard_control)
 
         self.metadata = Gtk.Label(xalign=0)
         self.label_nothing_to_show = Gtk.Label(label='nothing to show')
@@ -94,7 +90,6 @@ class Window(Gtk.ApplicationWindow):
         box_files_selection.append(self.box_sort_order)
         self.viewer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
         self.viewer.append(box_files_selection)
-        self.viewer.append(box_nav_buttons)
         self.viewer.append(self.metadata)
         self.viewer.append(self.label_nothing_to_show)
 
@@ -274,6 +269,18 @@ class Window(Gtk.ApplicationWindow):
         with open(CACHE_PATH, 'w', encoding='utf8') as f:
             json_dump(cache, f)
 
+    def handle_keypress(self, _keyval, keycode, _state, _user_data, _win):
+        if ord('G') == keycode:
+            self.move_selection(None, -1)
+        elif ord('j') == keycode:
+            self.move_selection(1, None)
+        elif ord('k') == keycode:
+            self.move_selection(-1, None)
+        elif ord('g') == keycode and 'g' == self.prev_key[0]:
+            self.move_selection(None, 0)
+        else:
+            self.prev_key[0] = chr(keycode)
+
 
 def on_activate(app_):
     win = Window(application=app_)