diff --git a/apps.json b/apps.json index 13bb5892d..58a67d8c2 100644 --- a/apps.json +++ b/apps.json @@ -4763,7 +4763,7 @@ "tags": "widget", "supports": ["BANGLEJS","BANGLEJS2"], "storage": [ - {"name":"widChargingStatus.wid.js","url":"widget.js"} + {"name":"widChargingStatus.wid.js","url":"./dist/widget.js"} ] }, { diff --git a/apps/widChargingStatus/dist/widget.js b/apps/widChargingStatus/dist/widget.js new file mode 100644 index 000000000..7772a5b87 --- /dev/null +++ b/apps/widChargingStatus/dist/widget.js @@ -0,0 +1,31 @@ +"use strict"; +exports.__esModule = true; +(function () { + var icon = require('heatshrink').decompress(atob('ikggMAiEAgYIBmEAg4EB+EAh0AgPggEeCAIEBnwQBAgP+gEP//x///j//8f//k///H//4BYOP/4lBv4bDvwEB4EAvAEBwEAuA7DCAI7BgAQBhEAA')); + var iconWidth = 18; + function draw() { + g.reset(); + if (Bangle.isCharging()) { + g.setColor('#FD0'); + g.drawImage(icon, this.x + 1, this.y + 1, { + scale: 0.6875 + }); + } + } + WIDGETS.chargingStatus = { + area: 'tr', + width: Bangle.isCharging() ? iconWidth : 0, + draw: draw + }; + Bangle.on('charging', function (charging) { + if (charging) { + Bangle.buzz(); + WIDGETS.chargingStatus.width = iconWidth; + } + else { + WIDGETS.chargingStatus.width = 0; + } + Bangle.drawWidgets(); // re-layout widgets + g.flip(); + }); +})(); diff --git a/apps/widChargingStatus/widget.ts b/apps/widChargingStatus/widget.ts index 90f9199fa..e3ce2d7e2 100644 --- a/apps/widChargingStatus/widget.ts +++ b/apps/widChargingStatus/widget.ts @@ -1,31 +1,37 @@ +import { loadGlobals } from '../../typescript/types/globals'; // TODO find a nicer way to load ambient type definitions than this + (() => { - const icon = require("heatshrink").decompress(atob("ikggMAiEAgYIBmEAg4EB+EAh0AgPggEeCAIEBnwQBAgP+gEP//x///j//8f//k///H//4BYOP/4lBv4bDvwEB4EAvAEBwEAuA7DCAI7BgAQBhEAA")); - const iconWidth = 18; + const icon = require('heatshrink').decompress( + atob( + 'ikggMAiEAgYIBmEAg4EB+EAh0AgPggEeCAIEBnwQBAgP+gEP//x///j//8f//k///H//4BYOP/4lBv4bDvwEB4EAvAEBwEAuA7DCAI7BgAQBhEAA' + ) + ); + const iconWidth = 18; - function draw() { - g.reset(); - if (Bangle.isCharging()) { - g.setColor("#FD0"); - g.drawImage(icon, this.x + 1, this.y + 1, { - scale: 0.6875 - }); - } - } + function draw() { + g.reset(); + if (Bangle.isCharging()) { + g.setColor('#FD0'); + g.drawImage(icon, this.x + 1, this.y + 1, { + scale: 0.6875, + }); + } + } - WIDGETS.chargingStatus = { - area: 'tr', - width: Bangle.isCharging() ? iconWidth : 0, - draw: draw, - }; + WIDGETS.chargingStatus = { + area: 'tr', + width: Bangle.isCharging() ? iconWidth : 0, + draw: draw, + }; - Bangle.on('charging', (charging) => { - if (charging) { - Bangle.buzz(); - WIDGETS.chargingStatus.width = iconWidth; - } else { - WIDGETS.chargingStatus.width = 0; - } - Bangle.drawWidgets(); // re-layout widgets - g.flip(); - }); -})(); \ No newline at end of file + Bangle.on('charging', (charging) => { + if (charging) { + Bangle.buzz(); + WIDGETS.chargingStatus.width = iconWidth; + } else { + WIDGETS.chargingStatus.width = 0; + } + Bangle.drawWidgets(); // re-layout widgets + g.flip(); + }); +})(); diff --git a/package.json b/package.json index b796044c9..1ecfb6280 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,16 @@ "author": "Gordon Williams (http://espruino.com)", "version": "0.0.1", "devDependencies": { - "eslint": "7.1.0" + "eslint": "7.1.0", + "@types/node": "16.11.12", + "typescript": "4.5.2" }, "scripts": { "lint-apps": "eslint ./apps --ext .js", "test": "node bin/sanitycheck.js && eslint ./apps --ext .js", - "start": "npx http-server -c-1" + "start": "npx http-server -c-1", + "build:example": "tsc ./apps/widChargingStatus/widget.ts --outDir ./apps/widChargingStatus/dist", + "build:types": "tsc .typescript/types/globals.d.ts" }, "dependencies": { "acorn": "^7.2.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..46a557e89 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "module": "es2015", + "noImplicitAny": true, + "target": "es2015" + } +} diff --git a/typescript/.gitignore b/typescript/.gitignore deleted file mode 100644 index b2af6e004..000000000 --- a/typescript/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules/ -!package-lock.json \ No newline at end of file diff --git a/typescript/package-lock.json b/typescript/package-lock.json deleted file mode 100644 index 9df411a91..000000000 --- a/typescript/package-lock.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "banglets", - "version": "0.1.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "banglets", - "version": "0.1.0", - "license": "MIT", - "devDependencies": { - "typescript": "^4.5.2" - } - }, - "node_modules/typescript": { - "version": "4.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.2.tgz", - "integrity": "sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==", - "dev": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=4.2.0" - } - } - }, - "dependencies": { - "typescript": { - "version": "4.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.2.tgz", - "integrity": "sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==", - "dev": true - } - } -} diff --git a/typescript/package.json b/typescript/package.json deleted file mode 100644 index 513e7b7df..000000000 --- a/typescript/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "bangle.ts", - "version": "0.1.0", - "description": "Typescript configuration and typings for Bangle.js", - "main": "app.js", - "types": "app.d.ts", - "scripts": { - "build:example": "tsc ../apps/widChargingStatus/widget.ts --outDir ../apps/widChargingStatus/dist", - "build:types": "tsc ./globals.d.ts" - }, - "author": { - "name": "Sebastian Di Luzio", - "email": "sebastian@diluz.io" - }, - "license": "MIT", - "devDependencies": { - "typescript": "^4.5.2" - } -} diff --git a/typescript/tsconfig.json b/typescript/tsconfig.json deleted file mode 100644 index ce8e6278b..000000000 --- a/typescript/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "compilerOptions": { - "module": "es2015", - "noImplicitAny": true, - "target": "es2015", - "outDir": "../dist", - "isolatedModules": false - }, - "include": ["./globals.d.ts"] -} diff --git a/typescript/globals.d.ts b/typescript/types/globals.d.ts similarity index 75% rename from typescript/globals.d.ts rename to typescript/types/globals.d.ts index 702ef26cb..97a40ae54 100644 --- a/typescript/globals.d.ts +++ b/typescript/types/globals.d.ts @@ -1,33 +1,19 @@ // TODO all of these globals (copied from eslintrc need to be typed at some point) /* "globals": { // Methods and Fields at https://banglejs.com/reference - "Array": "readonly", - "ArrayBuffer": "readonly", - "ArrayBufferView": "readonly", - "Bangle": "readonly", "BluetoothDevice": "readonly", "BluetoothRemoteGATTCharacteristic": "readonly", "BluetoothRemoteGATTServer": "readonly", "BluetoothRemoteGATTService": "readonly", - "Boolean": "readonly", - "console": "readonly", "DataView": "readonly", - "Date": "readonly", "E": "readonly", "Error": "readonly", "Flash": "readonly", - "Float32Array": "readonly", - "Float64Array": "readonly", "fs": "readonly", "Function": "readonly", - "Graphics": "readonly", "heatshrink": "readonly", "I2C": "readonly", - "Int16Array": "readonly", - "Int32Array": "readonly", - "Int8Array": "readonly", "InternalError": "readonly", - "JSON": "readonly", "Math": "readonly", "Modules": "readonly", "NRF": "readonly", @@ -122,24 +108,53 @@ "Terminal": "readonly", "trace": "readonly", "VIBRATE": "readonly", - // Aliases and not defined at https://banglejs.com/reference - "g": "readonly", */ -declare const Bangle: { - // functions - buzz: () => void; - drawWidgets: () => void; - isCharging: () => boolean; - // events - on(event: 'charging', listener: (charging: boolean) => void): void; - // TODO add more -}; +export type loadGlobals = {}; -type Widget = { - area: 'tr' | 'tl'; - width: number; - draw: () => void; -}; +declare global { + const Bangle: { + // functions + buzz: () => void; + drawWidgets: () => void; + isCharging: () => boolean; + // events + on(event: 'charging', listener: (charging: boolean) => void): void; + // TODO add more + }; -declare const WIDGETS: { [key: string]: Widget }; + type Image = { + width: number; + height: number; + bpp?: number; + buffer: ArrayBuffer | string; + transparent?: number; + palette?: Uint16Array; + }; + + type GraphicsApi = { + reset: () => void; + flip: () => void; + setColor: (color: string) => void; // TODO we can most likely type color more usefully than this + drawImage: ( + image: string | Image | ArrayBuffer, + xOffset: number, + yOffset: number, + options?: { + rotate?: number; + scale?: number; + } + ) => void; + // TODO add more + }; + + const Graphics: GraphicsApi; + const g: GraphicsApi; + + type Widget = { + area: 'tr' | 'tl'; + width: number; + draw: () => void; + }; + const WIDGETS: { [key: string]: Widget }; +}