[gnome-2048] Do not use GtkBuilder for hamburger menu.



commit 9798bb8a09135e88cdd6a1118350b22ec9c55aa5
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Jan 25 14:48:20 2019 +0100

    Do not use GtkBuilder for hamburger menu.
    
    That allows to add contextual entries.

 data/mainwindow.ui   | 27 ---------------------------
 src/application.vala | 42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 28 deletions(-)
---
diff --git a/data/mainwindow.ui b/data/mainwindow.ui
index 5d10554..f9fadea 100644
--- a/data/mainwindow.ui
+++ b/data/mainwindow.ui
@@ -76,7 +76,6 @@
             <property name="visible">True</property>
             <property name="halign">end</property>
             <property name="valign">center</property>
-            <property name="menu-model">hamburger-menu</property>
             <property name="focus-on-click">False</property>
             <style>
               <class name="image-button"/>
@@ -150,30 +149,4 @@
       <attribute name="target">5</attribute>
     </item>
   </menu>
-  <menu id="hamburger-menu">
-    <section>
-      <item>
-        <!-- Translators: entry in the hamburger menu; opens a window showing best scores -->
-        <attribute name="label" translatable="yes">Scores</attribute>
-        <attribute name="action">app.scores</attribute>
-      </item>
-    </section>
-    <section>
-      <item>
-        <!-- Translators: entry in the hamburger menu; opens a window for configuring application -->
-        <attribute name="label" translatable="yes">Preferences</attribute>
-        <attribute name="action">app.preferences</attribute>
-      </item>
-      <item>
-        <!-- Translators: usual menu entry of the hamburger menu -->
-        <attribute name="label" translatable="yes">Keyboard Shortcuts</attribute>
-        <attribute name="action">win.show-help-overlay</attribute>
-      </item>
-      <item>
-        <!-- Translators: entry in the hamburger menu -->
-        <attribute name="label" translatable="yes">About 2048</attribute>
-        <attribute name="action">app.about</attribute>
-      </item>
-    </section>
-  </menu>
 </interface>
diff --git a/src/application.vala b/src/application.vala
index 2153586..ec7a38c 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -227,6 +227,7 @@ public class Application : Gtk.Application
                 if (!_hamburger_button.active)
                     embed.grab_focus ();
             });
+        _update_hamburger_menu ();
     }
 
     private void _create_game_view (Builder builder)
@@ -238,9 +239,48 @@ public class Application : Gtk.Application
     }
 
     /*\
-    * * Hamburger-menu (and undo action) callbacks
+    * * hamburger menu (and undo action) callbacks
     \*/
 
+    private void _update_hamburger_menu ()
+    {
+        GLib.Menu menu = new GLib.Menu ();
+
+        _append_scores_section (ref menu);
+        _append_app_actions_section (ref menu);
+
+        menu.freeze ();
+        _hamburger_button.set_menu_model ((MenuModel) menu);
+    }
+
+    private static inline void _append_scores_section (ref GLib.Menu menu)
+    {
+        GLib.Menu section = new GLib.Menu ();
+
+        /* Translators: entry in the hamburger menu; opens a window showing best scores */
+        section.append (_("Scores"), "app.scores");
+
+        section.freeze ();
+        menu.append_section (null, section);
+    }
+
+    private static inline void _append_app_actions_section (ref GLib.Menu menu)
+    {
+        GLib.Menu section = new GLib.Menu ();
+
+        /* Translators: entry in the hamburger menu; opens a window for configuring application */
+        section.append (_("Preferences"), "app.preferences");
+
+        /* Translators: usual menu entry of the hamburger menu */
+        section.append (_("Keyboard Shortcuts"), "win.show-help-overlay");
+
+        /* Translators: entry in the hamburger menu */
+        section.append (_("About 2048"), "app.about");
+
+        section.freeze ();
+        menu.append_section (null, section);
+    }
+
     private void toggle_hamburger_menu (/* SimpleAction action, Variant? variant */)
     {
         _hamburger_button.active = !_hamburger_button.active;


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