Give every web view a separate user content manager

This commit is contained in:
Gergely Polonkai 2014-09-05 00:22:04 +02:00
parent 3445830a6d
commit 944d623379
3 changed files with 86 additions and 40 deletions

View File

@ -8,11 +8,7 @@
#include "config.h"
#include "astrognome.h"
typedef struct _AgAppPrivate {
WebKitUserContentManager *manager;
} AgAppPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(AgApp, ag_app, GTK_TYPE_APPLICATION);
G_DEFINE_TYPE(AgApp, ag_app, GTK_TYPE_APPLICATION);
GtkWindow *
ag_app_peek_first_window(AgApp *app)
@ -56,9 +52,8 @@ static GtkWidget *
ag_app_create_window(AgApp *app)
{
GtkWidget *window;
AgAppPrivate *priv = ag_app_get_instance_private(app);
window = ag_window_new(app, priv->manager);
window = ag_window_new(app);
gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW(window));
gtk_widget_show_all(window);
@ -432,33 +427,6 @@ ag_app_new(void)
static void
ag_app_init(AgApp *app)
{
AgAppPrivate *priv;
GBytes *css_data;
const gchar *css_source;
gsize css_length;
WebKitUserStyleSheet *stylesheet;
priv = ag_app_get_instance_private(app);
priv->manager = webkit_user_content_manager_new();
css_data = g_resources_lookup_data(
"/eu/polonkai/gergely/Astrognome/ui/chart-default.css",
G_RESOURCE_LOOKUP_FLAGS_NONE,
NULL
);
if ((css_source = g_bytes_get_data(css_data, &css_length)) != NULL) {
stylesheet = webkit_user_style_sheet_new(
css_source,
WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
WEBKIT_USER_STYLE_LEVEL_USER,
NULL, NULL
);
webkit_user_content_manager_add_style_sheet(priv->manager, stylesheet);
webkit_user_style_sheet_unref(stylesheet);
}
g_bytes_unref(css_data);
}
static void

View File

@ -62,6 +62,7 @@ struct _AgWindowPrivate {
GtkEntryCompletion *city_comp;
gchar *selected_country;
gchar *selected_city;
GList *style_sheets;
};
struct cc_search {
@ -2123,15 +2124,93 @@ ag_window_configure_event_cb(GtkWidget *widget,
return FALSE;
}
static void
ag_window_add_style_sheet(AgWindow *window, const gchar *path)
{
gchar *css_source;
gboolean source_free = FALSE;
AgWindowPrivate *priv = ag_window_get_instance_private(window);
if (strncmp("gres://", path, 7) == 0) {
gchar *res_path = g_strdup_printf(
"/eu/polonkai/gergely/Astrognome/%s",
path + 7
);
GBytes *css_data = g_resources_lookup_data(
res_path,
G_RESOURCE_LOOKUP_FLAGS_NONE,
NULL
);
css_source = g_strdup(g_bytes_get_data(css_data, NULL));
source_free = TRUE;
g_bytes_unref(css_data);
} else if (strncmp("raw:", path, 4) == 0) {
css_source = (gchar *)path + 4;
} else {
GFile *css_file = g_file_new_for_uri(path);
GError *err = NULL;
g_file_load_contents(
css_file,
NULL,
&css_source, NULL,
NULL,
&err
);
source_free = TRUE;
g_object_unref(css_file);
}
if (css_source) {
WebKitUserStyleSheet *style_sheet = webkit_user_style_sheet_new(
css_source,
WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
WEBKIT_USER_STYLE_LEVEL_USER,
NULL, NULL
);
priv->style_sheets = g_list_append(priv->style_sheets, style_sheet);
if (source_free) {
g_free(css_source);
}
}
}
static void
ag_window_update_style_sheets(AgWindow *window)
{
GList *item;
AgWindowPrivate *priv = ag_window_get_instance_private(window);
WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager(
WEBKIT_WEB_VIEW(priv->chart_web_view)
);
webkit_user_content_manager_remove_all_style_sheets(manager);
for (item = priv->style_sheets; item; item = g_list_next(item)) {
WebKitUserStyleSheet *style_sheet = item->data;
webkit_user_content_manager_add_style_sheet(manager, style_sheet);
}
}
GtkWidget *
ag_window_new(AgApp *app, WebKitUserContentManager *manager)
ag_window_new(AgApp *app)
{
AgWindow *window = g_object_new(AG_TYPE_WINDOW, NULL);
AgWindowPrivate *priv = ag_window_get_instance_private(window);
WebKitUserContentManager *manager = webkit_user_content_manager_new();
priv->chart_web_view = webkit_web_view_new_with_user_content_manager(
manager
);
ag_window_add_style_sheet(
window,
"gres://ui/chart-default.css"
);
ag_window_update_style_sheets(window);
gtk_box_pack_end(
GTK_BOX(priv->tab_chart),
priv->chart_web_view,

View File

@ -2,7 +2,6 @@
#define __AG_WINDOW_H__
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>
#include "ag-app.h"
#include "ag-chart.h"
@ -41,7 +40,7 @@ struct _AgWindowClass {
GType ag_window_get_type(void) G_GNUC_CONST;
GtkWidget *ag_window_new(AgApp *app, WebKitUserContentManager *manager);
GtkWidget *ag_window_new(AgApp *app);
void ag_window_set_chart(AgWindow *window,
AgChart *chart);