Added notification widget

pull/486/head
Gordon Williams 2020-06-04 14:28:44 +01:00
parent a509225eda
commit 3c41379206
6 changed files with 168 additions and 1 deletions

View File

@ -38,7 +38,7 @@
]
},
{ "id": "launch",
"name": "Default Launcher",
"name": "Launcher (Default)",
"shortName":"Launcher",
"icon": "app.png",
"version":"0.03",
@ -70,11 +70,25 @@
"tags": "tool,system,locale,translate",
"type": "locale",
"custom":"locale.html",
"readme": "README.md",
"storage": [
{"name":"locale"}
],
"sortorder" : -10
},
{ "id": "notify",
"name": "Notifications (default)",
"shortName":"Notifications",
"icon": "notify.png",
"version":"0.01",
"description": "A handler for displaying notifications that displays them in a bar at the top of the screen",
"tags": "widget",
"type": "notify",
"readme": "README.md",
"storage": [
{"name":"notify","url":"notify.js"}
]
},
{ "id": "welcome",
"name": "Welcome",
"icon": "app.png",

29
apps/locale/README.md Normal file
View File

@ -0,0 +1,29 @@
# Languages (locale)
Country-specific app internationalisation.
This is not an app, but instead it is a library that can be used by
other applications or widgets to display messages.
## Usage
Some menus that pop up are translated automatically, but if you're
writing an application you can use the `locale` library to
do all the translation for you.
See https://www.espruino.com/Bangle.js+Locale for full examples.
```JS
// Date to date string (long)
>require('locale').date(new Date())
="Donnerstag, 02. April 2020"
// Date to date string (short)
>require('locale').date(new Date(),1)
="02.04.2020"
```
Bangle.js has a `locale` library built in that is just a standard
British English (`en_GB`) localisation - so you can use `locale`
in your apps without requiring users to have this Language library
installed.

1
apps/notify/ChangeLog Normal file
View File

@ -0,0 +1 @@
0.01: New Library!

24
apps/notify/README.md Normal file
View File

@ -0,0 +1,24 @@
# Notifications (default)
A handler for displaying notifications that displays them in a bar at the top of the screen
This is not an app, but instead it is a library that can be used by
other applications or widgets to display messages.
## Usage
```JS
options = {
on : bool, // turn screen on, default true
size : int, // height of notification, default 80 (max)
title : string, // optional title
src : string, // optional source name
body : string, // optional body text
icon : string, // optional icon (image string)
render function(y) {} // function callback to render
};
// eg... show notification
require("notify").show({title:"Test", body:"Hello"});
// remove it (can also be removed by tapping)
require("notify").hide();
```

99
apps/notify/notify.js Normal file
View File

@ -0,0 +1,99 @@
var pos = 0;
/**
options = {
on : bool // turn screen on, default true
size : int // height of notification, default 80 (max)
title : string // optional title
src : string // optional source name
body : string // optional body text
icon : string // optional icon (image string)
render function(y) // function callback to render
}
*/
exports.show = function(options) {
options = options||{};
if (options.on===undefined) options.on=true;
var h = options.size||80;
var oldMode = Bangle.getLCDMode();
// TODO: throw exception if double-buffered?
Bangle.setLCDMode("direct");
var y = 320-h;
var x = 4;
g.setClipRect(0, y, 239, 319);
// clear area
g.setColor(0).fillRect(0, y+1, 239, 317);
// border
g.setColor(0x39C7).fillRect(0, 318, 239, 319);
// top bar
var top = 0;
if (options.title) {
g.setColor(0x39C7).fillRect(0, y, 239, y+20);
g.setColor(-1).setFontAlign(-1, -1, 0).setFont("6x8", 2);
g.drawString(options.title.trim().substring(0, 13), 25, y+3);
y+=20;
}
if (options.src) {
g.setColor(-1).setFontAlign(1, -1, 0).setFont("6x8", 1);
g.drawString(options.src.substring(0, 10), 215, 322-h);
}
if (options.icon) {
let i = options.icon;
g.drawImage(i, x,y+4);
if ("string"==typeof i) x += i.charCodeAt(0);
else x += i[0];
}
// body text
if (options.body) {
var body = options.body;
const maxChars = Math.floor((300-x)/8);
var limit = maxChars;
let row = 1;
let words = body.trim().replace("\n", " ").split(" ");
body = "";
for (var i = 0; i < words.length; i++) {
if (body.length + words[i].length + 1 > limit) {
if (row>=5) {
body += "...";
break;
}
body += "\n " + words[i];
row++;
limit += maxChars;
if (row==5) limit -= 4;
} else {
body += " " + words[i];
}
}
console.log(body);
g.setColor(-1).setFont("6x8", 1).setFontAlign(-1, -1, 0).drawString(body, x-4, y+4);
}
if (options.render) options.render(320 - h);
if (options.on) Bangle.setLCDPower(1); // light up
Bangle.setLCDMode(oldMode); // clears cliprect
function anim() {
pos -= 2;
if (pos < -h) {
pos = -h;
}
Bangle.setLCDOffset(pos);
if (pos > -h) setTimeout(anim, 15);
}
anim();
Bangle.on("touch", exports.hide);
}
exports.hide = function() {
Bangle.removeListener("touch", exports.hide);
function anim() {
pos += 4;
if (pos > 0) pos = 0;
Bangle.setLCDOffset(pos);
if (pos < 0) setTimeout(anim, 10);
}
anim();
}

BIN
apps/notify/notify.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 510 B