[gnome-tetravex] Use EventControllerKey.



commit 905a841eec0128428cad5dcf7b6e9e0017b258b3
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Feb 20 20:53:06 2020 +0100

    Use EventControllerKey.

 src/gnome-tetravex.vala | 16 +++++++++-------
 src/puzzle-view.vala    | 16 +++++++++++++---
 2 files changed, 22 insertions(+), 10 deletions(-)
---
diff --git a/src/gnome-tetravex.vala b/src/gnome-tetravex.vala
index 0e86da5..5b5632b 100644
--- a/src/gnome-tetravex.vala
+++ b/src/gnome-tetravex.vala
@@ -304,7 +304,8 @@ private class Tetravex : Gtk.Application
 
         window = (ApplicationWindow) builder.get_object ("gnome-tetravex-window");
         this.add_window (window);
-        window.key_press_event.connect (on_key_press_event);
+        key_controller = new EventControllerKey (window);
+        key_controller.key_pressed.connect (on_key_pressed);
         window.size_allocate.connect (size_allocate_cb);
         window.window_state_event.connect (window_state_event_cb);
         window.set_default_size (settings.get_int ("window-width"), settings.get_int ("window-height"));
@@ -955,9 +956,10 @@ private class Tetravex : Gtk.Application
         play_pause_stack.set_visible_child_name (puzzle.paused ? "play" : "pause");
     }
 
-    private bool on_key_press_event (Widget widget, Gdk.EventKey event)
+    private EventControllerKey key_controller;    // for keeping in memory
+    private inline bool on_key_pressed (EventControllerKey _key_controller, uint keyval, uint keycode, 
Gdk.ModifierType state)
     {
-        string name = (!) (Gdk.keyval_name (event.keyval) ?? "");
+        string name = (!) (Gdk.keyval_name (keyval) ?? "");
 
         if (name == "Escape" && !puzzle.is_solved)
         {
@@ -973,7 +975,7 @@ private class Tetravex : Gtk.Application
             }
         }
         else if (name == "F1")
-            return on_f1_pressed (event);   // TODO fix dance done with the F1 & <Primary>F1 shortcuts that 
show help overlay
+            return on_f1_pressed (state);   // TODO fix dance done with the F1 & <Primary>F1 shortcuts that 
show help overlay
 
         return false;
     }
@@ -982,12 +984,12 @@ private class Tetravex : Gtk.Application
     * * help/about
     \*/
 
-    private bool on_f1_pressed (Gdk.EventKey event)
+    private bool on_f1_pressed (Gdk.ModifierType state)
     {
         // TODO close popovers
-        if ((event.state & Gdk.ModifierType.CONTROL_MASK) != 0)
+        if ((state & Gdk.ModifierType.CONTROL_MASK) != 0)
             return false;                           // help overlay
-        if ((event.state & Gdk.ModifierType.SHIFT_MASK) == 0)
+        if ((state & Gdk.ModifierType.SHIFT_MASK) == 0)
         {
             help_cb ();
             return true;
diff --git a/src/puzzle-view.vala b/src/puzzle-view.vala
index 9901f89..d781b11 100644
--- a/src/puzzle-view.vala
+++ b/src/puzzle-view.vala
@@ -193,6 +193,8 @@ private class PuzzleView : Gtk.DrawingArea
 
     construct
     {
+        init_keyboard ();
+
         set_events (Gdk.EventMask.EXPOSURE_MASK
                   | Gdk.EventMask.BUTTON_PRESS_MASK
                   | Gdk.EventMask.POINTER_MOTION_MASK
@@ -875,9 +877,11 @@ private class PuzzleView : Gtk.DrawingArea
     }
 
     /*\
-    * * keyboard
+    * * keyboard user actions
     \*/
 
+    private Gtk.EventControllerKey key_controller;    // for keeping in memory
+
     private bool show_highlight = false;
 
     private bool highlight_set = false;
@@ -890,7 +894,13 @@ private class PuzzleView : Gtk.DrawingArea
     private uint8 kbd_selected_x = uint8.MAX;
     private uint8 kbd_selected_y = uint8.MAX;
 
-    protected override bool key_press_event (Gdk.EventKey event)
+    private void init_keyboard ()  // called on construct
+    {
+        key_controller = new Gtk.EventControllerKey (this);
+        key_controller.key_pressed.connect (on_key_pressed);
+    }
+
+    private inline bool on_key_pressed (Gtk.EventControllerKey _key_controller, uint keyval, uint keycode, 
Gdk.ModifierType state)
     {
         if (!puzzle_init_done)
             return false;
@@ -901,7 +911,7 @@ private class PuzzleView : Gtk.DrawingArea
         if (tile_selected)
             return false;
 
-        string key = (!) (Gdk.keyval_name (event.keyval) ?? "");
+        string key = (!) (Gdk.keyval_name (keyval) ?? "");
         if (key == "")
             return false;
 


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