[devhelp] Window: delegate InfoBar handling to DhTab, and use TeplInfoBar



commit 938fb8286d8ae1912606b41e67d1ded26a11f89c
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Feb 10 15:52:20 2018 +0100

    Window: delegate InfoBar handling to DhTab, and use TeplInfoBar
    
    TeplInfoBar is so much nicer, it simplifies a lot the code.
    
    Also, create each time a new TeplInfoBar widget, do not hide and re-use
    the same. To ensure that the previous content is fully destroyed.
    
    Destroy the info bar when a new page is loaded in the WebView, the code
    was broken in DhWindow, there was the following bug: first error -> info
    bar shown (OK) -> click on close button -> trigger another error -> info
    bar *not* shown… Now it works fine.

 src/dh-tab.c    |   85 ++++++++++++++++++++++++++++++++++++++++---------------
 src/dh-tab.h    |    2 -
 src/dh-window.c |   56 ------------------------------------
 3 files changed, 62 insertions(+), 81 deletions(-)
---
diff --git a/src/dh-tab.c b/src/dh-tab.c
index 6ec7f41..80aee2d 100644
--- a/src/dh-tab.c
+++ b/src/dh-tab.c
@@ -17,9 +17,11 @@
  */
 
 #include "dh-tab.h"
+#include <glib/gi18n.h>
+#include "tepl-info-bar.h"
 
 struct _DhTabPrivate {
-        GtkInfoBar *info_bar;
+        TeplInfoBar *info_bar;
         DhWebView *web_view;
 };
 
@@ -44,6 +46,55 @@ dh_tab_class_init (DhTabClass *klass)
         object_class->dispose = dh_tab_dispose;
 }
 
+static gboolean
+web_view_load_failed_cb (WebKitWebView   *web_view,
+                         WebKitLoadEvent  load_event,
+                         const gchar     *failing_uri,
+                         GError          *error,
+                         DhTab           *tab)
+{
+        /* Ignore cancellation errors, which happen when typing fast in the
+         * search entry.
+         */
+        if (g_error_matches (error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED))
+                return GDK_EVENT_STOP;
+
+        if (tab->priv->info_bar != NULL)
+                gtk_widget_destroy (GTK_WIDGET (tab->priv->info_bar));
+
+        tab->priv->info_bar = tepl_info_bar_new_simple (GTK_MESSAGE_ERROR,
+                                                        _("Error opening the requested link."),
+                                                        error->message);
+        tepl_info_bar_add_close_button (tab->priv->info_bar);
+
+        g_signal_connect (tab->priv->info_bar,
+                          "destroy",
+                          G_CALLBACK (gtk_widget_destroyed),
+                          &tab->priv->info_bar);
+
+        gtk_grid_attach_next_to (GTK_GRID (tab),
+                                 GTK_WIDGET (tab->priv->info_bar),
+                                 GTK_WIDGET (tab->priv->web_view),
+                                 GTK_POS_TOP,
+                                 1, 1);
+
+        gtk_widget_show (GTK_WIDGET (tab->priv->info_bar));
+
+        return GDK_EVENT_STOP;
+}
+
+static void
+web_view_load_changed_cb (WebKitWebView   *web_view,
+                          WebKitLoadEvent  load_event,
+                          DhTab           *tab)
+{
+        if (load_event == WEBKIT_LOAD_STARTED &&
+            tab->priv->info_bar != NULL) {
+                /* The error is no longer relevant. */
+                gtk_widget_destroy (GTK_WIDGET (tab->priv->info_bar));
+        }
+}
+
 static void
 dh_tab_init (DhTab *tab)
 {
@@ -51,23 +102,19 @@ dh_tab_init (DhTab *tab)
 
         gtk_orientable_set_orientation (GTK_ORIENTABLE (tab), GTK_ORIENTATION_VERTICAL);
 
-        /* GtkInfoBar */
-        tab->priv->info_bar = GTK_INFO_BAR (gtk_info_bar_new ());
-        gtk_widget_set_no_show_all (GTK_WIDGET (tab->priv->info_bar), TRUE);
-        gtk_info_bar_set_show_close_button (tab->priv->info_bar, TRUE);
-        gtk_info_bar_set_message_type (tab->priv->info_bar, GTK_MESSAGE_ERROR);
-
-        g_signal_connect (tab->priv->info_bar,
-                          "response",
-                          G_CALLBACK (gtk_widget_hide),
-                          NULL);
-
-        /* DhWebView */
         tab->priv->web_view = dh_web_view_new ();
         gtk_widget_show (GTK_WIDGET (tab->priv->web_view));
-
-        gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (tab->priv->info_bar));
         gtk_container_add (GTK_CONTAINER (tab), GTK_WIDGET (tab->priv->web_view));
