Three things an installed ihasmail did not do that a phone user expects, and all three are about the app once it is off the browser tab. The unread count was painted into the tab title and the favicon, neither of which exists in `display: standalone` -- so putting ihasmail on a home screen threw the count away entirely. It goes to the Badging API as well now. Web Push marks the icon while the app is closed, and marks it with a dot rather than a figure: the service worker has no session to ask how many messages are unread, and a push carries the new mail rather than a total, so counting the payload would badge "2" over an inbox holding forty. The next tab to open writes the real count over it. Sharing is new. Everything that left ihasmail left as a download, which on a phone is close to a dead end -- the file lands in Downloads and whoever meant to send it somewhere goes looking for it in a file manager. The share sheet is now on the message menu, on each attachment row, and in the file viewer, which is where an attachment is already open and where both callers meet. A message shares as text rather than as the .eml beside it: a share sheet is aimed at everything that is not a mail client, and an .eml in a chat app is an attachment nobody can open. Every control feature-detects, and sharing a file is a separate question from sharing at all -- desktop Linux and Firefox have neither, and not every browser with `share` takes files. Anything that fails, including the transient activation running out while a large attachment is fetched, falls through to the download the button sits beside, so the worst case costs a tap rather than the file. `NotAllowedError` is reported as unsupported for that reason: it cannot be told apart from a refusal, and a toast about activation is not something a reader can act on. The share strings are contextual keys rather than the existing "Share…". That one means granting another account access, and several languages use a different verb for it -- German had "Freigeben" where the sheet wants "Teilen". Three new strings, in all nine catalogues. The manifest gains `launch_handler: navigate-existing`, so a mailto:, a shortcut or a notification tapped while ihasmail is running arrives in the copy that is running: two windows on one inbox disagree about what has been read. `focus-existing` would have been wrong -- it only focuses and leaves the target URL to launchQueue, which nothing here consumes, so it would swallow the mailto. There is deliberately still no `id`, and the manifest now says why: it is the one member resolved against the origin of start_url rather than against the manifest's own address, so no relative form can name a subpath mount, and the default id already is start_url -- writing one now would give every installed copy a new identity and orphan it as a second app. Verified by test rather than on a device: the extension driving Chrome was not connected, and Chrome on Linux has no Web Share to drive anyway. The preview dialog is covered by a component test that stubs the browser both ways.
122 lines
4.2 KiB
TypeScript
122 lines
4.2 KiB
TypeScript
import { withBase } from "./basePath";
|
|
|
|
let baseTitle = "ihasmail";
|
|
let faviconCanvas: HTMLCanvasElement | null = null;
|
|
let baseFavicon: HTMLImageElement | null = null;
|
|
|
|
export function setBaseTitle(t: string) {
|
|
baseTitle = t;
|
|
}
|
|
|
|
/*
|
|
* The unread count on the installed app's icon.
|
|
*
|
|
* The title and the favicon below are the same idea for a tab, and an
|
|
* installed app has neither: in `display: standalone` there is no tab strip
|
|
* and no favicon anywhere on screen, so everything this file did for the
|
|
* unread count vanished at exactly the moment somebody put ihasmail on a home
|
|
* screen. The Badging API is where the count goes instead, and it is the one
|
|
* thing every phone user expects a mail icon to do.
|
|
*
|
|
* Silently nothing where it is unsupported, and silently nothing on iOS until
|
|
* notification permission has been granted, which is that platform's condition
|
|
* for showing a badge at all. Neither is worth reporting: a count that does not
|
|
* appear is not a failure anybody can act on.
|
|
*/
|
|
function setIconBadge(count: number): void {
|
|
if (!("setAppBadge" in navigator)) return;
|
|
const done = count > 0 ? navigator.setAppBadge(count) : navigator.clearAppBadge();
|
|
void done.catch(() => {
|
|
/* unsupported, or not permitted on this platform */
|
|
});
|
|
}
|
|
|
|
/** Update document title, favicon and app icon badge with unread count. */
|
|
export function setUnreadBadge(count: number): void {
|
|
document.title = count > 0 ? `(${count > 999 ? "999+" : count}) ${baseTitle}` : baseTitle;
|
|
setIconBadge(count);
|
|
try {
|
|
const link = document.querySelector<HTMLLinkElement>('link[rel="icon"][type="image/png"]');
|
|
if (!link) return;
|
|
if (!baseFavicon) {
|
|
baseFavicon = new Image();
|
|
baseFavicon.src = withBase("/img/favicon-64.png");
|
|
baseFavicon.onload = () => setUnreadBadge(count);
|
|
return;
|
|
}
|
|
if (!baseFavicon.complete) return;
|
|
if (count <= 0) {
|
|
link.href = withBase("/img/favicon-64.png");
|
|
return;
|
|
}
|
|
faviconCanvas ??= document.createElement("canvas");
|
|
const c = faviconCanvas;
|
|
c.width = 64;
|
|
c.height = 64;
|
|
const ctx = c.getContext("2d");
|
|
if (!ctx) return;
|
|
ctx.clearRect(0, 0, 64, 64);
|
|
ctx.drawImage(baseFavicon, 0, 0, 64, 64);
|
|
ctx.fillStyle = "#dc2626";
|
|
ctx.beginPath();
|
|
ctx.arc(46, 18, 16, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
ctx.fillStyle = "#fff";
|
|
ctx.font = "bold 22px system-ui, sans-serif";
|
|
ctx.textAlign = "center";
|
|
ctx.textBaseline = "middle";
|
|
ctx.fillText(count > 99 ? "99" : String(count), 46, 19);
|
|
link.href = c.toDataURL("image/png");
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
|
|
export async function requestNotificationPermission(): Promise<NotificationPermission> {
|
|
if (!("Notification" in window)) return "denied";
|
|
if (Notification.permission !== "default") return Notification.permission;
|
|
try {
|
|
return await Notification.requestPermission();
|
|
} catch {
|
|
return "denied";
|
|
}
|
|
}
|
|
|
|
export function showNotification(title: string, opts: NotificationOptions & { onClick?: () => void } = {}): void {
|
|
if (!("Notification" in window) || Notification.permission !== "granted") return;
|
|
if (document.visibilityState === "visible" && document.hasFocus()) return;
|
|
try {
|
|
const n = new Notification(title, { icon: withBase("/img/icon-192.png"), badge: withBase("/img/favicon-64.png"), ...opts });
|
|
n.onclick = () => {
|
|
window.focus();
|
|
opts.onClick?.();
|
|
n.close();
|
|
};
|
|
setTimeout(() => n.close(), 8000);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
|
|
let audioCtx: AudioContext | null = null;
|
|
/** Short, soft "ding" using WebAudio (no asset needed). */
|
|
export function playNewMailSound(): void {
|
|
try {
|
|
audioCtx ??= new AudioContext();
|
|
const ctx = audioCtx;
|
|
const o = ctx.createOscillator();
|
|
const g = ctx.createGain();
|
|
o.type = "sine";
|
|
o.frequency.setValueAtTime(880, ctx.currentTime);
|
|
o.frequency.exponentialRampToValueAtTime(1320, ctx.currentTime + 0.08);
|
|
g.gain.setValueAtTime(0.0001, ctx.currentTime);
|
|
g.gain.exponentialRampToValueAtTime(0.15, ctx.currentTime + 0.02);
|
|
g.gain.exponentialRampToValueAtTime(0.0001, ctx.currentTime + 0.4);
|
|
o.connect(g).connect(ctx.destination);
|
|
o.start();
|
|
o.stop(ctx.currentTime + 0.45);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|