BangleApps/apps/iconlaunch/app.js

182 lines
5.2 KiB
JavaScript
Raw Normal View History

{
const s = require("Storage");
const settings = Object.assign({
showClocks: true,
fullscreen: false,
direct: false,
oneClickExit: false,
swipeExit: false,
timeOut:"Off"
}, s.readJSON("iconlaunch.json", true) || {});
if (!settings.fullscreen) {
2022-10-13 20:26:33 +00:00
Bangle.loadWidgets();
Bangle.drawWidgets();
} else { // for fast-load, if we had widgets then we should hide them
require("widget_utils").hide();
2022-05-24 00:18:26 +00:00
}
let launchCache = s.readJSON("iconlaunch.cache.json", true)||{};
let launchHash = s.hash(/\.info/);
if (launchCache.hash!=launchHash) {
launchCache = {
hash : launchHash,
apps : s.list(/\.info$/)
.map(app=>{let a=s.readJSON(app,1);return a&&{name:a.name,type:a.type,icon:a.icon,sortorder:a.sortorder,src:a.src};})
.filter(app=>app && (app.type=="app" || (app.type=="clock" && settings.showClocks) || !app.type))
.sort((a,b)=>{
let n=(0|a.sortorder)-(0|b.sortorder);
if (n) return n; // do sortorder first
if (a.name<b.name) return -1;
if (a.name>b.name) return 1;
return 0;
}) };
s.writeJSON("iconlaunch.cache.json", launchCache);
}
2022-11-16 20:59:58 +00:00
// cache items
const ICON_MISSING = s.read("iconlaunch.na.img");
let count = 0;
let selectedItem = -1;
const R = Bangle.appRect;
const iconSize = 48;
const appsN = Math.floor(R.w / iconSize);
const whitespace = Math.floor((R.w - appsN * iconSize) / (appsN + 1));
const iconYoffset = Math.floor(whitespace/4)-1;
const itemSize = iconSize + whitespace;
launchCache.items = {};
for (let c of launchCache.apps){
let i = Math.floor(count/appsN);
if (!launchCache.items[i])
launchCache.items[i] = {};
launchCache.items[i][(count%3)] = c;
count++;
}
let texted;
let drawItem = function(itemI, r) {
let x = whitespace;
let i = itemI * appsN - 1;
let selectedApp;
let c;
let selectedRect;
let item = launchCache.items[itemI];
if (texted == itemI){
g.clearRect(r.x, r.y, r.x + r.w - 1, r.y + r.h - 1);
texted = undefined;
}
for (c of item) {
i++;
let id = c.icondata || (c.iconData = (c.icon ? s.read(c.icon) : ICON_MISSING));
g.drawImage(id,x + r.x - 1, r.y + iconYoffset - 1, x + r.x + iconSize, r.y + iconYoffset + iconSize);
if (selectedItem == i) {
selectedApp = c;
selectedRect = [
x + r.x - 1,
r.y + iconYoffset - 1,
x + r.x + iconSize,
r.y + iconYoffset + iconSize
];
}
x += iconSize + whitespace;
}
if (selectedRect) {
g.drawRect.apply(null, selectedRect);
drawText(itemI, r.y, selectedApp);
texted=itemI;
}
};
2022-11-16 20:59:58 +00:00
let drawText = function(i, appY, selectedApp) {
"jit";
const idy = (selectedItem - (selectedItem % 3)) / 3;
if (i != idy) return;
2022-11-16 20:59:58 +00:00
appY = appY + itemSize/2;
g.setFontAlign(0, 0, 0);
g.setFont("12x20");
const rect = g.stringMetrics(selectedApp.name);
g.clearRect(
2022-11-16 20:59:58 +00:00
R.w / 2 - rect.width / 2 - 2,
appY - rect.height / 2 - 2,
R.w / 2 + rect.width / 2 + 1,
appY + rect.height / 2 + 1
);
g.drawString(selectedApp.name, R.w / 2, appY);
};
2022-11-16 20:59:58 +00:00
let selectItem = function(id, e) {
const iconN = E.clip(Math.floor((e.x - R.x) / itemSize), 0, appsN - 1);
const appId = id * appsN + iconN;
if( settings.direct && launchCache.apps[appId])
{
2022-11-15 16:56:28 +00:00
load(launchCache.apps[appId].src);
return;
}
if (appId == selectedItem && launchCache.apps[appId]) {
const app = launchCache.apps[appId];
if (!app.src || s.read(app.src) === undefined) {
E.showMessage( /*LANG*/ "App Source\nNot found");
} else {
2022-11-15 16:56:28 +00:00
load(app.src);
}
}
selectedItem = appId;
2022-11-16 20:59:58 +00:00
if (scroller) scroller.draw();
};
const itemsN = Math.ceil(launchCache.apps.length / appsN);
2022-11-16 20:59:58 +00:00
2023-03-02 19:11:34 +00:00
let idWatch = null;
2022-11-16 20:59:58 +00:00
let options = {
h: itemSize,
c: itemsN,
draw: drawItem,
select: selectItem,
2022-11-14 17:42:06 +00:00
remove: function() {
if (timeout) clearTimeout(timeout);
2022-11-16 20:59:58 +00:00
Bangle.removeListener("drag", updateTimeout);
Bangle.removeListener("touch", updateTimeout);
Bangle.removeListener("swipe", swipeHandler);
if (settings.fullscreen) { // for fast-load, if we hid widgets then we should show them again
require("widget_utils").show();
}
2023-03-02 19:11:34 +00:00
if(idWatch) clearWatch(idWatch);
},
btn:Bangle.showClock
};
2023-03-02 19:11:34 +00:00
//work both the fullscreen and the oneClickExit
if( settings.fullscreen && settings.oneClickExit)
{
idWatch=setWatch(function(e) {
2023-03-02 19:11:34 +00:00
Bangle.showClock();
}, BTN, {repeat:false, edge:'rising' });
2023-03-02 19:11:34 +00:00
}
else if( settings.oneClickExit )
2023-03-02 19:11:34 +00:00
{
options.back=Bangle.showClock;
}
2023-03-02 19:11:34 +00:00
2022-11-16 20:59:58 +00:00
let scroller = E.showScroller(options);
let timeout;
const updateTimeout = function(){
if (settings.timeOut!="Off"){
let time=parseInt(settings.timeOut); //the "s" will be trimmed by the parseInt
if (timeout) clearTimeout(timeout);
timeout = setTimeout(Bangle.showClock,time*1000);
}
2022-11-14 17:42:06 +00:00
};
2022-11-16 20:59:58 +00:00
let swipeHandler = (h,_) => { if(settings.swipeExit && h==1) { Bangle.showClock(); } };
2022-11-16 20:59:58 +00:00
Bangle.on("swipe", swipeHandler)
Bangle.on("drag", updateTimeout);
Bangle.on("touch", updateTimeout);
2022-11-16 20:59:58 +00:00
updateTimeout();
2023-03-02 19:11:34 +00:00
}