buffer: make sure first buffer in window is type="content-primary"
[conkeror.git] / modules / eye-guide.js
blob5fbf50bb1e2b70e5ef1951f3bb0ebbed0fa7cfcd
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 in_module(null);
19 define_variable("eye_guide_interval", 800,
20     "Interval during which the eye guide is visible (in ms). "+
21     "When 0, the eye guide will remain visible.");
23 define_variable("eye_guide_context_size", 50,
24     "Context size in pixels for eye-guide-scroll-down and "+
25     "eye-guide-scroll-up.");
27 define_variable("eye_guide_highlight_new", false,
28     "Highlight the new contents of the screen, instead of the old.");
30 function eye_guide_scroll (buffer, scroll_down, hl_new, context, interval) {
31     let win = buffer.focused_frame;
32     let doc = win.document;
33     let scroll_amount = win.innerHeight - context;
34     let old_y = win.scrollY;
35     win.scrollBy(0, scroll_down ? scroll_amount : -scroll_amount);
36     if (win.scrollY == old_y)
37         return;
38     if (Math.abs(win.scrollY - old_y) < scroll_amount)
39         context = win.innerHeight - (scroll_down ? win.scrollY - old_y : old_y);
40     let guide = doc.getElementById("__conkeror_eye_guide");
41     if (! guide) {
42         guide = doc.createElementNS(XHTML_NS, "div");
43         guide.id = "__conkeror_eye_guide";
44         doc.documentElement.appendChild(guide);
45     }
46     if (hl_new) {
47         guide.style.top = scroll_down ? context + "px" : "0px";
48         guide.style.height = (win.innerHeight - context) + "px";
49     } else {
50         guide.style.top = scroll_down ? "0px"
51             : (win.innerHeight - context) + "px";
52         guide.style.height = context + "px";
53     }
54     guide.style.display = "block";
55     guide.className =
56         "__conkeror_eye_guide_scroll_" + (scroll_down ? "down" : "up");
57     if (win.eye_guide_timer) {
58         win.clearTimeout(win.eye_guide_timer);
59         win.eye_guide_timer = null;
60     }
61     if (interval != 0) {
62         win.eye_guide_timer = win.setTimeout(
63             function () {
64                 guide.style.display = "none";
65             },
66             interval);
67     }
70 interactive("eye-guide-scroll-down",
71     "Alternative to scroll-page-down, displays a guide to help "+
72     "your eyes follow the scroll.",
73     function (I) {
74         eye_guide_scroll(I.buffer, true, eye_guide_highlight_new,
75                          eye_guide_context_size, eye_guide_interval);
76     });
78 interactive("eye-guide-scroll-up",
79     "Alternative to scroll-page-up, displays a guide to help "+
80     "your eyes follow the scroll.",
81     function (I) {
82         eye_guide_scroll(I.buffer, false, eye_guide_highlight_new,
83                          eye_guide_context_size, eye_guide_interval);
84     });
86 provide("eye-guide");