[five-or-more/arnaudb/kill-preferences-dialog: 4/10] Improve hamburger menu.



commit 4259f20f5db7ad243dd9e863c2c7e6272d2156ba
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu May 7 14:13:14 2020 +0200

    Improve hamburger menu.
    
    One more click, but
    a better behaviour.

 data/five-or-more.ui |  9 ++++++++-
 src/game.vala        |  5 ++++-
 src/window.vala      | 51 +++++++++++++++------------------------------------
 3 files changed, 27 insertions(+), 38 deletions(-)
---
diff --git a/data/five-or-more.ui b/data/five-or-more.ui
index 027547b..8482e36 100644
--- a/data/five-or-more.ui
+++ b/data/five-or-more.ui
@@ -31,6 +31,13 @@
             <attribute name="target">BOARD_SIZE_LARGE</attribute>
           </item>
         </section>
+        <section>
+          <item>
+            <!-- Translators: entry of the Size submenu of the hamburger menu (with a mnemonic that appears 
when pressing Alt); starts a new game -->
+            <attribute name="label" translatable="yes">_New Game</attribute>
+            <attribute name="action">win.new-game</attribute>
+          </item>
+        </section>
       </submenu>
     </section>
     <section>
@@ -100,7 +107,7 @@
           </object>
         </child>
         <child>
-          <object class="GtkMenuButton" id="primary_menu_button">
+          <object class="GtkMenuButton">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <property name="menu_model">primary-menu</property>
diff --git a/src/game.vala b/src/game.vala
index 42edd72..9bbc812 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -81,7 +81,7 @@ private class Game : Object
         }
     }
 
-    private const GameDifficulty[] game_difficulty = {
+    internal const GameDifficulty[] game_difficulty = {
         { -1, -1, -1, -1 },
         { 7, 7, 5, 3 },
         { 9, 9, 7, 3 },
@@ -94,6 +94,7 @@ private class Game : Object
         { "Large",  NC_("board size", "Large")  }
     };
 
+    internal bool is_game_over { internal get; private set; default = false; }
     internal signal void game_over ();
     internal int n_categories = 3;
     internal string score_current_category = null;
@@ -108,6 +109,7 @@ private class Game : Object
 
     private void init_game ()
     {
+        is_game_over = false;
         var n_rows = game_difficulty[size].n_rows;
         var n_cols = game_difficulty[size].n_cols;
         this.n_next_pieces = game_difficulty[size].n_next_pieces;
@@ -187,6 +189,7 @@ private class Game : Object
     {
         if (n_cells - n_filled_cells == 0)
         {
+            is_game_over = true;
             game_over ();
             return true;
         }
diff --git a/src/window.vala b/src/window.vala
index f2ef94b..1e51dab 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -32,9 +32,6 @@ private class GameWindow : ApplicationWindow
     [GtkChild]
     private Box preview_hbox;
 
-    [GtkChild]
-    private MenuButton primary_menu_button;
-
     [GtkChild]
     private Games.GridFrame grid_frame;
 
@@ -80,11 +77,6 @@ private class GameWindow : ApplicationWindow
         game = new Game ((int) board_size);
         theme = new ThemeRenderer (settings);
 
-        settings.changed[FiveOrMoreApp.KEY_SIZE].connect (() => {
-            int size = settings.get_int (FiveOrMoreApp.KEY_SIZE);
-            game.new_game (size);
-        });
-
         set_default_size (settings.get_int ("window-width"), settings.get_int ("window-height"));
         if (settings.get_boolean ("window-is-maximized"))
             maximize ();
@@ -176,50 +168,37 @@ private class GameWindow : ApplicationWindow
 
     private inline void change_size (SimpleAction action, Variant? parameter)
     {
-        BoardSize new_size;
+        int size;
         action.set_state (parameter);
         switch (parameter.get_string()) {
-            case "BOARD_SIZE_SMALL":    new_size = BoardSize.SMALL;     break;
-            case "BOARD_SIZE_MEDIUM":   new_size = BoardSize.MEDIUM;    break;
-            case "BOARD_SIZE_LARGE":    new_size = BoardSize.LARGE;     break;
+            case "BOARD_SIZE_SMALL":    size = (int) BoardSize.SMALL;   break;
+            case "BOARD_SIZE_MEDIUM":   size = (int) BoardSize.MEDIUM;  break;
+            case "BOARD_SIZE_LARGE":    size = (int) BoardSize.LARGE;   break;
             default: assert_not_reached ();
         }
+        settings.set_int (FiveOrMoreApp.KEY_SIZE, size);
+    }
 
-        var old_size = settings.get_int ("size");
-        if (old_size == new_size)
-            return;
-
-        primary_menu_button.set_active (false);
-
-        if (game.score > 0) {
+    private inline void new_game (/* SimpleAction action, Variant? parameter */)
+    {
+        int size = settings.get_int (FiveOrMoreApp.KEY_SIZE);
+        int n_rows = Game.game_difficulty[size].n_rows;
+        int n_cols = Game.game_difficulty[size].n_cols;
+        if (game.score > 0 && !game.is_game_over) {
             var flags = DialogFlags.DESTROY_WITH_PARENT;
             var restart_game_dialog = new MessageDialog (this,
                                                          flags,
                                                          MessageType.WARNING,
                                                          ButtonsType.NONE,
-                                                         _("Are you sure you want to restart the game?"));
+                                                         _("Are you sure you want to start a new %u × %u 
game?").printf (n_rows, n_cols));
             restart_game_dialog.add_buttons (_("_Cancel"), ResponseType.CANCEL,
                                              _("_Restart"), ResponseType.OK);
 
             var result = restart_game_dialog.run ();
             restart_game_dialog.destroy ();
-            switch (result)
-            {
-                case ResponseType.OK:
-                     if (!settings.set_int (FiveOrMoreApp.KEY_SIZE, (int) new_size))
-                        warning ("Failed to set size: %d", (int) new_size);
-                    break;
-                case ResponseType.CANCEL:
-                    break;
-            }
-        } else {
-            settings.set_int (FiveOrMoreApp.KEY_SIZE, (int) new_size);
+            if (result != ResponseType.OK)
+                return;
         }
-    }
-
-    private inline void new_game (/* SimpleAction action, Variant? parameter */)
-    {
-        int size = settings.get_int (FiveOrMoreApp.KEY_SIZE);
         game.new_game (size);
     }
 


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