From 9f56b8d7f5485e6f6257474d981c2aadc4f50cfb Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Wed, 12 Dec 2007 05:27:11 +0000 Subject: [PATCH] advanced scrolling when focus is at a wrong element/frame --- content/buffers.js | 40 ++++++++++++++++++++++++++++++++++++---- content/mappings.js | 20 ++------------------ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/content/buffers.js b/content/buffers.js index 291a68d1..78d43c9c 100644 --- a/content/buffers.js +++ b/content/buffers.js @@ -179,10 +179,28 @@ vimperator.Buffer = function () //{{{ vimperator.beep(); } + function findScrollableWindow() + { + var win = window.document.commandDispatcher.focusedWindow; + if (win.scrollMaxX > 0 || win.scrollMaxY > 0) + return win; + + win = window.content; + if (win.scrollMaxX > 0 || win.scrollMaxY > 0) + return win; + + for (var i = 0; i < win.frames.length; i++) + if (win.frames[i].scrollMaxX > 0 || win.frames[i].scrollMaxY > 0) + return win.frames[i]; + + return win; + } + + // both values are given in percent, -1 means no change function scrollToPercentiles(horizontal, vertical) { - var win = document.commandDispatcher.focusedWindow; + var win = findScrollableWindow(); var h, v; if (horizontal < 0) @@ -376,7 +394,7 @@ vimperator.Buffer = function () //{{{ scrollColumns: function (cols) { - var win = window.document.commandDispatcher.focusedWindow; + var win = findScrollableWindow(); const COL_WIDTH = 20; if (cols > 0 && win.scrollX >= win.scrollMaxX || cols < 0 && win.scrollX == 0) @@ -392,18 +410,32 @@ vimperator.Buffer = function () //{{{ scrollLines: function (lines) { - var win = window.document.commandDispatcher.focusedWindow; + var win = findScrollableWindow(); checkScrollYBounds(win, lines); win.scrollByLines(lines); }, scrollPages: function (pages) { - var win = window.document.commandDispatcher.focusedWindow; + var win = findScrollableWindow(); checkScrollYBounds(win, pages); win.scrollByPages(pages); }, + scrollByScrollSize: function (count, direction) + { + if (count > 0) + vimperator.options["scroll"] = count; + + var win = findScrollableWindow(); + checkScrollYBounds(win, direction); + + if (vimperator.options["scroll"] > 0) + this.scrollLines(vimperator.options["scroll"] * direction); + else // scroll half a page down in pixels + win.scrollBy(0, win.innerHeight / 2 * direction); + }, + scrollToPercentile: function (percentage) { scrollToPercentiles(-1, percentage); diff --git a/content/mappings.js b/content/mappings.js index 6bd35c84..4669acc3 100644 --- a/content/mappings.js +++ b/content/mappings.js @@ -1018,24 +1018,8 @@ vimperator.Mappings = function () //{{{ flags: vimperator.Mappings.flags.COUNT } )); - function scrollByScrollSize(count, direction) - { - if (count > 0) - vimperator.options["scroll"] = count; - - if (vimperator.options["scroll"] > 0) - { - vimperator.buffer.scrollLines(vimperator.options["scroll"] * direction); - } - else - { - // scroll half a page down in pixels - var win = document.commandDispatcher.focusedWindow; - win.scrollBy(0, vimperator.buffer.pageHeight / 2 * direction); - } - } addDefaultMap(new vimperator.Map([vimperator.modes.NORMAL], [""], - function (count) { scrollByScrollSize(count, 1); }, + function (count) { vimperator.buffer.scrollByScrollSize(count, 1); }, { shortHelp: "Scroll window downwards in the buffer", help: "The number of lines is set by the 'scroll' option which defaults to half a page. " + @@ -1044,7 +1028,7 @@ vimperator.Mappings = function () //{{{ } )); addDefaultMap(new vimperator.Map([vimperator.modes.NORMAL], [""], - function (count) { scrollByScrollSize(count, -1); }, + function (count) { vimperator.buffer.scrollByScrollSize(count, -1); }, { shortHelp: "Scroll window upwards in the buffer", help: "The number of lines is set by the 'scroll' option which defaults to half a page. " +