more clean up
parent
d93e26be6b
commit
5953a99ac6
|
|
@ -1,233 +1,235 @@
|
||||||
let options = {
|
{
|
||||||
srcFont: "6x8",
|
let options = {
|
||||||
titleFont: "12x20",
|
srcFont: "6x8",
|
||||||
bodyFont: "12x20",
|
titleFont: "12x20",
|
||||||
headerBgColor: g.theme.bgH,
|
bodyFont: "12x20",
|
||||||
headerFgColor: g.theme.fg,
|
headerBgColor: g.theme.bgH,
|
||||||
headerHlColor: g.toColor(0, 1, 0),
|
headerFgColor: g.theme.fg,
|
||||||
margin: 3, // space on sides
|
headerHlColor: g.toColor(0, 1, 0),
|
||||||
padding: 8, // space between header and body
|
margin: 3, // space on sides
|
||||||
fh: 20, // footer height - leave this much space for a footer
|
padding: 8, // space between header and body
|
||||||
};
|
fh: 20, // footer height - leave this much space for a footer
|
||||||
|
};
|
||||||
|
|
||||||
setOptions = function(opts) {
|
setOptions = function(opts) {
|
||||||
options = Object.assign(options, opts);
|
options = Object.assign(options, opts);
|
||||||
};
|
};
|
||||||
|
|
||||||
setClipRect = function (x, y, x2, y2) {
|
setClipRect = function (x, y, x2, y2) {
|
||||||
if (y < Bangle.appRect.y) y = Bangle.appRect.y;
|
if (y < Bangle.appRect.y) y = Bangle.appRect.y;
|
||||||
if (y2 < Bangle.appRect.y) y2 = 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 (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;
|
if (y2 > Bangle.appRect.y2 - 10) y2 = Bangle.appRect.y2 - options.fh - 1;
|
||||||
return g.setClipRect(x, y, x2, y2);
|
return g.setClipRect(x, y, x2, y2);
|
||||||
};
|
};
|
||||||
|
|
||||||
const centerString = (str, x, y, w) => {
|
const centerString = (str, x, y, w) => {
|
||||||
g.setFontAlign(0, -1);
|
g.setFontAlign(0, -1);
|
||||||
g.drawString(str, x + (w / 2), y);
|
g.drawString(str, x + (w / 2), y);
|
||||||
};
|
};
|
||||||
|
|
||||||
HeaderBox = function(msg, messageRect) {
|
HeaderBox = function(msg, messageRect) {
|
||||||
this.icon = require("messageicons").getImage(msg);
|
this.icon = require("messageicons").getImage(msg);
|
||||||
this.color = require("messageicons").getColor(msg, {default:g.theme.fg2});
|
this.color = require("messageicons").getColor(msg, {default:g.theme.fg2});
|
||||||
this.iconSize = 48;
|
this.iconSize = 48;
|
||||||
this.titleW = messageRect.w - this.iconSize;
|
this.titleW = messageRect.w - this.iconSize;
|
||||||
if (!msg.body) this.titleLines = "";
|
if (!msg.body) this.titleLines = "";
|
||||||
else {
|
else {
|
||||||
this.titleLines = (() => {
|
this.titleLines = (() => {
|
||||||
if (msg.title) return g.setFont(options.titleFont).wrapString(msg.title, this.titleW);
|
if (msg.title) return g.setFont(options.titleFont).wrapString(msg.title, this.titleW);
|
||||||
else return "";
|
else return "";
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
let sw = g.stringWidth(msg.title); // string width
|
let sw = g.stringWidth(msg.title); // string width
|
||||||
let ew = g.stringWidth("..."); // elipsis width
|
let ew = g.stringWidth("..."); // elipsis width
|
||||||
let w = this.titleW - ew - options.margin; // max width for title
|
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
|
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.titleOneLine = msg.title.substr(0, Math.floor(w / cw)).concat("...");
|
||||||
this.titleLineH = g.setFont(options.titleFont).getFontHeight();
|
this.titleLineH = g.setFont(options.titleFont).getFontHeight();
|
||||||
this.titleX = 0;
|
this.titleX = 0;
|
||||||
this.titleY = 0;
|
this.titleY = 0;
|
||||||
this.srcLineH = g.setFont(options.srcFont).getFontHeight();
|
this.srcLineH = g.setFont(options.srcFont).getFontHeight();
|
||||||
this.srcLines = g.wrapString(msg.src, this.iconSize);
|
this.srcLines = g.wrapString(msg.src, this.iconSize);
|
||||||
this.srcH = this.srcLineH * this.srcLines.length;
|
this.srcH = this.srcLineH * this.srcLines.length;
|
||||||
this.srcX = this.x2 - this.iconSize;
|
this.srcX = this.x2 - this.iconSize;
|
||||||
this.srcY = 0;
|
this.srcY = 0;
|
||||||
this.im = g.imageMetrics(this.icon);
|
this.im = g.imageMetrics(this.icon);
|
||||||
this.imgX = this.titleW + ((this.iconSize - this.im.width) / 2);
|
this.imgX = this.titleW + ((this.iconSize - this.im.width) / 2);
|
||||||
this.minH = Math.max(this.titleLineH, this.srcH + this.im.height) + options.padding;
|
this.minH = Math.max(this.titleLineH, this.srcH + this.im.height) + options.padding;
|
||||||
this.maxH = Math.max(this.titleLineH * this.titleLines.length, this.iconSize) + options.padding;
|
this.maxH = Math.max(this.titleLineH * this.titleLines.length, this.iconSize) + options.padding;
|
||||||
this.h = this.maxH;
|
this.h = this.maxH;
|
||||||
this.new = msg.new ?? false;
|
this.new = msg.new ?? false;
|
||||||
}; // HeaderBox
|
}; // HeaderBox
|
||||||
|
|
||||||
HeaderBox.prototype.draw = function(messageRect) {
|
HeaderBox.prototype.draw = function(messageRect) {
|
||||||
const getImgY = () => (this.h - options.padding - this.im.height + (this.h == this.maxH ? this.srcH : 0)) / 2;
|
const getImgY = () => (this.h - options.padding - this.im.height + (this.h == this.maxH ? this.srcH : 0)) / 2;
|
||||||
const x = messageRect.x
|
const x = messageRect.x
|
||||||
const x2 = messageRect.x2;
|
const x2 = messageRect.x2;
|
||||||
const y = messageRect.y;
|
const y = messageRect.y;
|
||||||
if (this.h < this.minH) this.h = this.minH;
|
if (this.h < this.minH) this.h = this.minH;
|
||||||
else if (this.h > this.maxH) this.h = this.maxH;
|
else if (this.h > this.maxH) this.h = this.maxH;
|
||||||
let y2 = messageRect.y + this.h - 1;
|
let y2 = messageRect.y + this.h - 1;
|
||||||
exports.setClipRect(x, y, x2, y2);
|
exports.setClipRect(x, y, x2, y2);
|
||||||
g.setBgColor(options.headerBgColor).clearRect(x, y, x2, y2);
|
g.setBgColor(options.headerBgColor).clearRect(x, y, x2, y2);
|
||||||
g.setBgColor(g.theme.bg2).clearRect(x+3, y+3, x2-3, y2-3);
|
g.setBgColor(g.theme.bg2).clearRect(x+3, y+3, x2-3, y2-3);
|
||||||
if (this.new) g.setColor(options.headerHlColor).fillPoly([x, y, x + 20, y, x, y + 20]);
|
if (this.new) g.setColor(options.headerHlColor).fillPoly([x, y, x + 20, y, x, y + 20]);
|
||||||
g.setColor(options.headerFgColor);
|
g.setColor(options.headerFgColor);
|
||||||
g.setFont(options.titleFont);
|
g.setFont(options.titleFont);
|
||||||
if (this.h > this.minH) { // multi-line title
|
if (this.h > this.minH) { // multi-line title
|
||||||
let titleY = Math.floor(((this.h - options.padding - (this.titleLineH * this.titleLines.length)) / 2) + y);
|
let titleY = Math.floor(((this.h - options.padding - (this.titleLineH * this.titleLines.length)) / 2) + y);
|
||||||
if (this.titleLines) {
|
if (this.titleLines) {
|
||||||
for (let line of this.titleLines) {
|
for (let line of this.titleLines) {
|
||||||
centerString(line, x, titleY, this.titleW);
|
centerString(line, x, titleY, this.titleW);
|
||||||
titleY += this.titleLineH;
|
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+4;
|
||||||
|
for (let line of this.srcLines) {
|
||||||
|
centerString(line, x2 - this.iconSize, srcY, this.iconSize);
|
||||||
|
srcY += this.srcLineH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { // single-line title
|
g.setColor(this.color).drawImage(this.icon, x + this.imgX, y + getImgY());
|
||||||
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+4;
|
|
||||||
for (let line of this.srcLines) {
|
|
||||||
centerString(line, x2 - this.iconSize, srcY, this.iconSize);
|
|
||||||
srcY += this.srcLineH;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.setColor(this.color).drawImage(this.icon, x + this.imgX, y + getImgY());
|
|
||||||
};
|
|
||||||
|
|
||||||
const TextBox = 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.scrollY = 0;
|
|
||||||
this.minY = messageRect.y2 - (this.lineH * (this.lines.length + 3));
|
|
||||||
if (this.minY > 0) this.minY = 0;
|
|
||||||
}; // TextBox
|
|
||||||
|
|
||||||
TextBox.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);
|
|
||||||
exports.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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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.textBox = new TextBox(msg, this.rect);
|
|
||||||
let messageH = this.textBox.lines.length * this.textBox.lineH;
|
|
||||||
let boxH = this.rect.h - this.headerBox.maxH - options.fh;
|
|
||||||
if ( messageH <= boxH) this.oneScreen = true;
|
|
||||||
else this.oneScreen = false;
|
|
||||||
this.top = true;
|
|
||||||
if (this.oneScreen) {
|
|
||||||
this.bottom = true;
|
|
||||||
this.textBox.initScrollY = Math.round((boxH - messageH) / 2);
|
|
||||||
} else {
|
|
||||||
this.bottom = false;
|
|
||||||
this.textBox.initScrollY = 0;
|
|
||||||
}
|
|
||||||
this.textBox.scrollY = this.textBox.initScrollY;
|
|
||||||
this.sbH = this.rect.h / messageH * this.rect.h; // scrollbar height
|
|
||||||
this.sbRatio = (this.rect.h - this.sbH) / Math.abs(this.textBox.minY); // scrollbar ratio
|
|
||||||
}; // MessageBox
|
|
||||||
|
|
||||||
MessageBox.prototype.prevOffset = Bangle.appRect.y - Bangle.appRect.h;
|
const TextBox = function(msg, messageRect) {
|
||||||
MessageBox.prototype.nextOffset = Bangle.appRect.y2 + 1;
|
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.scrollY = 0;
|
||||||
|
this.minY = messageRect.y2 - (this.lineH * (this.lines.length + 3));
|
||||||
|
if (this.minY > 0) this.minY = 0;
|
||||||
|
}; // TextBox
|
||||||
|
|
||||||
MessageBox.prototype.setNew = function() {
|
TextBox.prototype.draw = function(messageRect, yOffset) {
|
||||||
this.headerBox.new = true;
|
x = messageRect.x;
|
||||||
}
|
y = messageRect.y + yOffset;
|
||||||
|
x2 = messageRect.x2;
|
||||||
|
y2 = messageRect.y2;
|
||||||
|
g.setColor(g.theme.fg).setBgColor(g.theme.bg);
|
||||||
|
exports.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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
MessageBox.prototype.clearNew = function() {
|
MessageBox = function(msg, yOffset) {
|
||||||
this.headerBox.new = false;
|
this.msg = msg;
|
||||||
}
|
this.yOffset = yOffset ?? 0;
|
||||||
|
this.xOffset = 0;
|
||||||
MessageBox.prototype.reset = function(yOffset) {
|
this.rect = {
|
||||||
if (!yOffset) yOffset = 0;
|
w: Bangle.appRect.w,
|
||||||
this.yOffset = yOffset;
|
h: Bangle.appRect.h,
|
||||||
this.headerBox.h = this.headerBox.maxH;
|
x: Bangle.appRect.x + this.xOffset,
|
||||||
this.textBox.scrollY = this.textBox.initScrollY;
|
y: Bangle.appRect.y + this.yOffset,
|
||||||
this.top = true;
|
x2:Bangle.appRect.x2 + this.xOffset,
|
||||||
if (this.oneScreen) this.bottom = true;
|
y2: Bangle.appRect.y2 + this.yOffset
|
||||||
else this.bottom = false;
|
};
|
||||||
};
|
this.headerBox = new HeaderBox(msg, this.rect);
|
||||||
|
this.textBox = new TextBox(msg, this.rect);
|
||||||
MessageBox.prototype.draw = function() {
|
let messageH = this.textBox.lines.length * this.textBox.lineH;
|
||||||
this.rect.x = Bangle.appRect.x + this.xOffset;
|
let boxH = this.rect.h - this.headerBox.maxH - options.fh;
|
||||||
this.rect.x2 = Bangle.appRect.x2 + this.xOffset;
|
if ( messageH <= boxH) this.oneScreen = true;
|
||||||
this.rect.y = Bangle.appRect.y + this.yOffset;
|
else this.oneScreen = false;
|
||||||
this.rect.y2 = Bangle.appRect.y2 + this.yOffset;
|
|
||||||
exports.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.textBox.draw(this.rect, this.headerBox.h);
|
|
||||||
exports.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.textBox.scrollY));
|
|
||||||
g.setColor(g.theme.fg).drawLine(this.rect.x, sbY, this.rect.x, sbY + this.sbH);
|
|
||||||
};
|
|
||||||
|
|
||||||
MessageBox.prototype.scroll = function(dy) {
|
|
||||||
if (this.oneScreen) return;
|
|
||||||
// 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.textBox.scrollY += dy;
|
|
||||||
else if (dy > 0) this.textBox.scrollY += dy;
|
|
||||||
if (this.textBox.scrollY <= this.textBox.minY) {
|
|
||||||
this.bottom = true;
|
|
||||||
this.textBox.scrollY = this.textBox.minY;
|
|
||||||
} else {
|
|
||||||
this.bottom = false;
|
|
||||||
}
|
|
||||||
if (this.textBox.scrollY >= 0 && this.headerBox.h == this.headerBox.maxH) {
|
|
||||||
this.top = true;
|
this.top = true;
|
||||||
|
if (this.oneScreen) {
|
||||||
|
this.bottom = true;
|
||||||
|
this.textBox.initScrollY = Math.round((boxH - messageH) / 2);
|
||||||
|
} else {
|
||||||
|
this.bottom = false;
|
||||||
|
this.textBox.initScrollY = 0;
|
||||||
|
}
|
||||||
this.textBox.scrollY = this.textBox.initScrollY;
|
this.textBox.scrollY = this.textBox.initScrollY;
|
||||||
} else {
|
this.sbH = this.rect.h / messageH * this.rect.h; // scrollbar height
|
||||||
this.top = false;
|
this.sbRatio = (this.rect.h - this.sbH) / Math.abs(this.textBox.minY); // scrollbar ratio
|
||||||
}
|
}; // MessageBox
|
||||||
this.draw();
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.HeaderBox = HeaderBox;
|
MessageBox.prototype.prevOffset = Bangle.appRect.y - Bangle.appRect.h;
|
||||||
module.exports.MessageBox = MessageBox;
|
MessageBox.prototype.nextOffset = Bangle.appRect.y2 + 1;
|
||||||
module.exports.setOptions = setOptions;
|
|
||||||
module.exports.setClipRect = setClipRect;
|
MessageBox.prototype.setNew = function() {
|
||||||
|
this.headerBox.new = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageBox.prototype.clearNew = function() {
|
||||||
|
this.headerBox.new = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageBox.prototype.reset = function(yOffset) {
|
||||||
|
if (!yOffset) yOffset = 0;
|
||||||
|
this.yOffset = yOffset;
|
||||||
|
this.headerBox.h = this.headerBox.maxH;
|
||||||
|
this.textBox.scrollY = this.textBox.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;
|
||||||
|
exports.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.textBox.draw(this.rect, this.headerBox.h);
|
||||||
|
exports.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.textBox.scrollY));
|
||||||
|
g.setColor(g.theme.fg).drawLine(this.rect.x, sbY, this.rect.x, sbY + this.sbH);
|
||||||
|
};
|
||||||
|
|
||||||
|
MessageBox.prototype.scroll = function(dy) {
|
||||||
|
if (this.oneScreen) return;
|
||||||
|
// 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.textBox.scrollY += dy;
|
||||||
|
else if (dy > 0) this.textBox.scrollY += dy;
|
||||||
|
if (this.textBox.scrollY <= this.textBox.minY) {
|
||||||
|
this.bottom = true;
|
||||||
|
this.textBox.scrollY = this.textBox.minY;
|
||||||
|
} else {
|
||||||
|
this.bottom = false;
|
||||||
|
}
|
||||||
|
if (this.textBox.scrollY >= 0 && this.headerBox.h == this.headerBox.maxH) {
|
||||||
|
this.top = true;
|
||||||
|
this.textBox.scrollY = this.textBox.initScrollY;
|
||||||
|
} else {
|
||||||
|
this.top = false;
|
||||||
|
}
|
||||||
|
this.draw();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.HeaderBox = HeaderBox;
|
||||||
|
module.exports.MessageBox = MessageBox;
|
||||||
|
module.exports.setOptions = setOptions;
|
||||||
|
module.exports.setClipRect = setClipRect;
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,6 @@ let alarm;
|
||||||
let map;
|
let map;
|
||||||
let music;
|
let music;
|
||||||
|
|
||||||
let setActive = function(newActive) {
|
|
||||||
if (!newActive) return;
|
|
||||||
if (active && !previous.includes(active) && newActive !== active) previous.push(active);
|
|
||||||
active = newActive;
|
|
||||||
}
|
|
||||||
|
|
||||||
let setListener = function(event, callback) {
|
let setListener = function(event, callback) {
|
||||||
if (!event || !callback) return;
|
if (!event || !callback) return;
|
||||||
events[event] = callback;
|
events[event] = callback;
|
||||||
|
|
@ -55,6 +49,53 @@ let clearTimeouts = function() {
|
||||||
for (let t in timeouts) clearTimeout(timeouts[t]);
|
for (let t in timeouts) clearTimeout(timeouts[t]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let setActive = function(newActive) {
|
||||||
|
if (!newActive) return;
|
||||||
|
if (active && !previous.includes(active) && newActive !== active) previous.push(active);
|
||||||
|
active = newActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
let setBusy = function() {
|
||||||
|
idle = false;
|
||||||
|
if (timeouts["idleTimer"]) clearTimeout(timeouts["idleTimer"]);
|
||||||
|
timeouts["idleTimer"] = undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
let goBack = function(timedOut) {
|
||||||
|
idle = true;
|
||||||
|
if (buzzing) require("messages").stopBuzz();
|
||||||
|
let backTo;
|
||||||
|
if (previous && previous.length) {
|
||||||
|
backTo = previous.pop();
|
||||||
|
clearTimeouts();
|
||||||
|
} else {
|
||||||
|
if (!timedOut) Bangle.MESSAGES.forEach((m) => {if (!m.new) m.show = false;});
|
||||||
|
require("messages").write(Bangle.MESSAGES);
|
||||||
|
cleanup = _cleanup;
|
||||||
|
cleanup();
|
||||||
|
Bangle.showClock();
|
||||||
|
}
|
||||||
|
switch (backTo) {
|
||||||
|
case "call": return showCall();
|
||||||
|
case "map": return showMap();
|
||||||
|
case "music": return showMusic();
|
||||||
|
case "alarm": return showAlarm();
|
||||||
|
case "text": return showText(textId);
|
||||||
|
}
|
||||||
|
}; // goBack
|
||||||
|
|
||||||
|
let filterMessages = function() {
|
||||||
|
call = Bangle.MESSAGES.find(m => m.type === "call");
|
||||||
|
alarm = Bangle.MESSAGES.find(m => m.type === "alarm");
|
||||||
|
map = Bangle.MESSAGES.find(m => m.type === "map");
|
||||||
|
music = Bangle.MESSAGES.find(m => m.type === "music");
|
||||||
|
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.type === "text");
|
||||||
|
let newUnhandledTexts = Bangle.MESSAGES.filter(m => m.new && !m.handled);
|
||||||
|
let newHandledTexts = Bangle.MESSAGES.filter(m => m.new && m.handled);
|
||||||
|
let otherTexts = Bangle.MESSAGES.filter(m => !m.new);
|
||||||
|
Bangle.MESSAGES = [].concat(newUnhandledTexts, newHandledTexts, otherTexts);
|
||||||
|
};
|
||||||
|
|
||||||
let showNoMessages = function() {
|
let showNoMessages = function() {
|
||||||
g.reset().clear();
|
g.reset().clear();
|
||||||
g.setFont("12x20").setFontAlign(0,0);
|
g.setFont("12x20").setFontAlign(0,0);
|
||||||
|
|
@ -69,6 +110,19 @@ let showNoMessages = function() {
|
||||||
goBack();
|
goBack();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let showMessage = function(id) {
|
||||||
|
if (call) {setActive("call"); return showCall();}
|
||||||
|
else if (alarm) {setActive("alarm"); return showAlarm();}
|
||||||
|
else if (map) {setActive("map"); return showMap();}
|
||||||
|
else if (music) {setActive("music"); return showMusic();}
|
||||||
|
else if (Bangle.MESSAGES.length){
|
||||||
|
Bangle.MESSAGES.every(m => m.handled = true); // set all text messages as handled
|
||||||
|
setActive("text");
|
||||||
|
return showText(id);
|
||||||
|
}
|
||||||
|
else goBack();
|
||||||
|
};
|
||||||
|
|
||||||
let showMusic = function() {
|
let showMusic = function() {
|
||||||
active = "music";
|
active = "music";
|
||||||
|
|
||||||
|
|
@ -531,7 +585,7 @@ let showText = function(id) {
|
||||||
};
|
};
|
||||||
|
|
||||||
let leftHandler = function(e) {
|
let leftHandler = function(e) {
|
||||||
let msgBox = msgBoxes[messageNum];
|
//let msgBox = msgBoxes[messageNum];
|
||||||
switching = true;
|
switching = true;
|
||||||
animateToLeft().then(() => {
|
animateToLeft().then(() => {
|
||||||
g.setBgColor(g.theme.bg);
|
g.setBgColor(g.theme.bg);
|
||||||
|
|
@ -542,7 +596,7 @@ let showText = function(id) {
|
||||||
};
|
};
|
||||||
|
|
||||||
let rightHandler = function(e) {
|
let rightHandler = function(e) {
|
||||||
let msgBox = msgBoxes[messageNum];
|
// let msgBox = msgBoxes[messageNum];
|
||||||
switching = true;
|
switching = true;
|
||||||
animateToRight().then(() => {
|
animateToRight().then(() => {
|
||||||
g.setBgColor(g.theme.bg);
|
g.setBgColor(g.theme.bg);
|
||||||
|
|
@ -584,85 +638,30 @@ let checkForNewMessages = function(idleTime) {
|
||||||
}, idleTime);
|
}, idleTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
let setBusy = function() {
|
|
||||||
idle = false;
|
|
||||||
if (timeouts["idleTimer"]) clearTimeout(timeouts["idleTimer"]);
|
|
||||||
timeouts["idleTimer"] = undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
let goBack = function(timedOut) {
|
|
||||||
idle = true;
|
|
||||||
if (buzzing) require("messages").stopBuzz();
|
|
||||||
let backTo;
|
|
||||||
if (previous && previous.length) {
|
|
||||||
backTo = previous.pop();
|
|
||||||
clearTimeouts();
|
|
||||||
} else {
|
|
||||||
if (!timedOut) Bangle.MESSAGES.forEach((m) => {if (!m.new) m.show = false;});
|
|
||||||
require("messages").write(Bangle.MESSAGES);
|
|
||||||
cleanup = _cleanup;
|
|
||||||
cleanup();
|
|
||||||
Bangle.showClock();
|
|
||||||
}
|
|
||||||
switch (backTo) {
|
|
||||||
case "call": return showCall();
|
|
||||||
case "map": return showMap();
|
|
||||||
case "music": return showMusic();
|
|
||||||
case "alarm": return showAlarm();
|
|
||||||
case "text": return showText(textId);
|
|
||||||
}
|
|
||||||
}; // goBack
|
|
||||||
|
|
||||||
let newMessage = (type, msg) => {
|
let newMessage = (type, msg) => {
|
||||||
filterMessages();
|
filterMessages();
|
||||||
msgId = msg.id;
|
msgId = msg.id;
|
||||||
if (type === "text" && msg.t === "remove") {
|
if (type === "call" && msg.t === "remove") require("messages").stopBuzz();
|
||||||
if (active === "text" && buzzing && msg.id === textId) require("messages").stopBuzz(); // if the message being viewed is removed stop buzzing
|
if (type === "text" && msg.t === "remove") {
|
||||||
|
if (active === "text" && buzzing && msg.id === textId) require("messages").stopBuzz(); // if the message being viewed is removed stop buzzing
|
||||||
let msgIdx = Bangle.MESSAGES.findIndex(m => m.id === msg.id);
|
let msgIdx = Bangle.MESSAGES.findIndex(m => m.id === msg.id);
|
||||||
if (Bangle.MESSAGES.length > 1 && msg.id === msgId) {
|
if (Bangle.MESSAGES.length > 1 && msg.id === msgId) {
|
||||||
if (msgIdx === 0) msgId = Bangle.MESSAGES[msgIdx+1].id;
|
if (msgIdx === 0) msgId = Bangle.MESSAGES[msgIdx+1].id;
|
||||||
else if (msgIdx === Bangle.MESSAGES.length - 1) msgId = Bangle.MESSAGES[msgIdx-1].id;
|
else if (msgIdx === Bangle.MESSAGES.length - 1) msgId = Bangle.MESSAGES[msgIdx-1].id;
|
||||||
else msgId = textId;
|
else msgId = textId;
|
||||||
|
} else if (Bangle.MESSAGES.length === 1) msgId = 0;
|
||||||
} else if (Bangle.MESSAGES.length === 1) msgId = 0;
|
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id); // remove
|
||||||
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id); // remove
|
require("messages").write(Bangle.MESSAGES); // write removal to flash
|
||||||
require("messages").write(Bangle.MESSAGES); // write removal to flash
|
if (textId === msg.id) msgId = 0; // just removed the message we were viewing
|
||||||
if (textId === msg.id) textId = 0; // just removed the message we were viewing
|
}
|
||||||
}
|
if (active === "text" && !idle) {
|
||||||
if (active === "text" && !idle) {
|
haveNewMessage = true;
|
||||||
haveNewMessage = true;
|
return;
|
||||||
return;
|
}
|
||||||
}
|
showMessage(msgId); // we are idle so updated messages right away
|
||||||
showMessage(msgId); // we are idle so updated messages right away
|
|
||||||
};
|
};
|
||||||
setListener("breakingnews", newMessage);
|
setListener("breakingnews", newMessage);
|
||||||
|
|
||||||
let filterMessages = function() {
|
|
||||||
call = Bangle.MESSAGES.find(m => m.type === "call");
|
|
||||||
alarm = Bangle.MESSAGES.find(m => m.type === "alarm");
|
|
||||||
map = Bangle.MESSAGES.find(m => m.type === "map");
|
|
||||||
music = Bangle.MESSAGES.find(m => m.type === "music");
|
|
||||||
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.type === "text");
|
|
||||||
let newUnhandledTexts = Bangle.MESSAGES.filter(m => m.new && !m.handled);
|
|
||||||
let newHandledTexts = Bangle.MESSAGES.filter(m => m.new && m.handled);
|
|
||||||
let otherTexts = Bangle.MESSAGES.filter(m => !m.new && !m.handled);
|
|
||||||
Bangle.MESSAGES = [].concat(newUnhandledTexts, newHandledTexts, otherTexts);
|
|
||||||
};
|
|
||||||
|
|
||||||
let showMessage = function(id) {
|
|
||||||
if (call) {setActive("call"); return showCall();}
|
|
||||||
else if (alarm) {setActive("alarm"); return showAlarm();}
|
|
||||||
else if (map) {setActive("map"); return showMap();}
|
|
||||||
else if (music) {setActive("music"); return showMusic();}
|
|
||||||
else if (Bangle.MESSAGES.length){
|
|
||||||
Bangle.MESSAGES.every(m => m.handled = true); // set all text messages as handled
|
|
||||||
setActive("text");
|
|
||||||
return showText(id);
|
|
||||||
}
|
|
||||||
else goBack();
|
|
||||||
};
|
|
||||||
|
|
||||||
// entry point
|
// entry point
|
||||||
saveMessages = function() {
|
saveMessages = function() {
|
||||||
require("messages").write(Bangle.MESSAGES);
|
require("messages").write(Bangle.MESSAGES);
|
||||||
|
|
@ -680,13 +679,13 @@ if (!Bangle.MESSAGES.length) showNoMessages();
|
||||||
|
|
||||||
if (Bangle.notify) { // notification arrived that opened the app. This is set in messagecenter.notify.js
|
if (Bangle.notify) { // notification arrived that opened the app. This is set in messagecenter.notify.js
|
||||||
let msg = Bangle.MESSAGES[0];
|
let msg = Bangle.MESSAGES[0];
|
||||||
if (msg.id !== "call" && msg.t !== "modify") {
|
if (msg.t !== "delete") {
|
||||||
buzzing = true;
|
buzzing = true;
|
||||||
require("messages").buzz(msg.src);
|
require("messages").buzz(msg.src);
|
||||||
delete Bangle.notify;
|
delete Bangle.notify;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
msgId = Bangle.MESSAGES.length ? Bangle.MESSAGES[0].id : 0;
|
||||||
filterMessages();
|
filterMessages();
|
||||||
textId = Bangle.MESSAGES.length ? Bangle.MESSAGES[0].id : 0;
|
showMessage(msgId);
|
||||||
showMessage(textId);
|
|
||||||
//}
|
//}
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,35 @@
|
||||||
const loadWillReset = () => Bangle.load === load || !Bangle.uiRemove;
|
{
|
||||||
|
const loadWillReset = () => Bangle.load === load || !Bangle.uiRemove;
|
||||||
|
|
||||||
exports.messageListener = function(type, msg) {
|
exports.messageListener = function(type, msg) {
|
||||||
let inApp = false;
|
let inApp = false;
|
||||||
if (msg.handled) return;
|
if (msg.handled) return;
|
||||||
if (global.__FILE__ === "messagecenter.app.js") inApp = true;
|
if (global.__FILE__ === "messagecenter.app.js") inApp = true;
|
||||||
if (!Bangle.MESSAGES || !Bangle.MESSAGES.length) Bangle.MESSAGES = require("messages").getMessages(msg);
|
if (!Bangle.MESSAGES || !Bangle.MESSAGES.length) Bangle.MESSAGES = require("messages").getMessages(msg);
|
||||||
msg.show = true;
|
msg.show = true;
|
||||||
msg.type = type;
|
msg.type = type;
|
||||||
if (msg.id === "call" && msg.t === "remove") {
|
|
||||||
msg.t = "modify"; // not sure why messages module puts everything as remove except for "incoming"
|
|
||||||
msg.new = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.t === "add") {
|
if (msg.t === "add") {
|
||||||
require("messages").apply(msg, Bangle.MESSAGES);
|
require("messages").apply(msg, Bangle.MESSAGES);
|
||||||
} else if (msg.t === "modify") {
|
} else if (msg.t === "modify" || type === "call") {
|
||||||
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id); // remove old version of message
|
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id); // remove old version of message
|
||||||
Bangle.MESSAGES.unshift(msg); // put modified message at start of list
|
Bangle.MESSAGES.unshift(msg); // put modified message at start of list
|
||||||
} // else {
|
|
||||||
// Bangle.MESSAGES.unshift(msg); // add new message to start of list
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (!inApp) {
|
|
||||||
if (msg.t === "remove") {
|
|
||||||
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id);
|
|
||||||
require("messages").write(Bangle.MESSAGES); // write removal to flash
|
|
||||||
return; // don't open app for removal
|
|
||||||
}
|
}
|
||||||
if (loadWillReset()) require("messages").write(Bangle.MESSAGES);
|
if (!inApp) {
|
||||||
Bangle.load("messagecenter.notify.js");
|
if (msg.t === "remove") {
|
||||||
} else { // in app
|
Bangle.MESSAGES = Bangle.MESSAGES.filter(m => m.id != msg.id);
|
||||||
Bangle.emit("breakingnews", type, msg);
|
require("messages").write(Bangle.MESSAGES); // write removal to flash
|
||||||
}
|
return; // don't open app for removal
|
||||||
}; // messageListener
|
}
|
||||||
|
if (loadWillReset()) require("messages").write(Bangle.MESSAGES);
|
||||||
|
Bangle.load("messagecenter.notify.js");
|
||||||
|
} else { // in app
|
||||||
|
Bangle.emit("breakingnews", type, msg);
|
||||||
|
}
|
||||||
|
}; // messageListener
|
||||||
|
|
||||||
|
|
||||||
exports.open = function(msg) {
|
exports.open = function(msg) {
|
||||||
Bangle.load("messagecenter.app.js");
|
Bangle.load("messagecenter.app.js");
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue