[gnome-boxes/wip/exalm/libhandy1: 3/6] app-window: Move icon and list view into a separate widget



commit 3123c5148bcea9d8e7452aba87c6ed817f136faf
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue May 26 16:16:50 2020 +0500

    app-window: Move icon and list view into a separate widget
    
    This will make it easier to switch filters to a stack later.

 data/gnome-boxes.gresource.xml      |  1 +
 data/ui/app-window.ui               | 16 ++--------
 data/ui/collection-filter-view.ui   | 35 ++++++++++++++++++++
 src/app-window.vala                 | 43 +++++++++----------------
 src/collection-filter-switcher.vala |  4 +--
 src/collection-filter-view.vala     | 64 +++++++++++++++++++++++++++++++++++++
 src/empty-boxes.vala                |  2 +-
 src/meson.build                     |  1 +
 8 files changed, 122 insertions(+), 44 deletions(-)
---
diff --git a/data/gnome-boxes.gresource.xml b/data/gnome-boxes.gresource.xml
index 09d478be..e8951023 100644
--- a/data/gnome-boxes.gresource.xml
+++ b/data/gnome-boxes.gresource.xml
@@ -12,6 +12,7 @@
     <file preprocess="xml-stripblanks">ui/app-window.ui</file>
     <file preprocess="xml-stripblanks">ui/auth-notification.ui</file>
     <file preprocess="xml-stripblanks">ui/collection-filter-switcher.ui</file>
+    <file preprocess="xml-stripblanks">ui/collection-filter-view.ui</file>
     <file preprocess="xml-stripblanks">ui/collection-toolbar.ui</file>
     <file preprocess="xml-stripblanks">ui/display-page.ui</file>
     <file preprocess="xml-stripblanks">ui/display-toolbar.ui</file>
diff --git a/data/ui/app-window.ui b/data/ui/app-window.ui
index 897df7a1..f20f5069 100644
--- a/data/ui/app-window.ui
+++ b/data/ui/app-window.ui
@@ -16,9 +16,7 @@
      |     |    |
      |     |    |-> below_bin = new Gtk.Stack ();
      |     |        |
-     |     |        |-> icon_view = new Boxes.IconView ();
-     |     |        |
-     |     |        |-> list_view = new Boxes.ListView ();
+     |     |        |-> collection_view = new Boxes.CollectionView ();
      |     |        |
      |     |        |-> empty_boxes = new Boxes.EmptyBoxes ();
      |     |        |
@@ -93,20 +91,12 @@
                 </child>
 
                 <child>
-                  <object class="BoxesIconView" id="icon_view">
+                  <object class="BoxesCollectionFilterView" id="collection_view">
                     <property name="visible">True</property>
-                  </object>
-                  <packing>
-                    <property name="name">icon-view</property>
-                  </packing>
-                </child>
 
-                <child>
-                  <object class="BoxesListView" id="list_view">
-                    <property name="visible">True</property>
                   </object>
                   <packing>
-                    <property name="name">list-view</property>
+                    <property name="name">collection-view</property>
                   </packing>
                 </child>
 
diff --git a/data/ui/collection-filter-view.ui b/data/ui/collection-filter-view.ui
new file mode 100644
index 00000000..8d80faeb
--- /dev/null
+++ b/data/ui/collection-filter-view.ui
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="BoxesCollectionFilterView" parent="GtkBin">
+    <property name="visible">True</property>
+
+    <child>
+      <object class="GtkStack" id="stack">
+        <property name="visible">True</property>
+        <property name="transition-type">crossfade</property>
+        <property name="transition-duration">400</property>
+
+        <child>
+          <object class="BoxesIconView" id="icon_view">
+            <property name="visible">True</property>
+            <property name="ui-state" bind-source="BoxesCollectionFilterView" bind-property="ui-state"/>
+          </object>
+          <packing>
+            <property name="name">icon-view</property>
+          </packing>
+        </child>
+
+        <child>
+          <object class="BoxesListView" id="list_view">
+            <property name="visible">True</property>
+            <property name="ui-state" bind-source="BoxesCollectionFilterView" bind-property="ui-state"/>
+          </object>
+          <packing>
+            <property name="name">list-view</property>
+          </packing>
+        </child>
+
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/app-window.vala b/src/app-window.vala
index 51cd94d3..862cb733 100644
--- a/src/app-window.vala
+++ b/src/app-window.vala
@@ -94,26 +94,16 @@
     [GtkChild]
     public Gtk.Stack below_bin;
     [GtkChild]
