[five-or-more/gsoc-vala-port: 9/29] Port preferences dialog to vala



commit 4785d790ca8aae497985d4a7e00518c0b593cf96
Author: Ruxandra Simion <ruxandra simion93 gmail com>
Date:   Mon Jun 11 23:26:28 2018 +0300

    Port preferences dialog to vala

 data/five-or-more-vala-preferences.ui     | 192 ++++++++++++++++++++++++++++++
 data/org.gnome.five-or-more.gresource.xml |   3 +-
 src-vala/game.vala                        |  34 ++++++
 src-vala/main.vala                        |  91 +++++++++-----
 src-vala/meson.build                      |   4 +-
 src-vala/preferences-dialog.vala          |  61 ++++++++++
 6 files changed, 355 insertions(+), 30 deletions(-)
---
diff --git a/data/five-or-more-vala-preferences.ui b/data/five-or-more-vala-preferences.ui
new file mode 100644
index 0000000..5637215
--- /dev/null
+++ b/data/five-or-more-vala-preferences.ui
@@ -0,0 +1,192 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.12 -->
+  <template class="PreferencesDialog" parent="GtkDialog">
+    <property name="can_focus">False</property>
+    <property name="resizable">False</property>
+    <property name="destroy_with_parent">True</property>
+    <property name="type_hint">dialog</property>
+    <property name="use-header-bar">1</property>
+    <child internal-child="headerbar">
+      <object class="GtkHeaderBar">
+        <property name="title" translatable="yes">Preferences</property>
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="show-close-button">True</property>
+      </object>
+    </child>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox1">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child>
+          <object class="GtkBox" id="content_area">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="border_width">5</property>
+            <property name="orientation">vertical</property>
+            <property name="spacing">6</property>
+            <child>
+              <object class="GtkLabel" id="appearance">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="halign">start</property>
+                <property name="label" translatable="yes">Appearance</property>
+                <attributes>
+                  <attribute name="weight" value="bold"/>
+                </attributes>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkGrid" id="grid1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="margin_start">12</property>
+                <property name="hexpand">True</property>
+                <property name="row_spacing">6</property>
+                <property name="column_spacing">12</property>
+                <child>
+                  <object class="GtkLabel" id="label2">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="halign">start</property>
+                    <property name="label" translatable="yes">B_ackground color:</property>
+                    <property name="use_underline">True</property>
+                    <property name="mnemonic_widget">color_button</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">1</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkColorButton" id="color_button">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <property name="hexpand">True</property>
+                    <property name="use_action_appearance">False</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">1</property>
+                    <property name="width">1</property>
+                    <property name="height">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <placeholder/>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <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>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/data/org.gnome.five-or-more.gresource.xml b/data/org.gnome.five-or-more.gresource.xml
index 1ace8b8..c271d91 100644
--- a/data/org.gnome.five-or-more.gresource.xml
+++ b/data/org.gnome.five-or-more.gresource.xml
@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <gresources>
   <gresource prefix="/org/gnome/five-or-more/ui">
-    <file>five-or-more-vala.ui</file>
+    <file preprocess="xml-stripblanks">five-or-more-vala.ui</file>
+    <file preprocess="xml-stripblanks" alias="preferences-dialog.ui">five-or-more-vala-preferences.ui</file>
   </gresource>
   <gresource prefix="/org/gnome/five-or-more/gtk">
     <file preprocess="xml-stripblanks" alias="menus.ui">five-or-more-menu.ui</file>
