Moved scrolling logic into javascript.
authorDaniel Carl <danielcarl@gmx.de>
Sun, 7 May 2017 23:12:00 +0000 (01:12 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Sun, 7 May 2017 23:34:55 +0000 (01:34 +0200)
src/normal.c
src/scripts/scroll.js [new file with mode: 0644]
src/setting.c

index 06db643..b66948a 100644 (file)
@@ -651,77 +651,9 @@ static VbResult normal_quit(Client *c, const NormalCmdInfo *info)
 
 static VbResult normal_scroll(Client *c, const NormalCmdInfo *info)
 {
-    int x = 0, y = 0, page_height = 0, count = info->count ? info->count : 1;
     char *js;
-    GtkAllocation alloc;
 
-    /* The overall page height is only required for the <C-*> commands. */
-    if (VB_IS_CTRL(info->key)) {
-        gtk_widget_get_allocation(GTK_WIDGET(c->webview), &alloc);
-        page_height = (int)alloc.height;
-    }
-
-    switch (info->key) {
-        case 'j':
-            y = count * c->config.scrollstep;
-            break;
-        case 'h':
-            x = -count * c->config.scrollstep;
-            break;
-        case 'k':
-            y = -count * c->config.scrollstep;
-            break;
-        case 'l':
-            x = count * c->config.scrollstep;
-            break;
-        case CTRL('D'):
-            y = count * page_height / 2;
-            break;
-        case CTRL('U'):
-            y = -count * page_height / 2;
-            break;
-        case CTRL('F'):
-            y = count * page_height;
-            break;
-        case CTRL('B'):
-            y = -count * page_height;
-            break;
-        case 'G':
-            if (info->count) {
-                js = g_strdup_printf(
-                        "window.scroll(window.scrollX, %d * ((document.documentElement.scrollHeight - window.innerHeight) / 100));",
-                        info->count);
-                ext_proxy_eval_script(c, js, NULL);
-                g_free(js);
-                return RESULT_COMPLETE;
-            }
-
-            /* Without count scroll to the end of the page. */
-            ext_proxy_eval_script(c, "window.scroll(window.scrollX, document.body.scrollHeight);", NULL);
-            return RESULT_COMPLETE;
-        case '0':
-            ext_proxy_eval_script(c, "window.scroll(0, window.scrollY);", NULL);
-            return RESULT_COMPLETE;
-        case '$':
-            ext_proxy_eval_script(c, "window.scroll(document.body.scrollWidth, window.scrollY);", NULL);
-            return RESULT_COMPLETE;
-        default:
-            if (info->key2 == 'g') {
-                if (info->count) {
-                    js = g_strdup_printf(
-                            "window.scroll(window.scrollX, %d * (1 + (document.height - window.innerHeight) / 100));",
-                            info->count);
-                    ext_proxy_eval_script(c, js, NULL);
-                    g_free(js);
-                    return RESULT_COMPLETE;
-                }
-                /* Without count gg scrolls to the top of the page. */
-                ext_proxy_eval_script(c, "window.scroll(window.scrollX, 0);", NULL);
-                return RESULT_COMPLETE;
-            }
-            return RESULT_ERROR;
-    }
-    js = g_strdup_printf("window.scrollBy(%d,%d);", x, y);
+    js = g_strdup_printf("vbscroll('%c',%d,%d);", info->key, c->config.scrollstep, info->count);
     ext_proxy_eval_script(c, js, NULL);
     g_free(js);
 
diff --git a/src/scripts/scroll.js b/src/scripts/scroll.js
new file mode 100644 (file)
index 0000000..92fc481
--- /dev/null
@@ -0,0 +1,61 @@
+function vbscroll(mode, scrollStep, count) {
+    var w = window,
+        d = document,
+        x = y = 0,
+        ph = d.documentElement.clientHeight,
+        c = count||1,
+        rel = true;
+    switch (mode) {
+        case 'j':
+            y = c * scrollStep;
+            break;
+        case 'h':
+            x = -c * scrollStep;
+            break;
+        case 'k':
+            y = -c * scrollStep;
+            break;
+        case 'l':
+            x = c * scrollStep;
+            break;
+        case '\x04': /* ^D */
+            y = c * ph / 2;
+            break;
+        case '\x15': /* ^U */
+            y = -c * ph / 2;
+            break;
+        case '\x06': /* ^F */
+            y = c * ph;
+            break;
+        case '\x02': /* ^B */
+            y = -c * ph;
+            break;
+        case 'G': /* fall through - gg and G differ only in y value when no count is given */
+        case 'g':
+            x = w.scrollX;
+            if (count) {
+                y = c * ((d.documentElement.scrollHeight - w.innerHeight) / 100);
+            } else {
+                y = 'G' == mode ? d.body.scrollHeight : 0;
+            }
+            rel = false;
+            break;
+        case '0':
+            y   = w.scrollY;
+            rel = false;
+            break;
+        case '$':
+            x   = d.body.scrollWidth;
+            y   = w.scrollY;
+            rel = false;
+            break;
+        default:
+            return 1;
+    }
+    if (rel) {
+        w.scrollBy(x, y);
+    } else {
+        w.scroll(x, y);
+    }
+    return 0;
+}
index 54e9b18..296fe53 100644 (file)
@@ -620,8 +620,8 @@ static int user_scripts(Client *c, const char *name, DataType type, void *value,
         webkit_user_content_manager_remove_all_scripts(ucm);
     }
 
-    /* Inject the global hints script. */
-    script = webkit_user_script_new(JS_HINTS,
+    /* Inject the global scripts. */
+    script = webkit_user_script_new(JS_HINTS " " JS_SCROLL,
             WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
             WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END, NULL, NULL);
     webkit_user_content_manager_add_script(ucm, script);