[gnome-usage] window: Use a GtkBuilder template



commit 286f028d9c29d0bf4ea41c342350f4bacaf9bbc5
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed Jun 24 16:53:17 2020 +0200

    window: Use a GtkBuilder template
    
    This makes the code simpler and more flexible.

 data/meson.build                   |  3 ++-
 data/org.gnome.Usage.gresource.xml |  1 +
 data/ui/window.ui                  | 33 ++++++++++++++++++++++++
 po/POTFILES.in                     |  1 +
 src/window.vala                    | 53 +++++++++++++++++---------------------
 5 files changed, 60 insertions(+), 31 deletions(-)
---
diff --git a/data/meson.build b/data/meson.build
index 6896701..9d95744 100644
--- a/data/meson.build
+++ b/data/meson.build
@@ -11,7 +11,8 @@ resource_data = files(
   'ui/storage-actionbar.ui',
   'ui/storage-row-popover.ui',
   'ui/storage-view.ui',
-  'ui/swap-speedometer.ui'
+  'ui/swap-speedometer.ui',
+  'ui/window.ui',
 )
 
 gresource = 'org.gnome.Usage'
diff --git a/data/org.gnome.Usage.gresource.xml b/data/org.gnome.Usage.gresource.xml
index 1133797..b2b59a0 100644
--- a/data/org.gnome.Usage.gresource.xml
+++ b/data/org.gnome.Usage.gresource.xml
@@ -17,5 +17,6 @@
         <file preprocess="xml-stripblanks">ui/storage-view-row.ui</file>
         <file preprocess="xml-stripblanks">ui/process-row.ui</file>
         <file preprocess="xml-stripblanks">ui/swap-speedometer.ui</file>
+        <file preprocess="xml-stripblanks">ui/window.ui</file>
     </gresource>
 </gresources>
diff --git a/data/ui/window.ui b/data/ui/window.ui
new file mode 100644
index 0000000..ca112a1
--- /dev/null
+++ b/data/ui/window.ui
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="3.24"/>
+  <requires lib="libhandy" version="1.0"/>
+  <template class="UsageWindow" parent="GtkApplicationWindow">
+    <property name="default-height">600</property>
+    <property name="default-width">950</property>
+    <property name="title" translatable="yes">Usage</property>
+    <property name="visible">True</property>
+    <property name="window-position">center</property>
+    <child>
+      <object class="GtkBox">
+        <property name="orientation">vertical</property>
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkStack" id="stack">
+            <property name="height-request">200</property>
+            <property name="vexpand">True</property>
+            <property name="visible">True</property>
+            <property name="width-request">360</property>
+            <signal name="notify::visible-child" handler="on_visible_child_changed"/>
+          </object>
+        </child>
+        <child>
+          <object class="HdyViewSwitcherBar" id="view_switcher_bar">
+            <property name="stack">stack</property>
+            <property name="visible">True</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 23c48a9..a45c400 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -13,6 +13,7 @@ data/ui/storage-actionbar.ui
 data/ui/storage-row-popover.ui
 data/ui/storage-view.ui
 data/ui/swap-speedometer.ui
+data/ui/window.ui
 src/app-item.vala
 src/application.vala
 src/cpu-sub-view.vala
diff --git a/src/window.vala b/src/window.vala
index cf2f05c..d6dfc3d 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -1,6 +1,7 @@
 /* window.vala
  *
  * Copyright (C) 2017 Red Hat, Inc.
+ * Copyright (C) 2020 Adrien Plazas <kekun plazas laposte net>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,20 +27,23 @@ namespace Usage
         STORAGE,
     }
 
+    [GtkTemplate (ui = "/org/gnome/Usage/ui/window.ui")]
     public class Window : Gtk.ApplicationWindow
     {
+        [GtkChild]
+        private Gtk.Stack stack;
+
         private Usage.HeaderBar header_bar;
-        private Hdy.ViewSwitcherBar viewswitcher_bar;
+
+        [GtkChild]
+        private Hdy.ViewSwitcherBar view_switcher_bar;
+
         private View[] views;
 
                public Window(Gtk.Application application)
         {
             GLib.Object(application : application);
 
-            this.set_default_size(950, 600);
-            this.window_position = Gtk.WindowPosition.CENTER;
-            this.set_title(_("Usage"));
-
             if(Config.PROFILE == "Devel") {
                 get_style_context().add_class("devel");
             }
@@ -50,16 +54,9 @@ namespace Usage
                 load_css();
             });
 
-                       var stack = new Gtk.Stack();
-            stack.set_size_request(360, 200);
-            stack.visible = true;
-            stack.vexpand = true;
                        header_bar = new Usage.HeaderBar(stack);
                        set_titlebar(header_bar);
-            viewswitcher_bar = new Hdy.ViewSwitcherBar();
-            viewswitcher_bar.visible = true;
-            viewswitcher_bar.stack = stack;
-            header_bar.bind_property ("title-visible", viewswitcher_bar, "reveal", BindingFlags.SYNC_CREATE);
+            header_bar.bind_property ("title-visible", view_switcher_bar, "reveal", 
BindingFlags.SYNC_CREATE);
 
             views = new View[]
             {
@@ -71,23 +68,6 @@ namespace Usage
                 stack.add_titled(view, view.name, view.title);
                 stack.child_set (view, "icon-name", view.icon_name, null);
             }
-
-            stack.notify.connect(() => {
-                if(stack.visible_child_name == views[Views.PERFORMANCE].name)
-                {
-                    header_bar.set_mode(HeaderBarMode.PERFORMANCE);
-                }
-                else if(stack.visible_child_name == views[Views.STORAGE].name)
-                {
-                    header_bar.set_mode(HeaderBarMode.STORAGE);
-                }
-            });
-
-            var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
-            box.visible = true;
-            box.add(stack);
-            box.add(viewswitcher_bar);
-            this.add(box);
         }
 
         public void action_on_search()
@@ -107,5 +87,18 @@ namespace Usage
             provider.load_from_resource("/org/gnome/Usage/interface/adwaita.css");
             Gtk.StyleContext.add_provider_for_screen(get_screen(), provider, 
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
         }
+
+        [GtkCallback]
+        private void on_visible_child_changed()
+        {
+            if(stack.visible_child_name == views[Views.PERFORMANCE].name)
+            {
+                header_bar.set_mode(HeaderBarMode.PERFORMANCE);
+            }
+            else if(stack.visible_child_name == views[Views.STORAGE].name)
+            {
+                header_bar.set_mode(HeaderBarMode.STORAGE);
+            }
+        }
     }
 }


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