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



commit 1d608bb4fb2b773b029fd89fa37fed214585aeed
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                | 14 +++++++------
 src/ui/keyboard-tester.vala                | 27 ++++++++++++-------------
 src/ui/konami-code.vala                    | 11 ++++++----
 6 files changed, 52 insertions(+), 42 deletions(-)
---
diff --git a/data/ui/application-window.ui b/data/ui/application-window.ui
index 28fe2a2e..dfffe4f0 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 c2b3d7f3..acc276fd 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -215,11 +215,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;
 
@@ -228,7 +228,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]
@@ -587,31 +587,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);
@@ -619,23 +621,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 505ed1f8..e40d9ecf 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;
@@ -26,6 +28,8 @@ private class Games.KeyboardMapper : Gtk.Bin {
                catch (Error e) {
                        critical ("Could not set up gamepad view: %s", e.message);
                }
+               controller = new Gtk.EventControllerKey ();
+               controller.key_released.connect (on_keyboard_event);
        }
 
        public void start () {
@@ -45,18 +49,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 1aba4703..b34d3e01 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; }
 
        public KeyboardTester (GamepadViewConfiguration configuration) {
@@ -14,6 +16,9 @@ private class Games.KeyboardTester : Gtk.Bin {
                catch (Error e) {
                        critical ("Could not set up gamepad view: %s", e.message);
                }
+               controller = new Gtk.EventControllerKey ();
+               controller.key_pressed.connect (on_key_press_event);
+               controller.key_released.connect (on_key_release_event);
        }
 
        public void start () {
@@ -26,32 +31,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 0f76471b..402f2754 100644
--- a/src/ui/konami-code.vala
+++ b/src/ui/konami-code.vala
@@ -40,18 +40,21 @@ private class Games.KonamiCode : Object {
        public signal void code_performed ();
 
        private uint current_index;
+       private Gtk.EventControllerKey controller;
 
        public KonamiCode (Gtk.Widget widget) {
-               widget.key_press_event.connect (on_key_pressed);
+               controller = new Gtk.EventControllerKey ();
+               controller.key_pressed.connect (on_key_pressed);
+               widget.add_controller (controller);
        }
 
        public void reset () {
                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]