[devhelp] Window: init headerbar with code, not in the *.ui file



commit 4a682a298e8bd555680101e1f58f47f216ba3fbe
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jun 8 15:58:39 2018 +0200

    Window: init headerbar with code, not in the *.ui file
    
    The *.ui file gets in the way for code re-usability, to move the code in
    the libdevhelp.
    
    This also has the advantage that fewer lines are needed.

 src/dh-window.c  |  50 ++++++++++++++++++++++++---
 src/dh-window.ui | 102 -------------------------------------------------------
 2 files changed, 46 insertions(+), 106 deletions(-)
---
diff --git a/src/dh-window.c b/src/dh-window.c
index bb543ab9..37d44ffd 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -68,6 +68,8 @@ dh_window_dispose (GObject *object)
 {
         DhWindowPrivate *priv = dh_window_get_instance_private (DH_WINDOW (object));
 
+        priv->header_bar = NULL;
+        priv->window_menu_button = NULL;
         priv->sidebar = NULL;
         priv->search_bar = NULL;
         priv->notebook = NULL;
@@ -87,8 +89,6 @@ dh_window_class_init (DhWindowClass *klass)
 
         /* Bind class to template */
         gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/devhelp/dh-window.ui");
-        gtk_widget_class_bind_template_child_private (widget_class, DhWindow, header_bar);
-        gtk_widget_class_bind_template_child_private (widget_class, DhWindow, window_menu_button);
         gtk_widget_class_bind_template_child_private (widget_class, DhWindow, hpaned);
         gtk_widget_class_bind_template_child_private (widget_class, DhWindow, grid_sidebar);
         gtk_widget_class_bind_template_child_private (widget_class, DhWindow, grid_documents);
@@ -558,11 +558,44 @@ create_window_menu_plus_app_menu (void)
 }
 
 static void
-set_window_menu (DhWindow *window)
+init_header_bar (DhWindow *window)
 {
         DhWindowPrivate *priv = dh_window_get_instance_private (window);
+        GtkWidget *back_forward_hgrid;
+        GtkStyleContext *style_context;
+        GtkWidget *back_button;
+        GtkWidget *forward_button;
         GtkApplication *app;
         GMenuModel *window_menu;
+        GtkWidget *new_tab_button;
+
+        g_assert (priv->header_bar == NULL);
+        g_assert (priv->window_menu_button == NULL);
+
+        priv->header_bar = GTK_HEADER_BAR (gtk_header_bar_new ());
+        gtk_header_bar_set_show_close_button (priv->header_bar, TRUE);
+
+        /* Back/forward buttons */
+        back_forward_hgrid = gtk_grid_new ();
+        style_context = gtk_widget_get_style_context (back_forward_hgrid);
+        gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_LINKED);
+
+        back_button = gtk_button_new_from_icon_name ("go-previous-symbolic", GTK_ICON_SIZE_BUTTON);
+        gtk_actionable_set_action_name (GTK_ACTIONABLE (back_button), "win.go-back");
+        gtk_widget_set_tooltip_text (back_button, _("Back"));
+
+        forward_button = gtk_button_new_from_icon_name ("go-next-symbolic", GTK_ICON_SIZE_BUTTON);
+        gtk_actionable_set_action_name (GTK_ACTIONABLE (forward_button), "win.go-forward");
+        gtk_widget_set_tooltip_text (forward_button, _("Forward"));
+
+        gtk_container_add (GTK_CONTAINER (back_forward_hgrid), back_button);
+        gtk_container_add (GTK_CONTAINER (back_forward_hgrid), forward_button);
+        gtk_header_bar_pack_start (priv->header_bar, back_forward_hgrid);
+
+        /* Menu */
+        priv->window_menu_button = GTK_MENU_BUTTON (gtk_menu_button_new ());
+        gtk_menu_button_set_direction (priv->window_menu_button, GTK_ARROW_NONE);
+        gtk_header_bar_pack_end (priv->header_bar, GTK_WIDGET (priv->window_menu_button));
 
         app = GTK_APPLICATION (g_application_get_default ());
         if (gtk_application_prefers_app_menu (app))
@@ -572,6 +605,14 @@ set_window_menu (DhWindow *window)
 
         gtk_menu_button_set_menu_model (priv->window_menu_button, window_menu);
         g_object_unref (window_menu);
