[gnome-games/wip/exalm/pref-sidebar: 4/6] ui: Add PreferencesSidebar



commit 6ddbaad4018b49ee4c2fce97e856ba69ee02fb2d
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Wed Sep 12 15:02:49 2018 +0500

    ui: Add PreferencesSidebar
    
    Implement a GtkStackSidebar replacement that:
     * Uses PreferencesPage titles for labels.
     * Removes all unused functionality, like 'needs-attention' handling.
     * Adds 'row-selected' signal that is emitted even when clicking
       currently selected item.
     * Removes separators between rows.
     * Allows to wrap row labels.

 data/org.gnome.Games.gresource.xml |  1 +
 data/ui/preferences-sidebar.ui     | 24 +++++++++++
 src/meson.build                    |  1 +
 src/ui/preferences-sidebar.vala    | 88 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 114 insertions(+)
---
diff --git a/data/org.gnome.Games.gresource.xml b/data/org.gnome.Games.gresource.xml
index a9692308..fdde9da8 100644
--- a/data/org.gnome.Games.gresource.xml
+++ b/data/org.gnome.Games.gresource.xml
@@ -36,6 +36,7 @@
     <file preprocess="xml-stripblanks">ui/preferences-page-plugins.ui</file>
     <file preprocess="xml-stripblanks">ui/preferences-page-plugins-item.ui</file>
     <file preprocess="xml-stripblanks">ui/preferences-page-video.ui</file>
+    <file preprocess="xml-stripblanks">ui/preferences-sidebar.ui</file>
     <file preprocess="xml-stripblanks">ui/preferences-sidebar-item.ui</file>
     <file preprocess="xml-stripblanks">ui/preferences-subpage-gamepad.ui</file>
     <file preprocess="xml-stripblanks">ui/preferences-subpage-keyboard.ui</file>
diff --git a/data/ui/preferences-sidebar.ui b/data/ui/preferences-sidebar.ui
new file mode 100644
index 00000000..8b9790a2
--- /dev/null
+++ b/data/ui/preferences-sidebar.ui
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GamesPreferencesSidebar" parent="GtkBin">
+    <property name="visible">True</property>
+    <style>
+      <class name="preferences"/>
+      <class name="sidebar"/>
+    </style>
+    <child>
+      <object class="GtkScrolledWindow">
+        <property name="visible">True</property>
+        <property name="hscrollbar-policy">never</property>
+        <child>
+          <object class="GtkListBox" id="list">
+            <property name="visible">True</property>
+            <property name="margin-top">4</property>
+            <property name="margin-bottom">4</property>
+            <signal name="row-activated" handler="row_activated"/>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/meson.build b/src/meson.build
index ab3ef027..ac57efbb 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -158,6 +158,7 @@ vala_sources = [
   'ui/preferences-page-plugins.vala',
   'ui/preferences-page-plugins-item.vala',
   'ui/preferences-page-video.vala',
+  'ui/preferences-sidebar.vala',
   'ui/preferences-sidebar-item.vala',
   'ui/preferences-subpage.vala',
   'ui/preferences-subpage-gamepad.vala',
diff --git a/src/ui/preferences-sidebar.vala b/src/ui/preferences-sidebar.vala
new file mode 100644
index 00000000..dc32843e
--- /dev/null
+++ b/src/ui/preferences-sidebar.vala
@@ -0,0 +1,88 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/preferences-sidebar.ui")]
+private class Games.PreferencesSidebar: Gtk.Bin {
+       private ulong child_added_id;
+
+       private Gtk.Stack _stack;
+       public Gtk.Stack stack {
+               get {
+                       return _stack;
+               }
+               set {
+                       if (_stack != null) {
+                               clear_sidebar ();
+                               stack.disconnect (child_added_id);
+                               _stack = null;
+                       }
+
+                       _stack = value;
+
+                       if (_stack != null)
+                               populate_sidebar ();
+
+                       child_added_id = stack.add.connect (add_child);
+
+                       queue_resize ();
+               }
+       }
+
+       public signal void row_selected ();
+
+       [GtkChild]
+       private Gtk.ListBox list;
+       private HashTable<PreferencesPage, PreferencesSidebarItem> rows;
+
+       static construct {
+               set_css_name ("stacksidebar");
+       }
+
+       construct {
+               rows = new HashTable<PreferencesPage, PreferencesSidebarItem> (null, null);
+       }
+
+       [GtkCallback]
+       private void row_activated (Gtk.ListBox box, Gtk.ListBoxRow? row) {
+               if (row == null)
+                       return;
+
+               var item = row as PreferencesSidebarItem;
+               var page = item.preferences_page;
+               stack.visible_child = page;
+
+               row_selected ();
+       }
+
+       private void add_child (Gtk.Widget widget) {
+               var page = widget as PreferencesPage;
+
+               var row = new PreferencesSidebarItem (page);
+
+               rows[page] = row;
+               list.add (row);
+       }
+
+       private void populate_sidebar () {
+               stack.foreach (add_child);
+
+               select_current_row ();
+       }
+
+       private void clear_sidebar () {
+               foreach (var page in rows.get_keys ()) {
+                       var row = rows[page];
+
+                       list.remove (row);
+                       rows.remove (page);
+               }
+       }
+
+       private void select_current_row () {
+               var page = stack.visible_child as PreferencesPage;
+
+               var row = rows[page];
+
+               if (row != null)
+                       list.select_row (row);
+       }
+}


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