[swell-foop/arnaudb/kill-preferences-dialog] Kill Preferences dialog.



commit e0f7689608b5fef0607da3dafd3879d46153d4f2
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat May 9 18:14:29 2020 +0200

    Kill Preferences dialog.

 data/org.gnome.swell-foop.gschema.xml |   1 +
 data/ui/preferences.ui                | 141 ----------------------------------
 data/ui/swell-foop.ui                 |  56 ++++++++++++--
 po/POTFILES.in                        |   1 -
 src/org.gnome.SwellFoop.gresource.xml |   1 -
 src/swell-foop.vala                   |  97 -----------------------
 src/window.vala                       |  61 +++++++++++++--
 7 files changed, 105 insertions(+), 253 deletions(-)
---
diff --git a/data/org.gnome.swell-foop.gschema.xml b/data/org.gnome.swell-foop.gschema.xml
index da10576..3c1d735 100644
--- a/data/org.gnome.swell-foop.gschema.xml
+++ b/data/org.gnome.swell-foop.gschema.xml
@@ -17,6 +17,7 @@
     </key>
     <key name="colors" type="i">
       <default>3</default>
+      <range min="2" max="4"/>
       <summary>Board color count</summary>
       <description>The number of colors of tiles to use in the game.</description>
     </key>
diff --git a/data/ui/swell-foop.ui b/data/ui/swell-foop.ui
index 06b71fd..e8c1e81 100644
--- a/data/ui/swell-foop.ui
+++ b/data/ui/swell-foop.ui
@@ -25,6 +25,57 @@
         <attribute name="label" translatable="yes">_New Game</attribute>
         <attribute name="action">win.new-game</attribute>
       </item>
+      <submenu>
+        <!-- Translators: submenu of the hamburger menu (with a mnemonic that appears when pressing Alt); 
allows to configure board size and number of colors -->
+        <attribute name="label" translatable="yes">_Setup</attribute>
+        <section>
+          <item>
+            <!-- Translators: entry of the Setup submenu of the hamburger menu; set size to "small"; other 
possible sizes are "normal" and "large" -->
+            <attribute name="label" translatable="yes">_Small (6 × 5)</attribute>
+            <attribute name="action">win.size</attribute>
+            <attribute name="target">small</attribute>
+          </item>
+          <item>
+            <!-- Translators: entry of the Setup submenu of the hamburger menu; set size to "normal"; other 
possible sizes are "small" and "large" -->
+            <attribute name="label" translatable="yes">Nor_mal (15 × 10)</attribute>
+            <attribute name="action">win.size</attribute>
+            <attribute name="target">normal</attribute>
+          </item>
+          <item>
+            <!-- Translators: entry of the Setup submenu of the hamburger menu; set size to "large"; other 
possible sizes are "small" and "normal" -->
+            <attribute name="label" translatable="yes">_Large (20 × 15)</attribute>
+            <attribute name="action">win.size</attribute>
+            <attribute name="target">large</attribute>
+          </item>
+        </section>
+        <section>
+          <item>
+            <!-- Translators: entry of the Setup submenu of the hamburger menu; set size to "small"; other 
possible sizes are "normal" and "large" -->
+            <attribute name="label" translatable="yes">_Two colors</attribute>
+            <attribute name="action">win.change-colors</attribute>
+            <attribute name="target">2</attribute>
+          </item>
+          <item>
+            <!-- Translators: entry of the Setup submenu of the hamburger menu; set size to "normal"; other 
possible sizes are "small" and "large" -->
+            <attribute name="label" translatable="yes">T_hree colors</attribute>
+            <attribute name="action">win.change-colors</attribute>
+            <attribute name="target">3</attribute>
+          </item>
+          <item>
+            <!-- Translators: entry of the Setup submenu of the hamburger menu; set size to "large"; other 
possible sizes are "small" and "normal" -->
+            <attribute name="label" translatable="yes">_Four colors</attribute>
+            <attribute name="action">win.change-colors</attribute>
+            <attribute name="target">4</attribute>
+          </item>
+        </section>
+        <section>
+          <item>
+            <!-- Translators: entry of the Setup 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>
       <item>
@@ -59,11 +110,6 @@
       </submenu>
     </section>
     <section>
-      <item>
-        <!-- Translators: entry of the hamburger menu (with a mnemonic that appears when pressing Alt); 
starts a new game -->
-        <attribute name="label" translatable="yes">_Preferences</attribute>
-        <attribute name="action">app.preferences</attribute>
-      </item>
       <item>
         <!-- Translators: entry of the hamburger menu (with a mnemonic that appears when pressing Alt); 
starts a new game -->
         <attribute name="label" translatable="yes">_Keyboard Shortcuts</attribute>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 775837b..bf2fff1 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -5,7 +5,6 @@ data/org.gnome.SwellFoop.desktop.in
 data/org.gnome.swell-foop.gschema.xml
 data/ui/first-run-stack.ui
 data/ui/help-overlay.ui
