forked from Blendman974/faceclaw
69 lines
3.5 KiB
TypeScript
69 lines
3.5 KiB
TypeScript
import { getDefaultSmallFont } from "../graphics/ui-fonts";
|
|
import { GrayImage } from "../graphics/image";
|
|
import { truncateText } from "../graphics/textwrap";
|
|
import { ALL_NOTIFICATIONS, readActiveNotifications } from "../native/notification-icons";
|
|
import { newNotificationAppsEnabledByDefault, setNewNotificationAppsEnabledByDefault, setAllNotificationSourcesEnabled, readNotificationSources, setNotificationSourceEnabled, shouldShowNotificationOnGlasses } from "../native/notification-sources";
|
|
import { type InputEvent } from "./gestures";
|
|
import { type Layer, type LayerContext, type PaintBelow } from "./layers";
|
|
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu";
|
|
|
|
/** Refresh rows as new sources arrive, keeping the current selection by package. */
|
|
export class NotificationFilterLayer implements Layer {
|
|
private menu: MenuLayer | null = null;
|
|
private menuWidth = 0;
|
|
/** Stable row keys, including controls, parallel to the menu's items. */
|
|
private packages: string[] = [];
|
|
|
|
private currentMenu(ctx: LayerContext): MenuLayer {
|
|
readActiveNotifications(ALL_NOTIFICATIONS);
|
|
const sources = readNotificationSources().sort((a, b) =>
|
|
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
|
const items: MenuItem[] = [{
|
|
label: "New apps enabled by default",
|
|
onSelect: () => setNewNotificationAppsEnabledByDefault(!newNotificationAppsEnabledByDefault()),
|
|
render: ({ image, x, y, width, selected }) => {
|
|
const font = getDefaultSmallFont();
|
|
drawToggleMenuItem(image, font, x, y, width,
|
|
truncateText(font, "New apps enabled by default", width - 48), newNotificationAppsEnabledByDefault(), selected);
|
|
},
|
|
}, {
|
|
label: "Enable all", onSelect: () => setAllNotificationSourcesEnabled(true),
|
|
}, {
|
|
label: "Disable all", onSelect: () => setAllNotificationSourcesEnabled(false),
|
|
}, ...sources.map<MenuItem>((source) => ({
|
|
label: source.appName,
|
|
onSelect: () => setNotificationSourceEnabled(source, !shouldShowNotificationOnGlasses(source.packageName)),
|
|
render: ({ image, x, y, width, selected }) => {
|
|
const font = getDefaultSmallFont();
|
|
drawToggleMenuItem(image, font, x, y, width, truncateText(font, source.appName, width - 48),
|
|
shouldShowNotificationOnGlasses(source.packageName), selected);
|
|
},
|
|
}))];
|
|
if (!sources.length) items.push({ label: "No notification sources discovered yet.", disabled: true, onSelect: () => {} });
|
|
const selectedPackage = this.menu?.selectedIndex != null ? this.packages[this.menu.selectedIndex] : undefined;
|
|
this.packages = ["default", "enable-all", "disable-all", ...sources.map((source) => `source:${source.packageName}`)];
|
|
const index = selectedPackage === undefined ? 0 : this.packages.indexOf(selectedPackage);
|
|
|
|
const width = ctx.stack.getBaseSize().width;
|
|
if (!this.menu || width !== this.menuWidth) {
|
|
this.menuWidth = width;
|
|
this.menu = new MenuLayer("Notification filter", items, {
|
|
x: 8, y: 8, width: width - 16,
|
|
opaque: true, showBorder: false,
|
|
footer: "On = show notifications on glasses",
|
|
}).selectItem(Math.max(0, index));
|
|
} else {
|
|
this.menu.setItems(items, index >= 0 ? index : undefined);
|
|
}
|
|
return this.menu;
|
|
}
|
|
|
|
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
|
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
|
}
|
|
|
|
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
|
await this.currentMenu(ctx).handleInput(event, ctx);
|
|
}
|
|
}
|