[five-or-more] Moved board selection to view menu (#3)



commit 1b584dfd21dcea4849c921f710cd4c93b8faa342
Author: Robert Roth <robert roth off gmail com>
Date:   Thu Oct 18 00:20:23 2018 +0300

    Moved board selection to view menu (#3)

 data/five-or-more-preferences.ui | 88 +---------------------------------------
 data/five-or-more.ui             | 19 ++++++++-
 src/game.vala                    |  2 +-
 src/main.vala                    | 20 +++++++++
 src/preferences-dialog.vala      | 76 ----------------------------------
 src/view.vala                    |  3 ++
 src/window.vala                  | 41 +++++++++++++++++++
 7 files changed, 84 insertions(+), 165 deletions(-)
---
diff --git a/data/five-or-more-preferences.ui b/data/five-or-more-preferences.ui
index e682420..218d8cc 100644
--- a/data/five-or-more-preferences.ui
+++ b/data/five-or-more-preferences.ui
@@ -120,93 +120,6 @@
                 <property name="position">1</property>
               </packing>
             </child>
-            <child>
-              <object class="GtkLabel" id="label3">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="margin_top">12</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">Board Size</property>
-                <attributes>
-                  <attribute name="weight" value="bold"/>
-                </attributes>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">True</property>
-                <property name="position">2</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkBox" id="box2">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="orientation">vertical</property>
-                <property name="spacing">6</property>
-                <child>
-                  <object class="GtkRadioButton" id="radiobutton_small">
-                    <property name="label" translatable="yes">_Small</property>
-                    <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="receives_default">False</property>
-                    <property name="margin_start">12</property>
-                    <property name="use_action_appearance">False</property>
-                    <property name="use_underline">True</property>
-                    <property name="halign">start</property>
-                    <property name="active">True</property>
-                    <property name="draw_indicator">True</property>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">0</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkRadioButton" id="radiobutton_medium">
-                    <property name="label" translatable="yes">_Medium</property>
-                    <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="receives_default">False</property>
-                    <property name="margin_start">12</property>
-                    <property name="use_action_appearance">False</property>
-                    <property name="use_underline">True</property>
-                    <property name="halign">start</property>
-                    <property name="draw_indicator">True</property>
-                    <property name="group">radiobutton_small</property>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">1</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkRadioButton" id="radiobutton_large">
-                    <property name="label" translatable="yes">_Large</property>
-                    <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="receives_default">False</property>
-                    <property name="margin_start">12</property>
-                    <property name="use_action_appearance">False</property>
-                    <property name="use_underline">True</property>
-                    <property name="halign">start</property>
-                    <property name="draw_indicator">True</property>
-                    <property name="group">radiobutton_small</property>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">2</property>
-                  </packing>
-                </child>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">True</property>
-                <property name="position">3</property>
-              </packing>
-            </child>
           </object>
           <packing>
             <property name="expand">True</property>
@@ -218,3 +131,4 @@
     </child>
   </template>
 </interface>
+
diff --git a/data/five-or-more.ui b/data/five-or-more.ui
index 5f68fd5..b8921da 100644
--- a/data/five-or-more.ui
+++ b/data/five-or-more.ui
@@ -2,6 +2,23 @@
 <interface>
   <!-- interface-requires gtk+ 3.10 -->
   <menu id="primary-menu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Small</attribute>
+        <attribute name="action">app.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="target">BOARD_SIZE_MEDIUM</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Large</attribute>
+        <attribute name="action">app.change-size</attribute>
+        <attribute name="target">BOARD_SIZE_LARGE</attribute>
+      </item>
+    </section>
     <section>
       <item>
         <attribute name="label" translatable="yes">Scores</attribute>
@@ -63,7 +80,7 @@
           </object>
         </child>
         <child>
-          <object class="GtkMenuButton" id="primary-menu-button">
+          <object class="GtkMenuButton" id="primary_menu_button">
             <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 0b257a3..2d04e7c 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -277,7 +277,7 @@ public class Game : Object
     }
 }
 
-enum BoardSize
+public enum BoardSize
 {
     UNSET,
     SMALL,
diff --git a/src/main.vala b/src/main.vala
index b93b429..ae914eb 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -35,6 +35,7 @@ public 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                },
@@ -61,6 +62,9 @@ public class FiveOrMoreApp: Gtk.Application
 
         add_action_entries (action_entries, this);
         set_accels_for_action ("app.new-game", {"<Primary>n"});
+        var board_size_action = lookup_action("change-size");
+        BoardSize size = (BoardSize)settings.get_int (FiveOrMoreApp.KEY_SIZE);
+        (board_size_action as SimpleAction).set_state (new Variant.string(size.to_string()));
     }
 
     public static int main (string[] args)
@@ -87,6 +91,22 @@ public class FiveOrMoreApp: Gtk.Application
         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;
