[gnome-2048] Use EventControllerKey.



commit 0af5cebc24814b263e1a799fba7da1db2257561d
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Mar 5 17:34:43 2020 +0100

    Use EventControllerKey.

 meson.build          |  2 +-
 src/game-window.vala | 17 ++++++++++-------
 2 files changed, 11 insertions(+), 8 deletions(-)
---
diff --git a/meson.build b/meson.build
index 52059b0..a25c53c 100644
--- a/meson.build
+++ b/meson.build
@@ -28,7 +28,7 @@ posix_dependency = valac.find_library('posix')
 libm_dependency = cc.find_library('m', required: false) # some platforms do not have libm separated from libc
 gio_dependency = dependency('gio-2.0', version: '>= 2.42.0')
 glib_dependency = dependency('glib-2.0', version: '>= 2.42.0')
-gtk_dependency = dependency('gtk+-3.0', version: '>= 3.22.23')
+gtk_dependency = dependency('gtk+-3.0', version: '>= 3.24.0')
 clutter_dependency = dependency('clutter-1.0', version: '>= 1.12.0')
 clutter_gtk_dependency = dependency('clutter-gtk-1.0', version: '>= 1.6.0')
 gee_dependency = dependency('gee-0.8', version: '>= 0.14.0')
diff --git a/src/game-window.vala b/src/game-window.vala
index c97a151..9deb780 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -292,35 +292,38 @@ private class GameWindow : ApplicationWindow
     }
 
     /*\
-    * * keyboard
+    * * keyboard user actions
     \*/
 
+    private EventControllerKey key_controller;      // for keeping in memory
+
     private const uint16 KEYCODE_W = 25;
     private const uint16 KEYCODE_A = 38;
     private const uint16 KEYCODE_S = 39;
     private const uint16 KEYCODE_D = 40;
 
-    private inline void _init_keyboard ()
+    private inline void _init_keyboard ()   // called on construct
     {
-        key_press_event.connect (key_press_event_cb);
+        key_controller = new EventControllerKey (this);
+        key_controller.key_pressed.connect (on_key_pressed);
     }
 
-    private static bool key_press_event_cb (Widget widget, Gdk.EventKey event)
+    private static inline bool on_key_pressed (EventControllerKey _key_controller, uint keyval, uint 
keycode, Gdk.ModifierType state)
     {
-        GameWindow _this = (GameWindow) widget;
+        GameWindow _this = (GameWindow) _key_controller.get_widget ();
         if (_this._header_bar.has_popover () || (_this.focus_visible && !_this._embed.is_focus))
             return false;
         if (_this._game.cannot_move ())
             return false;
 
-        switch (event.hardware_keycode)
+        switch (keycode)
         {
             case KEYCODE_W:     _this._game.move (MoveRequest.UP);      return true;    // or KEYCODE_UP    
= 111;
             case KEYCODE_A:     _this._game.move (MoveRequest.LEFT);    return true;    // or KEYCODE_LEFT  
= 113;
             case KEYCODE_S:     _this._game.move (MoveRequest.DOWN);    return true;    // or KEYCODE_DOWN  
= 116;
             case KEYCODE_D:     _this._game.move (MoveRequest.RIGHT);   return true;    // or KEYCODE_RIGHT 
= 114;
         }
-        switch (_upper_key (event.keyval))
+        switch (_upper_key (keyval))
         {
             case Gdk.Key.Up:    _this._game.move (MoveRequest.UP);      return true;
             case Gdk.Key.Left:  _this._game.move (MoveRequest.LEFT);    return true;


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