-    private IconView icon_view;
-    [GtkChild]
-    private ListView list_view;
+    private CollectionFilterView collection_view;
 
     public ViewType view_type { get; set; default = ViewType.ICON; }
 
     public ICollectionView view {
         get {
-            switch (view_type) {
-            default:
-            case ViewType.ICON:
-                return icon_view;
-            case ViewType.LIST:
-                return list_view;
-            }
+            return collection_view.view;
         }
     }
 
-    private ICollectionView[] views;
-
     public GLib.Settings settings;
 
     public bool first_run {
@@ -167,8 +157,6 @@ public AppWindow (Gtk.Application app) {
             move (x, y);
         }
 
-        views = { icon_view, list_view };
-
         if (app.application_id == "org.gnome.BoxesDevel") {
             get_style_context ().add_class ("devel");
         }
@@ -177,8 +165,7 @@ public AppWindow (Gtk.Application app) {
     public void setup_ui () {
         topbar.setup_ui (this);
         display_page.setup_ui (this);
-        icon_view.setup_ui (this);
-        list_view.setup_ui (this);
+        collection_view.setup_ui (this);
         selectionbar.setup_ui (this);
         searchbar.setup_ui (this);
         empty_boxes.setup_ui (this);
@@ -210,8 +197,7 @@ private void ui_state_changed () {
         // The order is important for some widgets here (e.g properties must change its state before wizard 
so it can
         // flush any deferred changes for wizard to pick-up when going back from properties to wizard 
(review).
         foreach (var ui in new Boxes.UI[] { topbar,
-                                            icon_view,
-                                            list_view,
+                                            collection_view,
                                             props_window,
                                             //wizard_window,
                                             empty_boxes }) {
@@ -226,11 +212,13 @@ private void ui_state_changed () {
         switch (ui_state) {
         case UIState.COLLECTION:
             if (App.app.collection.length != 0)
-                below_bin.visible_child = view;
+                below_bin.visible_child = collection_view;
             else
                 below_bin.visible_child = empty_boxes;
+
+            collection_view.view_type = view_type;
+
             fullscreened = false;
-            foreach_view ((view) => { view.visible = true; });
 
             if (status_bind != null) {
                 status_bind.unbind ();  // FIXME: We shouldn't neeed to explicitly unbind (Vala bug?)
@@ -277,11 +265,6 @@ private void ui_state_changed () {
             current_item.set_state (ui_state);
     }
 
-    public void foreach_view (Func<ICollectionView> func) {
-        foreach (var view in views)
-            func (view);
-    }
-
     public void show_remote_connection_assistant (string? uri = null) {
         new Boxes.RemoteConnectionAssistant (this, uri).run ();
     }
@@ -388,7 +371,7 @@ public void select_item (CollectionItem item) {
     }
 
     public void filter (string text) {
-        foreach_view ((view) => { view.filter.text = text; });
+        collection_view.foreach_view ((view) => { view.filter.text = text; });
     }
 
     [GtkCallback]
@@ -434,12 +417,12 @@ public bool on_key_pressed (Widget widget, Gdk.EventKey event) {
         } else if (event.keyval == Gdk.Key.a &&
                    (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
             selection_mode = true;
-            foreach_view ((view) => { view.select_all (); });
+            collection_view.foreach_view ((view) => { view.select_all (); });
 
             return true;
         } else if (event.keyval == Gdk.Key.A &&
                    (event.state & default_modifiers) == (Gdk.ModifierType.CONTROL_MASK | 
Gdk.ModifierType.SHIFT_MASK)) {
-            foreach_view ((view) => { view.unselect_all (); });
+            collection_view.foreach_view ((view) => { view.unselect_all (); });
 
             return true;
         } else if (((direction == Gtk.TextDirection.LTR && // LTR
@@ -516,4 +499,8 @@ private void on_machine_deleted_notify () {
        if (this != App.app.main_window && current_machine.deleted)
            on_delete_event ();
     }
+
+    public void set_filter_func (CollectionFilterFunc? filter_func) {
+        collection_view.filter_func = filter_func;
+    }
 }
diff --git a/src/collection-filter-switcher.vala b/src/collection-filter-switcher.vala
index 1270b722..cf198745 100644
--- a/src/collection-filter-switcher.vala
+++ b/src/collection-filter-switcher.vala
@@ -18,7 +18,7 @@ public void setup_ui (AppWindow window) {
         activate_button (all_button);
         App.app.call_when_ready (on_app_ready);
 
-        window.foreach_view ((view) => { view.filter.filter_func = null; });
+        window.set_filter_func (null);
     }
 
     private unowned CollectionFilterFunc? get_filter_func () {
@@ -62,6 +62,6 @@ private void activate_button (Gtk.ToggleButton button) {
                 toggle_button.active = toggle_button == active_button;
         }
 
-        window.foreach_view ((view) => { view.filter.filter_func = get_filter_func (); });
+        window.set_filter_func (get_filter_func ());
     }
 }
diff --git a/src/collection-filter-view.vala b/src/collection-filter-view.vala
new file mode 100644
index 00000000..d9d2f07e
--- /dev/null
+++ b/src/collection-filter-view.vala
@@ -0,0 +1,64 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+
+[GtkTemplate (ui = "/org/gnome/Boxes/ui/collection-filter-view.ui")]
+private class Boxes.CollectionFilterView: Gtk.Bin, Boxes.UI {
+    public UIState previous_ui_state { get; protected set; }
+    public UIState ui_state { get; protected set; }
+
+    [GtkChild]
+    private Gtk.Stack stack;
+    [GtkChild]
+    private Boxes.IconView icon_view;
+    [GtkChild]
+    private Boxes.ListView list_view;
+
+    public AppWindow.ViewType view_type { get; set; default = AppWindow.ViewType.ICON; }
+
+    private unowned CollectionFilterFunc _filter_func = null;
+    public unowned CollectionFilterFunc filter_func {
+        get { return _filter_func; }
+        set {
+            _filter_func = value;
+            icon_view.filter.filter_func = value;
+            list_view.filter.filter_func = value;
+        }
+    }
+
+    public ICollectionView view {
+        get {
+            switch (view_type) {
+            default:
+            case AppWindow.ViewType.ICON:
+                return icon_view;
+            case AppWindow.ViewType.LIST:
+                return list_view;
+            }
+        }
+    }
+
+    construct {
+        notify["view-type"].connect (ui_state_changed);
+    }
+
+    public void setup_ui (AppWindow window) {
+        icon_view.setup_ui (window);
+        list_view.setup_ui (window);
+    }
+
+    public void foreach_view (Func<ICollectionView> func) {
+        func (icon_view);
+        func (list_view);
+    }
+
+    private void ui_state_changed () {
+        icon_view.set_state (ui_state);
+        list_view.set_state (ui_state);
+
+        if (ui_state == UIState.COLLECTION) {
+            stack.visible_child = view;
+
+            icon_view.show ();
+            list_view.show ();
+        }
+    }
+}
diff --git a/src/empty-boxes.vala b/src/empty-boxes.vala
index 3f03d506..119585d9 100644
--- a/src/empty-boxes.vala
+++ b/src/empty-boxes.vala
@@ -36,6 +36,6 @@ private void update_visibility () {
         if (visible)
             window.below_bin.set_visible_child_name ("empty-boxes");
         else
-            window.below_bin.set_visible_child (window.view);
+            window.below_bin.set_visible_child_name ("collection-view");
     }
 }
diff --git a/src/meson.build b/src/meson.build
index d6705165..c5804d02 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -53,6 +53,7 @@ vala_sources = [
   'auth-notification.vala',
   'collection.vala',
   'collection-filter-switcher.vala',
+  'collection-filter-view.vala',
   'collection-toolbar.vala',
   'display-page.vala',
   'display-toolbar.vala',


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