add remove-functionality to setUI

reset theme on fastload exit
master
thyttan 2022-09-24 00:23:53 +02:00
parent 1f1b8a20d5
commit a9b9e5cc71
3 changed files with 74 additions and 56 deletions

View File

@ -22,3 +22,4 @@
0.22: Use the new clkinfo module for the menu. 0.22: Use the new clkinfo module for the menu.
0.23: Feedback of apps after run is now optional and decided by the corresponding clkinfo. 0.23: Feedback of apps after run is now optional and decided by the corresponding clkinfo.
0.24: Update clock_info to avoid a redraw 0.24: Update clock_info to avoid a redraw
0.25: Use Bangle.setUI({remove:...}) to allow loading the launcher without a full reset on fw2v16.

View File

@ -1,3 +1,5 @@
{ // must be inside our own scope here so that when we are unloaded everything disappears
/************************************************ /************************************************
* Includes * Includes
*/ */
@ -12,7 +14,7 @@ const clock_info = require("clock_info");
const SETTINGS_FILE = "bwclk.setting.json"; const SETTINGS_FILE = "bwclk.setting.json";
const W = g.getWidth(); const W = g.getWidth();
const H = g.getHeight(); const H = g.getHeight();
var lock_input = false; let lock_input = false;
/************************************************ /************************************************
@ -28,7 +30,7 @@ let settings = {
let saved_settings = storage.readJSON(SETTINGS_FILE, 1) || settings; let saved_settings = storage.readJSON(SETTINGS_FILE, 1) || settings;
for (const key in saved_settings) { for (const key in saved_settings) {
settings[key] = saved_settings[key] settings[key] = saved_settings[key];
} }
/************************************************ /************************************************
@ -74,20 +76,20 @@ Graphics.prototype.setMiniFont = function(scale) {
return this; return this;
}; };
function imgLock(){ let imgLock = function() {
return { return {
width : 16, height : 16, bpp : 1, width : 16, height : 16, bpp : 1,
transparent : 0, transparent : 0,
buffer : E.toArrayBuffer(atob("A8AH4A5wDDAYGBgYP/w//D/8Pnw+fD58Pnw//D/8P/w=")) buffer : E.toArrayBuffer(atob("A8AH4A5wDDAYGBgYP/w//D/8Pnw+fD58Pnw//D/8P/w="))
} };
} };
/************************************************ /************************************************
* Menu * Menu
*/ */
// Custom bwItems menu - therefore, its added here and not in a clkinfo.js file. // Custom bwItems menu - therefore, its added here and not in a clkinfo.js file.
var bwItems = { let bwItems = {
name: null, name: null,
img: null, img: null,
items: [ items: [
@ -99,7 +101,7 @@ var bwItems = {
] ]
}; };
function weekOfYear() { let weekOfYear = function() {
var date = new Date(); var date = new Date();
date.setHours(0, 0, 0, 0); date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year. // Thursday in current week decides the year.
@ -109,11 +111,11 @@ function weekOfYear() {
// Adjust to Thursday in week 1 and count number of weeks from date to week1. // Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7); - 3 + (week1.getDay() + 6) % 7) / 7);
} };
// Load menu // Load menu
var menu = clock_info.load(); let menu = clock_info.load();
menu = menu.concat(bwItems); menu = menu.concat(bwItems);
@ -126,7 +128,7 @@ if(settings.menuPosX >= menu.length || settings.menuPosY > menu[settings.menuPos
// Set draw functions for each item // Set draw functions for each item
menu.forEach((menuItm, x) => { menu.forEach((menuItm, x) => {
menuItm.items.forEach((item, y) => { menuItm.items.forEach((item, y) => {
function drawItem() { let drawItem = function() {
// For the clock, we have a special case, as we don't wanna redraw // For the clock, we have a special case, as we don't wanna redraw
// immediately when something changes. Instead, we update data each minute // immediately when something changes. Instead, we update data each minute
// to save some battery etc. Therefore, we hide (and disable the listener) // to save some battery etc. Therefore, we hide (and disable the listener)
@ -138,14 +140,14 @@ menu.forEach((menuItm, x) => {
var info = item.get(); var info = item.get();
drawMenuItem(info.text, info.img); drawMenuItem(info.text, info.img);
} };
item.on('redraw', drawItem); item.on('redraw', drawItem);
}) });
}); });
function canRunMenuItem(){ let canRunMenuItem = function() {
if(settings.menuPosY == 0){ if(settings.menuPosY == 0){
return false; return false;
} }
@ -153,10 +155,10 @@ function canRunMenuItem(){
var menuEntry = menu[settings.menuPosX]; var menuEntry = menu[settings.menuPosX];
var item = menuEntry.items[settings.menuPosY-1]; var item = menuEntry.items[settings.menuPosY-1];
return item.run !== undefined; return item.run !== undefined;
} };
function runMenuItem(){ let runMenuItem = function() {
if(settings.menuPosY == 0){ if(settings.menuPosY == 0){
return; return;
} }
@ -171,13 +173,13 @@ function runMenuItem(){
} catch (ex) { } catch (ex) {
// Simply ignore it... // Simply ignore it...
} }
} };
/************************************************ /************************************************
* Draw * Draw
*/ */
function draw() { let draw = function() {
// Queue draw again // Queue draw again
queueDraw(); queueDraw();
@ -186,10 +188,10 @@ function draw() {
drawMenuAndTime(); drawMenuAndTime();
drawLock(); drawLock();
drawWidgets(); drawWidgets();
} };
function drawDate(){ let drawDate = function() {
// Draw background // Draw background
var y = H/5*2 + (isFullscreen() ? 0 : 8); var y = H/5*2 + (isFullscreen() ? 0 : 8);
g.reset().clearRect(0,0,W,y); g.reset().clearRect(0,0,W,y);
@ -216,10 +218,10 @@ function drawDate(){
g.setMediumFont(); g.setMediumFont();
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.drawString(dateStr, W/2 - fullDateW / 2, y+2); g.drawString(dateStr, W/2 - fullDateW / 2, y+2);
} };
function drawTime(y, smallText){ let drawTime = function(y, smallText) {
// Draw background // Draw background
var date = new Date(); var date = new Date();
@ -244,9 +246,9 @@ function drawTime(y, smallText){
g.setLargeFont(); g.setLargeFont();
} }
g.drawString(timeStr, W/2, y); g.drawString(timeStr, W/2, y);
} };
function drawMenuItem(text, image){ let drawMenuItem = function(text, image) {
// First clear the time region // First clear the time region
var y = H/5*2 + (isFullscreen() ? 0 : 8); var y = H/5*2 + (isFullscreen() ? 0 : 8);
@ -279,10 +281,10 @@ function drawMenuItem(text, image){
// Draw time // Draw time
drawTime(y, hasText); drawTime(y, hasText);
} };
function drawMenuAndTime(){ let drawMenuAndTime = function() {
var menuEntry = menu[settings.menuPosX]; var menuEntry = menu[settings.menuPosX];
// The first entry is the overview... // The first entry is the overview...
@ -295,34 +297,34 @@ function drawMenuAndTime(){
lock_input = true; lock_input = true;
var item = menuEntry.items[settings.menuPosY-1]; var item = menuEntry.items[settings.menuPosY-1];
item.show(); item.show();
} };
function drawLock(){ let drawLock = function() {
if(settings.showLock && Bangle.isLocked()){ if(settings.showLock && Bangle.isLocked()){
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.drawImage(imgLock(), W-16, 2); g.drawImage(imgLock(), W-16, 2);
} }
} };
function drawWidgets(){ let drawWidgets = function() {
if(isFullscreen()){ if(isFullscreen()){
for (let wd of WIDGETS) {wd.draw=()=>{};wd.area="";} for (let wd of WIDGETS) {wd.draw=()=>{};wd.area="";}
} else { } else {
Bangle.drawWidgets(); Bangle.drawWidgets();
} }
} };
function isFullscreen(){ let isFullscreen = function() {
var s = settings.screen.toLowerCase(); var s = settings.screen.toLowerCase();
if(s == "dynamic"){ if(s == "dynamic"){
return Bangle.isLocked() return Bangle.isLocked();
} else { } else {
return s == "full" return s == "full";
} }
} };
@ -330,29 +332,30 @@ function isFullscreen(){
* Listener * Listener
*/ */
// timeout used to update every minute // timeout used to update every minute
var drawTimeout; let drawTimeout;
// schedule a draw for the next minute // schedule a draw for the next minute
function queueDraw() { let queueDraw = function() {
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(function() { drawTimeout = setTimeout(function() {
drawTimeout = undefined; drawTimeout = undefined;
draw(); draw();
}, 60000 - (Date.now() % 60000)); }, 60000 - (Date.now() % 60000));
} };
// Stop updates when LCD is off, restart when on // Stop updates when LCD is off, restart when on
Bangle.on('lcdPower',on=>{ let lcdListenerBw = function(on) {
if (on) { if (on) {
draw(); // draw immediately, queue redraw draw(); // draw immediately, queue redraw
} else { // stop draw timer } else { // stop draw timer
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined; drawTimeout = undefined;
} }
}); };
Bangle.on('lcdPower', lcdListenerBw);
Bangle.on('lock', function(isLocked) { let lockListenerBw = function(isLocked) {
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined; drawTimeout = undefined;
@ -363,9 +366,10 @@ Bangle.on('lock', function(isLocked) {
} }
draw(); draw();
}); };
Bangle.on('lock', lockListenerBw);
Bangle.on('charging',function(charging) { let chargingListenerBw = function(charging) {
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined; drawTimeout = undefined;
@ -373,9 +377,10 @@ Bangle.on('charging',function(charging) {
settings.menuPosX = 0; settings.menuPosX = 0;
settings.menuPosY = 1; settings.menuPosY = 1;
draw(); draw();
}); };
Bangle.on('charging', chargingListenerBw);
Bangle.on('touch', function(btn, e){ let touchListenerBw = function(btn, e) {
var widget_size = isFullscreen() ? 0 : 20; // Its not exactly 24px -- empirically it seems that 20 worked better... var widget_size = isFullscreen() ? 0 : 20; // Its not exactly 24px -- empirically it seems that 20 worked better...
var left = parseInt(g.getWidth() * 0.22); var left = parseInt(g.getWidth() * 0.22);
var right = g.getWidth() - left; var right = g.getWidth() - left;
@ -431,17 +436,11 @@ Bangle.on('touch', function(btn, e){
runMenuItem(); runMenuItem();
} }
} }
}); };
Bangle.on('touch', touchListenerBw);
E.on("kill", function(){
try{
storage.write(SETTINGS_FILE, settings);
} catch(ex){
// If this fails, we still kill the app...
}
});
let save = () => storage.write(SETTINGS_FILE, settings);
E.on("kill", save);
/************************************************ /************************************************
* Startup Clock * Startup Clock
@ -450,10 +449,26 @@ E.on("kill", function(){
// The upper part is inverse i.e. light if dark and dark if light theme // The upper part is inverse i.e. light if dark and dark if light theme
// is enabled. In order to draw the widgets correctly, we invert the // is enabled. In order to draw the widgets correctly, we invert the
// dark/light theme as well as the colors. // dark/light theme as well as the colors.
let themeBackup = g.theme;
g.setTheme({bg:g.theme.fg,fg:g.theme.bg, dark:!g.theme.dark}).clear(); g.setTheme({bg:g.theme.fg,fg:g.theme.bg, dark:!g.theme.dark}).clear();
// Show launcher when middle button pressed // Show launcher when middle button pressed
Bangle.setUI("clock"); Bangle.setUI({
mode : "clock",
remove : function() {
// Called to unload all of the clock app
Bangle.removeListener('lcdPower', lcdListenerBw);
Bangle.removeListener('lock', lockListenerBw);
Bangle.removeListener('charging', chargingListenerBw);
Bangle.removeListener('touch', touchListenerBw);
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined;
// save settings
save();
E.removeListener("kill", save);
g.setTheme(themeBackup);
}
});
// Load widgets and draw clock the first time // Load widgets and draw clock the first time
Bangle.loadWidgets(); Bangle.loadWidgets();
@ -464,3 +479,5 @@ for (let wd of WIDGETS) {wd._draw=wd.draw; wd._area=wd.area;}
// Draw first time // Draw first time
draw(); draw();
} // End of app scope

View File

@ -1,7 +1,7 @@
{ {
"id": "bwclk", "id": "bwclk",
"name": "BW Clock", "name": "BW Clock",
"version": "0.24", "version": "0.25",
"description": "A very minimalistic clock to mainly show date and time.", "description": "A very minimalistic clock to mainly show date and time.",
"readme": "README.md", "readme": "README.md",
"icon": "app.png", "icon": "app.png",