[iagno] Use EventControllerKey again.



commit 42372f5b2fa1db7a38a04671f572ac38398f2b51
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Feb 12 14:43:22 2020 +0100

    Use EventControllerKey again.

 data/ui/base-window.ui |  1 -
 src/base-window.vala   | 54 +++++++++++++++++++++++++++++++-------------------
 2 files changed, 34 insertions(+), 21 deletions(-)
---
diff --git a/data/ui/base-window.ui b/data/ui/base-window.ui
index 382d39a..260827f 100644
--- a/data/ui/base-window.ui
+++ b/data/ui/base-window.ui
@@ -19,7 +19,6 @@
   <requires lib="gtk+" version="3.12"/>
   <template class="BaseWindow" parent="AdaptativeWindow">
     <property name="visible">False</property>
-    <signal name="key-press-event" handler="on_key_press_event"/>
     <child>
       <object class="GtkOverlay" id="main_overlay">
         <property name="visible">True</property>
diff --git a/src/base-window.vala b/src/base-window.vala
index 8edd584..7181b42 100644
--- a/src/base-window.vala
+++ b/src/base-window.vala
@@ -61,6 +61,8 @@ private class BaseWindow : AdaptativeWindow, AdaptativeWidget
     {
         headerbar = (BaseHeaderBar) nta_headerbar;
 
+        init_keyboard ();
+
         install_action_entries ();
 
         add_adaptative_child (headerbar);
@@ -306,37 +308,46 @@ private class BaseWindow : AdaptativeWindow, AdaptativeWidget
     }
 
     /*\
-    * * global callbacks
+    * * keyboard user actions
     \*/
 
-    [CCode (notify = false)] public string help_string_or_empty { private get; protected construct; default 
= ""; }
+    private Gtk.EventControllerKey key_controller;    // for keeping in memory
+
+    private void init_keyboard ()  // called on construct
+    {
+        key_controller = new Gtk.EventControllerKey (this);
+        key_controller.key_pressed.connect (on_key_pressed);
+    }
 
-    [GtkCallback]
-    protected virtual bool on_key_press_event (Widget widget, Gdk.EventKey event)
+    protected inline bool on_key_pressed (Gtk.EventControllerKey _key_controller, uint keyval, uint keycode, 
Gdk.ModifierType state)
     {
-        return _on_key_press_event (widget, event, help_string_or_empty);
+        return _on_key_press_event (keyval, state);
     }
-    private static bool _on_key_press_event (Widget widget, Gdk.EventKey event, string help_string_or_empty)
+    private bool _on_key_press_event (uint keyval, Gdk.ModifierType state)
     {
-        string name = (!) (Gdk.keyval_name (event.keyval) ?? "");
+        string name = Gdk.keyval_name (keyval) ?? "";
 
         if (name == "F1") // TODO fix dance done with the F1 & <Primary>F1 shortcuts that show help overlay
         {
-            BaseWindow _this = (BaseWindow) widget;
-
-            _this.headerbar.close_popovers ();
-            _this.main_view.close_popovers ();
-            if ((event.state & Gdk.ModifierType.CONTROL_MASK) != 0)
+            headerbar.close_popovers ();
+            main_view.close_popovers ();
+            if ((state & Gdk.ModifierType.CONTROL_MASK) != 0)
                 return false;                           // help overlay
-            if ((event.state & Gdk.ModifierType.SHIFT_MASK) == 0)
-                return show_application_help (_this, help_string_or_empty);   // fallback on help overlay 
(TODO test)
-            _this.about ();
+            if ((state & Gdk.ModifierType.SHIFT_MASK) == 0)
+                return show_application_help (this, help_string_or_empty);   // fallback on help overlay 
(TODO test)
+            about ();
             return true;
         }
 
         return false;
     }
 
+    /*\
+    * * help
+    \*/
+
+    [CCode (notify = false)] public string help_string_or_empty { private get; protected construct; default 
= ""; }
+
     private void help (/* SimpleAction action, Variant? variant */)
     {
         show_application_help (this, help_string_or_empty);
@@ -422,24 +433,27 @@ private class BaseWindow : AdaptativeWindow, AdaptativeWidget
     \*/
 
     private AboutDialog about_dialog;
+    private Gtk.EventControllerKey about_dialog_key_controller;    // for keeping in memory
+
     private void show_about_dialog ()
     {
         if (should_init_about_dialog)
         {
             create_about_dialog ();
             about_dialog.response.connect ((_about_dialog, response) => _about_dialog.hide ());
-            about_dialog.key_press_event.connect (about_dialog_key_press_event);
+            about_dialog_key_controller = new Gtk.EventControllerKey (about_dialog);
+            about_dialog_key_controller.key_pressed.connect (on_about_dialog_key_pressed);
             about_dialog.set_transient_for (this);
             should_init_about_dialog = false;
         }
         about_dialog.run ();
     }
-    private static bool about_dialog_key_press_event (Widget _about_dialog_widget, Gdk.EventKey event)
+    private inline bool on_about_dialog_key_pressed (Gtk.EventControllerKey _about_dialog_key_controller, 
uint keyval, uint keycode, Gdk.ModifierType state)
     {
-        if (((!) (Gdk.keyval_name (event.keyval) ?? "") == "F1")
-         && ((event.state & Gdk.ModifierType.SHIFT_MASK) != 0))
+        if (((!) (Gdk.keyval_name (keyval) ?? "") == "F1")
+         && ((state & Gdk.ModifierType.SHIFT_MASK) != 0))
         {
-            ((Dialog) _about_dialog_widget).response (ResponseType.CANCEL);
+            about_dialog.response (ResponseType.CANCEL);
             return true;
         }
         return false;


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