[gnome-2048] Make the New Game button a menubutton.



commit a44888bf182bdec1f762f89c10aa706fca311695
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Jan 24 21:53:41 2019 +0100

    Make the New Game button a menubutton.
    
    That makes restarting by mistake harder,
    fixes a bit the Preferences dialog goal,
    and makes the 5-sized game more visible.
    
    Change the associated keyboard shortcut,
    and add a new one for toggling the menu.
    Use <Ctrl>n to toggle the New Game menu,
    and use <Ctrl>N to start a similar game.

 data/mainwindow.ui   | 18 ++++++++++--
 data/preferences.ui  | 30 --------------------
 src/application.vala | 78 +++++++++++++++++++++++++---------------------------
 src/game.vala        |  6 ++--
 4 files changed, 57 insertions(+), 75 deletions(-)
---
diff --git a/data/mainwindow.ui b/data/mainwindow.ui
index 623b12e..1d267b8 100644
--- a/data/mainwindow.ui
+++ b/data/mainwindow.ui
@@ -57,15 +57,15 @@
           </packing>
         </child>
         <child>
-          <object class="GtkButton">
+          <object class="GtkMenuButton" id="new-game-button">
             <!-- Translators: button in the headerbar -->
             <property name="label" translatable="yes">_New Game</property>
             <property name="visible">True</property>
             <property name="can-focus">True</property>
             <property name="receives-default">True</property>
-            <property name="action-name">app.new-game</property>
             <property name="use-underline">True</property>
             <property name="focus-on-click">False</property>
+            <property name="menu-model">new-game-menu</property>
           </object>
           <packing>
             <property name="position">2</property>
@@ -136,6 +136,20 @@
       </object>
     </child>
   </object>
+  <menu id="new-game-menu">
+    <item>
+      <!-- Translators: on main window, entry of the menu when clicking on the "New Game" button; to change 
grid size to 4 × 4 -->
+      <attribute name="label" translatable="yes">4 × 4</attribute>
+      <attribute name="action">app.new-game-sized</attribute>
+      <attribute name="target">4</attribute>
+    </item>
+    <item>
+      <!-- Translators: on main window, entry of the menu when clicking on the "New Game" button; to change 
grid size to 5 × 5 -->
+      <attribute name="label" translatable="yes">5 × 5</attribute>
+      <attribute name="action">app.new-game-sized</attribute>
+      <attribute name="target">5</attribute>
+    </item>
+  </menu>
   <menu id="hamburger-menu">
     <section>
       <item>
diff --git a/data/preferences.ui b/data/preferences.ui
index e289486..2a60e19 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -63,36 +63,6 @@
             <property name="can-focus">False</property>
             <property name="row-spacing">12</property>
             <property name="column-spacing">6</property>
-            <child>
-              <object class="GtkLabel">
-                <property name="visible">True</property>
-                <property name="can-focus">False</property>
-                <property name="xalign">1</property>
-                <!-- Translators: in the preferences dialog; label introducing an option, to change grid 
size to 4 × 4 or 5 × 5 -->
-                <property name="label" translatable="yes">Grid size</property>
-              </object>
-              <packing>
-                <property name="left-attach">0</property>
-                <property name="top-attach">0</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkComboBoxText" id="gridsizecombo">
-                <property name="visible">True</property>
-                <property name="can-focus">False</property>
-                <property name="active">0</property>
-                <items>
-                  <!-- Translators: in the preferences dialog; combobox entry, to change grid size to 4 × 4 
-->
-                  <item id="0" translatable="yes">4 × 4</item>
-                  <!-- Translators: in the preferences dialog; combobox entry, to change grid size to 5 × 5 
-->
-                  <item id="1" translatable="yes">5 × 5</item>
-                </items>
-              </object>
-              <packing>
-                <property name="left-attach">1</property>
-                <property name="top-attach">0</property>
-              </packing>
-            </child>
             <child>
               <object class="GtkLabel">
                 <property name="visible">True</property>
diff --git a/src/application.vala b/src/application.vala
index 690cfbe..62bc6d7 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -37,6 +37,7 @@ public class Application : Gtk.Application
     private HeaderBar _header_bar;
     private Label _congrats_message;
     private Label _score;
+    private MenuButton _new_game_button;
     private MenuButton _hamburger_button;
 
     private GtkClutter.Embed embed;
@@ -54,17 +55,18 @@ public class Application : Gtk.Application
     {
         { "undo",               undo_cb                 },
 
+        { "new-game",           new_game_cb             },
+        { "toggle-new-game",    toggle_new_game_cb      },
+        { "new-game-sized",     new_game_sized_cb, "s"  },  // no way to make it take an int8/int32
+
+        { "quit",               quit_cb                 },
+
         // hamburger-menu
         { "toggle-hamburger",   toggle_hamburger_menu   },
 
-        { "new-game",           new_game_cb             },
         { "scores",             scores_cb               },
-
         { "preferences",        preferences_cb          },
-
-/*      { "help",               help_cb                 }, */
         { "about",              about_cb                },
-        { "quit",               quit_cb                 }
     };
 
     public static int main (string[] args)
@@ -130,7 +132,8 @@ public class Application : Gtk.Application
 
         _create_scores ();
 
