Show current volume as badge on toolbar icon

This commit is contained in:
Tronax 2026-08-08 10:53:54 +02:00
parent 78a8c887d8
commit 067cba7195
Signed by: Tronax
SSH key fingerprint: SHA256:2pKKXDZucWvaF/GzXNz0FY53EAO1YDLN80bqS+TTz/o

View file

@ -8,6 +8,26 @@
let creating = null; // Promise-Lock, damit nicht zwei Offscreen-Docs entstehen let creating = null; // Promise-Lock, damit nicht zwei Offscreen-Docs entstehen
// Badge im Toolbar-Icon aktualisieren (pro Tab).
// Bei 100 % wird das Badge ausgeblendet, da das der Normalwert ist.
function updateBadge(tabId, value) {
const pct = Math.round(value * 100);
const text = pct === 100 ? "" : String(pct);
chrome.action.setBadgeText({ tabId, text });
let color;
if (value > 1) color = "#ff9d2f"; // Boost: Amber
else if (value === 0) color = "#ff5d5d"; // Mute: Rot
else color = "#8b8f99"; // Dämpfung: Grau
chrome.action.setBadgeBackgroundColor({ tabId, color });
try {
chrome.action.setBadgeTextColor({ tabId, color: "#1a1c22" });
} catch {
/* setBadgeTextColor nicht überall verfügbar */
}
}
async function ensureOffscreen() { async function ensureOffscreen() {
const contexts = await chrome.runtime.getContexts({ const contexts = await chrome.runtime.getContexts({
contextTypes: ["OFFSCREEN_DOCUMENT"], contextTypes: ["OFFSCREEN_DOCUMENT"],
@ -47,6 +67,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
type: "STOP", type: "STOP",
tabId, tabId,
}); });
updateBadge(tabId, 1);
sendResponse(res || { ok: true, boosting: false }); sendResponse(res || { ok: true, boosting: false });
return; return;
} }
@ -70,6 +91,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
value, value,
}); });
if (gainRes && gainRes.ok) { if (gainRes && gainRes.ok) {
updateBadge(tabId, value);
sendResponse(gainRes); sendResponse(gainRes);
return; return;
} }
@ -88,6 +110,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
streamId, streamId,
value, value,
}); });
if (applyRes && applyRes.ok) updateBadge(tabId, value);
sendResponse( sendResponse(
applyRes || { ok: false, error: "Keine Antwort vom Offscreen-Dokument" } applyRes || { ok: false, error: "Keine Antwort vom Offscreen-Dokument" }
); );
@ -98,6 +121,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
type: "STATE", type: "STATE",
tabId: msg.tabId, tabId: msg.tabId,
}); });
if (res && res.boosting) updateBadge(msg.tabId, res.value);
sendResponse(res || { ok: true, boosting: false, value: 1 }); sendResponse(res || { ok: true, boosting: false, value: 1 });
} else if (msg.type === "RESET") { } else if (msg.type === "RESET") {
await ensureOffscreen(); await ensureOffscreen();
@ -106,6 +130,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
type: "STOP", type: "STOP",
tabId: msg.tabId, tabId: msg.tabId,
}); });
updateBadge(msg.tabId, 1);
sendResponse(res || { ok: true }); sendResponse(res || { ok: true });
} }
} catch (err) { } catch (err) {
@ -121,4 +146,5 @@ chrome.tabs.onRemoved.addListener((tabId) => {
chrome.runtime chrome.runtime
.sendMessage({ target: "offscreen", type: "STOP", tabId }) .sendMessage({ target: "offscreen", type: "STOP", tabId })
.catch(() => {}); .catch(() => {});
chrome.action.setBadgeText({ tabId, text: "" });
}); });