[gnome-clocks/wip/vala] Rework toolbar updates



commit 1151afcaa1f43c47c075cbd4ec2220e815312d04
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Feb 17 14:41:56 2013 +0100

    Rework toolbar updates

 src/alarm.vala     |   70 ++++++++++++++++++---------------------------------
 src/stopwatch.vala |    5 ---
 src/timer.vala     |    5 ---
 src/widgets.vala   |   66 +++++++++++++++++++++++++++++++++++++++++-------
 src/window.vala    |   18 ++++++++++---
 src/world.vala     |   68 ++++++++++++++++++--------------------------------
 6 files changed, 120 insertions(+), 112 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index 2c22058..a60fb25 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -438,23 +438,19 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
 
         var builder = Utils.load_ui ("alarm.ui");
         var empty_view = builder.get_object ("empty_panel") as Gtk.Widget;
-        content_view = new ContentView (empty_view, icon_view);
+        content_view = new ContentView (empty_view, icon_view, toolbar);
         add (content_view);
 
-        content_view.notify["is-empty"].connect (() => {
-            if (visible_child != null) {
-                update_toolbar ();
-            }
-        });
-
         standalone = new StandalonePanel ();
         add (standalone);
 
         load ();
 
         var id = notify["visible-child"].connect (() => {
-            if (visible_child != null) {
-                update_toolbar ();
+            if (visible_child == content_view) {
+                toolbar.mode = Toolbar.Mode.NORMAL;
+            } else if (visible_child == standalone) {
+                toolbar.mode = Toolbar.Mode.STANDALONE;
             }
         });
         toolbar.destroy.connect(() => {
@@ -525,42 +521,26 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
     }
 
     public void update_toolbar () {
-        toolbar.clear ();
-        if (visible_child == content_view) {
-            if (icon_view.mode == IconView.Mode.SELECTION) {
-                toolbar.mode = Toolbar.Mode.SELECTION;
-                var done_button = toolbar.add_button (null, _("Done"), false);
-                done_button.get_style_context ().add_class ("suggested-action");
-                done_button.clicked.connect (() => {
-                    icon_view.mode = IconView.Mode.NORMAL;
-                    update_toolbar ();
-                });
-            } else {
-                toolbar.mode = Toolbar.Mode.NORMAL;
-                // Translators: "New" refers to an alarm
-                var new_button = toolbar.add_button (null, _("New"), true);
-                new_button.clicked.connect (() => {
-                    activate_new ();
-                });
-                var select_button = toolbar.add_button ("object-select-symbolic", null, false);
-                if (content_view.is_empty) {
-                    select_button.sensitive = false;
-                } else {
-                    select_button.clicked.connect (() => {
-                        icon_view.mode = IconView.Mode.SELECTION;
-                        update_toolbar ();
-                    });
-                }
-            }
-        } else if (visible_child == standalone) {
-            toolbar.mode = Toolbar.Mode.STANDALONE;
-                var back_button = toolbar.add_button ("go-previous-symbolic", null, true);
-                back_button.clicked.connect (() => {
-                    visible_child = content_view;
-                });
-                toolbar.add_button (null, _("Edit"), false);
-                toolbar.set_labels (GLib.Markup.escape_text (standalone.alarm.name), null);
-        } else {
+        switch (toolbar.mode) {
+        case Toolbar.Mode.NORMAL:
+            // Translators: "New" refers to an alarm
+            var new_button = toolbar.add_button (null, _("New"), true);
+            new_button.clicked.connect (() => {
+                activate_new ();
+            });
+            content_view.update_toolbar ();
+            break;
+        case Toolbar.Mode.SELECTION:
+            content_view.update_toolbar ();
+            break;
+        case Toolbar.Mode.STANDALONE:
+            var back_button = toolbar.add_button ("go-previous-symbolic", null, true);
+            back_button.clicked.connect (() => {
+                visible_child = content_view;
+            });
+            toolbar.set_labels (GLib.Markup.escape_text (standalone.alarm.name), null);
+            break;
+        default:
             assert_not_reached ();
         }
     }
diff --git a/src/stopwatch.vala b/src/stopwatch.vala
index 122a54c..6abe129 100644
--- a/src/stopwatch.vala
+++ b/src/stopwatch.vala
@@ -221,11 +221,6 @@ public class MainPanel : Gtk.Box, Clocks.Clock {
 
         return true;
     }
-
-    public void update_toolbar () {
-        toolbar.clear ();
-        toolbar.mode = Toolbar.Mode.NORMAL;
-    }
 }
 
 } // namespace Stopwatch
diff --git a/src/timer.vala b/src/timer.vala
index c1c73ef..111442c 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -212,11 +212,6 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
     private void update_countdown_label (int h, int m, int s) {
         time_label.set_text ("%02i:%02i:%02i".printf (h, m, s));
     }
-
-    public void update_toolbar () {
-        toolbar.clear ();
-        toolbar.mode = Toolbar.Mode.NORMAL;
-    }
 }
 
 } // namespace Timer