-data/ui/preferences.ui
 data/ui/swell-foop.ui
 src/game.vala
 src/game-view.vala
diff --git a/src/org.gnome.SwellFoop.gresource.xml b/src/org.gnome.SwellFoop.gresource.xml
index d9ce6a1..32dda5e 100644
--- a/src/org.gnome.SwellFoop.gresource.xml
+++ b/src/org.gnome.SwellFoop.gresource.xml
@@ -2,7 +2,6 @@
 <gresources>
   <gresource prefix="/org/gnome/SwellFoop/ui">
     <file preprocess="xml-stripblanks" alias="first-run-stack.ui">../data/ui/first-run-stack.ui</file>
-    <file preprocess="xml-stripblanks" alias="preferences.ui">../data/ui/preferences.ui</file>
     <file alias="swell-foop.css">../data/swell-foop.css</file>
     <file preprocess="xml-stripblanks" alias="swell-foop.ui">../data/ui/swell-foop.ui</file>
   </gresource>
diff --git a/src/swell-foop.vala b/src/swell-foop.vala
index 945f4d2..772e7d5 100644
--- a/src/swell-foop.vala
+++ b/src/swell-foop.vala
@@ -18,27 +18,8 @@ public class SwellFoop : Gtk.Application
     /* Main window */
     private SwellFoopWindow window;
 
-    private Gtk.Dialog? preferences_dialog = null;
-
-    /* Store size options */
-    internal static Size [] sizes;
-    class construct
-    {
-        sizes = {
-            /* Translators: name of a possible size of the grid, as seen in the Preferences dialog “board 
size” combobox */
-            { "small",  _("Small"),   6,  5 },
-
-            /* Translators: name of a possible size of the grid, as seen in the Preferences dialog “board 
size” combobox */
-            { "normal", _("Normal"), 15, 10 },
-
-            /* Translators: name of a possible size of the grid, as seen in the Preferences dialog “board 
size” combobox */
-            { "large",  _("Large"),  20, 15 }
-        };
-    }
-
     private const GLib.ActionEntry[] action_entries =
     {
-        { "preferences",   preferences_cb },
         { "help",          help_cb        },
         { "about",         about_cb       },
         { "quit",          quit_cb        }
@@ -80,75 +61,6 @@ public class SwellFoop : Gtk.Application
         window.present ();
     }
 
-    public inline void preferences_cb (/* SimpleAction action, Variant? variant */)
-    {
-        /* Show existing dialog */
-        if (preferences_dialog != null)
-        {
-            preferences_dialog.present ();
-            return;
-        }
-
-        var preferences_builder = new Gtk.Builder.from_resource ("/org/gnome/SwellFoop/ui/preferences.ui");
-
-        preferences_dialog = (Gtk.Dialog) preferences_builder.get_object ("preferences");
-        preferences_dialog.transient_for = window;
-        preferences_dialog.modal = true;
-
-        /* Board size */
-        var size_combo = (Gtk.ComboBox) preferences_builder.get_object ("size-selector");
-        var model = (Gtk.ListStore) size_combo.model;
-        Gtk.TreeIter iter;
-        for (int i = 0; i < sizes.length; i++)
-        {
-            model.append (out iter);
-            model.set (iter, 0, sizes[i].name, 1, sizes[i].id, -1);
-            if (settings.get_string ("size") == sizes[i].id)
-                size_combo.set_active_iter (iter);
-        }
-
-        /* Number of colors */
-        ((Gtk.SpinButton) preferences_builder.get_object ("colors-spinner")).value = settings.get_int 
("colors");
-
-        preferences_builder.connect_signals (this);
-        preferences_dialog.response.connect (preferences_response_cb);
-        preferences_dialog.present ();
-    }
-
-    [CCode (cname = "G_MODULE_EXPORT update_size", instance_pos = -1)]
-    public void update_size (Gtk.ComboBox size_combo)
-    {
-        Gtk.TreeIter iter;
-        if (!size_combo.get_active_iter (out iter))
-            return;
-        string new_size;
-        size_combo.model.get (iter, 1, out new_size, -1);
-
-        if (new_size == settings.get_string ("size"))
-            return;
-
-        settings.set_string ("size", new_size);
-        window.new_game ();
-    }
-
-    [CCode (cname = "G_MODULE_EXPORT update_colors", instance_pos = -1)]
-    public void update_colors (Gtk.SpinButton button)
-    {
-        int new_colors = (int) button.get_value ();
-
-        if (new_colors == settings.get_int ("colors"))
-            return;
-
-        settings.set_int ("colors", new_colors);
-        window.new_game ();
-    }
-
-    private inline void preferences_response_cb ()
-    {
-        preferences_dialog.destroy ();
-        preferences_dialog = null;
-    }
-
     private inline void quit_cb (/* SimpleAction action, Variant? variant */)
     {
         window.destroy ();
@@ -238,12 +150,3 @@ public class SwellFoop : Gtk.Application
         return app.run (args);
     }
 }
