Load a default app script into ClientWebViews at construction time.

* src/client/application/geary-controller.vala
  (GearyController::open_async): Load the app script for ClientWebView at
  startup.

* src/client/components/client-web-view.vala
  (ClientWebView::ClientWebView): Ensure we actually have a
  UserContentManager instance to work with, add the app script to the
  manager for the instance.
  (ClientWebView::load_scripts): Actually load client-web-view.js.
  (ClientWebView::load_user_stylesheet): Helper method for this class and
  subsclasses for doing the actual script load from the app.

* ui/client-web-view.js: Add boilerplaye for new file.

* ui/CMakeLists.txt: Include client-web-view.js.
This commit is contained in:
Michael James Gratton 2016-11-23 23:48:47 +11:00
parent adf912faae
commit 09c9a398f7
4 changed files with 39 additions and 4 deletions

View file

@ -207,11 +207,12 @@ public class GearyController : Geary.BaseObject {
);
});
// Load web view stylesheets
// Load web view resources
try {
ClientWebView.load_scripts(this.application);
ConversationWebView.load_stylehseets(this.application);
} catch (Error err) {
error("Error loading application CSS: %s", err.message);
error("Error loading web resources: %s", err.message);
}
// Use a global avatar session because a cache must be used

View file

@ -1,5 +1,6 @@
/*
* Copyright 2016 Software Freedom Conservancy Inc.
* Copyright 2016 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
@ -13,6 +14,14 @@ public class ClientWebView : WebKit.WebView {
private const double ZOOM_DEFAULT = 1.0;
private const double ZOOM_FACTOR = 0.1;
private static WebKit.UserScript? script = null;
public static void load_scripts(GearyApplication app)
throws Error {
ClientWebView.script = load_app_script(app, "client-web-view.js");
}
/** Loads an application-specific WebKit stylesheet. */
protected static WebKit.UserStyleSheet load_app_stylesheet(GearyApplication app,
string name)
throws Error {
@ -25,6 +34,7 @@ public class ClientWebView : WebKit.WebView {
);
}
/** Loads a user stylesheet, if any. */
protected static WebKit.UserStyleSheet? load_user_stylesheet(GearyApplication app,
string name) {
File stylesheet = app.get_user_config_directory().get_child(name);
@ -47,6 +57,19 @@ public class ClientWebView : WebKit.WebView {
return user_stylesheet;
}
/** Loads an application-specific WebKit JavaScript script. */
protected static WebKit.UserScript load_app_script(GearyApplication app,
string name)
throws Error {
return new WebKit.UserScript(
app.read_resource(name),
WebKit.UserContentInjectedFrames.TOP_FRAME,
WebKit.UserScriptInjectionTime.END,
null,
null
);
}
private static inline uint to_wk2_font_size(Pango.FontDescription font) {
Gdk.Screen? screen = Gdk.Screen.get_default();
double dpi = screen != null ? screen.get_resolution() : 96.0;
@ -102,7 +125,7 @@ public class ClientWebView : WebKit.WebView {
public signal void inline_resource_loaded(string cid);
public ClientWebView(WebKit.UserContentManager? content_manager = null) {
public ClientWebView(WebKit.UserContentManager? custom_manager = null) {
WebKit.Settings setts = new WebKit.Settings();
setts.allow_modal_dialogs = false;
setts.default_charset = "UTF-8";
@ -118,6 +141,10 @@ public class ClientWebView : WebKit.WebView {
setts.enable_plugins = false;
setts.javascript_can_access_clipboard = true;
WebKit.UserContentManager content_manager =
custom_manager ?? new WebKit.UserContentManager();
content_manager.add_script(ClientWebView.script);
Object(user_content_manager: content_manager, settings: setts);
// XXX get the allow prefix from the extension somehow
@ -253,4 +280,3 @@ public class ClientWebView : WebKit.WebView {
}
}

View file

@ -5,6 +5,7 @@ set(RESOURCE_LIST
STRIPBLANKS "account_list.glade"
STRIPBLANKS "account_spinner.glade"
STRIPBLANKS "certificate_warning_dialog.glade"
"client-web-view.js"
STRIPBLANKS "composer-headerbar.ui"
STRIPBLANKS "composer-menus.ui"
STRIPBLANKS "composer-widget.ui"

7
ui/client-web-view.js Normal file
View file

@ -0,0 +1,7 @@
/*
* Copyright 2016 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/