forked from Blendman974/faceclaw
UI improvements
Menus can paginate and be taller than quarter-screen. Text config setting editor selects all on enter, exits on confirm. Notification icons are displayed on the notifications-list and single-notification pages.
This commit is contained in:
1 parent
049824f489
commit
d2a942f5a3
11 files changed
+223
-26
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { ApplicationSettings } from "@nativescript/core";
|
||||
import { getDefaultSmallFont } from "~/graphics/bdffont";
|
||||
import { wrapText } from "~/graphics/textwrap";
|
||||
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, MenuLayer } from "./menu";
|
||||
import { DashboardInputEvent, Layer, type LayerContext } from "./layers";
|
||||
import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "~/graphics/image";
|
||||
@@ -403,7 +404,7 @@ function truncateSetting(value: string, maxLength = 22): string {
|
||||
return text.length <= maxLength ? text : `${text.slice(0, Math.max(0, maxLength - 3))}...`;
|
||||
}
|
||||
|
||||
class EditTextSettingLayer implements Layer {
|
||||
export class EditTextSettingLayer implements Layer {
|
||||
constructor(private readonly setting: ConfigSettingString) {}
|
||||
|
||||
paint(): GrayImage {
|
||||
@@ -411,8 +412,10 @@ class EditTextSettingLayer implements Layer {
|
||||
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
|
||||
image.drawText(font, 22, 16, this.setting.glassesEditTitle, 220);
|
||||
image.drawText(font, 22, 52, "Look at the phone app", 200);
|
||||
image.drawText(font, 22, 70, "to type a value.", 200);
|
||||
const message = wrapText(font, "Look at the phone app to type a value.", G2_LENS_WIDTH - 48);
|
||||
for (let index = 0; index < message.length; index++) {
|
||||
image.drawText(font, 22, 52 + index * 14, message[index]!, 200);
|
||||
}
|
||||
image.drawText(font, 22, 110, truncateSetting(this.setting.get(), 52), 220);
|
||||
image.drawText(font, 22, 252, "Double-click to go back", 110);
|
||||
return image;
|
||||
|
||||
+10
-2
@@ -5,7 +5,7 @@ import { mediaControllerBridge } from "../native/media-controller";
|
||||
import { nightscoutBridge } from "../native/nightscout-bridge";
|
||||
import { EventSourceType, OsEventTypeList } from "../g2/events";
|
||||
import { getDashboardPlugin, type DashboardPluginCardBounds, type DashboardPluginState } from "./dashboard-plugins";
|
||||
import { isNightscoutSettingsConfigured, screenTimeoutSettingToMs, screenTimeoutSetting, type DashboardSlotId, DashboardPluginId, bottomRightSlotSetting, bottomLeftSlotSetting, dashboardSlotIds } from "./dashboard-settings";
|
||||
import { EditTextSettingLayer, isNightscoutSettingsConfigured, screenTimeoutSettingToMs, screenTimeoutSetting, type DashboardSlotId, DashboardPluginId, bottomRightSlotSetting, bottomLeftSlotSetting, dashboardSlotIds } from "./dashboard-settings";
|
||||
import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers";
|
||||
import { SingleNotificationLayer } from "./notifications";
|
||||
import { TelepromptLayer } from "./apps/teleprompt";
|
||||
@@ -51,7 +51,7 @@ const dashboardActions: LayerActions = {
|
||||
playBuzzerNote: () => {},
|
||||
};
|
||||
const dashboardFont = getDefaultSmallFont();
|
||||
export const TOP_LEFT_MENU_LAYOUT = { x: 8, y: 8, width: 272, height: 128 };
|
||||
export const TOP_LEFT_MENU_LAYOUT = { x: 8, y: 8, width: 272 };
|
||||
|
||||
function rawInputEventToInputEvent(event: RawInputEvent): DashboardInputEvent {
|
||||
if (event.kind === "sys-event") {
|
||||
@@ -136,6 +136,14 @@ export function setDashboardActions(actions: Partial<LayerActions>): void {
|
||||
dashboardLayers.setActions(actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the glasses-side text-setting edit page, e.g. when the user finishes
|
||||
* typing on the phone. Returns whether the display changed.
|
||||
*/
|
||||
export function closeDashboardTextSettingEditor(): boolean {
|
||||
return dashboardLayers.popIfTop((layer) => layer instanceof EditTextSettingLayer);
|
||||
}
|
||||
|
||||
export function drawDashboard(): GrayImage {
|
||||
if (!dashboardState.screenOn) {
|
||||
return new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
|
||||
@@ -57,6 +57,15 @@ export class LayerStack {
|
||||
}
|
||||
}
|
||||
|
||||
/** Pop the top layer only if it matches; returns whether a layer was popped. */
|
||||
popIfTop(predicate: (layer: Layer) => boolean): boolean {
|
||||
if (this.layers.length > 1 && predicate(this.layers[this.layers.length - 1]!)) {
|
||||
this.layers.pop();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
clearToBase(): void {
|
||||
this.layers.splice(1);
|
||||
}
|
||||
|
||||
+53
-12
@@ -1,14 +1,18 @@
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { G2_LENS_HEIGHT, GrayImage } from "../graphics/image";
|
||||
import { wrapText } from "../graphics/textwrap";
|
||||
import { getDefaultSmallFont, type BdfFont } from "../graphics/bdffont";
|
||||
import { clamp } from "../util/numeric-util";
|
||||
import { DashboardInputEvent, Layer, LayerContext, PaintBelow } from "./layers";
|
||||
|
||||
const DEFAULT_MENU_X = 8;
|
||||
const DEFAULT_MENU_Y = 8;
|
||||
const DEFAULT_MENU_WIDTH = 272;
|
||||
const DEFAULT_MENU_HEIGHT = 128;
|
||||
// Menus grow with their item count, from half the screen (matching the old
|
||||
// fixed quarter-screen-era look) up to the full screen, then scroll.
|
||||
const DEFAULT_MENU_MIN_HEIGHT = G2_LENS_HEIGHT / 2 - 2 * DEFAULT_MENU_Y;
|
||||
const MENU_TITLE_HEIGHT = 16;
|
||||
const MENU_ROW_HEIGHT = 20;
|
||||
const MENU_BODY_PADDING = 8;
|
||||
const MENU_HIGHLIGHT_Y_OFFSET = 0;
|
||||
const MENU_TOGGLE_SWITCH_Y_OFFSET = 1;
|
||||
const MENU_HIGHLIGHT_HEIGHT = MENU_ROW_HEIGHT - 1;
|
||||
@@ -19,7 +23,10 @@ export type MenuLayout = {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
/** Smallest box to draw even when items don't fill it. Default: top half of the screen. */
|
||||
minHeight?: number;
|
||||
/** Height cap before the menu starts scrolling. Default: the full screen. */
|
||||
maxHeight?: number;
|
||||
};
|
||||
|
||||
export type MenuItemRenderArgs = {
|
||||
@@ -78,6 +85,7 @@ export function drawRightValueMenuItem(
|
||||
|
||||
export class MenuLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
|
||||
constructor(
|
||||
private readonly title: string | null,
|
||||
@@ -86,14 +94,24 @@ export class MenuLayer implements Layer {
|
||||
x: DEFAULT_MENU_X,
|
||||
y: DEFAULT_MENU_Y,
|
||||
width: DEFAULT_MENU_WIDTH,
|
||||
height: DEFAULT_MENU_HEIGHT,
|
||||
},
|
||||
public readonly paintOverBase = false,
|
||||
) {}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { x, y, width, height } = this.layout;
|
||||
const { x, y, width } = this.layout;
|
||||
const chromeTop = (this.title ? MENU_TITLE_HEIGHT : 0) + MENU_BODY_PADDING;
|
||||
const minHeight = this.layout.minHeight ?? DEFAULT_MENU_MIN_HEIGHT;
|
||||
const maxHeight = Math.min(
|
||||
this.layout.maxHeight ?? G2_LENS_HEIGHT - y - DEFAULT_MENU_Y,
|
||||
G2_LENS_HEIGHT - y,
|
||||
);
|
||||
const contentHeight = chromeTop + this.items.length * MENU_ROW_HEIGHT + MENU_BODY_PADDING;
|
||||
const height = clamp(contentHeight, Math.min(minHeight, maxHeight), maxHeight);
|
||||
const visibleRowCount = Math.max(1, ((height - chromeTop - MENU_BODY_PADDING) / MENU_ROW_HEIGHT) | 0);
|
||||
this.clampScrollToSelection(visibleRowCount);
|
||||
|
||||
const image = paintBelow();
|
||||
image.fillRoundedRect(x, y, width, height, 0);
|
||||
image.drawRoundedRect(x, y, width, height, 72);
|
||||
@@ -101,20 +119,21 @@ export class MenuLayer implements Layer {
|
||||
image.drawText(font, x + 12, y + 8, this.title, 220);
|
||||
}
|
||||
|
||||
const bodyY = y + (this.title ? MENU_TITLE_HEIGHT : 0) + 8;
|
||||
for (let index = 0; index < this.items.length; index++) {
|
||||
const bodyY = y + chromeTop;
|
||||
const lastVisibleRow = Math.min(this.items.length, this.scrollRow + visibleRowCount);
|
||||
for (let index = this.scrollRow; index < lastVisibleRow; index++) {
|
||||
const item = this.items[index]!;
|
||||
const y = bodyY + index * MENU_ROW_HEIGHT;
|
||||
const rowY = bodyY + (index - this.scrollRow) * MENU_ROW_HEIGHT;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
image.fillRoundedRect(x + 12, y + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL);
|
||||
image.drawRoundedRect(x + 12, y + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE);
|
||||
image.fillRoundedRect(x + 12, rowY + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL);
|
||||
image.drawRoundedRect(x + 12, rowY + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE);
|
||||
}
|
||||
if (item.render) {
|
||||
item.render({
|
||||
image,
|
||||
x: x + 22,
|
||||
y,
|
||||
y: rowY,
|
||||
width: width - 44,
|
||||
height: MENU_ROW_HEIGHT - 3,
|
||||
selected,
|
||||
@@ -122,13 +141,35 @@ export class MenuLayer implements Layer {
|
||||
ctx,
|
||||
});
|
||||
} else {
|
||||
image.drawText(font, x + 22, y + 3, item.label, selected ? 255 : 200);
|
||||
image.drawText(font, x + 22, rowY + 3, item.label, selected ? 255 : 200);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.items.length > visibleRowCount) {
|
||||
this.drawScrollbar(image, x + width - 7, bodyY, visibleRowCount);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private clampScrollToSelection(visibleRowCount: number): void {
|
||||
if (this.selectedIndex < this.scrollRow) {
|
||||
this.scrollRow = this.selectedIndex;
|
||||
} else if (this.selectedIndex >= this.scrollRow + visibleRowCount) {
|
||||
this.scrollRow = this.selectedIndex - visibleRowCount + 1;
|
||||
}
|
||||
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, this.items.length - visibleRowCount));
|
||||
}
|
||||
|
||||
private drawScrollbar(image: GrayImage, trackX: number, trackY: number, visibleRowCount: number): void {
|
||||
const trackHeight = visibleRowCount * MENU_ROW_HEIGHT - 4;
|
||||
image.fillRect(trackX, trackY, 3, trackHeight, 30);
|
||||
const thumbHeight = Math.max(8, (trackHeight * visibleRowCount / this.items.length) | 0);
|
||||
const maxScrollRow = this.items.length - visibleRowCount;
|
||||
const thumbY = trackY + (((trackHeight - thumbHeight) * this.scrollRow / maxScrollRow) | 0);
|
||||
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (!this.items.length) {
|
||||
if (event.type === "double-click") {
|
||||
|
||||
+32
-6
@@ -7,9 +7,11 @@ import {
|
||||
dismissNotification,
|
||||
invokeNotificationAction,
|
||||
readActiveNotifications,
|
||||
readNotificationIconByKey,
|
||||
type AndroidNotification,
|
||||
type AndroidNotificationAction,
|
||||
} from "../native/notification-icons";
|
||||
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../util/render-freshness";
|
||||
import { type DashboardInputEvent, type Layer, type LayerContext, type PaintBelow } from "./layers";
|
||||
|
||||
const PAGE_X = 12;
|
||||
@@ -18,11 +20,23 @@ const LIST_TOP = 38;
|
||||
const LIST_BOTTOM = G2_LENS_HEIGHT;
|
||||
const CARD_X = 20;
|
||||
const CARD_WIDTH = G2_LENS_WIDTH - 40;
|
||||
const CARD_TEXT_WIDTH = CARD_WIDTH - 24;
|
||||
const ICON_SIZE = 24;
|
||||
const ICON_TEXT_GAP = 8;
|
||||
const CARD_TEXT_X = 10 + ICON_SIZE + ICON_TEXT_GAP;
|
||||
const CARD_TEXT_WIDTH = CARD_WIDTH - CARD_TEXT_X - 14;
|
||||
const LINE_HEIGHT = 14;
|
||||
const CARD_GAP = 6;
|
||||
const MAX_NOTIFICATIONS = 50;
|
||||
|
||||
/** Icon for a paint pass: allow-stale, reporting staleness to the render loop. */
|
||||
function iconForNotification(key: string): GrayImage | null {
|
||||
const { icon, stale } = readNotificationIconByKey(key, renderPassAllowsStaleData());
|
||||
if (stale) {
|
||||
noteStaleDataUsed();
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
type CardLayout = {
|
||||
notification: AndroidNotification;
|
||||
height: number;
|
||||
@@ -69,7 +83,10 @@ export class NotificationsListLayer implements Layer {
|
||||
for (let index = 0; index < layouts.length; index++) {
|
||||
const layout = layouts[index]!;
|
||||
if (cursorY + layout.height >= LIST_TOP && cursorY <= LIST_BOTTOM) {
|
||||
drawNotificationCard(image, font, layout, CARD_X, cursorY, CARD_WIDTH, index === selectedIndex);
|
||||
// Icons are only resolved for cards actually drawn, so a long list
|
||||
// does not fetch icons for everything below the fold.
|
||||
const icon = iconForNotification(layout.notification.key);
|
||||
drawNotificationCard(image, font, layout, CARD_X, cursorY, CARD_WIDTH, index === selectedIndex, icon);
|
||||
}
|
||||
cursorY += layout.height + CARD_GAP;
|
||||
if (cursorY > LIST_BOTTOM + 80) break;
|
||||
@@ -139,7 +156,7 @@ export class SingleNotificationLayer implements Layer {
|
||||
|
||||
const menu = buildDetailMenu(notification);
|
||||
this.selectedMenuIndex = clamp(this.selectedMenuIndex, 0, Math.max(0, menu.length - 1));
|
||||
drawDetailContent(image, font, notification);
|
||||
drawDetailContent(image, font, notification, iconForNotification(notification.key));
|
||||
drawDetailMenu(image, font, menu, this.selectedMenuIndex);
|
||||
return image;
|
||||
}
|
||||
@@ -230,14 +247,18 @@ function drawNotificationCard(
|
||||
y: number,
|
||||
width: number,
|
||||
selected: boolean,
|
||||
icon: GrayImage | null,
|
||||
): void {
|
||||
const fill = selected ? 15 : 0;
|
||||
const stroke = selected ? 110 : 38;
|
||||
image.fillRoundedRect(x, y, width, layout.height, fill, 8);
|
||||
image.drawRoundedRect(x, y, width, layout.height, stroke, 8);
|
||||
if (icon) {
|
||||
image.bitBlt(icon, x + 10, y + 8);
|
||||
}
|
||||
for (let index = 0; index < layout.lines.length; index++) {
|
||||
const value = index === 0 ? 140 : selected ? 235 : 185;
|
||||
image.drawText(font, x + 10, y + 7 + index * LINE_HEIGHT, layout.lines[index]!, value);
|
||||
image.drawText(font, x + CARD_TEXT_X, y + 7 + index * LINE_HEIGHT, layout.lines[index]!, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,11 +275,16 @@ function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewpor
|
||||
return clamp(centered | 0, 0, maxScroll);
|
||||
}
|
||||
|
||||
function drawDetailContent(image: GrayImage, font: BdfFont, notification: AndroidNotification): void {
|
||||
function drawDetailContent(image: GrayImage, font: BdfFont, notification: AndroidNotification, icon: GrayImage | null): void {
|
||||
const contentX = 24;
|
||||
const contentWidth = 360;
|
||||
image.drawText(font, PAGE_X + 12, PAGE_Y + 9, "Notification", 220);
|
||||
image.drawText(font, contentX, 42, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150);
|
||||
let appLineX = contentX;
|
||||
if (icon) {
|
||||
image.bitBlt(icon, contentX, 36);
|
||||
appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP;
|
||||
}
|
||||
image.drawText(font, appLineX, 42, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150);
|
||||
|
||||
const lines: string[] = [];
|
||||
lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth));
|
||||
|
||||
Reference in new issue
Block a user