[gnome-sudoku/arnaudb/wip/gtk4: 16/43] Use EventControllerFocus.




commit 17de1fb8241dcbd419f49c8d7f5e5e9000cdc954
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sun Apr 12 14:16:42 2020 +0200

    Use EventControllerFocus.

 src/sudoku-view.vala | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
---
diff --git a/src/sudoku-view.vala b/src/sudoku-view.vala
index 725bbc5..c3b2175 100644
--- a/src/sudoku-view.vala
+++ b/src/sudoku-view.vala
@@ -92,6 +92,7 @@ private class SudokuCellView : DrawingArea
     private NumberPicker number_picker;
     private NumberPicker earmark_picker;
 
+    internal EventControllerFocus focus_controller; // for keeping in memory
     private EventControllerKey key_controller;      // for keeping in memory
     private GestureClick click_controller;          // for keeping in memory
 
@@ -114,7 +115,6 @@ private class SudokuCellView : DrawingArea
         if (is_fixed && game.mode == GameMode.PLAY)
             return;
 
-        focus_out_event.connect (focus_out_cb);
         game.cell_changed.connect (cell_changed_cb);
     }
 
@@ -169,6 +169,7 @@ private class SudokuCellView : DrawingArea
         earmark_picker.set_earmarks (row, col);
     }
 
+    private EventControllerFocus popover_focus_controller;
     private void show_number_picker ()
     {
         if (earmark_popover != null)
@@ -193,14 +194,14 @@ private class SudokuCellView : DrawingArea
             if (!popover.visible)
                 destroy_popover (ref popover, ref number_picker);
         });
-        popover.focus_out_event.connect (() => {
-            popover.hide ();
-            return true;
-        });
+        popover_focus_controller = new EventControllerFocus ();
+        popover_focus_controller.leave.connect (popover.hide);
+        ((Widget) popover).add_controller (popover_focus_controller);
 
         popover.show ();
     }
 
+    private EventControllerFocus earmark_focus_controller;
     private void show_earmark_picker ()
     {
         if (popover != null)
@@ -216,10 +217,9 @@ private class SudokuCellView : DrawingArea
             if (!earmark_popover.visible)
                 destroy_popover (ref earmark_popover, ref earmark_picker);
         });
-        earmark_popover.focus_out_event.connect (() => {
-            earmark_popover.hide ();
-            return true;
-        });
+        earmark_focus_controller = new EventControllerFocus ();
+        earmark_focus_controller.leave.connect (earmark_popover.hide);
+        ((Widget) earmark_popover).add_controller (earmark_focus_controller);
 
         earmark_popover.show ();
     }
@@ -242,12 +242,6 @@ private class SudokuCellView : DrawingArea
             earmark_popover.hide ();
     }
 
-    private bool focus_out_cb (Widget widget, EventFocus event)
-    {
-        hide_both_popovers ();
-        return false;
-    }
-
     /* Key mapping function to help convert Gdk.keyval_name string to numbers */
     private int key_map_keypad (string key_name)
     {
@@ -277,11 +271,20 @@ private class SudokuCellView : DrawingArea
 
     private inline void init_keyboard ()  // called on construct
     {
+        focus_controller = new EventControllerFocus ();
+        focus_controller.leave.connect (on_focus_out);
+        add_controller (focus_controller);
+
         key_controller = new EventControllerKey ();
         key_controller.key_pressed.connect (on_key_pressed);
         add_controller (key_controller);
     }
 
+    private inline void on_focus_out ()
+    {
+        hide_both_popovers ();
+    }
+
     private inline bool on_key_pressed (EventControllerKey _key_controller, uint keyval, uint keycode, 
ModifierType state)
     {
         if (game.mode == GameMode.PLAY && (is_fixed || game.paused))
@@ -557,9 +560,9 @@ public class SudokuView : AspectFrame
                 var cell_row = row;
                 var cell_col = col;
 
-                cell.focus_in_event.connect (() => {
+                cell.focus_controller.enter.connect (() => {
                     if (game.paused)
-                        return false;
+                        return;
 
                     this.set_selected (cell_row, cell_col);
                     this.update_highlights ();
@@ -575,8 +578,6 @@ public class SudokuView : AspectFrame
                     this.set_selected (-1, -1);
                     this.update_highlights ();
                     queue_draw ();
-
-                    return false;
                 });
 
                 cell.notify["value"].connect ((s, p)=> {


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