Move badge update code.

This commit is contained in:
Bauke 2023-04-16 12:41:56 +02:00
parent 861c340ec7
commit 451ebb8189
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 18 additions and 23 deletions

View File

@ -161,3 +161,21 @@ export async function nextItem(): Promise<Value<Item> | undefined> {
serialize: serializeItem,
});
}
/**
* Set the WebExtension's badge text to show the current {@link Item} count.
*/
export async function setBadgeText(): Promise<void> {
const keys = await getItemKeys();
const count = keys.length;
const action: browser.Action.Static =
$browser === "firefox" ? browser.browserAction : browser.action;
await action.setBadgeBackgroundColor({color: "#2a2041"});
await action.setBadgeText({text: count === 0 ? "" : count.toString()});
// Only Firefox supports the `setBadgeTextColor` function.
if ($browser === "firefox") {
action.setBadgeTextColor({color: "#f2efff"});
}
}

View File

@ -1,23 +0,0 @@
import browser from 'webextension-polyfill';
import type {Settings} from '../settings/settings.js';
export async function updateBadge(settings: Settings): Promise<void> {
let action: browser.Action.Static = browser.browserAction;
if (import.meta.env.VITE_BROWSER === 'chromium') {
action = browser.action;
}
const queueLength = settings.queue.length.toString();
await action.setBadgeText({
text: queueLength === '0' ? '' : queueLength,
});
await action.setBadgeBackgroundColor({
color: '#2a2041',
});
if (import.meta.env.VITE_BROWSER === 'firefox') {
action.setBadgeTextColor({color: '#f2efff'});
}
}