-
-/* An array will store multiply game size options. */
-private struct Size
-{
-    public string id;
-    public string name;
-    public int    columns;
-    public int    rows;
-}
diff --git a/src/window.vala b/src/window.vala
index 74de345..4fa6997 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -39,9 +39,39 @@ private class SwellFoopWindow : ApplicationWindow
 
     private bool game_in_progress = false;
 
+    /* Store size options */
+    private struct Size
+    {
+        public string id;
+        public string name;
+        public int    columns;
+        public int    rows;
+    }
+    private static Size [] sizes;
+    private static inline void class_init_sizes ()     // called on class construct
+    {
+        sizes = {
+            /* Translators: name of a possible size of the grid */
+            { "small",  _("Small"),   6,  5 },
+
+            /* Translators: name of a possible size of the grid */
+            { "normal", _("Normal"), 15, 10 },
+
+            /* Translators: name of a possible size of the grid */
+            { "large",  _("Large"),  20, 15 }
+        };
+    }
+
+    class construct
+    {
+        class_init_sizes ();
+        class_init_scores ();
+    }
+
     private const GLib.ActionEntry[] win_actions =
     {
-        { "change-theme",       null,       "s", "'shapesandcolors'",   change_theme_cb },  // cannot be 
done via create_action as long as it’s an open form
+        { "change-theme",       null,       "s", "'shapesandcolors'",   change_theme_cb     },  // cannot be 
done via create_action as long as it’s an open form
+        { "change-colors",      null,       "s", "'3'",                 change_colors_cb    },  // cannot be 
done via create_action because it’s an int
         { "new-game",           new_game_cb         },
         { "scores",             scores_cb           },
         { "toggle-hamburger",   toggle_hamburger    }
@@ -50,6 +80,7 @@ private class SwellFoopWindow : ApplicationWindow
     construct
     {
         add_action_entries (win_actions, this);
+        add_action (settings.create_action ("size"));
 
         add_action (settings.create_action ("zealous"));
         settings.changed ["zealous"].connect ((_settings, _key_name) => { view.is_zealous = 
_settings.get_boolean (_key_name); });
@@ -60,6 +91,10 @@ private class SwellFoopWindow : ApplicationWindow
         SimpleAction theme_action = (SimpleAction) lookup_action ("change-theme");
         theme_action.set_state (new Variant.@string (theme));
 
+        int32 colors = settings.get_int ("colors"); // 2 <= colors <= 4, per schema file
+        SimpleAction colors_action = (SimpleAction) lookup_action ("change-colors");
+        colors_action.set_state (new Variant.@string (colors.to_string ()));
+
         add_events (Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.KEY_RELEASE_MASK);
 
         init_scores ();
@@ -171,13 +206,14 @@ private class SwellFoopWindow : ApplicationWindow
 
     private Size get_board_size ()
     {
-        for (var i = 0; i < SwellFoop.sizes.length; i++)
+        string current_size = settings.get_string ("size");
+        for (var i = 0; i < sizes.length; i++)
         {
-            if (SwellFoop.sizes [i].id == settings.get_string ("size"))
-                return SwellFoop.sizes [i];
+            if (sizes [i].id == current_size)
+                return sizes [i];
         }
 
-        return SwellFoop.sizes [0];
+        return sizes [0];
     }
 
     /*\
@@ -228,6 +264,15 @@ private class SwellFoopWindow : ApplicationWindow
             settings.set_string ("theme", new_theme);
     }
 
+    private inline void change_colors_cb (SimpleAction action, Variant? variant)
+        requires (variant != null)
+    {
+        int32 new_colors = (int32) int.parse (((!) variant).get_string ());
+        action.set_state ((!) variant);
+        if (settings.get_int ("colors") != new_colors)
+            settings.set_int ("colors", new_colors);
+    }
+
     private inline void scores_cb (/* SimpleAction action, Variant? variant */)
     {
         scores_context.run_dialog ();
@@ -321,11 +366,11 @@ private class SwellFoopWindow : ApplicationWindow
     private Games.Scores.Context scores_context;
     private static HashTable<string, Games.Scores.Category> score_categories;
 
-    class construct
+    private static inline void class_init_scores ()    // called on class construct
     {
         score_categories = new HashTable<string, Games.Scores.Category> (str_hash, str_equal);
         for (uint8 i = 2; i <= 4; i++)
-            foreach (unowned Size size in SwellFoop.sizes)
+            foreach (unowned Size size in sizes)
             {
                 string id = @"$(size.id)-$i";
                 string name = ngettext ("%s, %d color", "%s, %d colors", i).printf (size.name, i);
@@ -385,7 +430,7 @@ private class SwellFoopWindow : ApplicationWindow
         rows = (uint8) number_64;
 
         string id = "";
-        foreach (unowned Size size in SwellFoop.sizes)
+        foreach (unowned Size size in sizes)
         {
             if (size.rows == rows && size.columns == cols)
             {


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