2016-09-23 21:57:07 +02:00
|
|
|
/*
|
2022-06-06 17:05:26 +02:00
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2022 Taler Systems S.A.
|
2016-09-23 21:57:07 +02:00
|
|
|
|
2022-06-06 17:05:26 +02:00
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
2016-09-23 21:57:07 +02:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
2022-06-06 17:05:26 +02:00
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
2016-09-23 21:57:07 +02:00
|
|
|
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
|
2022-06-06 17:05:26 +02:00
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2016-09-23 21:57:07 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-04 19:44:28 +01:00
|
|
|
import { platform } from "./platform/background.js";
|
2018-01-22 01:28:07 +01:00
|
|
|
|
2016-09-23 21:57:07 +02:00
|
|
|
/**
|
|
|
|
* Polyfill for requestAnimationFrame, which
|
|
|
|
* doesn't work from a background page.
|
|
|
|
*/
|
2020-04-06 20:02:01 +02:00
|
|
|
function rAF(cb: (ts: number) => void): void {
|
2016-09-23 21:57:07 +02:00
|
|
|
window.setTimeout(() => {
|
|
|
|
cb(performance.now());
|
2016-09-24 02:25:24 +02:00
|
|
|
}, 100 /* 100 ms delay between frames */);
|
2016-09-23 21:57:07 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* Badge for Chrome that renders a Taler logo with a rotating ring if some
|
|
|
|
* background activity is happening.
|
|
|
|
*/
|
2019-12-05 19:38:19 +01:00
|
|
|
export class ChromeBadge {
|
2017-05-28 16:27:34 +02:00
|
|
|
private canvas: HTMLCanvasElement;
|
|
|
|
private ctx: CanvasRenderingContext2D;
|
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.
|
|
|
|
*/
|
2020-04-06 17:45:41 +02:00
|
|
|
private animationRunning = false;
|
2016-09-24 01:31:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the wallet still busy? Note that we do not stop the
|
|
|
|
* animation immediately when the wallet goes idle, but
|
|
|
|
* instead slowly close the gap.
|
|
|
|
*/
|
2020-04-06 17:45:41 +02:00
|
|
|
private isBusy = false;
|
2016-09-24 01:31:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current rotation angle, ranges from 0 to rotationAngleMax.
|
|
|
|
*/
|
2020-04-06 17:45:41 +02:00
|
|
|
private rotationAngle = 0;
|
2016-09-24 01:31:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* While animating, how wide is the current gap in the circle?
|
|
|
|
* Ranges from 0 to openMax.
|
|
|
|
*/
|
2020-04-06 17:45:41 +02:00
|
|
|
private gapWidth = 0;
|
2016-09-24 01:31:52 +02:00
|
|
|
|
2017-12-12 16:39:55 +01:00
|
|
|
/**
|
|
|
|
* Should we show the notification dot?
|
|
|
|
*/
|
|
|
|
private hasNotification = false;
|
|
|
|
|
2016-09-24 01:31:52 +02:00
|
|
|
/**
|
|
|
|
* Maximum value for our rotationAngle, corresponds to 2 Pi.
|
|
|
|
*/
|
2016-09-23 22:15:23 +02:00
|
|
|
static rotationAngleMax = 1000;
|
2016-09-23 21:57:07 +02:00
|
|
|
|
2016-09-24 01:31:52 +02:00
|
|
|
/**
|
|
|
|
* How fast do we rotate? Given in rotation angle (relative to rotationAngleMax) per millisecond.
|
|
|
|
*/
|
|
|
|
static rotationSpeed = 0.5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How fast to we open? Given in rotation angle (relative to rotationAngleMax) per millisecond.
|
|
|
|
*/
|
|
|
|
static openSpeed = 0.15;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How fast to we close? Given as a multiplication factor per frame update.
|
|
|
|
*/
|
|
|
|
static closeSpeed = 0.7;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How far do we open? Given relative to rotationAngleMax.
|
|
|
|
*/
|
|
|
|
static openMax = 100;
|
|
|
|
|
2016-09-23 23:29:54 +02:00
|
|
|
constructor(window?: Window) {
|
|
|
|
// Allow injecting another window for testing
|
2017-05-28 01:10:54 +02:00
|
|
|
const bg = window || chrome.extension.getBackgroundPage();
|
2016-09-26 18:00:21 +02:00
|
|
|
if (!bg) {
|
|
|
|
throw Error("no window available");
|
|
|
|
}
|
2016-09-23 21:57:07 +02:00
|
|
|
this.canvas = bg.document.createElement("canvas");
|
2016-09-24 01:31:52 +02:00
|
|
|
// Note: changing the width here means changing the font
|
|
|
|
// size in draw() as well!
|
2016-09-23 21:57:07 +02:00
|
|
|
this.canvas.width = 32;
|
|
|
|
this.canvas.height = 32;
|
2020-04-06 20:02:01 +02:00
|
|
|
const ctx = this.canvas.getContext("2d");
|
|
|
|
if (!ctx) {
|
|
|
|
throw Error("unable to get canvas context");
|
|
|
|
}
|
|
|
|
this.ctx = ctx;
|
2016-09-23 21:57:07 +02:00
|
|
|
this.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw the badge based on the current state.
|
|
|
|
*/
|
2020-04-06 20:02:01 +02:00
|
|
|
private draw(): void {
|
2016-09-24 02:25:24 +02:00
|
|
|
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
2016-09-23 21:57:07 +02:00
|
|
|
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
2016-09-24 02:25:24 +02:00
|
|
|
|
|
|
|
this.ctx.translate(this.canvas.width / 2, this.canvas.height / 2);
|
|
|
|
|
|
|
|
this.ctx.beginPath();
|
|
|
|
this.ctx.arc(0, 0, this.canvas.width / 2 - 2, 0, 2 * Math.PI);
|
|
|
|
this.ctx.fillStyle = "white";
|
|
|
|
this.ctx.fill();
|
|
|
|
|
|
|
|
// move into the center, off by 2 for aligning the "T" with the bottom
|
2016-09-24 01:31:52 +02:00
|
|
|
// of the circle.
|
2016-09-24 02:25:24 +02:00
|
|
|
this.ctx.translate(0, 2);
|
|
|
|
|
2016-09-24 01:31:52 +02:00
|
|
|
// pick sans-serif font; note: 14px is based on the 32px width above!
|
2016-09-24 02:25:24 +02:00
|
|
|
this.ctx.font = "bold 24px sans-serif";
|
2016-09-24 01:31:52 +02:00
|
|
|
// draw the "T" perfectly centered (x and y) to the current position
|
|
|
|
this.ctx.textAlign = "center";
|
|
|
|
this.ctx.textBaseline = "middle";
|
2016-09-24 02:25:24 +02:00
|
|
|
this.ctx.fillStyle = "black";
|
|
|
|
this.ctx.fillText("T", 0, 0);
|
2016-09-24 01:31:52 +02:00
|
|
|
// now move really into the center
|
2016-09-24 02:25:24 +02:00
|
|
|
this.ctx.translate(0, -2);
|
2016-09-24 01:31:52 +02:00
|
|
|
// start drawing the (possibly open) circle
|
|
|
|
this.ctx.beginPath();
|
2016-09-24 02:25:24 +02:00
|
|
|
this.ctx.lineWidth = 2.5;
|
2016-09-24 01:31:52 +02:00
|
|
|
if (this.animationRunning) {
|
2016-09-24 02:25:24 +02:00
|
|
|
/* Draw circle around the "T" with an opening of this.gapWidth */
|
2017-05-28 01:10:54 +02:00
|
|
|
const aMax = ChromeBadge.rotationAngleMax;
|
2020-03-30 12:39:32 +02:00
|
|
|
const startAngle = (this.rotationAngle / aMax) * Math.PI * 2;
|
|
|
|
const stopAngle =
|
|
|
|
((this.rotationAngle + aMax - this.gapWidth) / aMax) * Math.PI * 2;
|
|
|
|
this.ctx.arc(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this.canvas.width / 2 - 2,
|
|
|
|
/* radius */ startAngle,
|
|
|
|
stopAngle,
|
|
|
|
false,
|
|
|
|
);
|
2017-05-28 01:10:54 +02:00
|
|
|
} else {
|
2016-09-24 02:25:24 +02:00
|
|
|
/* Draw full circle */
|
2020-03-30 12:39:32 +02:00
|
|
|
this.ctx.arc(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this.canvas.width / 2 - 2 /* radius */,
|
|
|
|
0,
|
|
|
|
Math.PI * 2,
|
|
|
|
false,
|
|
|
|
);
|
2016-09-24 01:31:52 +02:00
|
|
|
}
|
|
|
|
this.ctx.stroke();
|
|
|
|
// go back to the origin
|
2016-09-23 21:57:07 +02:00
|
|
|
this.ctx.translate(-this.canvas.width / 2, -this.canvas.height / 2);
|
|
|
|
|
2017-12-12 16:39:55 +01:00
|
|
|
if (this.hasNotification) {
|
|
|
|
// We draw a circle with a soft border in the
|
|
|
|
// lower right corner.
|
|
|
|
const r = 8;
|
|
|
|
const cw = this.canvas.width;
|
|
|
|
const ch = this.canvas.height;
|
|
|
|
this.ctx.beginPath();
|
|
|
|
this.ctx.arc(cw - r, ch - r, r, 0, 2 * Math.PI, false);
|
2020-03-30 12:39:32 +02:00
|
|
|
const gradient = this.ctx.createRadialGradient(
|
|
|
|
cw - r,
|
|
|
|
ch - r,
|
|
|
|
r,
|
|
|
|
cw - r,
|
|
|
|
ch - r,
|
|
|
|
5,
|
|
|
|
);
|
2017-12-12 16:39:55 +01:00
|
|
|
gradient.addColorStop(0, "rgba(255, 255, 255, 1)");
|
|
|
|
gradient.addColorStop(1, "blue");
|
|
|
|
this.ctx.fillStyle = gradient;
|
|
|
|
this.ctx.fill();
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:29:54 +02:00
|
|
|
// Allow running outside the extension for testing
|
2017-05-28 01:10:54 +02:00
|
|
|
// tslint:disable-next-line:no-string-literal
|
2016-09-24 00:54:20 +02:00
|
|
|
if (window["chrome"] && window.chrome["browserAction"]) {
|
2017-04-19 01:12:16 +02:00
|
|
|
try {
|
2020-03-30 12:39:32 +02:00
|
|
|
const imageData = this.ctx.getImageData(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this.canvas.width,
|
|
|
|
this.canvas.height,
|
|
|
|
);
|
2022-01-16 21:54:48 +01:00
|
|
|
chrome.action.setIcon({ imageData });
|
2017-04-19 01:12:16 +02:00
|
|
|
} catch (e) {
|
|
|
|
// Might fail if browser has over-eager canvas fingerprinting countermeasures.
|
|
|
|
// There's nothing we can do then ...
|
|
|
|
}
|
2016-09-23 23:29:54 +02:00
|
|
|
}
|
2016-09-23 21:57:07 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
private animate(): void {
|
2016-09-23 22:15:23 +02:00
|
|
|
if (this.animationRunning) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-23 14:50:12 +01:00
|
|
|
if (platform.isFirefox()) {
|
2018-01-22 01:28:07 +01:00
|
|
|
// Firefox does not support badge animations properly
|
|
|
|
return;
|
|
|
|
}
|
2016-09-23 22:15:23 +02:00
|
|
|
this.animationRunning = true;
|
2020-03-30 12:39:32 +02:00
|
|
|
let start: number | undefined;
|
2020-04-06 20:02:01 +02:00
|
|
|
const step = (timestamp: number): void => {
|
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;
|
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
if (!this.isBusy && 0 === this.gapWidth) {
|
2016-09-23 21:57:07 +02:00
|
|
|
// stop if we're close enough to origin
|
|
|
|
this.rotationAngle = 0;
|
|
|
|
} else {
|
2020-03-30 12:39:32 +02:00
|
|
|
this.rotationAngle =
|
|
|
|
(this.rotationAngle +
|
|
|
|
(timestamp - start) * ChromeBadge.rotationSpeed) %
|
|
|
|
ChromeBadge.rotationAngleMax;
|
2016-09-23 21:57:07 +02:00
|
|
|
}
|
2016-09-24 02:25:24 +02:00
|
|
|
if (this.isBusy) {
|
|
|
|
if (this.gapWidth < ChromeBadge.openMax) {
|
2016-09-24 01:31:52 +02:00
|
|
|
this.gapWidth += ChromeBadge.openSpeed * (timestamp - start);
|
2016-09-24 02:25:24 +02:00
|
|
|
}
|
|
|
|
if (this.gapWidth > ChromeBadge.openMax) {
|
2016-09-24 01:31:52 +02:00
|
|
|
this.gapWidth = ChromeBadge.openMax;
|
2016-09-24 02:25:24 +02:00
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
} else {
|
2016-09-24 02:25:24 +02:00
|
|
|
if (this.gapWidth > 0) {
|
2016-09-24 01:31:52 +02:00
|
|
|
this.gapWidth--;
|
|
|
|
this.gapWidth *= ChromeBadge.closeSpeed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isBusy || this.gapWidth > 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);
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:39:55 +01:00
|
|
|
/**
|
|
|
|
* Draw the badge such that it shows the
|
|
|
|
* user that something happened (balance changed).
|
|
|
|
*/
|
2020-04-06 20:02:01 +02:00
|
|
|
showNotification(): void {
|
2017-12-12 16:39:55 +01:00
|
|
|
this.hasNotification = true;
|
|
|
|
this.draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw the badge without the notification mark.
|
|
|
|
*/
|
2020-04-06 20:02:01 +02:00
|
|
|
clearNotification(): void {
|
2017-12-12 16:39:55 +01:00
|
|
|
this.hasNotification = false;
|
|
|
|
this.draw();
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
startBusy(): void {
|
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();
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
stopBusy(): void {
|
2016-09-23 21:57:07 +02:00
|
|
|
this.isBusy = false;
|
|
|
|
}
|
2016-09-26 18:00:21 +02:00
|
|
|
}
|