[gnome-games/wip/exalm/gtk4: 205/238] ui: Stop using 'key-press-event' and 'key-release-event' signals



commit 6fa490917552957c003a624a496c76ef3c27e6cc
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sun Jul 29 00:06:18 2018 +0500

    ui: Stop using 'key-press-event' and 'key-release-event' signals
    
    Replace them with GtkEventControllerKey instead.

 data/ui/application-window.ui              |  6 +++++-
 src/keyboard/keyboard-mapping-builder.vala |  4 ++--
 src/ui/application-window.vala             |  8 ++++----
 src/ui/collection-view.vala                | 13 +++++--------
 src/ui/display-view.vala                   | 12 ++++--------
 src/ui/keyboard-mapper.vala                | 15 +++++++++------
 src/ui/keyboard-tester.vala                | 30 ++++++++++++++++--------------
 src/ui/konami-code.vala                    | 18 +++++++++++-------
 src/ui/ui-view.vala                        |  2 +-
 9 files changed, 57 insertions(+), 51 deletions(-)
---
diff --git a/data/ui/application-window.ui b/data/ui/application-window.ui
index 6aa394b7..eeecb5d2 100644
--- a/data/ui/application-window.ui
+++ b/data/ui/application-window.ui
@@ -6,9 +6,13 @@
     <property name="default-height">600</property>
     <property name="show-menubar">False</property>
     <signal name="delete-event" after="yes" handler="on_delete_event"/>
-    <signal name="key-press-event" after="yes" handler="on_key_pressed"/>
     <signal name="window-state-event" after="yes" handler="on_window_state_event"/>
     <signal name="notify::is-active" after="yes" handler="on_active_changed"/>
+    <child>
+      <object class="GtkEventControllerKey">
+        <signal name="key-pressed" handler="on_key_pressed" swapped="no"/>
+      </object>
+    </child>
     <child>
       <object class="GtkGestureClick">
         <!-- Mouse button 8 is the navigation previous button -->
diff --git a/src/keyboard/keyboard-mapping-builder.vala b/src/keyboard/keyboard-mapping-builder.vala
index f3327b13..40db2bbf 100644
--- a/src/keyboard/keyboard-mapping-builder.vala
+++ b/src/keyboard/keyboard-mapping-builder.vala
@@ -7,7 +7,7 @@ private class Games.KeyboardMappingBuilder : Object {
                mapping = new Retro.KeyJoypadMapping ();
        }
 
