[geary/wip/728002-webkit2: 45/48] Load ConversationWebView HTML CSS using a WK2 UserContentManager.



commit 23cb07142e03771361fad97a4400d230530d1ec9
Author: Michael James Gratton <mike vee net>
Date:   Sun Oct 16 21:42:48 2016 +1100

    Load ConversationWebView HTML CSS using a WK2 UserContentManager.
    
    * src/client/application/geary-controller.vala (GearyController): Load
      the ConversationWebView's stylesheets at startup.
    
    * src/client/components/client-web-view.vala (ClientWebView): Add
      ::load_app_stylesheet and ::load_user_stylesheet static methods for use
      by derived classes.
    
    * src/client/conversation-viewer/conversation-web-view.vala
      (ConversationWebView): Add static ::load_stylehseets method to load app
      and user CSS. Remove old WK1 user style loading code.
    
    * src/client/web-process/util-conversation.vala (Util.Conversation):
      Remove old WK1 app style loading code.

 src/client/application/geary-controller.vala       |    7 +++
 src/client/components/client-web-view.vala         |   34 ++++++++++++++
 .../conversation-viewer/conversation-web-view.vala |   49 +++++++-------------
 src/client/web-process/util-conversation.vala      |   12 -----
 4 files changed, 58 insertions(+), 44 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 7ff5b8c..f945955 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -208,6 +208,13 @@ public class GearyController : Geary.BaseObject {
                 );
             });
 
+        // Load web view stylesheets
+        try {
+            ConversationWebView.load_stylehseets(this.application);
+        } catch (Error err) {
+            error("Error loading application CSS: %s", err.message);
+        }
+
         // Use a global avatar session because a cache must be used
         // per-session, and we don't want to have to load the cache
         // for each conversation load.
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index e81119a..51672e0 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -13,6 +13,40 @@ public class ClientWebView : WebKit.WebView {
     private const double ZOOM_DEFAULT = 1.0;
     private const double ZOOM_FACTOR = 0.1;
 
+    protected static WebKit.UserStyleSheet load_app_stylesheet(GearyApplication app,
+                                                               string name)
+        throws Error {
+        return new WebKit.UserStyleSheet(
+            app.read_resource(name),
+            WebKit.UserContentInjectedFrames.TOP_FRAME,
+            WebKit.UserStyleLevel.USER,
+            null,
+            null
+        );
+    }
+
+    protected static WebKit.UserStyleSheet? load_user_stylesheet(GearyApplication app,
+                                                                 string name) {
+        File stylesheet = app.get_user_config_directory().get_child(name);
+        WebKit.UserStyleSheet? user_stylesheet = null;
+        try {
+            Geary.Memory.FileBuffer buf =
+                new Geary.Memory.FileBuffer(stylesheet, true);
+            user_stylesheet = new WebKit.UserStyleSheet(
+                buf.get_valid_utf8(),
+                WebKit.UserContentInjectedFrames.ALL_FRAMES,
+                WebKit.UserStyleLevel.USER,
+                null,
+                null
+            );
+        } catch (IOError.NOT_FOUND err) {
+            warning("User CSS file does not exist: %s", err.message);
+        } catch (Error err) {
+            warning("Failed to load user CSS file: %s", err.message);
+        }
+        return user_stylesheet;
+    }
+
 
     public bool is_loaded { get; private set; default = false; }
     public string allow_prefix { get; private set; default = ""; }
diff --git a/src/client/conversation-viewer/conversation-web-view.vala 
b/src/client/conversation-viewer/conversation-web-view.vala
index 4402c2b..cda1da5 100644
--- a/src/client/conversation-viewer/conversation-web-view.vala
+++ b/src/client/conversation-viewer/conversation-web-view.vala
@@ -10,43 +10,28 @@ public class ConversationWebView : ClientWebView {
 
     private const string USER_CSS = "user-message.css";
 
+    private static WebKit.UserStyleSheet? user_stylesheet = null;
+    private static WebKit.UserStyleSheet? app_stylesheet = null;
+
+    public static void load_stylehseets(GearyApplication app)
+        throws Error {
+        ConversationWebView.app_stylesheet =
+            ClientWebView.load_app_stylesheet(app, "conversation-web-view.css");
+        ConversationWebView.user_stylesheet =
+            ClientWebView.load_user_stylesheet(app, "user-message.css");
+    }
+
 
     public bool is_height_valid { get; private set; default = false; }
 
 
     public ConversationWebView() {
-        File user_css = GearyApplication.instance.get_user_config_directory().get_child(USER_CSS);
-        // Print out a debug line here if the user CSS file exists, so
-        // we get warning about it when debugging visual issues.
-        user_css.query_info_async.begin(
-            FileAttribute.STANDARD_TYPE,
-            FileQueryInfoFlags.NONE,
-            Priority.DEFAULT_IDLE,
-            null,
-            (obj, res) => {
-                try {
-                    user_css.query_info_async.end(res);
-                    debug("User CSS file exists: %s", USER_CSS);
-                } catch (Error e) {
-                    // No problem, file does not exist
-                }
-            });
-
-        WebKit.UserStyleSheet user_style = new WebKit.UserStyleSheet(
-            user_css.get_uri(),
-            WebKit.UserContentInjectedFrames.ALL_FRAMES,
-            WebKit.UserStyleLevel.USER,
-            null,
-            null
-        );
-
-        WebKit.UserContentManager content = new WebKit.UserContentManager();
-        content.add_style_sheet(user_style);
-
-        base(content);
-
-        // Set defaults.
-        set_border_width(0);
+        WebKit.UserContentManager manager = new WebKit.UserContentManager();
+        manager.add_style_sheet(ConversationWebView.app_stylesheet);
+        if (ConversationWebView.user_stylesheet != null) {
+            manager.add_style_sheet(ConversationWebView.user_stylesheet);
+        }
+        base(manager);
     }
 
     public bool clean_and_load(string html) {
diff --git a/src/client/web-process/util-conversation.vala b/src/client/web-process/util-conversation.vala
index 578bf99..275cd7d 100644
--- a/src/client/web-process/util-conversation.vala
+++ b/src/client/web-process/util-conversation.vala
@@ -89,18 +89,6 @@ namespace Util.Conversation {
                 html.set_dir("auto");
             }
 
-            // Add application CSS to the document
-            WebKit.DOM.HTMLElement? head = Util.DOM.select(html, "head");
-            if (head == null) {
-                head = create(page, "head");
-                html.insert_before(head, html.get_first_child());
-            }
-            WebKit.DOM.HTMLElement style_element = create(page, "style");
-            string css_text = ""; // XXX 
GearyApplication.instance.read_resource("conversation-web-view.css");
-            WebKit.DOM.Text text_node = page.get_dom_document().create_text_node(css_text);
-            style_element.append_child(text_node);
-            head.insert_before(style_element, head.get_first_child());
-
             // Get all the top level block quotes and stick them into a hide/show controller.
             WebKit.DOM.NodeList blockquote_list = html.query_selector_all("blockquote");
             for (int i = 0; i < blockquote_list.length; ++i) {


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