Skip to content

Commit

Permalink
messagegui: further refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thyttan committed Jan 2, 2025
1 parent 840e832 commit c04ec08
Showing 1 changed file with 50 additions and 47 deletions.
97 changes: 50 additions & 47 deletions apps/messagegui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,65 +250,62 @@ function showMusicMessage(msg) {
function showMessagesScroller(msg, persist, alreadyProcessed) {
if (persist===undefined) {persist = true;}
const MSG_IDX = msg ? MESSAGES.findIndex((m)=>m.id==msg.id) : 0;
const INITIATED_FROM =
const INITIATED_FROM = (
alreadyProcessed===undefined ? "other function" :
(MSG_IDX<=alreadyProcessed.startIdx ? "scrolling up" :
(MSG_IDX >= alreadyProcessed.stopIdx-1 ? "scrolling down" : undefined));
(MSG_IDX<=alreadyProcessed.idxSpan.start ? "scrolling up" :
(MSG_IDX >= alreadyProcessed.idxSpan.stop-1 ? "scrolling down" :
undefined))
);
if (!alreadyProcessed) {alreadyProcessed = {idxSpan:{}};} // So `alreadyProcessed.idxSpan.start/stop` can be looked for on first run.

const WU = require("widget_utils");
WU.hide();

if (replying) { return; }
if (persist) {cancelReloadTimeout();} else {resetReloadTimeout();}

let updatedProcessed = {};
let startIdx = 0;
let stopIdx = 0;

if (INITIATED_FROM === "other function") {
startIdx = Math.max(MSG_IDX-1, 0);
stopIdx = Math.min(MSG_IDX+2, MESSAGES.length);
Object.assign(updatedProcessed, {startIdx:startIdx, stopIdx:stopIdx});
} else if (INITIATED_FROM === "scrolling up") {
startIdx = MSG_IDX;
stopIdx = alreadyProcessed.startIdx;
Object.assign(updatedProcessed,
{startIdx:startIdx, stopIdx:alreadyProcessed.stopIdx});
} else if (INITIATED_FROM === "scrolling down") {
startIdx = alreadyProcessed.stopIdx;
stopIdx = Math.min(MSG_IDX+1, MESSAGES.length);
Object.assign(updatedProcessed,
{startIdx:alreadyProcessed.startIdx, stopIdx:stopIdx});
}
let idxSpan = (
INITIATED_FROM==="other function" ?
{start : Math.max(MSG_IDX-1, 0), stop : Math.min(MSG_IDX+2, MESSAGES.length)} :
(INITIATED_FROM==="scrolling up" ?
{start : MSG_IDX, stop : alreadyProcessed.idxSpan.start } :
(INITIATED_FROM==="scrolling down" ?
{ start : alreadyProcessed.idxSpan.stop, stop : Math.min(MSG_IDX+1, MESSAGES.length) } :
undefined))
);

active = "scroller";
var bodyFont = fontBig;
g.setFont(bodyFont);
var titleLines = [];
var messagesWrapped = [];
for (let i=startIdx ; i<stopIdx ; i++) {
for (let i=idxSpan.start ; i<idxSpan.stop ; i++) {
let msgLocal = MESSAGES[i];
msgLocal.new = false;

if (msgLocal.id=="music" || msgLocal.source=="maps" || msgLocal.id =="call") {
stopIdx++
if (stopIdx>=MESSAGES.length) {break;}
idxSpan.stop++
if (idxSpan.stop>=MESSAGES.length) {break;}
continue;
}

var lines = [];
const TITLE_STRING = "".concat(msgLocal.title, msgLocal.title&&"\n",
msgLocal.sender, msgLocal.sender&&"\n", msgLocal.subject,
msgLocal.subject&&"\n", msgLocal.src) || "No Title";
const TITLE_STRING = msgLocal.title||msgLocal.sender||msgLocal.subject||msgLocal.src||"No Title";
//const TITLE_STRING = "".concat(msgLocal.title, msgLocal.title&&"\n",
// msgLocal.sender, msgLocal.sender&&"\n",
// msgLocal.subject, msgLocal.subject&&"\n", msgLocal.src) || "No Title";
lines = g.wrapString(TITLE_STRING, g.getWidth()-10);
for (let i=0; i<lines.length; i++) {
titleLines.push(i + (messagesWrapped[0]?messagesWrapped[0].length:0) +
(messagesWrapped[1]?messagesWrapped[1].length:0));
}
var titleCnt = lines.length;
if (titleCnt) {lines.push("");} // add blank line after title
//const PAD_LEN = (MESSAGES.length-i)/2;
//const END_LINE = "-".repeat(PAD_LEN)+"<"+"=".repeat(i)+">"+"-".repeat(PAD_LEN);
const END_LINE = "-".repeat(12);
lines = lines.concat(g.wrapString(msgLocal.body, g.getWidth()-10),
["-".repeat(i+1)]);
[END_LINE]);
messagesWrapped.push(lines);
}

Expand All @@ -325,7 +322,7 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
map((x) => x + allLines.length));
allLines = allLines.concat(alreadyProcessed.lines);
} else if (INITIATED_FROM === "scrolling down") {
initScroll = alreadyProcessed.lines.length-9; // FIXME: `-9` corresponds to the Bangle.js 2 screen height - should probably be variable by screen height, font height.
initScroll = alreadyProcessed.lines.length-(g.getHeight()/g.getFontHeight());
titleLines = alreadyProcessed.titleLines.concat(titleLines.
map((x) => x + alreadyProcessed.lines.length));
allLines = alreadyProcessed.lines.concat(allLines);
Expand All @@ -336,49 +333,55 @@ function showMessagesScroller(msg, persist, alreadyProcessed) {
returnToClockIfEmpty();
}

Object.assign(updatedProcessed,
{ lines : allLines,
titleLines : titleLines,
messagesWrappedLength : messagesWrapped.length });
alreadyProcessed = { // Update with the newly processed messages.
lines : allLines,
titleLines : titleLines,
idxSpan: {
start : Math.min(idxSpan.start, alreadyProcessed.idxSpan.start||MESSAGES.length),
stop : Math.max(idxSpan.stop, alreadyProcessed.idxSpan.stop||0)}
};

let prevScrollIdx; // Used for stopping multiple triggers of next message by the scroller.
let prevScrollIdx; // Used for stopping repeated triggering of next message by the scroller.

E.showScroller({
scroll : initScroll*g.getFontHeight(),
h : g.getFontHeight(), // height of each menu item in pixels
c : allLines.length, // number of menu items
// a function to draw a menu item
draw : function(scrollIdx, r) {
"ram"
g.setBgColor(titleLines.find((e)=>e==scrollIdx)!==undefined ? g.theme.bg2 : g.theme.bg).
setColor(titleLines.find((e)=>e==scrollIdx)!==undefined ? g.theme.fg2 : g.theme.fg).
"ram";
g.setBgColor(titleLines.find(e=>e==scrollIdx)!==undefined ? g.theme.bg2 : g.theme.bg).
setColor(titleLines.find(e=>e==scrollIdx)!==undefined ? g.theme.fg2 : g.theme.fg).
clearRect(r);
g.setFont(bodyFont).setFontAlign(0,-1).drawString(allLines[scrollIdx], r.x+r.w/2, r.y);
// Load in next/previous message on demand by reinitializing showMessagesScroller while passing on the processed messages.
if (scrollIdx>=allLines.length-1 && scrollIdx!=prevScrollIdx &&
updatedProcessed.stopIdx<MESSAGES.length) {
alreadyProcessed.idxSpan.stop<MESSAGES.length) {
setTimeout(() => {
E.showScroller();
showMessagesScroller(MESSAGES[updatedProcessed.stopIdx], true, updatedProcessed);
showMessagesScroller(MESSAGES[alreadyProcessed.idxSpan.stop],
true, alreadyProcessed);
}, 40);
}
if (scrollIdx==0 && scrollIdx!=prevScrollIdx && updatedProcessed.startIdx>0) {
if (scrollIdx==0 && scrollIdx!=prevScrollIdx && alreadyProcessed.idxSpan.start>0) {
setTimeout(() => {
E.showScroller();
showMessagesScroller(MESSAGES[updatedProcessed.startIdx-1], true, updatedProcessed);
showMessagesScroller(MESSAGES[alreadyProcessed.idxSpan.start-1],
true, alreadyProcessed);
}, 40);
}
prevScrollIdx = scrollIdx;
}, select : function(scrollIdx, touch) {
},
select : function(scrollIdx, touch) {
let msgSelect;
let titleLinesInitials = [titleLines[0]];
let firstTitleLinePerMsg = [titleLines[0]];
for (let i=1; i<titleLines.length; i++) {
if (titleLines[i]-titleLines[i-1] === 1) {continue;}
titleLinesInitials.push(titleLines[i]);
firstTitleLinePerMsg.push(titleLines[i]);
}
for (let i=titleLines.length-1; i>=0 ; i--) {
if (scrollIdx>=titleLinesInitials[i]) {
msgSelect = MESSAGES[i + updatedProcessed.startIdx];
if (scrollIdx>=firstTitleLinePerMsg[i]) {
msgSelect = MESSAGES[i + alreadyProcessed.idxSpan.start];
break;
}
}
Expand Down

0 comments on commit c04ec08

Please sign in to comment.