+
+        /* New tab button */
+        new_tab_button = gtk_button_new_from_icon_name ("tab-new-symbolic", GTK_ICON_SIZE_BUTTON);
+        gtk_actionable_set_action_name (GTK_ACTIONABLE (new_tab_button), "win.new-tab");
+        gtk_widget_set_tooltip_text (new_tab_button, _("New Tab"));
+        gtk_header_bar_pack_end (priv->header_bar, new_tab_button);
+
+        gtk_widget_show_all (GTK_WIDGET (priv->header_bar));
 }
 
 static void
@@ -655,7 +696,8 @@ dh_window_init (DhWindow *window)
 
         gtk_widget_init_template (GTK_WIDGET (window));
 
-        set_window_menu (window);
+        init_header_bar (window);
+        gtk_window_set_titlebar (GTK_WINDOW (window), GTK_WIDGET (priv->header_bar));
 
         settings = dh_settings_app_get_singleton ();
         paned_settings = dh_settings_app_peek_paned_settings (settings);
diff --git a/src/dh-window.ui b/src/dh-window.ui
index fdada16f..12b179e4 100644
--- a/src/dh-window.ui
+++ b/src/dh-window.ui
@@ -5,108 +5,6 @@
     <property name="can_focus">False</property>
     <property name="has_focus">False</property>
     <property name="is_focus">False</property>
-    <child type="titlebar">
-      <object class="GtkHeaderBar" id="header_bar">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="vexpand">False</property>
-        <property name="show_close_button">True</property>
-        <child>
-          <object class="GtkBox" id="box">
-            <property name="visible">True</property>
-            <property name="valign">center</property>
-            <property name="can_focus">False</property>
-            <style>
-              <class name="linked"/>
-            </style>
-            <child>
-              <object class="GtkButton" id="back_button">
-                <property name="visible">True</property>
-                <property name="valign">center</property>
-                <property name="can_focus">False</property>
-                <property name="tooltip_text" translatable="yes">Back</property>
-                <property name="action_name">win.go-back</property>
-                <style>
-                  <class name="image-button"/>
-                </style>
-                <child>
-                  <object class="GtkImage" id="back_button_image">
-                    <property name="visible">True</property>
-                    <property name="icon_size">1</property>
-                    <property name="icon_name">go-previous-symbolic</property>
-                  </object>
-                </child>
-              </object>
-            </child>
-            <child>
-              <object class="GtkButton" id="forward_button">
-                <property name="visible">True</property>
-                <property name="valign">center</property>
-                <property name="can_focus">False</property>
-                <property name="tooltip_text" translatable="yes">Forward</property>
-                <property name="action_name">win.go-forward</property>
-                <style>
-                  <class name="image-button"/>
-                </style>
-                <child>
-                  <object class="GtkImage" id="forward_button_image">
-                    <property name="visible">True</property>
-                    <property name="icon_size">1</property>
-                    <property name="icon_name">go-next-symbolic</property>
-                  </object>
-                </child>
-              </object>
-            </child>
-          </object>
-          <packing>
-            <property name="pack_type">start</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GtkMenuButton" id="window_menu_button">
-            <property name="visible">True</property>
-            <property name="valign">center</property>
-            <property name="can_focus">False</property>
-            <property name="use_popover">True</property>
-            <style>
-              <class name="image-button"/>
-            </style>
-            <child>
-              <object class="GtkImage" id="window_menu_image">
-                <property name="visible">True</property>
-                <property name="icon_size">1</property>
-                <property name="icon_name">open-menu-symbolic</property>
-              </object>
-            </child>
-          </object>
-          <packing>
-            <property name="pack_type">end</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GtkButton" id="new_tab_button">
-            <property name="visible">True</property>
-            <property name="valign">center</property>
-            <property name="can_focus">False</property>
-            <property name="tooltip_text" translatable="yes">New Tab</property>
-            <property name="action_name">win.new-tab</property>
-            <style>
-              <class name="image-button"/>
-            </style>
-            <child>
-              <object class="GtkImage" id="new_tab_button_image">
-                <property name="visible">True</property>
-                <property name="icon_size">1</property>
-                <property name="icon_name">tab-new-symbolic</property>
-              </object>
-            </child>
-          </object>
-          <packing>
-            <property name="pack_type">end</property>
-          </packing>
-        </child>
-      </object>
-    </child>
     <child>
       <object class="GtkGrid" id="grid">
         <property name="visible">True</property>


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