+        }
+    }
+
     private void preferences_cb ()
     {
         if (preferences_dialog != null)
diff --git a/src/preferences-dialog.vala b/src/preferences-dialog.vala
index 1228a46..c8f2ed7 100644
--- a/src/preferences-dialog.vala
+++ b/src/preferences-dialog.vala
@@ -32,13 +32,6 @@ public class PreferencesDialog : Gtk.Dialog
     [GtkChild]
     private Gtk.ColorButton color_button;
 
-    [GtkChild]
-    private Gtk.RadioButton radiobutton_small;
-    [GtkChild]
-    private Gtk.RadioButton radiobutton_medium;
-    [GtkChild]
-    private Gtk.RadioButton radiobutton_large;
-
     private void theme_set_cb (Gtk.ComboBox self)
     {
         var combo_box_text = self as Gtk.ComboBoxText;
@@ -62,55 +55,6 @@ public class PreferencesDialog : Gtk.Dialog
             warning ("Failed to set color: %s", color.to_string ());
     }
 
-    private void size_cb (Gtk.ToggleButton button, BoardSize size)
-    {
-        var game_size = settings.get_int ("size");
-
-        if (game_size == size || !button.get_active ())
-            return;
-
-        var flags = Gtk.DialogFlags.DESTROY_WITH_PARENT;
-        var restart_game_dialog = new Gtk.MessageDialog (this,
-                                                         flags,
-                                                         Gtk.MessageType.WARNING,
-                                                         Gtk.ButtonsType.NONE,
-                                                         _("Are you sure you want to restart the game?"),
-                                                         null);
-        restart_game_dialog.add_buttons (_("_Cancel"), Gtk.ResponseType.CANCEL,
-                                         _("_Restart"), Gtk.ResponseType.OK,
-                                         null);
-
-        var result = restart_game_dialog.run ();
-        restart_game_dialog.destroy ();
-        switch (result)
-        {
-            case Gtk.ResponseType.OK:
-                 if (!settings.set_int (FiveOrMoreApp.KEY_SIZE, size))
-                    warning ("Failed to set size: %d", size);
-                button.set_active (true);
-                break;
-            case Gtk.ResponseType.CANCEL:
-                Gtk.ToggleButton radiobutton;
-                switch (game_size)
-                {
-                    case BoardSize.SMALL:
-                        radiobutton = radiobutton_small;
-                        break;
-                    case BoardSize.MEDIUM:
-                        radiobutton = radiobutton_medium;
-                        break;
-                    case BoardSize.LARGE:
-                        radiobutton = radiobutton_large;
-                        break;
-                    default:
-                        radiobutton = radiobutton_small;
-                        break;
-                }
-                radiobutton.set_active (true);
-                break;
-        }
-    }
-
     public PreferencesDialog (Settings settings)
     {
         this.settings = settings;
@@ -131,25 +75,5 @@ public class PreferencesDialog : Gtk.Dialog
         color_button.set_rgba (color);
         color_button.color_set.connect (color_set_cb);
 
-        /* Set up size radio buttons */
-        var size = settings.get_int (FiveOrMoreApp.KEY_SIZE);
-        switch (size) {
-            case BoardSize.SMALL:
-                radiobutton_small.set_active (true);
-                break;
-            case BoardSize.MEDIUM:
-                radiobutton_medium.set_active (true);
-                break;
-            case BoardSize.LARGE:
-                radiobutton_large.set_active (true);
-                break;
-            default:
-                radiobutton_medium.set_active (true);
-                break;
-        }
-
-        radiobutton_small.toggled.connect ((button) => { size_cb (button, SMALL); });
-        radiobutton_medium.toggled.connect ((button) => { size_cb (button, MEDIUM); });
-        radiobutton_large.toggled.connect ((button) => { size_cb (button, LARGE); });
     }
 }
diff --git a/src/view.vala b/src/view.vala
index aedc0d9..eab2493 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -72,6 +72,9 @@ public class View : Gtk.DrawingArea
         board_rectangle.x = board_rectangle.y = 0;
         update_sizes (MINIMUM_BOARD_SIZE, MINIMUM_BOARD_SIZE);
         game.board.board_changed.connect (() => {
+            show_cursor = false;
+            keyboard_cursor_x = -1;
+            keyboard_cursor_y = -1;
             update_sizes (get_allocated_width (), get_allocated_height ());
             queue_draw ();
         });
diff --git a/src/window.vala b/src/window.vala
index 7ce1f10..83dfe7c 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -33,6 +33,9 @@ public class GameWindow : Gtk.ApplicationWindow
     [GtkChild]
     private Gtk.Box hbox;
 
+    [GtkChild]
+    private Gtk.MenuButton primary_menu_button;
+
     [GtkChild]
     private Games.GridFrame grid_frame;
 
@@ -142,6 +145,44 @@ public class GameWindow : Gtk.ApplicationWindow
         highscores.run_dialog ();
     }
 
+    public void change_size (BoardSize size)
+    {
+        var game_size = settings.get_int ("size");
+
+        if (game_size == size)
+            return;
+
+        primary_menu_button.set_active (false);
+
+        if (game.score > 0) {
+            var flags = Gtk.DialogFlags.DESTROY_WITH_PARENT;
+            var restart_game_dialog = new Gtk.MessageDialog (this,
+                                                             flags,
+                                                             Gtk.MessageType.WARNING,
+                                                             Gtk.ButtonsType.NONE,
+                                                             _("Are you sure you want to restart the game?"),
+                                                             null);
+            restart_game_dialog.add_buttons (_("_Cancel"), Gtk.ResponseType.CANCEL,
+                                             _("_Restart"), Gtk.ResponseType.OK,
+                                             null);
+
+            var result = restart_game_dialog.run ();
+            restart_game_dialog.destroy ();
+            switch (result)
+            {
+                case Gtk.ResponseType.OK:
+                     if (!settings.set_int (FiveOrMoreApp.KEY_SIZE, size))
+                        warning ("Failed to set size: %d", size);
+                    break;
+                case Gtk.ResponseType.CANCEL:
+                    break;
+            }
+        } else {
+            settings.set_int (FiveOrMoreApp.KEY_SIZE, size);
+        }
+
+    }
+
     private Games.Scores.Category? create_category_from_key (string key)
     {
         string? name = category_name_from_key (key);


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