[swell-foop/arnaudb/code-improvements: 2/9] Use a GtkBuilder template.



commit 11457a91217ec2ef8ee843389d6fc20c4421d775
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri May 1 23:36:12 2020 +0200

    Use a GtkBuilder template.

 data/swell-foop.ui                    | 94 +++++++++++++++++++++++++++++++++++
 src/meson.build                       |  5 +-
 src/org.gnome.SwellFoop.gresource.xml |  6 +++
 src/window.vala                       | 44 +++-------------
 4 files changed, 110 insertions(+), 39 deletions(-)
---
diff --git a/data/swell-foop.ui b/data/swell-foop.ui
new file mode 100644
index 0000000..8b17aac
--- /dev/null
+++ b/data/swell-foop.ui
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   This file is part of Swell-Foop.
+
+   Copyright (C) 2020 Arnaud Bonatti <arnaud bonatti gmail com>
+
+   Swell-Foop is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 2 of the License, or
+   (at your option) any later version.
+
+   Swell-Foop is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Swell-Foop.  If not, see <https://www.gnu.org/licenses/>.
+-->
+<interface>
+  <menu id="hamburger-menu">
+    <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">_New Game</attribute>
+        <attribute name="action">win.new-game</attribute>
+      </item>
+    </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">High _Scores</attribute>
+        <attribute name="action">win.scores</attribute>
+      </item>
+    </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">_Help</attribute>
+        <attribute name="action">app.help</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">_About Swell Foop</attribute>
+        <attribute name="action">app.about</attribute>
+      </item>
+    </section>
+  </menu>
+  <requires lib="gtk+" version="3.12"/>
+  <template class="SwellFoopWindow" parent="GtkApplicationWindow">
+    <property name="visible">True</property>
+    <property name="resizable">False</property>
+    <property name="icon-name">org.gnome.SwellFoop</property>
+    <!-- Translators: title of the window displayed by window managers; name of the application -->
+    <property name="title" translatable="yes">Swell Foop</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="headerbar">
+        <property name="visible">True</property>
+        <property name="show-close-button">True</property>
+        <!-- Translators: title of the window displayed on the headerbar; name of the application -->
+        <property name="title" translatable="yes">Swell Foop</property>
+        <child>
+          <object class="GtkMenuButton">
+            <property name="visible">True</property>
+            <property name="menu-model">hamburger-menu</property>
+            <style>
+              <class name="image-button"/>
+            </style>
+            <child>
+              <object class="GtkImage">
+                <property name="icon-name">open-menu-symbolic</property>
+                <property name="visible">True</property>
+                <property name="icon-size">1</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack-type">end</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkBox" id="main_box">
+        <property name="visible">True</property>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/meson.build b/src/meson.build
index 12750a9..b5ef3f9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -7,6 +7,9 @@ sources = [
   'window.vala',
 ]
 
+resource_files = files ('@0  gresource xml'.format('org.gnome.SwellFoop'))
+resources = gnome.compile_resources ('org.gnome.SwellFoop', resource_files)
+
 deps = [
   config_dep,
   posix_dep,
@@ -25,7 +28,7 @@ c_flags = [
   '-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name())
 ]
 
-executable('swell-foop', sources,
+executable('swell-foop', sources + resources,
   dependencies: deps,
   include_directories: config_h_inc,
   c_args: c_flags,
diff --git a/src/org.gnome.SwellFoop.gresource.xml b/src/org.gnome.SwellFoop.gresource.xml
new file mode 100644
index 0000000..b56feaa
--- /dev/null
+++ b/src/org.gnome.SwellFoop.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/SwellFoop/ui">
+    <file preprocess="xml-stripblanks" alias="swell-foop.ui">../data/swell-foop.ui</file>
+  </gresource>
+</gresources>
diff --git a/src/window.vala b/src/window.vala
index 84677e4..9f3bd69 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -19,9 +19,12 @@
 
 using Gtk;
 
+[GtkTemplate (ui = "/org/gnome/SwellFoop/ui/swell-foop.ui")]
 private class SwellFoopWindow : ApplicationWindow
 {
-    private HeaderBar headerbar;
+    [GtkChild] private HeaderBar    headerbar;
+    [GtkChild] private Box          main_box;
+
     public GLib.Settings settings { private get; protected construct; }
 
     /* Game history */
@@ -48,43 +51,8 @@ private class SwellFoopWindow : ApplicationWindow
     {
         add_action_entries (win_actions, this);
 
-        set_title (_("Swell Foop"));
-        icon_name = "org.gnome.SwellFoop";
-        resizable = false;
         add_events (Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.KEY_RELEASE_MASK);
 
-        var vbox = new Box (Orientation.VERTICAL, 0);
-        vbox.show ();
-        add (vbox);
-
-        /* Create the menus */
-        var menu = new GLib.Menu ();
-        var section = new GLib.Menu ();
-        menu.append_section (null, section);
-        section.append (_("_New Game"), "win.new-game");
-        section = new GLib.Menu ();
-        menu.append_section (null, section);
-        section.append (_("High _Scores"), "win.scores");
-        section = new GLib.Menu ();
-        menu.append_section (null, section);
-        section.append (_("_Preferences"), "app.preferences");
-        section.append (_("_Help"), "app.help");
-        section.append (_("_About Swell Foop"), "app.about");
-
-        /* Create a headerbar */
-        headerbar = new HeaderBar ();
-        headerbar.show ();
-        headerbar.title = _("Swell Foop");
-        headerbar.show_close_button = true;
-        set_titlebar (headerbar);
-
-        /* Add the primary menu button */
-        var primary_menu = new MenuButton ();
-        primary_menu.show ();
-        primary_menu.set_image (new Image.from_icon_name ("open-menu-symbolic", IconSize.BUTTON));
-        primary_menu.set_menu_model (menu);
-        headerbar.pack_end (primary_menu);
-
         /* show the current score */
         update_score_cb (0);
 
@@ -97,11 +65,11 @@ private class SwellFoopWindow : ApplicationWindow
         {
             var stack = build_first_run_stack ();
             stack.add_named (clutter_embed, "game");
-            vbox.pack_start (stack, true, true);
+            main_box.pack_start (stack, true, true);
         }
         else
         {
-            vbox.pack_start (clutter_embed, true, true);
+            main_box.pack_start (clutter_embed, true, true);
             key_press_event.connect (key_press_event_cb);
         }
     }


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