diff --git a/src/widgets.vala b/src/widgets.vala
index 3b65a37..31a6c42 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -28,6 +28,7 @@ public class Toolbar : Gd.MainToolbar {
     private List<Gtk.Widget> buttons;
     private List<Clock> clocks;
 
+    [CCode (notify = false)]
     public Mode mode {
         get {
             return _mode;
@@ -44,6 +45,8 @@ public class Toolbar : Gd.MainToolbar {
                 } else {
                     get_style_context ().remove_class ("selection-mode");
                 }
+
+                notify_property ("mode");
             }
         }
     }
@@ -223,13 +226,12 @@ public class IconView : Gtk.IconView {
         set {
             if (_mode != value) {
                 _mode = value;
-
                 // clear selection
                 if (_mode != Mode.SELECTION) {
                     unselect_all ();
                     var model = get_model () as Gtk.ListStore;
                     model.foreach ((model, path, iter) => {
-                        // TODO model.set (iter, selection_col, false, -1);
+                        ((Gtk.ListStore) model).set (iter, selection_col, false);
                         return false;
                     });
                 }
@@ -308,20 +310,25 @@ public class IconView : Gtk.IconView {
 public class ContentView : Gtk.Bin {
     private const int SELECTION_TOOLBAR_WIDTH = 300;
 
-    public bool is_empty { get; private set; default = true; }
+    public bool empty { get; private set; default = true; }
 
     private Gtk.Widget empty_page;
+    private IconView icon_view;
+    private Toolbar main_toolbar;
+    private Gtk.Toolbar selection_toolbar;
     private Gtk.Overlay overlay;
     private Gtk.ScrolledWindow scrolled_window;
 
-    public ContentView (Gtk.Widget empty_view, IconView icon_view) {
-        empty_page = empty_view;
+    public ContentView (Gtk.Widget e, IconView iv, Toolbar t) {
+        empty_page = e;
+        icon_view = iv;
+        main_toolbar = t;
 
         overlay = new Gtk.Overlay ();
         overlay.add (icon_view);
 
-        var toolbar = create_selection_toolbar ();
-        overlay.add_overlay (toolbar);
+        selection_toolbar = create_selection_toolbar ();
+        overlay.add_overlay (selection_toolbar);
 
         scrolled_window = new Gtk.ScrolledWindow (null, null);
         scrolled_window.add (overlay);
@@ -334,9 +341,27 @@ public class ContentView : Gtk.Bin {
             update_empty_view (model);
         });
 
+        icon_view.notify["mode"].connect (() => {
+            if (icon_view.mode == IconView.Mode.SELECTION) {
+                main_toolbar.mode = Toolbar.Mode.SELECTION;
+            } else if (icon_view.mode == IconView.Mode.NORMAL) {
+                main_toolbar.mode = Toolbar.Mode.NORMAL;
+            }
+        });
+
         icon_view.selection_changed.connect (() => {
             var items = icon_view.get_selected_items ();
-            toolbar.set_visible (items != null);
+            var n_items = items.length ();
+
+            string label;
+            if (n_items == 0) {
+                label = _("Click on items to select them");
+            } else {
+                label = ngettext ("%d selected", "%d selected", n_items).printf (n_items);
+            }
+            main_toolbar.set_labels (label, null);
+
+            selection_toolbar.set_visible (n_items != 0);
         });
 
         add (empty_page);
@@ -383,17 +408,38 @@ public class ContentView : Gtk.Bin {
             if (child != scrolled_window) {
                 remove (child);
                 add (scrolled_window);
-                is_empty = false;
+                empty = false;
             }
         } else {
             if (child != empty_page) {
                 remove (child);
                 add (empty_page);
-                is_empty = true;
+                empty = true;
             }
         }
         show_all ();
     }
+
+    public void update_toolbar () {
+        switch (main_toolbar.mode) {
+        case Toolbar.Mode.SELECTION:
+            var done_button = main_toolbar.add_button (null, _("Done"), false);
+            main_toolbar.set_labels (_("Click on items to select them"), null);
+            done_button.get_style_context ().add_class ("suggested-action");
+            done_button.clicked.connect (() => {
+                selection_toolbar.set_visible (false);
+                icon_view.mode = IconView.Mode.NORMAL;
+            });
+            break;
+        case Toolbar.Mode.NORMAL:
+            var select_button = main_toolbar.add_button ("object-select-symbolic", null, false);
+            select_button.clicked.connect (() => {
+                icon_view.mode = IconView.Mode.SELECTION;
+            });
+            bind_property ("empty", select_button, "sensitive", BindingFlags.SYNC_CREATE | 
BindingFlags.INVERT_BOOLEAN);
+            break;
+        }
+    }
 }
 
 public class AmPmToggleButton : Gtk.Button {
diff --git a/src/window.vala b/src/window.vala
index 3c4b5ce..4078afc 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -67,11 +67,13 @@ public class Window : Gtk.ApplicationWindow {
         });
 
         var id = stack.notify["visible-child"].connect (() => {
-            var clock = (Clock) stack.visible_child;
-            if (clock != null) {
-                clock.update_toolbar ();
-            }
+            update_toolbar ();
         });
+
+        toolbar.notify["mode"].connect (() => {
+            update_toolbar ();
+        });
+
         toolbar.destroy.connect(() => {
             stack.disconnect (id);
             id = 0;
@@ -139,6 +141,14 @@ public class Window : Gtk.ApplicationWindow {
                                "translator-credits", _("translator-credits"),
                                null);
     }
+
+    private void update_toolbar () {
+        toolbar.clear ();
+        var clock = (Clock) stack.visible_child;
+        if (clock != null) {
+            clock.update_toolbar ();
+        }
+    }
 }
 
 } // namespace Clocks
diff --git a/src/world.vala b/src/world.vala
index 4dd5287..fbf25bb 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -275,26 +275,22 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
 
         var builder = Utils.load_ui ("world.ui");
         var empty_view = builder.get_object ("empty_panel") as Gtk.Widget;
-        content_view = new ContentView (empty_view, icon_view);
+        content_view = new ContentView (empty_view, icon_view, toolbar);
         add (content_view);
 
         content_view.delete_selected.connect (() => {
         });
 
-        content_view.notify["is-empty"].connect (() => {
-            if (visible_child != null) {
-                update_toolbar ();
-            }
-        });
-
         standalone = new StandalonePanel ();
         add (standalone);
 
         load ();
 
         var id = notify["visible-child"].connect (() => {
-            if (visible_child != null) {
-                update_toolbar ();
+            if (visible_child == content_view) {
+                toolbar.mode = Toolbar.Mode.NORMAL;
+            } else if (visible_child == standalone) {
+                toolbar.mode = Toolbar.Mode.STANDALONE;
             }
         });
         toolbar.destroy.connect(() => {
@@ -347,40 +343,26 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
 
     public void update_toolbar () {
         toolbar.clear ();
-        if (visible_child == content_view) {
-            if (icon_view.mode == IconView.Mode.SELECTION) {
-                toolbar.mode = Toolbar.Mode.SELECTION;
-                var done_button = toolbar.add_button (null, _("Done"), false);
-                done_button.get_style_context ().add_class ("suggested-action");
-                done_button.clicked.connect (() => {
-                    icon_view.mode = IconView.Mode.NORMAL;
-                    update_toolbar ();
-                });
-            } else {
-                toolbar.mode = Toolbar.Mode.NORMAL;
-                // Translators: "New" refers to a world clock
-                var new_button = toolbar.add_button (null, _("New"), true);
-                new_button.clicked.connect (() => {
-                    activate_new ();
-                });
-                var select_button = toolbar.add_button ("object-select-symbolic", null, false);
-                if (content_view.is_empty) {
-                    select_button.sensitive = false;
-                } else {
-                    select_button.clicked.connect (() => {
-                        icon_view.mode = IconView.Mode.SELECTION;
-                        update_toolbar ();
-                    });
-                }
-            }
-        } else if (visible_child == standalone) {
-            toolbar.mode = Toolbar.Mode.STANDALONE;
-                var back_button = toolbar.add_button ("go-previous-symbolic", null, true);
-                back_button.clicked.connect (() => {
-                    visible_child = content_view;
-                });
-                toolbar.set_labels (GLib.Markup.escape_text (standalone.location.name), null);
-        } else {
+        switch (toolbar.mode) {
+        case Toolbar.Mode.NORMAL:
+            // Translators: "New" refers to a world clock
+            var new_button = toolbar.add_button (null, _("New"), true);
+            new_button.clicked.connect (() => {
+                activate_new ();
+            });
+            content_view.update_toolbar ();
+            break;
+        case Toolbar.Mode.SELECTION:
+            content_view.update_toolbar ();
+            break;
+        case Toolbar.Mode.STANDALONE:
+            var back_button = toolbar.add_button ("go-previous-symbolic", null, true);
+            back_button.clicked.connect (() => {
+                visible_child = content_view;
+            });
+            toolbar.set_labels (GLib.Markup.escape_text (standalone.location.name), null);
+            break;
+        default:
             assert_not_reached ();
         }
     }


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