-        set_accels_for_action ("app.new-game",          {        "<Primary>n"       });
+        set_accels_for_action ("app.toggle-new-game",   {        "<Primary>n"       });
+        set_accels_for_action ("app.new-game",          { "<Shift><Primary>n"       });
         set_accels_for_action ("app.quit",              {        "<Primary>q"       });
         set_accels_for_action ("app.about",             { "<Shift><Primary>F1",
                                                                  "<Primary>F1",
@@ -230,6 +233,8 @@ public class Application : Gtk.Application
 
         ((SimpleAction) lookup_action ("undo")).set_enabled (false);
 
+        _new_game_button = (MenuButton) builder.get_object ("new-game-button");
+
         _hamburger_button = (MenuButton) builder.get_object ("hamburger-button");
         _hamburger_button.notify ["active"].connect (() => {
                 if (!_hamburger_button.active)
@@ -268,17 +273,17 @@ public class Application : Gtk.Application
     * * Hamburger-menu (and undo action) callbacks
     \*/
 
-    private void toggle_hamburger_menu ()
+    private void toggle_hamburger_menu (/* SimpleAction action, Variant? variant */)
     {
         _hamburger_button.active = !_hamburger_button.active;
     }
 
-    private void undo_cb ()
+    private void undo_cb (/* SimpleAction action, Variant? variant */)
     {
         _game.undo ();
     }
 
-    private void new_game_cb ()
+    private void new_game_cb (/* SimpleAction action, Variant? variant */)
     {
         _header_bar.subtitle = null;
         _game_restored = false;
@@ -288,7 +293,23 @@ public class Application : Gtk.Application
         embed.grab_focus ();
     }
 
-    private void scores_cb ()
+    private void toggle_new_game_cb (/* SimpleAction action, Variant? variant */)
+    {
+        _new_game_button.active = !_new_game_button.active;
+    }
+
+    private void new_game_sized_cb (SimpleAction action, Variant? variant)
+        requires (variant != null)
+    {
+        int size = int.parse (((!) variant).get_string ());
+        _settings.set_int ("rows", size);
+        _settings.set_int ("cols", size);
+
+        _game.reload_settings ();
+        new_game_cb ();
+    }
+
+    private void scores_cb (/* SimpleAction action, Variant? variant */)
     {
         _scores_ctx.run_dialog ();
     }
@@ -302,7 +323,7 @@ public class Application : Gtk.Application
         }
     } */
 
-    private void about_cb ()
+    private void about_cb (/* SimpleAction action, Variant? variant */)
     {
         string [] authors = { "Juan R. García Blanco", "Arnaud Bonatti" };
         show_about_dialog (_window,
@@ -327,7 +348,7 @@ public class Application : Gtk.Application
                            null);
     }
 
-    private void quit_cb ()
+    private void quit_cb (/* SimpleAction action, Variant? variant */)
     {
         _window.destroy ();
     }
@@ -365,8 +386,7 @@ public class Application : Gtk.Application
     * * preferences dialog
     \*/
 
-    private Dialog          _preferences_dialog;
-    private ComboBoxText    _grid_size_combo;
+    private Dialog _preferences_dialog;
 
     private bool _should_create_preferences_dialog = true;
     private inline void _create_preferences_dialog ()
@@ -376,34 +396,17 @@ public class Application : Gtk.Application
         _preferences_dialog = (Dialog) builder.get_object ("preferencesdialog");
         _preferences_dialog.set_transient_for (_window);
 
-        _grid_size_combo = (ComboBoxText) builder.get_object ("gridsizecombo");
-
         _preferences_dialog.response.connect ((response_id) => {
                 _preferences_dialog.hide_on_delete ();
             });
         _preferences_dialog.delete_event.connect ((response_id) => {
-                int grid_size;
-                int rows, cols;
-                bool settings_changed;
-
-                grid_size = _grid_size_combo.get_active ();
-                if (grid_size == 0)
-                    rows = cols = 4;
-                else
-                    rows = cols = 5;
-
-                _settings.set_int ("rows", rows);
-                _settings.set_int ("cols", cols);
-
-                settings_changed = _game.reload_settings ();
-                if (settings_changed)
-                    new_game_cb ();
+                _game.reload_settings ();
                 return _preferences_dialog.hide_on_delete ();
             });
 
-        _settings.bind ("do-congrat", builder.get_object ("congratswitch"), "active", 
GLib.SettingsBindFlags.DEFAULT);
-        _settings.bind ("animations-speed", builder.get_object ("animationsspeed"), "value", 
GLib.SettingsBindFlags.DEFAULT);
-        _settings.bind ("allow-undo", builder.get_object ("undoswitch"), "active", 
GLib.SettingsBindFlags.DEFAULT);
+        _settings.bind ("do-congrat",       builder.get_object ("congratswitch"),   "active", 
GLib.SettingsBindFlags.DEFAULT);
+        _settings.bind ("animations-speed", builder.get_object ("animationsspeed"), "value",  
GLib.SettingsBindFlags.DEFAULT);
+        _settings.bind ("allow-undo",       builder.get_object ("undoswitch"),      "active", 
GLib.SettingsBindFlags.DEFAULT);
     }
 
     private inline void preferences_cb (/* SimpleAction action, Variant? variant */)
@@ -414,11 +417,6 @@ public class Application : Gtk.Application
             _should_create_preferences_dialog = false;
         }
 
-        if (_settings.get_int ("rows") == 4)
-            _grid_size_combo.set_active (0);
-        else
-            _grid_size_combo.set_active (1);
-
         _preferences_dialog.present ();
     }
 
diff --git a/src/game.vala b/src/game.vala
index d27a339..e0aee9a 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -208,7 +208,7 @@ public class Game : Object
         return false;
     }
 
-    public bool reload_settings ()
+    public void reload_settings ()
     {
         int rows, cols;
         bool allow_undo;
@@ -237,10 +237,10 @@ public class Game : Object
 
             _init_background ();
 
-            return true;
+         // return true;
         }
 
-        return false;
+     // return false;
     }
 
     private uint _upper_key (uint keyval)


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