wallet-core/lib/wallet/chromeBadge.ts

140 lines
4.0 KiB
TypeScript
Raw Normal View History

2016-09-23 21:57:07 +02:00
/*
This file is part of TALER
(C) 2016 INRIA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import {
Badge
} from "./wallet";
/**
* Polyfill for requestAnimationFrame, which
* doesn't work from a background page.
*/
function rAF(cb: (ts: number) => void) {
window.setTimeout(() => {
cb(performance.now());
}, 100);
}
export class ChromeBadge implements Badge {
canvas: HTMLCanvasElement;
ctx: CanvasRenderingContext2D;
talerLogo: HTMLImageElement;
2016-09-23 22:15:23 +02:00
/**
* True if animation running. The animation
* might still be running even if we're not busy anymore,
* just to transition to the "normal" state in a animated way.
*/
animationRunning: boolean = false;
2016-09-23 21:57:07 +02:00
isBusy: boolean = false;
rotationAngle: number = 0;
2016-09-23 22:15:23 +02:00
static rotationAngleMax = 1000;
2016-09-23 21:57:07 +02:00
2016-09-23 23:29:54 +02:00
constructor(window?: Window) {
// Allow injecting another window for testing
let bg = window || chrome.extension.getBackgroundPage();
2016-09-23 21:57:07 +02:00
this.canvas = bg.document.createElement("canvas");
this.canvas.width = 32;
this.canvas.height = 32;
this.ctx = this.canvas.getContext("2d")!;
this.talerLogo = bg.document.getElementById("taler-logo") as HTMLImageElement;
if (!(this.talerLogo instanceof HTMLImageElement)) {
throw Error();
}
this.draw();
}
/**
* Draw the badge based on the current state.
*/
private draw() {
// Reset to identity
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
this.ctx.translate(this.canvas.width / 2, this.canvas.height / 2);
this.ctx.rotate(this.rotationAngle / ChromeBadge.rotationAngleMax * Math.PI * 2);
this.ctx.translate(-this.canvas.width / 2, -this.canvas.height / 2);
this.ctx.drawImage(this.talerLogo,
0,
0,
this.talerLogo.width,
this.talerLogo.height,
0, 0, this.canvas.width, this.canvas.height);
2016-09-23 23:29:54 +02:00
// Allow running outside the extension for testing
2016-09-24 00:54:20 +02:00
if (window["chrome"] && window.chrome["browserAction"]) {
2016-09-23 23:29:54 +02:00
let imageData = this.ctx.getImageData(0,
0,
this.canvas.width,
this.canvas.height);
chrome.browserAction.setIcon({imageData});
}
2016-09-23 21:57:07 +02:00
}
private animate() {
2016-09-23 22:15:23 +02:00
if (this.animationRunning) {
return;
}
this.animationRunning = true;
2016-09-23 21:57:07 +02:00
let start: number|undefined = undefined;
let step = (timestamp: number) => {
2016-09-23 22:15:23 +02:00
if (!this.animationRunning) {
return;
}
2016-09-23 23:50:41 +02:00
if (!start) {
2016-09-23 21:57:07 +02:00
start = timestamp;
}
let delta = (timestamp - start);
if (!this.isBusy && this.rotationAngle + delta >= ChromeBadge.rotationAngleMax) {
// stop if we're close enough to origin
this.rotationAngle = 0;
} else {
this.rotationAngle = (this.rotationAngle + timestamp - start) % ChromeBadge.rotationAngleMax;
}
if (this.isBusy || this.rotationAngle != 0) {
2016-09-23 22:15:23 +02:00
start = timestamp;
2016-09-23 21:57:07 +02:00
rAF(step);
2016-09-23 22:15:23 +02:00
} else {
this.animationRunning = false;
2016-09-23 21:57:07 +02:00
}
this.draw();
};
rAF(step);
}
setText(s: string) {
chrome.browserAction.setBadgeText({text: s});
}
setColor(c: string) {
chrome.browserAction.setBadgeBackgroundColor({color: c});
}
startBusy() {
2016-09-23 22:15:23 +02:00
if (this.isBusy) {
return;
}
2016-09-23 21:57:07 +02:00
this.isBusy = true;
this.animate();
}
stopBusy() {
this.isBusy = false;
}
}