[gnome-klotski] Add some keyboard shortcuts.



commit 0214cda690457d22bc99110edd878359c2e46b58
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed May 6 09:28:46 2020 +0200

    Add some keyboard shortcuts.

 data/klotski.ui         |  3 ++-
 src/klotski-window.vala | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
---
diff --git a/data/klotski.ui b/data/klotski.ui
index 7de3282..b2ce510 100644
--- a/data/klotski.ui
+++ b/data/klotski.ui
@@ -339,6 +339,7 @@
             <property name="use-underline">True</property>
             <!-- Translators: in the headerbar, button that allows via a popover to select a different 
puzzle (with a mnemonic that appears pressing Alt) -->
             <property name="label" translatable="yes">_Change Puzzle</property>
+            <accelerator key="N" signal="activate" modifiers="GDK_CONTROL_MASK"/>
             <property name="can-focus">True</property>
             <property name="focus-on-click">False</property>
             <!-- Translators: tooltip text of the Change Puzzle menubutton, in the headerbar -->
@@ -363,7 +364,7 @@
           </packing>
         </child>
         <child>
-          <object class="GtkMenuButton" id="menu_button">
+          <object class="GtkMenuButton" id="main_menubutton">
             <property name="visible">True</property>
             <property name="valign">center</property>
             <property name="can_focus">True</property>
diff --git a/src/klotski-window.vala b/src/klotski-window.vala
index 4da5a65..c9ada7f 100644
--- a/src/klotski-window.vala
+++ b/src/klotski-window.vala
@@ -49,6 +49,7 @@ private class KlotskiWindow : ApplicationWindow
     [GtkChild] private Stack stack_puzzles;
     [GtkChild] private Popover puzzles_popover;
     [GtkChild] private MenuButton game_menubutton;
+    [GtkChild] private MenuButton main_menubutton;
     private PuzzleView view;
 
     [GtkChild] private TreeView treeview_huarong;
@@ -534,6 +535,7 @@ private class KlotskiWindow : ApplicationWindow
         if (settings.get_boolean ("window-is-maximized"))
             maximize ();
 
+        init_keyboard ();
         manage_high_contrast ();
 
         add_action_entries (win_actions, this);
@@ -1020,4 +1022,39 @@ private class KlotskiWindow : ApplicationWindow
         else
             window_style_context.remove_class ("hc-theme");
     }
+
+    /*\
+    * * keyboard shortcuts
+    \*/
+
+    private EventControllerKey key_controller;          // for keeping in memory
+
+    private inline void init_keyboard ()
+    {
+        key_controller = new EventControllerKey (this);
+        key_controller.propagation_phase = PropagationPhase.CAPTURE;
+        key_controller.key_pressed.connect (on_key_pressed);
+    }
+
+    private inline bool on_key_pressed (EventControllerKey _key_controller, uint keyval, uint keycode, 
Gdk.ModifierType state)
+    {
+        string name = (!) (Gdk.keyval_name (keyval) ?? "");
+
+        if (name == "F10")  // Gtk handles badly having F10 and Ctrl-F10 as actions shortcuts
+        {
+            if (state == 0)
+            {
+                main_menubutton.active = !main_menubutton.active;
+                return true;
+            }
+            if (game_menubutton.sensitive
+             && (state & Gdk.ModifierType.CONTROL_MASK) != 0)
+            {
+                game_menubutton.active = !game_menubutton.active;
+                return true;
+            }
+            return false;
+        }
+        return false;
+    }
 }


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