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



commit 74ba8fb340b1ea634e731a9301452feb4ae165f2
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             | 32 ++++++++++++++++--------------
 src/ui/keyboard-mapper.vala                | 15 ++++++++------
 src/ui/keyboard-tester.vala                | 30 +++++++++++++++-------------
 src/ui/konami-code.vala                    | 18 ++++++++++-------
 6 files changed, 60 insertions(+), 45 deletions(-)
---
diff --git a/data/ui/application-window.ui b/data/ui/application-window.ui
index e12823cc..ef01eb89 100644
--- a/data/ui/application-window.ui
+++ b/data/ui/application-window.ui
@@ -7,9 +7,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="GtkGestureMultiPress">
         <!-- 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 bb5dec0c..f952e45e 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -228,11 +228,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;
 
@@ -241,7 +241,7 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                        return true;
                }
 
-               return handle_collection_key_event (event) || handle_display_key_event (event);
+               return handle_collection_key_event (keyval, state) || handle_display_key_event (keyval, 
state);
        }
 
        [GtkCallback]
@@ -617,31 +617,33 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                return true;
        }
 
-       private bool handle_collection_key_event (Gdk.EventKey event) {
+       private bool handle_collection_key_event (uint keyval, Gdk.ModifierType state) {
                if (ui_state != UiState.COLLECTION)
                        return false;
 
                var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
 
-               if ((event.keyval == Gdk.Key.f || event.keyval == Gdk.Key.F) &&
-                   (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
+               if ((keyval == Gdk.Key.f || keyval == Gdk.Key.F) &&
+                   (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 collection_box.search_bar_handle_event (event);
        }
 
-       private bool handle_display_key_event (Gdk.EventKey event) {
+       private bool handle_display_key_event (uint keyval, Gdk.ModifierType state) {
                if (ui_state != UiState.DISPLAY)
                        return false;
 
                var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
 
-               if ((event.keyval == Gdk.Key.f || event.keyval == Gdk.Key.F) &&
-                   (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK &&
+               if ((keyval == Gdk.Key.f || keyval == Gdk.Key.F) &&
+                   (state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK &&
                    display_header_bar.can_fullscreen) {
                        is_fullscreen = !is_fullscreen;
                        settings.set_boolean ("fullscreen", is_fullscreen);
@@ -649,23 +651,23 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                        return true;
                }
 
-               if (event.keyval == Gdk.Key.F11 && display_header_bar.can_fullscreen) {
+               if (keyval == Gdk.Key.F11 && display_header_bar.can_fullscreen) {
                        is_fullscreen = !is_fullscreen;
                        settings.set_boolean ("fullscreen", is_fullscreen);
 
                        return true;
                }
 
-               if (event.keyval == Gdk.Key.Escape && display_header_bar.can_fullscreen) {
+               if (keyval == Gdk.Key.Escape && display_header_bar.can_fullscreen) {
                        is_fullscreen = false;
                        settings.set_boolean ("fullscreen", false);
 
                        return true;
                }
 
-               if (((event.state & default_modifiers) == Gdk.ModifierType.MOD1_MASK) &&
-                   (((get_direction () == Gtk.TextDirection.LTR) && event.keyval == Gdk.Key.Left) ||
-                    ((get_direction () == Gtk.TextDirection.RTL) && event.keyval == Gdk.Key.Right))) {
+               if (((state & default_modifiers) == Gdk.ModifierType.MOD1_MASK) &&
+                   (((get_direction () == Gtk.TextDirection.LTR) && keyval == Gdk.Key.Left) ||
+                    ((get_direction () == Gtk.TextDirection.RTL) && keyval == Gdk.Key.Right))) {
                        on_display_back ();
 
                        return true;
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;


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