[five-or-more/arnaudb/kill-preferences-dialog: 1/10] Use window actions.



commit 3746f0e1e9337a7b3959e043e1b859d088d2d477
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu May 7 12:01:01 2020 +0200

    Use window actions.

 data/five-or-more.ui | 11 ++++---
 src/main.vala        | 51 +++++---------------------------
 src/window.vala      | 82 +++++++++++++++++++++++++++++++++++-----------------
 3 files changed, 67 insertions(+), 77 deletions(-)
---
diff --git a/data/five-or-more.ui b/data/five-or-more.ui
index f7258b1..f506240 100644
--- a/data/five-or-more.ui
+++ b/data/five-or-more.ui
@@ -5,29 +5,28 @@
     <section>
       <item>
         <attribute name="label" translatable="yes">_New Game</attribute>
-        <attribute name="action">app.new-game</attribute>
-        <attribute name="accel">&lt;Primary&gt;n</attribute>
+        <attribute name="action">win.new-game</attribute>
       </item>
 
       <item>
         <attribute name="label" translatable="yes">S_cores</attribute>
-        <attribute name="action">app.scores</attribute>
+        <attribute name="action">win.scores</attribute>
       </item>
     </section>
     <section>
       <item>
         <attribute name="label" translatable="yes">_Small</attribute>
-        <attribute name="action">app.change-size</attribute>
+        <attribute name="action">win.change-size</attribute>
         <attribute name="target">BOARD_SIZE_SMALL</attribute>
       </item>
       <item>
         <attribute name="label" translatable="yes">_Medium</attribute>
-        <attribute name="action">app.change-size</attribute>
+        <attribute name="action">win.change-size</attribute>
         <attribute name="target">BOARD_SIZE_MEDIUM</attribute>
       </item>
       <item>
         <attribute name="label" translatable="yes">_Large</attribute>
-        <attribute name="action">app.change-size</attribute>
+        <attribute name="action">win.change-size</attribute>
         <attribute name="target">BOARD_SIZE_LARGE</attribute>
       </item>
     </section>
diff --git a/src/main.vala b/src/main.vala
index 7436a68..7be3c3e 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -34,13 +34,10 @@ private class FiveOrMoreApp: Gtk.Application
 
     private const GLib.ActionEntry action_entries[] =
     {
-        {"new-game", new_game_cb        },
-        {"change-size", null, "s", "'SMALL'", change_size_cb },
-        {"scores", scores_cb            },
-        {"preferences", preferences_cb  },
-        {"help", help_cb                },
-        {"about", about_cb              },
-        {"quit", quit                   }
+        { "preferences",    preferences_cb  },
+        { "help",           help_cb         },
+        { "about",          about_cb        },
+        { "quit",           quit            }
     };
 
     private static int main (string[] args)
@@ -75,43 +72,9 @@ private class FiveOrMoreApp: Gtk.Application
         window = new GameWindow (this, settings);
 
         add_action_entries (action_entries, this);
-        set_accels_for_action ("app.new-game", {"<Primary>n"});
-        set_accels_for_action ("app.quit", {"<Primary>q"});
-        set_accels_for_action ("app.help", {"F1"});
-        var board_size_action = lookup_action("change-size");
-        BoardSize size = (BoardSize)settings.get_int (FiveOrMoreApp.KEY_SIZE);
-        ((SimpleAction)board_size_action).set_state (new Variant.string(size.to_string()));
-    }
-
-    private void new_game_cb ()
-    {
-        if (window == null)
-        {
-            warning ("Failed to restart game");
-            return;
-        }
-        window.restart_game ();
-    }
-
-    private void scores_cb ()
-    {
-        window.show_scores ();
-    }
-
-    private void change_size_cb (SimpleAction action, Variant? parameter)
-    {
-        action.set_state (parameter);
-        switch (parameter.get_string()) {
-            case "BOARD_SIZE_SMALL":
-                window.change_size (BoardSize.SMALL);
-                break;
-            case "BOARD_SIZE_MEDIUM":
-                window.change_size (BoardSize.MEDIUM);
-                break;
-            case "BOARD_SIZE_LARGE":
-                window.change_size (BoardSize.LARGE);
-                break;
-        }
+        set_accels_for_action ("win.new-game",  { "<Primary>n"  });
+        set_accels_for_action ("app.quit",      { "<Primary>q"  });
+        set_accels_for_action ("app.help",      {          "F1" });
     }
 
     private void preferences_cb ()
