Debian package: Add support for xulrunner 18
[conkeror.git] / modules / eye-guide.js
blob255c448f9b656cbe8e8ccf12f8f247ba7ff2c5d6
1 /**
2  * (C) Copyright 2009 John J. Foerch
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 /*
9  * Example usage:
10  *
11  * require('eye-guide.js');
12  * define_key(content_buffer_normal_keymap, "space", "eye-guide-scroll-down");
13  * define_key(content_buffer_normal_keymap, "back_space", "eye-guide-scroll-up");
14  *
15  */
17 define_variable("eye_guide_interval", 800,
18     "Interval during which the eye guide is visible (in ms). "+
19     "When 0, the eye guide will remain visible.");
21 define_variable("eye_guide_context_size", 50,
22     "Context size in pixels for eye-guide-scroll-down and "+
23     "eye-guide-scroll-up.");
25 define_variable("eye_guide_highlight_new", false,
26     "Highlight the new contents of the screen, instead of the old.");
28 function eye_guide_scroll (buffer, scroll_down, hl_new, context, interval) {
29     let win = buffer.focused_frame;
30     let doc = win.document;
31     let scroll_amount = win.innerHeight - context;
32     let old_y = win.scrollY;
33     win.scrollBy(0, scroll_down ? scroll_amount : -scroll_amount);
34     if (win.scrollY == old_y)
35         return;
36     if (Math.abs(win.scrollY - old_y) < scroll_amount)
37         context = win.innerHeight - (scroll_down ? win.scrollY - old_y : old_y);
38     let guide = doc.getElementById("__conkeror_eye_guide");
39     if (! guide) {
40         guide = doc.createElementNS(XHTML_NS, "div");
41         guide.id = "__conkeror_eye_guide";
42         doc.documentElement.appendChild(guide);
43     }
44     if (hl_new) {
45         guide.style.top = scroll_down ? context + "px" : "0px";
46         guide.style.height = (win.innerHeight - context) + "px";
47     } else {
48         guide.style.top = scroll_down ? "0px"
49             : (win.innerHeight - context) + "px";
50         guide.style.height = context + "px";
51     }
52     guide.style.display = "block";
53     guide.className =
54         "__conkeror_eye_guide_scroll_" + (scroll_down ? "down" : "up");
55     if (win.eye_guide_timer) {
56         win.clearTimeout(win.eye_guide_timer);
57         win.eye_guide_timer = null;
58     }
59     if (interval != 0) {
60         win.eye_guide_timer = win.setTimeout(
61             function () {
62                 guide.style.display = "none";
63             },
64             interval);
65     }
68 interactive("eye-guide-scroll-down",
69     "Alternative to scroll-page-down, displays a guide to help "+
70     "your eyes follow the scroll.",
71     function (I) {
72         eye_guide_scroll(I.buffer, true, eye_guide_highlight_new,
73                          eye_guide_context_size, eye_guide_interval);
74     });
76 interactive("eye-guide-scroll-up",
77     "Alternative to scroll-page-up, displays a guide to help "+
78     "your eyes follow the scroll.",
79     function (I) {
80         eye_guide_scroll(I.buffer, false, eye_guide_highlight_new,
81                          eye_guide_context_size, eye_guide_interval);
82     });
84 provide("eye-guide");