Get rid of JavaScript for scrolling in views

This is still a hack, until WebKitGTK gives us a more practical and
stable way to do that. Manipulating directly the DOM inside a
webextension is a pain and only usable with unstable API atm.
This commit is contained in:
Quentin Rameau 2016-03-02 14:29:21 +01:00
parent 50befe4a95
commit af7522006b
2 changed files with 61 additions and 18 deletions

View File

@ -109,13 +109,16 @@ static Key keys[] = {
{ MODKEY, GDK_KEY_l, navigate, { .i = +1 } }, { MODKEY, GDK_KEY_l, navigate, { .i = +1 } },
{ MODKEY, GDK_KEY_h, navigate, { .i = -1 } }, { MODKEY, GDK_KEY_h, navigate, { .i = -1 } },
/* in page % */ /* Currently we have to use scrolling steps that WebKit2GTK+ gives us
{ MODKEY, GDK_KEY_j, scroll_v, { .i = +10 } }, * d: step down, u: step up, r: step right, l:step left
{ MODKEY, GDK_KEY_k, scroll_v, { .i = -10 } }, * D: page down, U: page up */
{ MODKEY, GDK_KEY_b, scroll_v, { .i = -50 } }, { MODKEY, GDK_KEY_j, scroll, { .i = 'd' } },
{ MODKEY, GDK_KEY_space, scroll_v, { .i = +50 } }, { MODKEY, GDK_KEY_k, scroll, { .i = 'u' } },
{ MODKEY, GDK_KEY_i, scroll_h, { .i = +10 } }, { MODKEY, GDK_KEY_b, scroll, { .i = 'U' } },
{ MODKEY, GDK_KEY_u, scroll_h, { .i = -10 } }, { MODKEY, GDK_KEY_space, scroll, { .i = 'D' } },
{ MODKEY, GDK_KEY_i, scroll, { .i = 'r' } },
{ MODKEY, GDK_KEY_u, scroll, { .i = 'l' } },
{ MODKEY|GDK_SHIFT_MASK, GDK_KEY_j, zoom, { .i = -1 } }, { MODKEY|GDK_SHIFT_MASK, GDK_KEY_j, zoom, { .i = -1 } },
{ MODKEY|GDK_SHIFT_MASK, GDK_KEY_k, zoom, { .i = +1 } }, { MODKEY|GDK_SHIFT_MASK, GDK_KEY_k, zoom, { .i = +1 } },

60
surf.c
View File

@ -126,6 +126,7 @@ static void destroyclient(Client *c);
static void cleanup(void); static void cleanup(void);
/* GTK/WebKit */ /* GTK/WebKit */
static GdkDevice *getkbdevice(void);
static WebKitWebView *newview(Client *c, WebKitWebView *rv); static WebKitWebView *newview(Client *c, WebKitWebView *rv);
static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a, static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
Client *c); Client *c);
@ -160,8 +161,7 @@ static void reload(Client *c, const Arg *a);
static void print(Client *c, const Arg *a); static void print(Client *c, const Arg *a);
static void clipboard(Client *c, const Arg *a); static void clipboard(Client *c, const Arg *a);
static void zoom(Client *c, const Arg *a); static void zoom(Client *c, const Arg *a);
static void scroll_v(Client *c, const Arg *a); static void scroll(Client *c, const Arg *a);
static void scroll_h(Client *c, const Arg *a);
static void navigate(Client *c, const Arg *a); static void navigate(Client *c, const Arg *a);
static void stop(Client *c, const Arg *a); static void stop(Client *c, const Arg *a);
static void toggle(Client *c, const Arg *a); static void toggle(Client *c, const Arg *a);
@ -185,6 +185,7 @@ static int showxid;
static int cookiepolicy; static int cookiepolicy;
static Display *dpy; static Display *dpy;
static Client *clients; static Client *clients;
static GdkDevice *gdkkb;
static char *stylefile; static char *stylefile;
static const char *useragent; static const char *useragent;
char *argv0; char *argv0;
@ -232,6 +233,8 @@ setup(void)
scriptfile = buildfile(scriptfile); scriptfile = buildfile(scriptfile);
cachedir = buildpath(cachedir); cachedir = buildpath(cachedir);
gdkkb = getkbdevice();
if (!stylefile) { if (!stylefile) {
styledir = buildpath(styledir); styledir = buildpath(styledir);
for (i = 0; i < LENGTH(styles); ++i) { for (i = 0; i < LENGTH(styles); ++i) {
@ -660,6 +663,22 @@ cleanup(void)
g_free(cachedir); g_free(cachedir);
} }
static GdkDevice *
getkbdevice(void)
{
GList *l, *gdl = gdk_device_manager_list_devices(
gdk_display_get_device_manager(gdk_display_get_default()),
GDK_DEVICE_TYPE_MASTER);
GdkDevice *gd = NULL;
for (l = gdl; l != NULL; l = l->next)
if (gdk_device_get_source(l->data) == GDK_SOURCE_KEYBOARD)
gd = l->data;
g_list_free(gdl);
return gd;
}
WebKitWebView * WebKitWebView *
newview(Client *c, WebKitWebView *rv) newview(Client *c, WebKitWebView *rv)
{ {
@ -1257,17 +1276,38 @@ zoom(Client *c, const Arg *a)
} }
void void
scroll_v(Client *c, const Arg *a) scroll(Client *c, const Arg *a)
{ {
evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))", GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
a->i);
gdk_event_set_device(ev, gdkkb);
// gdk_event_set_screen(ev, gdk_screen_get_default());
ev->key.window = gtk_widget_get_window(GTK_WIDGET(c->win));
ev->key.state = GDK_CONTROL_MASK;
ev->key.time = GDK_CURRENT_TIME;
switch (a->i) {
case 'd':
ev->key.keyval = GDK_KEY_Down;
break;
case 'D':
ev->key.keyval = GDK_KEY_Page_Down;
break;
case 'l':
ev->key.keyval = GDK_KEY_Left;
break;
case 'r':
ev->key.keyval = GDK_KEY_Right;
break;
case 'U':
ev->key.keyval = GDK_KEY_Page_Up;
break;
case 'u':
ev->key.keyval = GDK_KEY_Up;
break;
} }
void gdk_event_put(ev);
scroll_h(Client *c, const Arg *a)
{
evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
a->i);
} }
void void