[geary/gtk-3.10: 3/4] Gtk.HeaderBar for main window and composer toolbar: Closes bgo#713478



commit 6539a7e3d39ec45b4c8d44e189f80b981b2e9c3b
Author: Yosef Or Boczko <yoseforb src gnome org>
Date:   Wed Jan 29 17:05:01 2014 -0800

    Gtk.HeaderBar for main window and composer toolbar: Closes bgo#713478
    
    Nice code reduction using Gtk.HeaderBar in place of Gtk.Toolbar.
    Main window no longer has chrome but composer window still does.

 src/client/application/geary-controller.vala |    1 +
 src/client/components/main-toolbar.vala      |   34 +++++-------
 src/client/components/main-window.vala       |   44 +++++++++++++-
 src/client/components/pill-toolbar.vala      |   78 ++++++-------------------
 src/client/composer/composer-toolbar.vala    |   14 ++---
 5 files changed, 79 insertions(+), 92 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 6218f56..338081f 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -941,6 +941,7 @@ public class GearyController : Geary.BaseObject {
         if (folder == null) {
             current_folder = null;
             main_window.conversation_list_store.clear();
+            main_window.main_toolbar.subtitle = null;
             folder_selected(null);
             
             return;
diff --git a/src/client/components/main-toolbar.vala b/src/client/components/main-toolbar.vala
index 0e71ca0..3caf466 100644
--- a/src/client/components/main-toolbar.vala
+++ b/src/client/components/main-toolbar.vala
@@ -17,7 +17,6 @@ public class MainToolbar : PillToolbar {
     
     private Gtk.Button archive_button;
     private Gtk.Button trash_buttons[2];
-    private Gtk.ToolItem search_container = new Gtk.ToolItem();
     private Gtk.SearchEntry search_entry = new Gtk.SearchEntry();
     private Geary.ProgressMonitor? search_upgrade_progress_monitor = null;
     private MonitoredProgressBar search_upgrade_progress_bar = new MonitoredProgressBar();
@@ -42,27 +41,25 @@ public class MainToolbar : PillToolbar {
         application_menu.foreach(GtkUtil.show_menuitem_accel_labels);
         
         // Toolbar setup.
-        orientation = Gtk.Orientation.HORIZONTAL;
-        get_style_context().add_class(Gtk.STYLE_CLASS_MENUBAR); // Drag window via toolbar.
         Gee.List<Gtk.Button> insert = new Gee.ArrayList<Gtk.Button>();
         
         // Compose.
         insert.add(create_toolbar_button("text-editor-symbolic", GearyController.ACTION_NEW_MESSAGE));
-        add(create_pill_buttons(insert, false));
+        add_start(create_pill_buttons(insert, false));
         
         // Reply buttons
         insert.clear();
         insert.add(create_toolbar_button(rtl ? "reply-rtl-symbolic" : "reply-symbolic", 
GearyController.ACTION_REPLY_TO_MESSAGE));
         insert.add(create_toolbar_button(rtl ? "reply-all-rtl-symbolic" : "reply-all-symbolic", 
GearyController.ACTION_REPLY_ALL_MESSAGE));
         insert.add(create_toolbar_button(rtl ? "forward-rtl-symbolic" : "forward-symbolic", 
GearyController.ACTION_FORWARD_MESSAGE));
-        add(create_pill_buttons(insert));
+        add_start(create_pill_buttons(insert));
         
         // Mark, copy, move.
         insert.clear();
         insert.add(create_menu_button("marker-symbolic", mark_menu, GearyController.ACTION_MARK_AS_MENU));
         insert.add(create_menu_button(rtl ? "tag-rtl-symbolic" : "tag-symbolic", copy_folder_menu, 
GearyController.ACTION_COPY_MENU));
         insert.add(create_menu_button("folder-symbolic", move_folder_menu, 
GearyController.ACTION_MOVE_MENU));
-        add(create_pill_buttons(insert));
+        add_start(create_pill_buttons(insert));
         
         // The toolbar looks bad when you hide one of a pair of pill buttons.
         // Unfortunately, this means we have to have one pair for archive/trash
@@ -71,13 +68,10 @@ public class MainToolbar : PillToolbar {
         insert.clear();
         insert.add(archive_button = create_toolbar_button(null, GearyController.ACTION_ARCHIVE_MESSAGE, 
true));
         insert.add(trash_buttons[0] = create_toolbar_button(null, GearyController.ACTION_TRASH_MESSAGE, 
true));
-        add(create_pill_buttons(insert));
+        add_start(create_pill_buttons(insert));
         insert.clear();
         insert.add(trash_buttons[1] = create_toolbar_button(null, GearyController.ACTION_TRASH_MESSAGE, 
true));
-        add(create_pill_buttons(insert, false));
-        
-        // Spacer.
-        add(create_spacer());
+        add_start(create_pill_buttons(insert, false));
         
         // Search bar.
         search_entry.width_chars = 32;
@@ -86,8 +80,7 @@ public class MainToolbar : PillToolbar {
         search_entry.key_press_event.connect(on_search_key_press);
         on_search_entry_changed(); // set initial state
         search_entry.has_focus = true;
-        search_container.add(search_entry);
-        add(search_container);
+        add_end(search_entry);
         
         // Search upgrade progress bar.
         search_upgrade_progress_bar.margin_top = 3;
@@ -104,7 +97,7 @@ public class MainToolbar : PillToolbar {
         if (!Gtk.Settings.get_default().gtk_shell_shows_app_menu) {
             insert.clear();
             insert.add(create_menu_button("emblem-system-symbolic", application_menu, 
GearyController.ACTION_GEAR_MENU));
-            add(create_pill_buttons(insert));
+            add_end(create_pill_buttons(insert));
         }
         
         set_search_placeholder_text(DEFAULT_SEARCH_TEXT);
@@ -164,14 +157,13 @@ public class MainToolbar : PillToolbar {
     }
     
     private void on_search_upgrade_start() {
-        search_container.remove(search_container.get_child());
-        search_container.add(search_upgrade_progress_bar);
+        search_entry.hide();
         search_upgrade_progress_bar.show();
     }
     
     private void on_search_upgrade_finished() {
-        search_container.remove(search_container.get_child());
-        search_container.add(search_entry);
+        search_entry.show();
+        search_upgrade_progress_bar.hide();
     }
     
     private void on_account_changed(Geary.Account? account) {
@@ -183,9 +175,10 @@ public class MainToolbar : PillToolbar {
             search_upgrade_progress_monitor = null;
         }
         
-        if (current_account != null)
+        if (current_account != null) {
             current_account.information.notify[Geary.AccountInformation.PROP_NICKNAME].disconnect(
                 on_nickname_changed);
+        }
         
         if (account != null) {
             search_upgrade_progress_monitor = account.search_upgrade_monitor;
@@ -198,11 +191,12 @@ public class MainToolbar : PillToolbar {
             
             account.information.notify[Geary.AccountInformation.PROP_NICKNAME].connect(
                 on_nickname_changed);
+            
+            search_upgrade_progress_bar.text = _("Indexing %s account").printf(account.information.nickname);
         }
         
         current_account = account;
         
-        search_upgrade_progress_bar.text = _("Indexing %s account").printf(account.information.nickname);
         on_nickname_changed(); // Set new account name.
     }
     
diff --git a/src/client/components/main-window.vala b/src/client/components/main-window.vala
index c5bd7b6..ee2f310 100644
--- a/src/client/components/main-window.vala
+++ b/src/client/components/main-window.vala
@@ -30,6 +30,7 @@ public class MainWindow : Gtk.ApplicationWindow {
     private MonitoredSpinner spinner = new MonitoredSpinner();
     private Geary.AggregateProgressMonitor progress_monitor = new Geary.AggregateProgressMonitor();
     private Geary.ProgressMonitor? conversation_monitor_progress = null;
+    private Geary.Folder? current_folder = null;
     
     public MainWindow(GearyApplication application) {
         Object(application: application);
@@ -65,10 +66,16 @@ public class MainWindow : Gtk.ApplicationWindow {
         key_release_event.connect(on_key_release_event);
         GearyApplication.instance.controller.notify[GearyController.PROP_CURRENT_CONVERSATION].
             connect(on_conversation_monitor_changed);
+        GearyApplication.instance.controller.folder_selected.connect(on_folder_selected);
         Geary.Engine.instance.account_available.connect(on_account_available);
         Geary.Engine.instance.account_unavailable.connect(on_account_unavailable);
         
         create_layout();
+        
+        // Toolbar.
+        main_toolbar = new MainToolbar();
+        main_toolbar.show_close_button = true;
+        set_titlebar(main_toolbar);
     }
     
     public override void show_all() {
@@ -107,10 +114,6 @@ public class MainWindow : Gtk.ApplicationWindow {
     private void create_layout() {
         Gtk.Box main_layout = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
         
-        // Toolbar.
-        main_toolbar = new MainToolbar();
-        main_layout.pack_start(main_toolbar, false, false, 0);
-        
         // folder list
         Gtk.ScrolledWindow folder_list_scrolled = new Gtk.ScrolledWindow(null, null);
         folder_list_scrolled.set_size_request(FOLDER_LIST_WIDTH, -1);
@@ -220,5 +223,38 @@ public class MainWindow : Gtk.ApplicationWindow {
             debug("Could not access account progress monitors: %s", e.message);
         }
     }
+    
+    private void on_folder_selected(Geary.Folder? folder) {
+        // disconnect from old folder
+        if (current_folder != null)
+            current_folder.properties.notify.disconnect(update_headerbar);
+        
+        // connect to new folder
+        if (folder != null)
+            folder.properties.notify.connect(update_headerbar);
+        
+        // swap it in
+        current_folder = folder;
+        
+        update_headerbar();
+    }
+    
+    private void update_headerbar() {
+        if (current_folder == null) {
+            main_toolbar.title = null;
+            main_toolbar.subtitle = null;
+            
+            return;
+        }
+        
+        main_toolbar.title = current_folder.account.information.nickname;
+        if(current_folder.properties.email_unread > 0) {
+            /// Current folder's name followed by its unread count, i.e. "Inbox (42)"
+            main_toolbar.subtitle = _("%s (%d)").printf(current_folder.get_display_name(),
+                current_folder.properties.email_unread);
+        } else {
+            main_toolbar.subtitle = current_folder.get_display_name();
+        }
+    }
 }
 
diff --git a/src/client/components/pill-toolbar.vala b/src/client/components/pill-toolbar.vala
index 6cb41b0..1446e36 100644
--- a/src/client/components/pill-toolbar.vala
+++ b/src/client/components/pill-toolbar.vala
@@ -7,13 +7,24 @@
 /**
  * Class for creating a Nautilus-style "pill" toolbar.  Use only as directed.
  */
-public class PillToolbar : Gtk.Toolbar {
+public class PillToolbar : Gtk.HeaderBar {
     private Gtk.ActionGroup action_group;
+    private Gtk.SizeGroup size = new Gtk.SizeGroup(Gtk.SizeGroupMode.VERTICAL);
     
     public PillToolbar(Gtk.ActionGroup toolbar_action_group) {
         action_group = toolbar_action_group;
     }
     
+    public void add_start(Gtk.Widget *widget) {
+        pack_start(widget);
+        size.add_widget(widget);
+    }
+    
+    public void add_end(Gtk.Widget *widget) {
+        pack_end(widget);
+        size.add_widget(widget);
+    }
+    
     protected void setup_button(Gtk.Button b, string? icon_name, string action_name,
         bool show_label = false) {
         b.related_action = action_group.get_action(action_name);
@@ -21,16 +32,9 @@ public class PillToolbar : Gtk.Toolbar {
         b.image = new Gtk.Image.from_icon_name(icon_name != null ? icon_name :
             b.related_action.icon_name, Gtk.IconSize.MENU);
         b.always_show_image = true;
-        b.image.margin = get_icon_margin();
         
         if (!show_label)
-            b.label = "";
-        
-        if (show_label && !Geary.String.is_empty(b.related_action.label))
-            if (b.get_direction() == Gtk.TextDirection.RTL)
-                b.image.margin_left += 4;
-            else
-                b.image.margin_right += 4;
+            b.label = null;
     }
     
     /**
@@ -69,65 +73,21 @@ public class PillToolbar : Gtk.Toolbar {
      * toolbar.  Optionally adds spacers "before" and "after" the buttons (those terms depending
      * on Gtk.TextDirection)
      */
-    public Gtk.ToolItem create_pill_buttons(Gee.Collection<Gtk.Button> buttons,
+    public Gtk.Box create_pill_buttons(Gee.Collection<Gtk.Button> buttons,
         bool before_spacer = true, bool after_spacer = false) {
         Gtk.Box box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
+        box.valign = Gtk.Align.CENTER;
+        box.halign = Gtk.Align.CENTER;
         
         if (buttons.size > 1) {
             box.get_style_context().add_class(Gtk.STYLE_CLASS_RAISED);
             box.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED);
         }
         
-        int i = 0;
-        foreach(Gtk.Button button in buttons) {
+        foreach(Gtk.Button button in buttons)
             box.add(button);
-            
-            // Place the right spacer on the button itself.  This way if the button is not displayed,
-            // the spacer will not appear.
-            if (i == buttons.size - 1 && after_spacer) {
-                if (button.get_direction() == Gtk.TextDirection.RTL)
-                    button.set_margin_left(12);
-                else
-                    button.set_margin_right(12);
-            }
-            
-            i++;
-        }
-        
-        Gtk.ToolItem tool_item = new Gtk.ToolItem();
-        tool_item.add(box);
-        
-        if (before_spacer) {
-            if (box.get_direction() == Gtk.TextDirection.RTL)
-                box.set_margin_right(12);
-            else
-                box.set_margin_left(12);
-        }
-        
-        return tool_item;
-    }
-    
-    /**
-     * Computes the margin for each icon (shamelessly stolen from Nautilus.)
-     */
-    public int get_icon_margin() {
-        Gtk.IconSize toolbar_size = get_icon_size();
-        int toolbar_size_px, menu_size_px;
-        
-        Gtk.icon_size_lookup(Gtk.IconSize.MENU, out menu_size_px, null);
-        Gtk.icon_size_lookup(toolbar_size, out toolbar_size_px, null);
-        
-        return Geary.Numeric.int_floor((int) ((toolbar_size_px - menu_size_px) / 2.0), 0);
-    }
-    
-    /**
-     * Returns an expandable spacer item.
-     */
-    public Gtk.ToolItem create_spacer() {
-        Gtk.ToolItem spacer = new Gtk.ToolItem();
-        spacer.set_expand(true);
-        
-        return spacer;
+                
+        return box;
     }
 }
 
diff --git a/src/client/composer/composer-toolbar.vala b/src/client/composer/composer-toolbar.vala
index 543621e..845dc5a 100644
--- a/src/client/composer/composer-toolbar.vala
+++ b/src/client/composer/composer-toolbar.vala
@@ -15,32 +15,28 @@ public class ComposerToolbar : PillToolbar {
         insert.add(create_toggle_button(null, ComposerWindow.ACTION_ITALIC));
         insert.add(create_toggle_button(null, ComposerWindow.ACTION_UNDERLINE));
         insert.add(create_toggle_button(null, ComposerWindow.ACTION_STRIKETHROUGH));
-        Gtk.ToolItem font_format_item = create_pill_buttons(insert, false, true);
-        add(font_format_item);
+        add_start(create_pill_buttons(insert, false, true));
         
         // Indent level.
         insert.clear();
         insert.add(create_toolbar_button(null, ComposerWindow.ACTION_INDENT));
         insert.add(create_toolbar_button(null, ComposerWindow.ACTION_OUTDENT));
-        add(create_pill_buttons(insert, false));
+        add_start(create_pill_buttons(insert, false));
         
         // Link.
         insert.clear();
         insert.add(create_toolbar_button(null, ComposerWindow.ACTION_INSERT_LINK));
-        add(create_pill_buttons(insert));
+        add_start(create_pill_buttons(insert));
         
         // Remove formatting.
         insert.clear();
         insert.add(create_toolbar_button(null, ComposerWindow.ACTION_REMOVE_FORMAT));
-        add(create_pill_buttons(insert));
-        
-        // Spacer.
-        add(create_spacer());
+        add_start(create_pill_buttons(insert));
         
         // Menu.
         insert.clear();
         insert.add(create_menu_button(null, menu, ComposerWindow.ACTION_MENU));
-        add(create_pill_buttons(insert));
+        add_end(create_pill_buttons(insert));
     }
 }
 


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