Skip to content

Commit 0b19397

Browse files
committed
basic: fix live scrolling in web view
1 parent a5b664f commit 0b19397

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

variety/basic/www/live.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ basic_row(ev)
4343
*/
4444
LAST_EVENT = ev;
4545

46+
/*
47+
* Before we adjust the screen, determine whether we were already
48+
* looking at the base of the log output:
49+
*/
50+
let was_at_base = at_base();
51+
4652
try {
4753
/*
4854
* The server is sending us an EventRow object:
@@ -128,9 +134,9 @@ basic_row(ev)
128134
*/
129135
LAST_NEW_RECORD = tr;
130136

131-
if (at_base()) {
137+
if (was_at_base) {
132138
/*
133-
* If we're at the base already, continue to scroll down when
139+
* If we were at the base already, continue to scroll down when
134140
* new log records arrive.
135141
*/
136142
basic_show_new();
@@ -159,7 +165,7 @@ function
159165
basic_show_new()
160166
{
161167
if (LAST_NEW_RECORD !== null) {
162-
LAST_NEW_RECORD.scrollIntoView(false);
168+
LAST_NEW_RECORD.scrollIntoView({ block: "start" });
163169
}
164170

165171
basic_new_records();
@@ -172,15 +178,28 @@ basic_show_new()
172178
function
173179
at_base()
174180
{
175-
var sy = window.scrollY;
181+
if (LAST_NEW_RECORD === null) {
182+
return (false);
183+
}
184+
185+
var bcr = LAST_NEW_RECORD.getBoundingClientRect();
186+
187+
var bcrb = bcr.bottom;
188+
var bcrh = bcr.height;
176189
var ih = window.innerHeight;
177-
var bsh = document.body.scrollHeight;
178-
if (typeof (sy) !== "number" || typeof (ih) !== "number" ||
179-
typeof (bsh) !== "number") {
190+
if (typeof (bcrb) !== "number" || typeof (bcrh) !== "number" ||
191+
typeof (ih) !== "number") {
180192
return (false);
181193
}
182194

183-
return ((sy + ih) + 25 >= bsh);
195+
/*
196+
* If the most recently appended element is within the window, we
197+
* assume we're essentially scrolled to the bottom of the output. We
198+
* use a fudge factor of 1.5 output lines, to try and work around the
199+
* fact that browsers are inscrutable nightmare cathedrals that fight
200+
* against developers and users alike.
201+
*/
202+
return (bcrb <= (ih + bcrh * 1.5));
184203
}
185204

186205
/*
@@ -244,7 +263,7 @@ basic_new_records()
244263
if (at_base() || !NEW_RECORDS) {
245264
/*
246265
* If we're at the base of the page, we'll scroll live for new
247-
* records that show up and the button should disappear. We
266+
* records that show up and the button should disappear. We
248267
* also shouldn't show the button if there are, as yet, no new
249268
* records.
250269
*/

0 commit comments

Comments
 (0)