diff --git a/src/window.vala b/src/window.vala
index 1550428..53bd882 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -55,11 +55,28 @@ private class GameWindow : ApplicationWindow
             _("Score: %d")
     };
 
+    private const GLib.ActionEntry win_actions [] =
+    {
+        {"change-size", null,       "s", "'SMALL'", change_size },
+        {"new-game",    new_game    },
+        {"scores",      show_scores }
+    };
+
+    construct
+    {
+        add_action_entries (win_actions, this);
+    }
+
     internal GameWindow (Gtk.Application app, GLib.Settings settings)
     {
         Object (application: app);
 
         this.settings = settings;
+
+        var board_size_action = lookup_action ("change-size");
+        BoardSize board_size = (BoardSize) settings.get_int (FiveOrMoreApp.KEY_SIZE);
+        ((SimpleAction) board_size_action).set_state (new Variant.string (board_size.to_string ()));
+
         game = new Game (settings);
         theme = new ThemeRenderer (settings);
 
@@ -86,11 +103,11 @@ private class GameWindow : ApplicationWindow
 
         var importer = new Games.Scores.DirectoryImporter ();
         highscores = new Games.Scores.Context.with_importer ("five-or-more",
-                                                _("Board Size: "),
-                                                this,
-                                                create_category_from_key,
-                                                Games.Scores.Style.POINTS_GREATER_IS_BETTER,
-                                                importer);
+                                                             _("Board Size: "),
+                                                             this,
+                                                             create_category_from_key,
+                                                             Games.Scores.Style.POINTS_GREATER_IS_BETTER,
+                                                             importer);
         game.game_over.connect (score_cb);
     }
 
@@ -128,26 +145,43 @@ private class GameWindow : ApplicationWindow
         show_scores ();
     }
 
-    internal void restart_game ()
+    private void set_status_message (string? message)
     {
-        game.restart ();
+        headerbar.set_subtitle (message);
     }
 
-    private void set_status_message (string? message)
+    private Games.Scores.Category? create_category_from_key (string key)
     {
-        headerbar.set_subtitle (message);
+        string? name = category_name_from_key (key);
+        return new Games.Scores.Category (key, name);
     }
 
-    internal void show_scores ()
+    private string category_name_from_key (string key)
     {
-        highscores.run_dialog ();
+        for (int i = 0; i < game.n_categories; i++) {
+            if (Game.scorecats[i].key == key)
+                return Game.scorecats[i].name;
+        }
+        return "";
     }
 
-    internal void change_size (BoardSize size)
+    /*\
+    * * actions
+    \*/
+
+    private inline void change_size (SimpleAction action, Variant? parameter)
     {
-        var game_size = settings.get_int ("size");
+        BoardSize new_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;
+            default: assert_not_reached ();
+        }
 
-        if (game_size == size)
+        var old_size = settings.get_int ("size");
+        if (old_size == new_size)
             return;
 
         primary_menu_button.set_active (false);
@@ -167,30 +201,24 @@ private class GameWindow : ApplicationWindow
             switch (result)
             {
                 case ResponseType.OK:
-                     if (!settings.set_int (FiveOrMoreApp.KEY_SIZE, size))
-                        warning ("Failed to set size: %d", size);
+                     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, size);
+            settings.set_int (FiveOrMoreApp.KEY_SIZE, (int) new_size);
         }
-
     }
 
-    private Games.Scores.Category? create_category_from_key (string key)
+    private inline void new_game (/* SimpleAction action, Variant? parameter */)
     {
-        string? name = category_name_from_key (key);
-        return new Games.Scores.Category (key, name);
+        game.restart ();
     }
 
-    private string category_name_from_key (string key)
+    private inline void show_scores (/* SimpleAction action, Variant? parameter */)
     {
-        for (int i = 0; i < game.n_categories; i++) {
-            if (Game.scorecats[i].key == key)
-                return Game.scorecats[i].name;
-        }
-        return "";
+        highscores.run_dialog ();
     }
 }


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