[devhelp] Window: factor out dh_util_bind_sidebar_and_notebook()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp] Window: factor out dh_util_bind_sidebar_and_notebook()
- Date: Sun, 3 Jun 2018 17:24:26 +0000 (UTC)
commit 5e3a47980bf8a65dd547120d6e238fc573c399df
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sun Jun 3 17:57:05 2018 +0200
Window: factor out dh_util_bind_sidebar_and_notebook()
It will be useful to have that code separated, for the libdevhelp, to
have something more flexible. An IDE may want to have several pairs of
DhSidebar+DhNotebook in a single window (to show different profiles).
As a public API, dh_util is not great. But nothing prevents us from
adding a dh_window_bind_sidebar_and_notebook() public function (without
taking a self param), which internally calls dh_util_blabla.
src/dh-util-app.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/dh-util-app.h | 13 ++++--
src/dh-window.c | 65 +--------------------------
3 files changed, 141 insertions(+), 67 deletions(-)
---
diff --git a/src/dh-util-app.c b/src/dh-util-app.c
index d129cfb4..500a9265 100644
--- a/src/dh-util-app.c
+++ b/src/dh-util-app.c
@@ -125,3 +125,133 @@ dh_util_window_settings_restore (GtkWindow *gtk_window,
if (has_maximized_key && g_settings_get_boolean (settings, "maximized"))
gtk_window_maximize (gtk_window);
}
+
+static void
+sidebar_link_selected_cb (DhSidebar *sidebar,
+ DhLink *link,
+ DhNotebook *notebook)
+{
+ gchar *uri;
+ DhWebView *web_view;
+
+ uri = dh_link_get_uri (link);
+ if (uri == NULL)
+ return;
+
+ web_view = dh_notebook_get_active_web_view (notebook);
+ if (web_view != NULL)
+ webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), uri);
+
+ g_free (uri);
+}
+
+static void
+sync_active_web_view_uri_to_sidebar (DhNotebook *notebook,
+ DhSidebar *sidebar)
+{
+ DhWebView *web_view;
+ const gchar *uri = NULL;
+
+ g_signal_handlers_block_by_func (sidebar,
+ sidebar_link_selected_cb,
+ notebook);
+
+ web_view = dh_notebook_get_active_web_view (notebook);
+ if (web_view != NULL)
+ uri = webkit_web_view_get_uri (WEBKIT_WEB_VIEW (web_view));
+ if (uri != NULL)
+ dh_sidebar_select_uri (sidebar, uri);
+
+ g_signal_handlers_unblock_by_func (sidebar,
+ sidebar_link_selected_cb,
+ notebook);
+}
+
+static DhNotebook *
+get_notebook_containing_web_view (DhWebView *web_view)
+{
+ GtkWidget *widget;
+
+ widget = GTK_WIDGET (web_view);
+
+ while (widget != NULL) {
+ widget = gtk_widget_get_parent (widget);
+
+ if (DH_IS_NOTEBOOK (widget))
+ return DH_NOTEBOOK (widget);
+ }
+
+ g_return_val_if_reached (NULL);
+}
+
+static void
+web_view_load_changed_cb (DhWebView *web_view,
+ WebKitLoadEvent load_event,
+ DhSidebar *sidebar)
+{
+ DhNotebook *notebook;
+
+ notebook = get_notebook_containing_web_view (web_view);
+
+ if (load_event == WEBKIT_LOAD_COMMITTED &&
+ web_view == dh_notebook_get_active_web_view (notebook)) {
+ sync_active_web_view_uri_to_sidebar (notebook, sidebar);
+ }
+}
+
+static void
+notebook_page_added_after_cb (GtkNotebook *notebook,
+ GtkWidget *child,
+ guint page_num,
+ DhSidebar *sidebar)
+{
+ DhTab *tab;
+ DhWebView *web_view;
+
+ g_return_if_fail (DH_IS_TAB (child));
+
+ tab = DH_TAB (child);
+ web_view = dh_tab_get_web_view (tab);
+
+ g_signal_connect_object (web_view,
+ "load-changed",
+ G_CALLBACK (web_view_load_changed_cb),
+ sidebar,
+ 0);
+}
+
+static void
+notebook_switch_page_after_cb (DhNotebook *notebook,
+ GtkWidget *new_page,
+ guint new_page_num,
+ DhSidebar *sidebar)
+{
+ sync_active_web_view_uri_to_sidebar (notebook, sidebar);
+}
+
+void
+dh_util_bind_sidebar_and_notebook (DhSidebar *sidebar,
+ DhNotebook *notebook)
+{
+ g_return_if_fail (DH_IS_SIDEBAR (sidebar));
+ g_return_if_fail (DH_IS_NOTEBOOK (notebook));
+ g_return_if_fail (dh_notebook_get_active_tab (notebook) == NULL);
+
+ g_signal_connect_object (sidebar,
+ "link-selected",
+ G_CALLBACK (sidebar_link_selected_cb),
+ notebook,
+ 0);
+
+ g_signal_connect_object (notebook,
+ "page-added",
+ G_CALLBACK (notebook_page_added_after_cb),
+ sidebar,
+ G_CONNECT_AFTER);
+
+ g_signal_connect_object (notebook,
+ "switch-page",
+ G_CALLBACK (notebook_switch_page_after_cb),
+ sidebar,
+ G_CONNECT_AFTER);
+}
diff --git a/src/dh-util-app.h b/src/dh-util-app.h
index 3a4f3764..533b558b 100644
--- a/src/dh-util-app.h
+++ b/src/dh-util-app.h
@@ -24,14 +24,19 @@
#define DH_UTIL_APP_H
#include <gtk/gtk.h>
+#include <devhelp/devhelp.h>
+#include "dh-notebook.h"
G_BEGIN_DECLS
-void dh_util_window_settings_save (GtkWindow *window,
- GSettings *settings);
+void dh_util_window_settings_save (GtkWindow *window,
+ GSettings *settings);
-void dh_util_window_settings_restore (GtkWindow *gtk_window,
- GSettings *settings);
+void dh_util_window_settings_restore (GtkWindow *gtk_window,
+ GSettings *settings);
+
+void dh_util_bind_sidebar_and_notebook (DhSidebar *sidebar,
+ DhNotebook *notebook);
G_END_DECLS
diff --git a/src/dh-window.c b/src/dh-window.c
index df39739a..378b87a4 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -578,47 +578,6 @@ set_window_menu (DhWindow *window)
g_object_unref (window_menu);
}
-static void
-sidebar_link_selected_cb (DhSidebar *sidebar,
- DhLink *link,
- DhWindow *window)
-{
- gchar *uri;
- DhWebView *web_view;
-
- uri = dh_link_get_uri (link);
- if (uri == NULL)
- return;
-
- web_view = get_active_web_view (window);
- if (web_view != NULL)
- webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), uri);
-
- g_free (uri);
-}
-
-static void
-sync_active_web_view_uri_to_sidebar (DhWindow *window)
-{
- DhWindowPrivate *priv = dh_window_get_instance_private (window);
- DhWebView *web_view;
- const gchar *uri = NULL;
-
- g_signal_handlers_block_by_func (priv->sidebar,
- sidebar_link_selected_cb,
- window);
-
- web_view = get_active_web_view (window);
- if (web_view != NULL)
- uri = webkit_web_view_get_uri (WEBKIT_WEB_VIEW (web_view));
- if (uri != NULL)
- dh_sidebar_select_uri (priv->sidebar, uri);
-
- g_signal_handlers_unblock_by_func (priv->sidebar,
- sidebar_link_selected_cb,
- window);
-}
-
static void
web_view_title_notify_cb (DhWebView *web_view,
GParamSpec *param_spec,
@@ -637,17 +596,6 @@ web_view_zoom_level_notify_cb (DhWebView *web_view,
update_zoom_actions_sensitivity (window);
}
-static void
-web_view_load_changed_cb (DhWebView *web_view,
- WebKitLoadEvent load_event,
- DhWindow *window)
-{
- if (load_event == WEBKIT_LOAD_COMMITTED &&
- web_view == get_active_web_view (window)) {
- sync_active_web_view_uri_to_sidebar (window);
- }
-}
-
static void
notebook_page_added_after_cb (GtkNotebook *notebook,
GtkWidget *child,
@@ -673,11 +621,6 @@ notebook_page_added_after_cb (GtkNotebook *notebook,
G_CALLBACK (web_view_zoom_level_notify_cb),
window);
- g_signal_connect (web_view,
- "load-changed",
- G_CALLBACK (web_view_load_changed_cb),
- window);
-
back_forward_list = webkit_web_view_get_back_forward_list (WEBKIT_WEB_VIEW (web_view));
g_signal_connect_object (back_forward_list,
"changed",
@@ -705,7 +648,6 @@ notebook_switch_page_after_cb (GtkNotebook *notebook,
update_window_title (window);
update_zoom_actions_sensitivity (window);
update_back_forward_actions_sensitivity (window);
- sync_active_web_view_uri_to_sidebar (window);
}
static void
@@ -732,15 +674,12 @@ dh_window_init (DhWindow *window)
gtk_container_add (GTK_CONTAINER (priv->grid_sidebar),
GTK_WIDGET (priv->sidebar));
- g_signal_connect (priv->sidebar,
- "link-selected",
- G_CALLBACK (sidebar_link_selected_cb),
- window);
-
/* HTML tabs GtkNotebook */
priv->notebook = dh_notebook_new ();
gtk_widget_show (GTK_WIDGET (priv->notebook));
+ dh_util_bind_sidebar_and_notebook (priv->sidebar, priv->notebook);
+
g_signal_connect_after (priv->notebook,
"page-added",
G_CALLBACK (notebook_page_added_after_cb),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]