+
+        g_signal_connect (tab->priv->web_view,
+                          "load-failed",
+                          G_CALLBACK (web_view_load_failed_cb),
+                          tab);
+
+        g_signal_connect (tab->priv->web_view,
+                          "load-changed",
+                          G_CALLBACK (web_view_load_changed_cb),
+                          tab);
 }
 
 DhTab *
@@ -83,11 +130,3 @@ dh_tab_get_web_view (DhTab *tab)
 
         return tab->priv->web_view;
 }
-
-GtkInfoBar *
-dh_tab_get_info_bar (DhTab *tab)
-{
-        g_return_val_if_fail (DH_IS_TAB (tab), NULL);
-
-        return tab->priv->info_bar;
-}
diff --git a/src/dh-tab.h b/src/dh-tab.h
index f7d472e..c9f1009 100644
--- a/src/dh-tab.h
+++ b/src/dh-tab.h
@@ -54,8 +54,6 @@ DhTab *         dh_tab_new              (void);
 
 DhWebView *     dh_tab_get_web_view     (DhTab *tab);
 
-GtkInfoBar *    dh_tab_get_info_bar     (DhTab *tab);
-
 G_END_DECLS
 
 #endif /* DH_TAB_H */
diff --git a/src/dh-window.c b/src/dh-window.c
index 6bb3651..269334d 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -134,15 +134,6 @@ get_active_web_view (DhWindow *window)
         return tab != NULL ? dh_tab_get_web_view (tab) : NULL;
 }
 
-static GtkWidget *
-window_get_active_info_bar (DhWindow *window)
-{
-        DhTab *tab;
-
-        tab = get_active_tab (window);
-        return tab != NULL ? GTK_WIDGET (dh_tab_get_info_bar (tab)) : NULL;
-}
-
 static void
 update_window_title (DhWindow *window)
 {
@@ -802,9 +793,6 @@ window_web_view_decide_policy_cb (WebKitWebView            *web_view,
         navigation_action = webkit_navigation_policy_decision_get_navigation_action (navigation_decision);
         uri = webkit_uri_request_get_uri (webkit_navigation_action_get_request (navigation_action));
 
-        /* make sure to hide the info bar on page change */
-        gtk_widget_hide (window_get_active_info_bar (window));
-
         /* middle click or ctrl-click -> new tab */
         button = webkit_navigation_action_get_mouse_button (navigation_action);
         state = webkit_navigation_action_get_modifiers (navigation_action);
@@ -850,45 +838,6 @@ web_view_load_changed_cb (WebKitWebView   *web_view,
         }
 }
 
-static gboolean
-web_view_load_failed_cb (WebKitWebView   *web_view,
-                         WebKitLoadEvent  load_event,
-                         const gchar     *failing_uri,
-                         GError          *error,
-                         DhTab           *tab)
-{
-        GtkInfoBar *info_bar;
-        GtkWidget *content_area;
-        GtkWidget *message_label;
-        GList *children;
-        gchar *markup;
-
-        /* Ignore cancellation errors, which happen when typing fast in the
-         * search entry.
-         */
-        if (g_error_matches (error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED))
-                return GDK_EVENT_STOP;
-
-        info_bar = dh_tab_get_info_bar (tab);
-        markup = g_strdup_printf ("<b>%s</b>", _("Error opening the requested link."));
-        message_label = gtk_label_new (markup);
-        gtk_label_set_xalign (GTK_LABEL (message_label), 0.0);
-        gtk_label_set_use_markup (GTK_LABEL (message_label), TRUE);
-        content_area = gtk_info_bar_get_content_area (info_bar);
-        children = gtk_container_get_children (GTK_CONTAINER (content_area));
-        if (children != NULL) {
-                gtk_container_remove (GTK_CONTAINER (content_area), children->data);
-                g_list_free (children);
-        }
-        gtk_container_add (GTK_CONTAINER (content_area), message_label);
-        gtk_widget_show (message_label);
-
-        gtk_widget_show (GTK_WIDGET (info_bar));
-        g_free (markup);
-
-        return GDK_EVENT_STOP;
-}
-
 static void
 window_web_view_title_changed_cb (DhWebView  *web_view,
                                   GParamSpec *param_spec,
@@ -990,11 +939,6 @@ window_open_new_tab (DhWindow    *window,
                           G_CALLBACK (web_view_load_changed_cb),
                           window);
 
-        g_signal_connect (web_view,
-                          "load-failed",
-                          G_CALLBACK (web_view_load_failed_cb),
-                          tab);
-
         back_forward_list = webkit_web_view_get_back_forward_list (WEBKIT_WEB_VIEW (web_view));
         g_signal_connect_object (back_forward_list,
                                  "changed",


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