diff --git a/src-vala/game.vala b/src-vala/game.vala
new file mode 100644
index 0000000..1634878
--- /dev/null
+++ b/src-vala/game.vala
@@ -0,0 +1,34 @@
+public class Game
+{
+    private Settings settings;
+
+    private Gdk.RGBA background_color;
+    private int size;
+
+    public Game (Settings settings)
+    {
+        this.settings = settings;
+
+        background_color = Gdk.RGBA ();
+        var color_str = settings.get_string (FiveOrMoreApp.KEY_BACKGROUND_COLOR);
+        background_color.parse (color_str);
+        settings.changed[FiveOrMoreApp.KEY_BACKGROUND_COLOR].connect (() => {
+            color_str = settings.get_string (FiveOrMoreApp.KEY_BACKGROUND_COLOR);
+            background_color.parse (color_str);
+        });
+
+        size = settings.get_int (FiveOrMoreApp.KEY_SIZE);
+        settings.changed[FiveOrMoreApp.KEY_SIZE].connect (() => {
+            size = settings.get_int (FiveOrMoreApp.KEY_SIZE);
+        });
+    }
+}
+
+enum BoardSize
+{
+    UNSET = 0,
+    SMALL = 1,
+    MEDIUM = 2,
+    LARGE = 3,
+    MAX_SIZE = 4,
+}
diff --git a/src-vala/main.vala b/src-vala/main.vala
index 53c9c2d..1bd3116 100644
--- a/src-vala/main.vala
+++ b/src-vala/main.vala
@@ -1,5 +1,14 @@
-public class FiveOrMoreApp: Gtk.Application {
-    private Gtk.Window window;
+public class FiveOrMoreApp: Gtk.Application
+{
+    public const string KEY_SIZE = "size";
+    public const string KEY_BACKGROUND_COLOR = "background-color";
+
+    private Settings settings;
+
+    private Gtk.ApplicationWindow window;
+    private PreferencesDialog? preferences_dialog = null;
+
+    private Game? game = null;
 
     private const GLib.ActionEntry action_entries[] =
     {
@@ -11,12 +20,14 @@ public class FiveOrMoreApp: Gtk.Application {
         {"quit", quit                   }
     };
 
-    public FiveOrMoreApp () {
+    public FiveOrMoreApp ()
+    {
         Object (application_id: "org.gnome.five-or-more", flags: ApplicationFlags.FLAGS_NONE);
     }
 
-    public override void activate () {
-        window = new FiveOrMore.Window(this);
+    public override void activate ()
+    {
+        window = new FiveOrMore.Window (this);
         window.present ();
     }
 
@@ -24,10 +35,15 @@ public class FiveOrMoreApp: Gtk.Application {
     {
         base.startup ();
 
+        settings = new Settings ("org.gnome.five-or-more");
+
+        game = new Game (settings);
+
         add_action_entries (action_entries, this);
     }
 
-    public static int main (string[] args) {
+    public static int main (string[] args)
+    {
         Environment.set_application_name (_("Five or More"));
         Gtk.Window.set_default_icon_name ("five-or-more");
 
@@ -47,7 +63,21 @@ public class FiveOrMoreApp: Gtk.Application {
 
     private void preferences_cb ()
     {
+        if (preferences_dialog != null)
+        {
+            preferences_dialog.present ();
+            return;
+        }
 
+        preferences_dialog = new PreferencesDialog (settings);
+        preferences_dialog.set_transient_for (window);
+
+        preferences_dialog.response.connect (() => {
+            preferences_dialog.destroy ();
+            preferences_dialog = null;
+        });
+
+        preferences_dialog.present ();
     }
 
     private void help_cb ()
@@ -65,31 +95,36 @@ public class FiveOrMoreApp: Gtk.Application {
     private void about_cb ()
     {
         /* Appears on the About dialog. */
-        const string authors[] = {  "Robert Szokovacs <szo appaloosacorp hu>",
-                                    "Szabolcs B\xc3\xa1n <shooby gnome hu>",
-                                    null
-                                };
+        const string authors[] = {
+            "Robert Szokovacs <szo appaloosacorp hu>",
+            "Szabolcs B\xc3\xa1n <shooby gnome hu>",
+            null
+        };
+
+        const string artists[] = {
+            null
+        };
 
-        const string artists[] = { null
-                                };
+        const string documenters[] = {
+            "Tiffany Antopolski",
+            "Lanka Rathnayaka",
+            null
+        };
 
-        const string documenters[] = {  "Tiffany Antopolski",
-                                        "Lanka Rathnayaka",
-                                        null
-                                    };
+        const string copyright = "Copyright © 1997–2008 Free Software Foundation, Inc.\n
+            Copyright © 2013–2014 Michael Catanzaro";
 
         Gtk.show_about_dialog (window,
-                                "program-name", _("Five or More"),
-                                "logo-icon-name", "five-or-more",
-                                "version", VERSION,
-                                "comments", _("GNOME port of the once-popular Color Lines game"),
-                                "copyright", "Copyright © 1997–2008 Free Software Foundation, Inc.\n 
Copyright © 2013–2014 Michael Catanzaro",
-                                "license-type", Gtk.License.GPL_2_0,
-                                "authors", authors,
-                                "artists", artists,
-                                "documenters", documenters,
-                                "translator-credits", _("translator-credits"),
-                                "website", "https://wiki.gnome.org/Apps/Five%20or%20more";
-                            );
+                               "program-name", _("Five or More"),
+                               "logo-icon-name", "five-or-more",
+                               "version", VERSION,
+                               "comments", _("GNOME port of the once-popular Color Lines game"),
+                               "copyright", copyright,
+                               "license-type", Gtk.License.GPL_2_0,
+                               "authors", authors,
+                               "artists", artists,
+                               "documenters", documenters,
+                               "translator-credits", _("translator-credits"),
+                               "website", "https://wiki.gnome.org/Apps/Five%20or%20more";);
     }
 }
diff --git a/src-vala/meson.build b/src-vala/meson.build
index 1cbc0eb..7b31d1a 100644
--- a/src-vala/meson.build
+++ b/src-vala/meson.build
@@ -2,9 +2,11 @@
 
 five_or_more_sources = [
   'config.vapi',
+  'game.vala',
   'main.vala',
+  'preferences-dialog.vala',
   'window.vala',
-  resources
+  resources,
 ]
 
 five_or_more_deps = [
diff --git a/src-vala/preferences-dialog.vala b/src-vala/preferences-dialog.vala
new file mode 100644
index 0000000..0c3bdbe
--- /dev/null
+++ b/src-vala/preferences-dialog.vala
@@ -0,0 +1,61 @@
+[GtkTemplate (ui = "/org/gnome/five-or-more/ui/preferences-dialog.ui")]
+public class PreferencesDialog : Gtk.Dialog
+{
+    private Settings settings;
+
+    [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 color_set_cb (Gtk.ColorButton self)
+    {
+        var color = self.get_rgba ();
+        if (!settings.set_string ("background-color", color.to_string ()))
+            warning ("Failed to set color: %s", color.to_string ());
+    }
+
+    private void size_cb (BoardSize size)
+    {
+        if (!settings.set_int ("size", size))
+            warning ("Failed to set size: %d", size);
+    }
+
+    public PreferencesDialog (Settings settings)
+    {
+        this.settings = settings;
+
+        /* Set up board color */
+        var color_str = settings.get_string (FiveOrMoreApp.KEY_BACKGROUND_COLOR);
+        Gdk.RGBA color = Gdk.RGBA ();
+        color.parse (color_str);
+        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 (() => { size_cb (SMALL); });
+        radiobutton_medium.toggled.connect (() => { size_cb (MEDIUM); });
+        radiobutton_large.toggled.connect (() => { size_cb (LARGE); });
+    }
+}


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