Skip to content

Commit 244a7c3

Browse files
author
thyttan
committed
messagegui: simpler logic for the scroller
1 parent 95d6f45 commit 244a7c3

File tree

1 file changed

+10
-84
lines changed

1 file changed

+10
-84
lines changed

apps/messagegui/app.js

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -280,32 +280,14 @@ function showMusicMessage(msg) {
280280
}, 400);
281281
}
282282

283-
function showMessagesScroller(msg, persist, alreadyProcessed) {
283+
function showMessagesScroller(msg, persist) {
284284
if (persist===undefined) {persist = true;}
285285
const MSG_IDX = msg ? MESSAGES.findIndex((m)=>m.id==msg.id) : undefined;
286-
const INITIATED_FROM = (
287-
alreadyProcessed===undefined ? "other function" :
288-
(MSG_IDX<=alreadyProcessed.idxSpan.start ? "scrolling up" :
289-
(MSG_IDX >= alreadyProcessed.idxSpan.stop-1 ? "scrolling down" :
290-
undefined))
291-
);
292286

293287
if (replying) { return; }
294288
active = "scroller";
295289
if (persist) {cancelReloadTimeout();} else {resetReloadTimeout();}
296290

297-
if (!alreadyProcessed) {alreadyProcessed = {idxSpan:{}};} // So `alreadyProcessed.idxSpan.start/stop` can be looked for on first run.
298-
299-
let idxSpan = (
300-
INITIATED_FROM==="other function" ?
301-
{start : Math.max(MSG_IDX-1, 0), stop : Math.min(MSG_IDX+2, MESSAGES.length)} :
302-
(INITIATED_FROM==="scrolling up" ?
303-
{start : MSG_IDX, stop : alreadyProcessed.idxSpan.start } :
304-
(INITIATED_FROM==="scrolling down" ?
305-
{ start : alreadyProcessed.idxSpan.stop, stop : Math.min(MSG_IDX+1, MESSAGES.length) } :
306-
undefined))
307-
);
308-
309291
const WU = require("widget_utils");
310292
WU.hide();
311293
const APP_RECT = Bangle.appRect;
@@ -314,65 +296,30 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
314296
g.setFont(bodyFont);
315297
const FONT_HEIGHT = g.getFontHeight();
316298
const LINES_PER_SCREEN = APP_RECT.h/FONT_HEIGHT;
299+
let initScroll;
317300
var titleLines = [];
318-
var messagesWrapped = [];
319-
for (let i=idxSpan.start ; i<idxSpan.stop ; i++) {
301+
let allLines = [];
302+
for (let i=0 ; i<MESSAGES.length ; i++) {
303+
if (MSG_IDX === i) {initScroll = allLines.length*FONT_HEIGHT}
320304
let msgIter = MESSAGES[i];
321305
msgIter.new = false;
322306

323-
//if (/*msgIter.id=="music" ||*/ msgIter.source=="maps" || msgIter.id =="call") {
324-
// idxSpan.stop++
325-
// if (idxSpan.stop>=MESSAGES.length) {break;}
326-
// continue;
327-
//}
328-
329307
var lines = [];
330308
const TITLE_STRING = msgIter.title||msgIter.sender||msgIter.subject||msgIter.src||/*LANG*/"No Title";
331309
lines = g.wrapString(TITLE_STRING, APP_RECT.w-10);
332310
for (let i=0; i<lines.length; i++) {
333-
titleLines.push(i + (messagesWrapped[0]?messagesWrapped[0].length:0) +
334-
(messagesWrapped[1]?messagesWrapped[1].length:0));
311+
titleLines.push(i + allLines.length);
335312
}
336313
lines = lines.concat(g.wrapString(msgIter.body, APP_RECT.w-10),
337314
["-".repeat(12)]);
338-
messagesWrapped.push(lines);
315+
allLines = allLines.concat(lines);
339316
}
340317

341-
let allLines = [];
342-
for (let i=0 ; i<messagesWrapped.length ; i++) {
343-
allLines = allLines.concat(messagesWrapped[i]);
344-
}
345-
346-
var initScroll = messagesWrapped[0].length;
347-
if (INITIATED_FROM === "other function") {
348-
if (MSG_IDX==0) {initScroll = 0;}
349-
} else if (INITIATED_FROM === "scrolling up") {
350-
titleLines = titleLines.concat(alreadyProcessed.titleLines.
351-
map((x) => x + allLines.length));
352-
allLines = allLines.concat(alreadyProcessed.lines);
353-
} else if (INITIATED_FROM === "scrolling down") {
354-
initScroll = alreadyProcessed.lines.length-(LINES_PER_SCREEN);
355-
titleLines = alreadyProcessed.titleLines.concat(titleLines.
356-
map((x) => x + alreadyProcessed.lines.length));
357-
allLines = alreadyProcessed.lines.concat(allLines);
358-
}
359-
initScroll = initScroll*FONT_HEIGHT;
360-
361-
if (allLines.length == 0 && alreadyProcessed.lines.length == 0) {
318+
if (allLines.length == 0) {
362319
cancelReloadTimeout();
363320
returnToClockIfEmpty();
364321
}
365322

366-
alreadyProcessed = { // Update with the newly processed messages.
367-
lines : allLines,
368-
titleLines : titleLines,
369-
idxSpan: {
370-
start : Math.min(idxSpan.start,
371-
(alreadyProcessed.idxSpan.start===undefined) ?
372-
MESSAGES.length : alreadyProcessed.idxSpan.start),
373-
stop : Math.max(idxSpan.stop, alreadyProcessed.idxSpan.stop||0)}
374-
};
375-
376323
function identifyDisplayedMsg(scrollIdx) {
377324
let firstTitleLinePerMsg = [titleLines[0]];
378325
for (let i=1; i<titleLines.length; i++) {
@@ -381,32 +328,14 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
381328
}
382329
for (let i=titleLines.length-1; i>=0 ; i--) {
383330
if (scrollIdx>=firstTitleLinePerMsg[i]) {
384-
return MESSAGES[i + alreadyProcessed.idxSpan.start];
331+
return MESSAGES[i];
385332
}
386333
}
387334
}
388335

389-
// Used for stopping repeated triggering of next message by the scroller and
390-
// to choose how to identify displayed message when selecting with hw button.
336+
// Used to choose how to identify displayed message when selecting with hw button.
391337
let prevScrollIdxs = [undefined, undefined]; // [prevIdx, prevPrevIdx]
392338

393-
function shouldAddNext(scrollIdx) {"ram"; return (scrollIdx>=allLines.length-1 &&
394-
scrollIdx!=prevScrollIdxs[0] && alreadyProcessed.idxSpan.stop<MESSAGES.length);}
395-
function shouldAddPrev(scrollIdx) {"ram"; return (scrollIdx==0 &&
396-
scrollIdx!=prevScrollIdxs[0] && alreadyProcessed.idxSpan.start>0);}
397-
function reinitAdding(idx) {
398-
setTimeout(() => {
399-
Bangle.emit("drag", {dy:0}); // Make sure scrolling doesn't continue after reinit. FIXME: Is this a hack that begs for a bug being fixed elsewhere?
400-
// Zero ms timeout as to not move on before the scroller has registered the emitted drag event.
401-
setTimeout(()=>{
402-
E.showScroller();
403-
clearBtnHandler();
404-
showMessagesScroller(MESSAGES[idx],
405-
persist, alreadyProcessed);
406-
},0);
407-
}, 40);
408-
}
409-
410339
E.showScroller({
411340
scroll : initScroll,
412341
h : FONT_HEIGHT, // height of each menu item in pixels
@@ -417,9 +346,6 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
417346
setColor(titleLines.find(e=>e==scrollIdx)!==undefined ? g.theme.fg2 : g.theme.fg).
418347
clearRect(r);
419348
g.setFont(bodyFont).setFontAlign(0,-1).drawString(allLines[scrollIdx], r.x+r.w/2, r.y);
420-
// Load in next/previous message on demand by reinitializing showMessagesScroller while passing on the processed messages.
421-
if (shouldAddNext(scrollIdx)) {reinitAdding(alreadyProcessed.idxSpan.stop);}
422-
if (shouldAddPrev(scrollIdx)) {reinitAdding(alreadyProcessed.idxSpan.start-1);}
423349
if (prevScrollIdxs[1]!==prevScrollIdxs[0]) {
424350
prevScrollIdxs[1] = prevScrollIdxs[0];
425351
if (!persist) {resetReloadTimeout();}

0 commit comments

Comments
 (0)