-       public bool set_input_mapping (GamepadInput input, uint16 keycode) {
+       public bool set_input_mapping (GamepadInput input, uint keycode) {
                var joypad_id = Retro.JoypadId.from_button_code (input.code);
                if (joypad_id == Retro.JoypadId.COUNT)
                        return false;
@@ -15,7 +15,7 @@ private class Games.KeyboardMappingBuilder : Object {
                for (Retro.JoypadId i = 0; i < Retro.JoypadId.COUNT; i += 1)
                        if (mapping.get_button_key (i) == keycode)
                                return false;
-               mapping.set_button_key (joypad_id, keycode);
+               mapping.set_button_key (joypad_id, (uint16) keycode);
 
                return true;
        }
diff --git a/src/ui/application-window.vala b/src/ui/application-window.vala
index a10f8cd4..401f9ce0 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -163,11 +163,11 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
        }
 
        [GtkCallback]
-       public bool on_key_pressed (Gdk.EventKey event) {
+       public bool on_key_pressed (Gtk.EventControllerKey controller, uint keyval, uint keycode, 
Gdk.ModifierType state) {
                var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
 
-               if ((event.keyval == Gdk.Key.q || event.keyval == Gdk.Key.Q) &&
-                   (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
+               if ((keyval == Gdk.Key.q || keyval == Gdk.Key.Q) &&
+                   (state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
                        if (!quit_game ())
                                return false;
 
@@ -176,7 +176,7 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                        return true;
                }
 
-               return current_view.on_key_pressed (event);
+               return current_view.on_key_pressed (keyval, state);
        }
 
        [GtkCallback]
diff --git a/src/ui/collection-view.vala b/src/ui/collection-view.vala
index 159e3fec..de764c7f 100644
--- a/src/ui/collection-view.vala
+++ b/src/ui/collection-view.vala
@@ -102,15 +102,10 @@ private class Games.CollectionView : Object, UiView {
                box.show_error (error_message);
        }
 
-       public bool on_key_pressed (Gdk.EventKey event) {
+       public bool on_key_pressed (uint keyval, Gdk.ModifierType state) {
                var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
 
-               uint keyval;
-               var keymap = Gdk.Keymap.get_for_display (window.get_display ());
-               keymap.translate_keyboard_state (event.hardware_keycode, event.state,
-                                                event.group, out keyval, null, null, null);
-
-               if (((event.state & default_modifiers) == Gdk.ModifierType.MOD1_MASK) &&
+               if (((state & default_modifiers) == Gdk.ModifierType.MOD1_MASK) &&
                    (((window.get_direction () == Gtk.TextDirection.LTR) && keyval == Gdk.Key.Left) ||
                     ((window.get_direction () == Gtk.TextDirection.RTL) && keyval == Gdk.Key.Right)) &&
                     adaptive_state.is_subview_open) {
@@ -120,13 +115,15 @@ private class Games.CollectionView : Object, UiView {
                }
 
                if ((keyval == Gdk.Key.f || keyval == Gdk.Key.F) &&
-                   (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
+                   (state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
                        if (!search_mode)
                                search_mode = true;
 
                        return true;
                }
 
+               // TODO: Replace with gtk_search_bar_set_key_capture_widget()
+               var event = Gtk.get_current_event ();
                return box.search_bar_handle_event (event);
        }
 
diff --git a/src/ui/display-view.vala b/src/ui/display-view.vala
index 0639957d..96c99f90 100644
--- a/src/ui/display-view.vala
+++ b/src/ui/display-view.vala
@@ -81,16 +81,12 @@ private class Games.DisplayView : Object, UiView {
                focus_out_timeout_id = -1;
        }
 
-       public bool on_key_pressed (Gdk.EventKey event) {
+       public bool on_key_pressed (uint keyval, Gdk.ModifierType state) {
                var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
 
-               uint keyval;
-               var keymap = Gdk.Keymap.get_for_display (window.get_display ());
-               keymap.translate_keyboard_state (event.hardware_keycode, event.state,
-                                                event.group, out keyval, null, null, null);
-               var ctrl_pressed = (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK;
+               var ctrl_pressed = (state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK;
 
-               if (box.on_key_press_event (keyval, event.state & default_modifiers))
+               if (box.on_key_press_event (keyval, state & default_modifiers))
                        return true;
 
                if ((keyval == Gdk.Key.f || keyval == Gdk.Key.F) && ctrl_pressed &&
@@ -115,7 +111,7 @@ private class Games.DisplayView : Object, UiView {
                        return true;
                }
 
-               if (((event.state & default_modifiers) == Gdk.ModifierType.MOD1_MASK) &&
+               if (((state & default_modifiers) == Gdk.ModifierType.MOD1_MASK) &&
                    (((window.get_direction () == Gtk.TextDirection.LTR) && keyval == Gdk.Key.Left) ||
                     ((window.get_direction () == Gtk.TextDirection.RTL) && keyval == Gdk.Key.Right))) {
                        on_display_back ();
diff --git a/src/ui/keyboard-mapper.vala b/src/ui/keyboard-mapper.vala
index 8606ef85..016b912e 100644
--- a/src/ui/keyboard-mapper.vala
+++ b/src/ui/keyboard-mapper.vala
@@ -7,6 +7,8 @@ private class Games.KeyboardMapper : Gtk.Bin {
        [GtkChild]
        private GamepadView gamepad_view;
 
+       private Gtk.EventControllerKey controller;
+
        private KeyboardMappingBuilder mapping_builder;
        private GamepadInput[] mapping_inputs;
        private GamepadInput input;
@@ -25,6 +27,9 @@ private class Games.KeyboardMapper : Gtk.Bin {
 
        construct {
                info_message = _("Press suitable key on your keyboard");
+
+               controller = new Gtk.EventControllerKey ();
+               controller.key_released.connect (on_keyboard_event);
        }
 
        public KeyboardMapper (GamepadViewConfiguration configuration, GamepadInput[] mapping_inputs) {
@@ -49,18 +54,16 @@ private class Games.KeyboardMapper : Gtk.Bin {
        }
 
        private void connect_to_keyboard () {
-               get_toplevel ().key_release_event.connect (on_keyboard_event);
+               get_toplevel ().add_controller (controller);
        }
 
        private void disconnect_from_keyboard () {
-               get_toplevel ().key_release_event.disconnect (on_keyboard_event);
+               get_toplevel ().remove_controller (controller);
        }
 
-       private bool on_keyboard_event (Gdk.EventKey key) {
-               if (mapping_builder.set_input_mapping (input, key.hardware_keycode))
+       private void on_keyboard_event (Gtk.EventControllerKey controller, uint keyval, uint keycode, 
Gdk.ModifierType state) {
+               if (mapping_builder.set_input_mapping (input, keycode))
                        next_input ();
-
-               return true;
        }
 
        private void next_input () {
diff --git a/src/ui/keyboard-tester.vala b/src/ui/keyboard-tester.vala
index 43a8dc0c..e23d7389 100644
--- a/src/ui/keyboard-tester.vala
+++ b/src/ui/keyboard-tester.vala
@@ -5,6 +5,8 @@ private class Games.KeyboardTester : Gtk.Bin {
        [GtkChild]
        private GamepadView gamepad_view;
 
+       private Gtk.EventControllerKey controller;
+
        public Retro.KeyJoypadMapping mapping { get; set; }
 
        private GamepadViewConfiguration _configuration;
@@ -16,6 +18,12 @@ private class Games.KeyboardTester : Gtk.Bin {
                }
        }
 
+       construct {
+               controller = new Gtk.EventControllerKey ();
+               controller.key_pressed.connect (on_key_press_event);
+               controller.key_released.connect (on_key_release_event);
+       }
+
        public KeyboardTester (GamepadViewConfiguration configuration) {
                Object (configuration: configuration);
        }
@@ -30,32 +38,26 @@ private class Games.KeyboardTester : Gtk.Bin {
        }
 
        private void connect_to_keyboard () {
-               var window = get_toplevel ();
-               window.key_press_event.connect (on_key_press_event);
-               window.key_release_event.connect (on_key_release_event);
+               get_toplevel ().add_controller (controller);
        }
 
        private void disconnect_from_keyboard () {
-               var window = get_toplevel ();
-               window.key_press_event.disconnect (on_key_press_event);
-               window.key_release_event.disconnect (on_key_release_event);
+               get_toplevel ().remove_controller (controller);
        }
 
-       private bool on_key_press_event (Gdk.EventKey key) {
-               update_gamepad_view (key, true);
+       private bool on_key_press_event (Gtk.EventControllerKey controller, uint keyval, uint keycode, 
Gdk.ModifierType state) {
+               update_gamepad_view (keycode, true);
 
                return true;
        }
 
-       private bool on_key_release_event (Gdk.EventKey key) {
-               update_gamepad_view (key, false);
-
-               return true;
+       private void on_key_release_event (Gtk.EventControllerKey controller, uint keyval, uint keycode, 
Gdk.ModifierType state) {
+               update_gamepad_view (keycode, false);
        }
 
-       private void update_gamepad_view (Gdk.EventKey key, bool highlight) {
+       private void update_gamepad_view (uint keycode, bool highlight) {
                for (Retro.JoypadId joypad_id = 0; joypad_id < Retro.JoypadId.COUNT; joypad_id += 1) {
-                       if (mapping.get_button_key (joypad_id) == key.hardware_keycode) {
+                       if (mapping.get_button_key (joypad_id) == keycode) {
                                var code = joypad_id.to_button_code ();
                                gamepad_view.highlight ({ EventCode.EV_KEY, code }, highlight);
                        }
diff --git a/src/ui/konami-code.vala b/src/ui/konami-code.vala
index ecc0eafd..efb84615 100644
--- a/src/ui/konami-code.vala
+++ b/src/ui/konami-code.vala
@@ -40,11 +40,15 @@ private class Games.KonamiCode : Object {
        public signal void code_performed ();
 
        private uint current_index;
+       private Gtk.EventControllerKey controller;
 
-       public Gtk.Widget widget {
-               construct {
-                       value.key_press_event.connect (on_key_pressed);
-               }
+       public Gtk.Widget widget { get; construct; }
+
+       construct {
+               controller = new Gtk.EventControllerKey ();
+               controller.key_pressed.connect (on_key_pressed);
+
+               widget.add_controller (controller);
        }
 
        public KonamiCode (Gtk.Widget widget) {
@@ -55,9 +59,9 @@ private class Games.KonamiCode : Object {
                current_index = 0;
        }
 
-       private bool on_key_pressed (Gdk.EventKey event) {
-               if (event.keyval != CODE_LOWER_KEYS[current_index] &&
-                   event.keyval != CODE_UPPER_KEYS[current_index]) {
+       private bool on_key_pressed (Gtk.EventControllerKey self, uint keyval, uint keycode, Gdk.ModifierType 
state) {
+               if (keyval != CODE_LOWER_KEYS[current_index] &&
+                   keyval != CODE_UPPER_KEYS[current_index]) {
                        current_index = 0;
 
                        return false;
diff --git a/src/ui/ui-view.vala b/src/ui/ui-view.vala
index 7feb66c4..6a527afd 100644
--- a/src/ui/ui-view.vala
+++ b/src/ui/ui-view.vala
@@ -7,7 +7,7 @@ private interface Games.UiView : Object {
 
        public abstract bool is_view_active { get; set; }
 
-       public abstract bool on_key_pressed (Gdk.EventKey event);
+       public abstract bool on_key_pressed (uint keyval, Gdk.ModifierType state);
 
        public abstract bool gamepad_button_press_event (Manette.Event event);
 


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