forked from Blendman974/faceclaw
Adjust UI to account for user-configured variable font size
This commit is contained in:
1 parent
96842ceb23
commit
eef9fbaab4
25 files changed
+350
-222
No files matched your search
+22
-21
@@ -5,18 +5,14 @@ import { clamp } from "../util/numeric-util";
|
||||
import { DashboardInputEvent, Layer, LayerContext, PaintBelow } from "./layers";
|
||||
|
||||
import { GESTURE_DOUBLE_CLICK } from "./gestures";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
|
||||
const DEFAULT_MENU_X = 8;
|
||||
const DEFAULT_MENU_Y = 8;
|
||||
const DEFAULT_MENU_WIDTH = 272;
|
||||
// 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;
|
||||
const MENU_HIGHLIGHT_SELECTED_BACKGROUND_FILL = 15;
|
||||
const MENU_HIGHLIGHT_SELECTED_BORDER_STROKE = 45;
|
||||
|
||||
@@ -147,8 +143,9 @@ export function drawToggleMenuItem(
|
||||
const switchWidth = 34;
|
||||
const switchHeight = 16;
|
||||
const switchX = x + width - switchWidth - 2;
|
||||
const switchY = y + MENU_TOGGLE_SWITCH_Y_OFFSET;
|
||||
image.drawText(font, x, y + 3, label, 200);
|
||||
// Center the switch on the row's line box (legacy y+1 at the 12px default).
|
||||
const switchY = y + Math.max(1, Math.round((listRowHeight(font) - 2 - switchHeight) / 2));
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, label, 200);
|
||||
const offFill = selected ? 1 : 18;
|
||||
image.fillRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 70 : offFill, 8);
|
||||
image.drawRoundedRect(switchX, switchY, switchWidth, switchHeight, enabled ? 130 : 55, 8);
|
||||
@@ -166,9 +163,9 @@ export function drawRightValueMenuItem(
|
||||
label: string,
|
||||
value: string,
|
||||
): void {
|
||||
image.drawText(font, x, y + 3, label, 200);
|
||||
image.drawText(font, x, y + LIST_ROW_TEXT_INSET, label, 200);
|
||||
const valueX = x + width - font.measureText(value) - 2;
|
||||
image.drawText(font, valueX, y + 3, value, 220);
|
||||
image.drawText(font, valueX, y + LIST_ROW_TEXT_INSET, value, 220);
|
||||
}
|
||||
|
||||
export class MenuLayer implements Layer {
|
||||
@@ -194,9 +191,10 @@ export class MenuLayer implements Layer {
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const rowHeight = listRowHeight(font);
|
||||
const image = paintBelow();
|
||||
const { x, y, width } = this.layout;
|
||||
const chromeTop = (this.title ? MENU_TITLE_HEIGHT : 0) + MENU_BODY_PADDING;
|
||||
const chromeTop = (this.title ? menuTitleHeight(font) : 0) + MENU_BODY_PADDING;
|
||||
const minHeight = this.layout.minHeight ?? DEFAULT_MENU_MIN_HEIGHT;
|
||||
// Cap to the surface being painted on: a window's stack image may be much
|
||||
// shorter than the full screen (min-height windows).
|
||||
@@ -204,9 +202,9 @@ export class MenuLayer implements Layer {
|
||||
this.layout.maxHeight ?? image.height - y - DEFAULT_MENU_Y,
|
||||
image.height - y,
|
||||
);
|
||||
const contentHeight = chromeTop + this.items.length * MENU_ROW_HEIGHT + MENU_BODY_PADDING;
|
||||
const contentHeight = chromeTop + this.items.length * rowHeight + 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);
|
||||
const visibleRowCount = Math.max(1, ((height - chromeTop - MENU_BODY_PADDING) / rowHeight) | 0);
|
||||
this.scrollRow = scrollToKeepSelectionVisible(
|
||||
this.scrollRow,
|
||||
this.selectedIndex,
|
||||
@@ -229,16 +227,16 @@ export class MenuLayer implements Layer {
|
||||
const lastVisibleRow = Math.min(this.items.length, this.scrollRow + visibleRowCount);
|
||||
for (let index = this.scrollRow; index < lastVisibleRow; index++) {
|
||||
const item = this.items[index]!;
|
||||
const rowY = bodyY + (index - this.scrollRow) * MENU_ROW_HEIGHT;
|
||||
const rowY = bodyY + (index - this.scrollRow) * rowHeight;
|
||||
const selected = index === this.selectedIndex;
|
||||
const disabled = isMenuItemDisabled(item);
|
||||
if (selected) {
|
||||
drawSelectionHighlight(
|
||||
image,
|
||||
x + 12,
|
||||
rowY + MENU_HIGHLIGHT_Y_OFFSET,
|
||||
rowY,
|
||||
width - 24,
|
||||
MENU_HIGHLIGHT_HEIGHT,
|
||||
rowHeight - 1,
|
||||
focused,
|
||||
8,
|
||||
);
|
||||
@@ -249,14 +247,14 @@ export class MenuLayer implements Layer {
|
||||
x: x + 22,
|
||||
y: rowY,
|
||||
width: width - 44,
|
||||
height: MENU_ROW_HEIGHT - 3,
|
||||
height: rowHeight - 3,
|
||||
selected,
|
||||
disabled,
|
||||
text: item.label,
|
||||
ctx,
|
||||
});
|
||||
} else {
|
||||
image.drawText(font, x + 22, rowY + 3, item.label, disabled ? 70 : selected ? 255 : 200);
|
||||
image.drawText(font, x + 22, rowY + LIST_ROW_TEXT_INSET, item.label, disabled ? 70 : selected ? 255 : 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,7 +263,7 @@ export class MenuLayer implements Layer {
|
||||
image,
|
||||
x + width - 7,
|
||||
bodyY,
|
||||
visibleRowCount * MENU_ROW_HEIGHT - 4,
|
||||
visibleRowCount * rowHeight - 4,
|
||||
this.scrollRow,
|
||||
visibleRowCount,
|
||||
this.items.length,
|
||||
@@ -311,8 +309,9 @@ export function openModalMenu(
|
||||
initialSelectedIndex = 0,
|
||||
): void {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const font = getDefaultSmallFont();
|
||||
const menuWidth = Math.min(320, width - 40);
|
||||
const naturalHeight = MENU_TITLE_HEIGHT + 2 * MENU_BODY_PADDING + items.length * MENU_ROW_HEIGHT;
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font);
|
||||
const menuHeight = Math.min(naturalHeight, height - 40);
|
||||
const layout: MenuLayout = {
|
||||
x: ((width - menuWidth) / 2) | 0,
|
||||
@@ -341,10 +340,12 @@ export class TextPageLayer implements Layer {
|
||||
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
|
||||
|
||||
const wrapped = wrapText(font, this.body, G2_LENS_WIDTH - 60);
|
||||
const step = lineStep(font);
|
||||
const bodyTop = 28 + font.lineHeight;
|
||||
const footerY = G2_LENS_HEIGHT - 48;
|
||||
const maxBodyLines = Math.max(0, Math.floor((footerY - 50) / 14));
|
||||
const maxBodyLines = Math.max(0, Math.floor((footerY - bodyTop - 8) / step));
|
||||
for (let index = 0; index < Math.min(wrapped.length, maxBodyLines); index++) {
|
||||
image.drawText(font, 24, 42 + index * 14, wrapped[index]!, 190);
|
||||
image.drawText(font, 24, bodyTop + index * step, wrapped[index]!, 190);
|
||||
}
|
||||
image.drawText(font, 24, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
|
||||
Reference in new issue
Block a user