eye_guide_on_image_buffers: ability to disable eye-guide in image buffers
[conkeror.git] / modules / eye-guide.js
blobe590841277456f07e541aa45149d26296343dcc0
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 define_variable("eye_guide_on_image_buffers", true,
29     "Enable eye guide on image-only buffers.");
31 function eye_guide_scroll (buffer, scroll_down, hl_new, context, interval) {
32     let win = buffer.focused_frame;
33     let doc = win.document;
34     let scroll_amount = win.innerHeight - context;
35     let old_y = win.scrollY;
36     win.scrollBy(0, scroll_down ? scroll_amount : -scroll_amount);
37     if (win.scrollY == old_y)
38         return;
39     if (!eye_guide_on_image_buffers && doc instanceof Ci.nsIImageDocument) {
40         return;
41     }
42     if (Math.abs(win.scrollY - old_y) < scroll_amount)
43         context = win.innerHeight - (scroll_down ? win.scrollY - old_y : old_y);
44     let guide = doc.getElementById("__conkeror_eye_guide");
45     if (! guide) {
46         guide = doc.createElementNS(XHTML_NS, "div");
47         guide.id = "__conkeror_eye_guide";
48         doc.documentElement.appendChild(guide);
49     }
50     if (hl_new) {
51         guide.style.top = scroll_down ? context + "px" : "0px";
52         guide.style.height = (win.innerHeight - context) + "px";
53     } else {
54         guide.style.top = scroll_down ? "0px"
55             : (win.innerHeight - context) + "px";
56         guide.style.height = context + "px";
57     }
58     guide.style.display = "block";
59     guide.className =
60         "__conkeror_eye_guide_scroll_" + (scroll_down ? "down" : "up");
61     if (win.eye_guide_timer) {
62         win.clearTimeout(win.eye_guide_timer);
63         win.eye_guide_timer = null;
64     }
65     if (interval != 0) {
66         win.eye_guide_timer = win.setTimeout(
67             function () {
68                 guide.style.display = "none";
69             },
70             interval);
71     }
74 interactive("eye-guide-scroll-down",
75     "Alternative to scroll-page-down, displays a guide to help "+
76     "your eyes follow the scroll.",
77     function (I) {
78         eye_guide_scroll(I.buffer, true, eye_guide_highlight_new,
79                          eye_guide_context_size, eye_guide_interval);
80     });
82 interactive("eye-guide-scroll-up",
83     "Alternative to scroll-page-up, displays a guide to help "+
84     "your eyes follow the scroll.",
85     function (I) {
86         eye_guide_scroll(I.buffer, false, eye_guide_highlight_new,
87                          eye_guide_context_size, eye_guide_interval);
88     });
90 provide("eye-guide");