const Gdk = imports.gi.Gdk;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const WebKit2 = imports.gi.WebKit2;

const Config = imports.config;

/**
 * Class: EknWebview
 * WebKit WebView subclass which provides utility functions for loading
 * and handling content from the knowledge engine and a clean interface
 * for injecting custom JS and CSS from gresource files.
 *
 * Calling <inject_js_from_resource()> or <inject_css_from_resource()> will
 * ensure that the current page (if any) as well as all subsequently loaded
 * pages in this webview will have the given JS or CSS injected only when the
 * HTML document has finished loading
 *
 * Parent class:
 *     WebKit2.WebView
 */
const EknWebview = new Lang.Class({
    Name: 'EknWebview',
    GTypeName: 'EknWebview',
    Extends: WebKit2.WebView,

    _init: function (params) {
        this.parent(params);

        let settings = this.get_settings();
        settings.enable_developer_extras = Config.inspector_enabled;
        settings.javascript_can_access_clipboard = true;

        this.connect('context-menu', this._load_context_menu.bind(this));
        this.connect('decide-policy', this._onNavigation.bind(this));
    },

    _load_context_menu: function (webview, context_menu, event) {
        context_menu.get_items().forEach(function (item) {
            // Remove all menu items except 'Copy'
            if (item.get_stock_action() !== WebKit2.ContextMenuAction.COPY) {
                context_menu.remove(item);
            }
        });
    },

    _onNavigation: function (webview, decision, decision_type) {
        if (decision_type === WebKit2.PolicyDecisionType.NAVIGATION_ACTION) {
            let uri = decision.request.uri;
            let scheme = GLib.uri_parse_scheme(uri);
            if (scheme !== null && scheme.startsWith('browser-')) {
                // Open everything that starts with 'browser-' in the system
                // browser
                let realURI = uri.slice('browser-'.length);
                Gtk.show_uri(null, realURI, Gdk.CURRENT_TIME);
                decision.ignore();
                return true; // handled
            }
        }
        return false; // not handled, default behavior
    }
});

@@ -936,21 +936,29 @@
         
-let s
+this._webKitS
 ettings 
@@ -980,33 +980,45 @@
 ings();%0A        
-s
+this._webKitS
 ettings.enable_d
@@ -1069,17 +1069,29 @@
         
-s
+this._webKitS
 ettings.
@@ -1131,16 +1131,356 @@
  true;%0A%0A
+        this._defaultFontSize = this._webKitSettings.default_font_size;%0A        this._defaultMonospaceFontSize = this._webKitSettings.default_monospace_font_size;%0A%0A        let screen = Gdk.Screen.get_default()%0A        this._gtkSettings = Gtk.Settings.get_for_screen(screen)%0A        this._baseXftDPI = this._gtkSettings.gtk_xft_dpi / 1024;%0A%0A
         
@@ -1611,24 +1611,116 @@
 ind(this));%0A
+        this._gtkSettings.connect('notify::gtk-xft-dpi', this._onXftDPIChanged.bind(this));%0A
     %7D,%0A%0A    
@@ -2672,32 +2672,32 @@
     %7D%0A        %7D%0A
-
         return f
@@ -2735,16 +2735,447 @@
 ehavior%0A
+    %7D,%0A%0A    _onXftDPIChanged: function (settings) %7B%0A        let newDPI = settings.gtk_xft_dpi / 1024;%0A        this._webKitSettings.default_font_size = this._normalizeFontSize(this._defaultFontSize, newDPI)%0A        this._webKitSettings.default_monospace_font_size = this._normalizeFontSize(this._defaultMonospaceFontSize, newDPI);%0A    %7D,%0A%0A    _normalizeFontSize: function (size, dpi) %7B%0A        return size * dpi / this._baseXftDPI;%0A
     %7D%0A%7D)

Update WebView's default font size on DPI changes