[gnome-music/wip/jfelder/event-controller-key: 16/17] window: Use a controller to handle key press




commit e0a9c5986a3827c9a1648322a10a78a3eff0714c
Author: Jean Felder <jfelder src gnome org>
Date:   Wed Mar 24 20:47:44 2021 +0100

    window: Use a controller to handle key press
    
    The "key-press-event" event has been removed from GTK4. Use a
    controller instead.

 data/ui/Window.ui    |  5 +++++
 gnomemusic/window.py | 10 ++++------
 2 files changed, 9 insertions(+), 6 deletions(-)
---
diff --git a/data/ui/Window.ui b/data/ui/Window.ui
index 19e7dc851..fa2288edc 100644
--- a/data/ui/Window.ui
+++ b/data/ui/Window.ui
@@ -44,4 +44,9 @@
       </object>
     </child>
   </template>
+  <object class="GtkEventControllerKey" id="_key_controller">
+    <property name="widget">Window</property>
+    <property name="propagation-phase">capture</property>
+    <signal name="key-pressed" handler="_on_key_press" swapped="no"/>
+  </object>
 </interface>
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index d776a43ea..b4ca3676d 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -57,6 +57,7 @@ class Window(Gtk.ApplicationWindow):
 
     notifications_popup = Gtk.Template.Child()
     _headerbar_stack = Gtk.Template.Child()
+    _key_controller = Gtk.Template.Child()
     _overlay = Gtk.Template.Child()
     _player_toolbar = Gtk.Template.Child()
     _selection_toolbar = Gtk.Template.Child()
@@ -228,8 +229,6 @@ class Window(Gtk.ApplicationWindow):
         self._on_notify_model_id = self._stack.connect(
             'notify::visible-child', self._on_notify_mode)
         self.connect('destroy', self._notify_mode_disconnect)
-        self._key_press_event_id = self.connect(
-            'key_press_event', self._on_key_press)
 
         self._btn_ctrl = Gtk.GestureMultiPress().new(self)
         self._btn_ctrl.props.propagation_phase = Gtk.PropagationPhase.CAPTURE
@@ -289,10 +288,9 @@ class Window(Gtk.ApplicationWindow):
 
         self.props.active_view.deselect_all()
 
-    def _on_key_press(self, widget, event):
-        modifiers = event.get_state() & Gtk.accelerator_get_default_mod_mask()
-        (_, keyval) = event.get_keyval()
-
+    @Gtk.Template.Callback()
+    def _on_key_press(self, controller, keyval, keycode, state):
+        modifiers = state & Gtk.accelerator_get_default_mod_mask()
         control_mask = Gdk.ModifierType.CONTROL_MASK
         shift_mask = Gdk.ModifierType.SHIFT_MASK
         mod1_mask = Gdk.ModifierType.MOD1_MASK


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]