247 lines
8.8 KiB
JavaScript
247 lines
8.8 KiB
JavaScript
|
|
let options = {
|
|
srcFont: "6x8",
|
|
titleFont: "12x20",
|
|
bodyFont: "12x20",
|
|
headerBgColor: g.theme.bgH,
|
|
headerFgColor: g.theme.fg,
|
|
margin: 3, // space on sides
|
|
padding: 8, // space between header and body
|
|
fh: 10, // footer height - leave this much space for a footer
|
|
};
|
|
|
|
exports.setOptions = function(opts) {
|
|
options = Object.assign(options, opts);
|
|
};
|
|
|
|
const setClipRect = function (x, y, x2, y2) {
|
|
if (y < Bangle.appRect.y) y = Bangle.appRect.y;
|
|
if (y2 < Bangle.appRect.y) y2 = Bangle.appRect.y;
|
|
if (y > Bangle.appRect.y2 - 10) y = Bangle.appRect.y2 - options.fh - 1;
|
|
if (y2 > Bangle.appRect.y2 - 10) y2 = Bangle.appRect.y2 - options.fh - 1;
|
|
return g.setClipRect(x, y, x2, y2);
|
|
};
|
|
|
|
const centerString = (str, x, y, w) => {
|
|
g.setFontAlign(0, -1);
|
|
g.drawString(str, x + (w / 2), y);
|
|
};
|
|
|
|
const HeaderBox = function(msg, messageRect) {
|
|
this.icon = require("messageicons").getImage(msg);
|
|
this.color = require("messageicons").getColor(msg, {default:g.theme.fg2});
|
|
this.iconW = 48;
|
|
this.titleW = messageRect.w - this.iconW;
|
|
if (!msg.body) this.titleLines = "";
|
|
else {
|
|
this.titleLines = (() => {
|
|
if (msg.title) return g.setFont(options.titleFont).wrapString(msg.title, this.titleW);
|
|
else return "";
|
|
})();
|
|
}
|
|
let sw = g.stringWidth(msg.title); // string width
|
|
let ew = g.stringWidth("..."); // elipsis width
|
|
let w = this.titleW - ew - options.margin; // max width for title
|
|
let cw = sw / msg.title.length; // character width - will only be an average if not a fixed width font
|
|
this.titleOneLine = msg.title.substr(0, Math.floor(w / cw)).concat("...");
|
|
this.titleLineH = g.setFont(options.titleFont).getFontHeight();
|
|
this.titleX = 0;
|
|
this.titleY = 0;
|
|
this.srcLineH = g.setFont(options.srcFont).getFontHeight();
|
|
this.srcLines = g.wrapString(msg.src, this.iconW);
|
|
this.srcH = this.srcLineH * this.srcLines.length;
|
|
this.srcX = this.x2 - this.iconW;
|
|
this.srcY = 0;
|
|
this.im = g.imageMetrics(this.icon);
|
|
this.imgX = this.titleW + ((this.iconW - this.im.width) / 2);
|
|
this.minH = Math.max(this.titleLineH, this.srcH + this.im.height) + options.padding;
|
|
this.maxH = Math.max(this.titleLineH * this.titleLines.length, this.iconW) + options.padding;
|
|
this.h = this.maxH;
|
|
}; // HeaderBox
|
|
|
|
HeaderBox.prototype.draw = function(messageRect) {
|
|
const getImgY = () => (this.h - options.padding - this.im.height + (this.h == this.maxH ? this.srcH : 0)) / 2;
|
|
const x = messageRect.x
|
|
const x2 = messageRect.x2;
|
|
const y = messageRect.y;
|
|
if (this.h < this.minH) this.h = this.minH;
|
|
else if (this.h > this.maxH) this.h = this.maxH;
|
|
let y2 = messageRect.y + this.h - 1;
|
|
setClipRect(x, y, x2, y2);
|
|
g.setBgColor(options.headerBgColor).clearRect(x, y, x2, y2);
|
|
g.setColor(options.headerFgColor);
|
|
g.setFont(options.titleFont);
|
|
if (this.h > this.minH) { // multi-line title
|
|
let titleY = y;
|
|
if (this.titleLines) {
|
|
for (let line of this.titleLines) {
|
|
centerString(line, x, titleY, this.titleW);
|
|
titleY += this.titleLineH;
|
|
}
|
|
}
|
|
} else { // single-line title
|
|
const titleY = Math.floor(((this.h - options.padding - this.titleLineH) / 2) + y);
|
|
centerString(this.titleOneLine, x + options.margin, titleY, this.titleW);
|
|
}
|
|
g.setFont(options.srcFont);
|
|
if (this.h == this.maxH) {
|
|
let srcY = y;
|
|
for (let line of this.srcLines) {
|
|
centerString(line, x2 - this.iconW, srcY, this.iconW);
|
|
srcY += this.srcLineH;
|
|
}
|
|
}
|
|
g.setColor(this.color).drawImage(this.icon, x + this.imgX, y + getImgY());
|
|
g.setColor(g.theme.bg).fillRect(x, y2 - 6, x2, y2); // bar at bottom of header
|
|
};
|
|
|
|
const BodyBox = function(msg, messageRect) {
|
|
if (msg.body) body = msg.body;
|
|
else body = msg.title;
|
|
this.lineH = (g.setFont(options.bodyFont).getFontHeight()) + 2; // plus 2 to add a little spacing between lines
|
|
this.lines = g.wrapString(body, messageRect.w - options.margin);
|
|
if (msg.subject) { // If there's a subject, add it to the top
|
|
lines.splice(0, 0, msg.subject);
|
|
}
|
|
this.initScrollY = 0;
|
|
this.scrollY = this.initScrollY;
|
|
this.minY = messageRect.y2 - (this.lineH * (this.lines.length + 3));
|
|
if (this.minY > 0) this.minY = 0;
|
|
}; // BodyBox
|
|
|
|
BodyBox.prototype.draw = function(messageRect, yOffset) {
|
|
x = messageRect.x;
|
|
y = messageRect.y + yOffset;
|
|
x2 = messageRect.x2;
|
|
y2 = messageRect.y2;
|
|
g.setColor(g.theme.fg).setBgColor(g.theme.bg);
|
|
setClipRect(x, y, x2, y2);
|
|
g.clearRect(x, y, x2, y2);
|
|
g.setFont(options.bodyFont).setFontAlign(-1, -1);
|
|
let lineY = this.scrollY;
|
|
for (let line of this.lines) {
|
|
if (y + lineY >= y - this.lineH) g.drawString(line, x + options.margin, y + lineY);
|
|
lineY += this.lineH;
|
|
}
|
|
};
|
|
|
|
let MessageBox = function(msg, yOffset) {
|
|
this.msg = msg;
|
|
this.yOffset = yOffset ?? 0;
|
|
this.xOffset = 0;
|
|
this.rect = {
|
|
w: Bangle.appRect.w,
|
|
h: Bangle.appRect.h,
|
|
x: Bangle.appRect.x + this.xOffset,
|
|
y: Bangle.appRect.y + this.yOffset,
|
|
x2:Bangle.appRect.x2 + this.xOffset,
|
|
y2: Bangle.appRect.y2 + this.yOffset
|
|
};
|
|
this.headerBox = new HeaderBox(msg, this.rect);
|
|
this.bodyBox = new BodyBox(msg, this.rect);
|
|
let messageH = this.bodyBox.lines.length * this.bodyBox.lineH;
|
|
let boxH = this.rect.h - this.headerBox.maxH;
|
|
if ( messageH <= boxH) this.oneScreen = true;
|
|
else this.oneScreen = false;
|
|
this.top = true;
|
|
if (this.oneScreen) {
|
|
this.bottom = true;
|
|
this.bodyBox.initScrollY = Math.round((boxH - messageH) / 2);
|
|
} else {
|
|
this.bottom = false;
|
|
this.bodyBox.initScrollY = 0;
|
|
}
|
|
this.sbH = this.rect.h / messageH * this.rect.h; // scrollbar height
|
|
this.sbRatio = (this.rect.h - this.sbH) / Math.abs(this.bodyBox.minY); // scrollbar ratio
|
|
}; // MessageBox
|
|
|
|
MessageBox.prototype.prevOffset = Bangle.appRect.y - Bangle.appRect.h;
|
|
MessageBox.prototype.nextOffset = Bangle.appRect.y2 + 1;
|
|
|
|
MessageBox.prototype.reset = function(yOffset) {
|
|
if (!yOffset) yOffset = 0;
|
|
this.yOffset = yOffset;
|
|
this.headerBox.h = this.headerBox.maxH;
|
|
this.bodyBox.scrollY = this.bodyBox.initScrollY;
|
|
this.top = true;
|
|
if (this.oneScreen) this.bottom = true;
|
|
else this.bottom = false;
|
|
};
|
|
|
|
MessageBox.prototype.draw = function() {
|
|
this.rect.x = Bangle.appRect.x + this.xOffset;
|
|
this.rect.x2 = Bangle.appRect.x2 + this.xOffset;
|
|
this.rect.y = Bangle.appRect.y + this.yOffset;
|
|
this.rect.y2 = Bangle.appRect.y2 + this.yOffset;
|
|
setClipRect(this.rect.x, this.rect.y, this.rect.x2, this.rect.y2);
|
|
g.clearRect(this.rect.x, this.rect.y, this.rect.x2, this.rect.y2);
|
|
this.headerBox.draw(this.rect);
|
|
this.bodyBox.draw(this.rect, this.headerBox.h);
|
|
setClipRect(this.rect.x, this.rect.y, this.rect.x2, this.rect.y2);
|
|
this.drawScrollbar();
|
|
//this.drawFooter();
|
|
};
|
|
|
|
MessageBox.prototype.drawScrollbar = function() {
|
|
if (this.oneScreen) return;
|
|
let sbY = this.rect.y + (this.sbRatio * Math.abs(this.bodyBox.scrollY));
|
|
g.setColor(g.theme.fg).drawLine(this.rect.x, sbY, this.rect.x, sbY + this.sbH);
|
|
};
|
|
|
|
MessageBox.prototype.scroll = function(dy) {
|
|
if ((dy > 0 && this.top) || (dy < 0 && this.bottom)) {
|
|
return;
|
|
}
|
|
if (this.headerBox.h > this.headerBox.minH && dy < 0) {
|
|
this.headerBox.h += dy;
|
|
if (this.headerBox.h < this.headerBox.minH) this.headerBox.h = this.headerBox.minH;
|
|
} else if (this.headerBox.h < this.headerBox.maxH && dy > 0) {
|
|
this.headerBox.h += dy;
|
|
if (this.headerBox.h > this.headerBox.maxH) this.headerBox.h = this.headerBox.maxH;
|
|
}
|
|
if (this.headerBox.h == this.headerBox.minH && dy < 0) this.bodyBox.scrollY += dy;
|
|
else if (dy > 0) this.bodyBox.scrollY += dy;
|
|
if (this.bodyBox.scrollY <= this.bodyBox.minY) {
|
|
this.bottom = true;
|
|
this.bodyBox.scrollY = this.bodyBox.minY;
|
|
} else {
|
|
this.bottom = false;
|
|
}
|
|
if (this.bodyBox.scrollY >= 0 && this.headerBox.h == this.headerBox.maxH) {
|
|
this.top = true;
|
|
this.bodyBox.scrollY = this.bodyBox.initScrollY;
|
|
} else {
|
|
this.top = false;
|
|
}
|
|
this.draw();
|
|
};
|
|
|
|
// MessageBox.prototype.drawFooter = function() {
|
|
// // left hint: swipe from left for main menu
|
|
// g.reset().clearRect(Bangle.appRect.x, Bangle.appRect.y2-options.fh, Bangle.appRect.x2, Bangle.appRect.y2)
|
|
// .setFont(fontTiny)
|
|
// .setFontAlign(-1, 1) // bottom left
|
|
// .drawString(
|
|
// "\0"+atob("CAiBAEgkEgkSJEgA"), // >>
|
|
// Bangle.appRect.x + 1, Bangle.appRect.y2
|
|
// );
|
|
// // center message count+hints: swipe up/down for next/prev message
|
|
// const footer = ` ${messageNum+1}/${MESSAGES.length} `;
|
|
// const fw = g.stringWidth(footer);
|
|
// g.setFontAlign(0, 1); // bottom center
|
|
// g.drawString(footer, Bangle.appRect.x+Bangle.appRect.w/2, Bangle.appRect.y2);
|
|
// if (messageNum < MESSAGES.length - 1 && this.bottom)
|
|
// g.drawString("\0"+atob("CAiBAABBIhRJIhQI"), Bangle.appRect.x+Bangle.appRect.w/2-fw/2, Bangle.appRect.y2); // v swipe to next
|
|
// if (messageNum > 0 && this.top)
|
|
// g.drawString("\0"+atob("CAiBABAoRJIoRIIA"), Bangle.appRect.x+Bangle.appRect.w/2+fw/2, Bangle.appRect.y2); // ^ swipe to prev
|
|
// // right hint: swipe from right for message actions
|
|
// g.setFontAlign(1, 1) // bottom right
|
|
// .drawString(
|
|
// "\0"+atob("CAiBABIkSJBIJBIA"), // <<
|
|
// Bangle.appRect.x2 - 1, Bangle.appRect.y2
|
|
// );
|
|
// };
|
|
|
|
module.exports.MessageBox = MessageBox;
|
|
module.exports.setOptions = this.setOptions;
|