[gnome-2048] Move some code.



commit d193e5ab432c4d74350a41893aac1ac2eb507ebe
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Jan 25 14:26:43 2019 +0100

    Move some code.
    
    Not allowed to create the Scores dialog
    on the fly, so just group related code.

 src/application.vala | 91 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 50 insertions(+), 41 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 7a15682..2153586 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -41,10 +41,6 @@ public class Application : Gtk.Application
 
     private GtkClutter.Embed embed;
 
-    private Scores.Context _scores_ctx;
-    private Scores.Category _grid4_cat;
-    private Scores.Category _grid5_cat;
-
     private bool _game_restored;
 
     private Game _game;
@@ -129,7 +125,7 @@ public class Application : Gtk.Application
 
         _create_window ();
 
-        _create_scores ();
+        _create_scores_dialog ();   // the library forbids to delay the dialog creation
 
         set_accels_for_action ("app.toggle-new-game",   {        "<Primary>n"       });
         set_accels_for_action ("app.new-game",          { "<Shift><Primary>n"       });
@@ -178,18 +174,7 @@ public class Application : Gtk.Application
                 _header_bar.subtitle = _("Game Over");
 
                 if (!_game_restored)
-                {
-                    Scores.Category cat = (_settings.get_int ("rows") == 4) ? _grid4_cat : _grid5_cat;
-                    _scores_ctx.add_score.begin (_game.score, cat, null, (object, result) => {
-                            try {
-                                _scores_ctx.add_score.end (result);
-                            } catch (GLib.Error e) {
-                                stderr.printf ("%s\n", e.message);
-                            }
-                            ((SimpleAction) lookup_action ("scores")).set_enabled (true);
-                            debug ("score added");
-                        });
-                }
+                    _show_best_scores ();
 
                 debug ("finished");
             });
@@ -252,25 +237,6 @@ public class Application : Gtk.Application
         _game.view = embed.get_stage ();
     }
 
-    private Games.Scores.Category category_request (string key)
-    {
-        if (key == "grid4") return _grid4_cat;
-        if (key == "grid5") return _grid5_cat;
-        assert_not_reached ();
-    }
-
-    private void _create_scores ()
-    {
-        /* Translators: combobox entry in the dialog that appears when the user clicks the "Scores" entry in 
the hamburger menu, if the user has already finished at least one 4 × 4 and one 5 × 5 game */
-        _grid4_cat = new Scores.Category ("grid4", _("Grid 4 × 4"));
-
-        /* Translators: combobox entry in the dialog that appears when the user clicks the "Scores" entry in 
the hamburger menu, if the user has already finished at least one 4 × 4 and one 5 × 5 game */
-        _grid5_cat = new Scores.Category ("grid5", _("Grid 5 × 5"));
-
-        /* Translators: label introducing a combobox in the dialog that appears when the user clicks the 
"Scores" entry in the hamburger menu, if the user has already finished at least one 4 × 4 and one 5 × 5 game 
*/
-        _scores_ctx = new Scores.Context ("gnome-2048", _("Grid Size:"), _window, category_request, 
Scores.Style.POINTS_GREATER_IS_BETTER);
-    }
-
     /*\
     * * Hamburger-menu (and undo action) callbacks
     \*/
@@ -311,11 +277,6 @@ public class Application : Gtk.Application
         new_game_cb ();
     }
 
-    private void scores_cb (/* SimpleAction action, Variant? variant */)
-    {
-        _scores_ctx.run_dialog ();
-    }
-
     private void about_cb (/* SimpleAction action, Variant? variant */)
     {
         string [] authors = { "Juan R. García Blanco", "Arnaud Bonatti" };
@@ -454,4 +415,52 @@ public class Application : Gtk.Application
         }
         debug ("target value reached");
     }
+
+    /*\
+    * * scores dialog
+    \*/
+
+    private Scores.Context _scores_ctx;
+    private Scores.Category _grid4_cat;
+    private Scores.Category _grid5_cat;
+
+    private inline void _create_scores_dialog ()
+    {
+        /* Translators: combobox entry in the dialog that appears when the user clicks the "Scores" entry in 
the hamburger menu, if the user has already finished at least one 4 × 4 and one 5 × 5 game */
+        _grid4_cat = new Scores.Category ("grid4", _("Grid 4 × 4"));
+
+        /* Translators: combobox entry in the dialog that appears when the user clicks the "Scores" entry in 
the hamburger menu, if the user has already finished at least one 4 × 4 and one 5 × 5 game */
+        _grid5_cat = new Scores.Category ("grid5", _("Grid 5 × 5"));
+
+        /* Translators: label introducing a combobox in the dialog that appears when the user clicks the 
"Scores" entry in the hamburger menu, if the user has already finished at least one 4 × 4 and one 5 × 5 game 
*/
+        _scores_ctx = new Scores.Context ("gnome-2048", _("Grid Size:"), _window, category_request, 
Scores.Style.POINTS_GREATER_IS_BETTER);
+    }
+    private inline Games.Scores.Category category_request (string key)
+    {
+        switch (key)
+        {
+            case "grid4": return _grid4_cat;
+            case "grid5": return _grid5_cat;
+            default: assert_not_reached ();
+        }
+    }
+
+    private inline void scores_cb (/* SimpleAction action, Variant? variant */)
+    {
+        _scores_ctx.run_dialog ();  // TODO open it for current Scores.Category
+    }
+
+    private inline void _show_best_scores ()
+    {
+        Scores.Category cat = (_settings.get_int ("rows") == 4) ? _grid4_cat : _grid5_cat;
+        _scores_ctx.add_score.begin (_game.score, cat, null, (object, result) => {
+                try {
+                    _scores_ctx.add_score.end (result);
+                } catch (GLib.Error e) {
+                    stderr.printf ("%s\n", e.message);
+                }
+                _scores_ctx.run_dialog ();
+                debug ("score added");
+            });
+    }
 }


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