Skip to content

Commit 41934ba

Browse files
author
thyttan
committed
messagegui: refactor more to make scroller logic simpler
1 parent 53ef0c3 commit 41934ba

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

apps/messagegui/app.js

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ function showMessagesScroller(msg, persist) {
295295
var bodyFont = fontBig;
296296
g.setFont(bodyFont);
297297
const FONT_HEIGHT = g.getFontHeight();
298-
const LINES_PER_SCREEN = APP_RECT.h/FONT_HEIGHT;
299298
let initScroll;
300299
var titleLines = [];
301300
let allLines = [];
301+
let firstTitleLinePerMsg = [];
302302
for (let i=0 ; i<MESSAGES.length ; i++) {
303303
if (MSG_IDX === i) {initScroll = allLines.length*FONT_HEIGHT}
304304
let msgIter = MESSAGES[i];
@@ -309,6 +309,7 @@ function showMessagesScroller(msg, persist) {
309309
lines = g.wrapString(TITLE_STRING, APP_RECT.w-10);
310310
for (let i=0; i<lines.length; i++) {
311311
titleLines.push(i + allLines.length);
312+
if (i==0) firstTitleLinePerMsg.push(i + allLines.length);
312313
}
313314
lines = lines.concat(g.wrapString(msgIter.body, APP_RECT.w-10),
314315
["-".repeat(12)]);
@@ -320,22 +321,6 @@ function showMessagesScroller(msg, persist) {
320321
returnToClockIfEmpty();
321322
}
322323

323-
function identifyDisplayedMsg(scrollIdx) {
324-
let firstTitleLinePerMsg = [titleLines[0]];
325-
for (let i=1; i<titleLines.length; i++) {
326-
if (titleLines[i]-titleLines[i-1] === 1) {continue;}
327-
firstTitleLinePerMsg.push(titleLines[i]);
328-
}
329-
for (let i=titleLines.length-1; i>=0 ; i--) {
330-
if (scrollIdx>=firstTitleLinePerMsg[i]) {
331-
return MESSAGES[i];
332-
}
333-
}
334-
}
335-
336-
// Used to choose how to identify displayed message when selecting with hw button.
337-
let prevScrollIdxs = [undefined, undefined]; // [prevIdx, prevPrevIdx]
338-
339324
E.showScroller({
340325
scroll : initScroll,
341326
h : FONT_HEIGHT, // height of each menu item in pixels
@@ -346,39 +331,35 @@ function showMessagesScroller(msg, persist) {
346331
setColor(titleLines.find(e=>e==scrollIdx)!==undefined ? g.theme.fg2 : g.theme.fg).
347332
clearRect(r);
348333
g.setFont(bodyFont).setFontAlign(0,-1).drawString(allLines[scrollIdx], r.x+r.w/2, r.y);
349-
if (prevScrollIdxs[1]!==prevScrollIdxs[0]) {
350-
prevScrollIdxs[1] = prevScrollIdxs[0];
351-
if (!persist) {resetReloadTimeout();}
352-
}
353-
prevScrollIdxs[0] = scrollIdx;
334+
if (!persist) {resetReloadTimeout();}
354335
},
355336
select : function(scrollIdx, touch) {
356337
WU.show();
357-
const MSG_SELECT = identifyDisplayedMsg(scrollIdx);
358338
clearBtnHandler();
359-
if (!touch) {showMessageRouter(MSG_SELECT, true, "overview"); return;}
360-
if (touch.type == 0) {showMessageRouter(MSG_SELECT, true, "overview");}
361-
if (touch.type == 2) {showMessageSettings(MSG_SELECT);}
339+
for (let i=firstTitleLinePerMsg.length-1; i>=0 ; i--) {
340+
if (scrollIdx>=firstTitleLinePerMsg[i]) {
341+
if (!touch || touch.type===0) {showMessageRouter(MESSAGES[i], true,
342+
"overview"); return;}
343+
if (touch.type == 2) {showMessageSettings(MESSAGES[i]);}
344+
break;
345+
}
346+
}
362347
}
363348
});
364349

365-
function clearBtnHandler() {
366-
if (Bangle.btnHandler) {clearWatch(Bangle.btnHandler); Bangle.btnHandler=undefined;}
367-
}
368-
clearBtnHandler();
369-
370350
// If Bangle.js 2 add an external select hw button handler.
371-
Bangle.btnHandler = ((2===process.env.HWVERSION) && (setWatch(()=>{
351+
let btnHandler = ((2===process.env.HWVERSION) && (setWatch(()=>{
372352
Bangle.emit("drag", {dy:0}); // Compatibility with `kineticscroll`, stopping the scroller so it doesn't continue scrolling when the `showMessageOverview` screen is loaded.
373353
// Zero ms timeout as to not move on before the scroller has registered the emitted drag event.
374354
setTimeout(()=>{
375355
if ("messagegui.new.js"===global.__FILE__) {return load();}
376-
const SCROLL_IDX_CENTER_SCREEN = prevScrollIdxs[0]>prevScrollIdxs[1] ?
377-
prevScrollIdxs[0]-LINES_PER_SCREEN/2:prevScrollIdxs[0]+LINES_PER_SCREEN/2;
378-
WU.show();
379-
showMessageOverview(identifyDisplayedMsg(SCROLL_IDX_CENTER_SCREEN).id, true);
356+
Bangle.emit("touch", 1, {x:Bangle.appRect.x2/2, y:Bangle.appRect.y2/2, type:0});
380357
},0);
381-
}, BTN, {edge:'rising'})));
358+
}, BTN, {edge:'rising', repeat:true})));
359+
360+
function clearBtnHandler() {
361+
if (btnHandler) {clearWatch(btnHandler); btnHandler=undefined;}
362+
}
382363
}
383364

384365
function showMessageSettings(msg) {

0 commit comments

Comments
 (0)