Screen test for dithering experimentation; landscape layout for phone preview

This commit is contained in:
James Babcock committed 2026-05-17 18:17:06 -07:00
1 parent 8d6fe60c79
commit 468291aac3
9 files changed
+215 -34

No files matched your search

+6 -1
View File
@@ -251,7 +251,7 @@ export class GrayImage {
y: ty,
width,
height,
bmp: buildEvenHubBmp(width, height, (x, y) => this.getPixel(tx + x, ty + y) >> 4),
bmp: buildEvenHubBmp(width, height, (x, y) => grayToEvenHubNibble(this.getPixel(tx + x, ty + y))),
});
}
}
@@ -327,6 +327,11 @@ function buildEvenHubBmp(
return buf;
}
function grayToEvenHubNibble(value: number): number {
const clamped = clampByte(value);
return clamped === 0 ? 0 : Math.min(15, (clamped + 15) >> 4);
}
function clampByte(value: number): number {
return Math.max(0, Math.min(255, Math.round(value)));
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

+42 -15
View File
@@ -1,4 +1,4 @@
import { Color, EventData, isAndroid, Observable, Page, ScrollView, TextField } from '@nativescript/core'
import { Application, Color, EventData, isAndroid, Observable, Page, ScrollView, TextField } from '@nativescript/core'
import { MainViewModel } from './main-view-model'
import { dashboardController } from '../g2/dashboard-controller'
@@ -12,9 +12,12 @@ export function navigatingTo(args: EventData) {
}
type MainPageState = {
model: MainViewModel
isPinnedToBottom: boolean
scrollHandler: () => void
scrollViews: ScrollView[]
scrollHandler: (args: EventData) => void
propertyChangeHandler: (args: EventData & { propertyName?: string }) => void
orientationHandler: () => void
}
function getPageState(page: Page): MainPageState | undefined {
@@ -27,14 +30,15 @@ function setPageState(page: Page, state?: MainPageState): void {
function cleanupPage(page: Page): void {
const state = getPageState(page)
const model = page.bindingContext as MainViewModel | null
const scrollView = page.getViewById<ScrollView>('logScrollView')
if (!state || !model || !scrollView) {
if (!state) {
setPageState(page, undefined)
return
}
scrollView.off(ScrollView.scrollEvent, state.scrollHandler)
model.off(Observable.propertyChangeEvent, state.propertyChangeHandler)
for (const scrollView of state.scrollViews) {
scrollView.off(ScrollView.scrollEvent, state.scrollHandler)
}
state.model.off(Observable.propertyChangeEvent, state.propertyChangeHandler)
Application.off(Application.orientationChangedEvent, state.orientationHandler)
setPageState(page, undefined)
}
@@ -48,6 +52,12 @@ function scrollToBottom(scrollView: ScrollView): void {
}, 0)
}
function scrollLogsToBottom(scrollViews: ScrollView[]): void {
for (const scrollView of scrollViews) {
scrollToBottom(scrollView)
}
}
function applySettingsTextFieldContrast(textField: TextField): void {
textField.color = SETTINGS_TEXT_COLOR
textField.backgroundColor = SETTINGS_BACKGROUND_COLOR
@@ -82,25 +92,39 @@ export function loaded(args: EventData) {
dashboardController.refreshEvenAppStatus()
const model = page.bindingContext as MainViewModel | null
const scrollView = page.getViewById<ScrollView>('logScrollView')
const scrollViews = [
page.getViewById<ScrollView>('logScrollView'),
page.getViewById<ScrollView>('logScrollViewLandscape'),
].filter((scrollView): scrollView is ScrollView => !!scrollView)
const settingsTextField = page.getViewById<TextField>('settingsTextField')
model?.refreshLayoutMetrics()
if (settingsTextField) {
applySettingsTextFieldContrast(settingsTextField)
}
if (!model || !scrollView) {
if (!model || scrollViews.length === 0) {
return
}
const state: MainPageState = {
model,
isPinnedToBottom: true,
scrollHandler: () => {
state.isPinnedToBottom = isAtBottom(scrollView)
scrollViews,
scrollHandler: (scrollArgs) => {
state.isPinnedToBottom = isAtBottom(scrollArgs.object as ScrollView)
},
orientationHandler: () => {
setTimeout(() => {
model.refreshLayoutMetrics()
if (state.isPinnedToBottom) {
scrollLogsToBottom(scrollViews)
}
}, 0)
},
propertyChangeHandler: (propertyArgs) => {
if (propertyArgs.propertyName === 'showLog') {
if (model.showLog) {
state.isPinnedToBottom = true
scrollToBottom(scrollView)
scrollLogsToBottom(scrollViews)
}
return
}
@@ -114,15 +138,18 @@ export function loaded(args: EventData) {
return
}
if (state.isPinnedToBottom) {
scrollToBottom(scrollView)
scrollLogsToBottom(scrollViews)
}
},
}
scrollView.on(ScrollView.scrollEvent, state.scrollHandler)
for (const scrollView of scrollViews) {
scrollView.on(ScrollView.scrollEvent, state.scrollHandler)
}
model.on(Observable.propertyChangeEvent, state.propertyChangeHandler)
Application.on(Application.orientationChangedEvent, state.orientationHandler)
setPageState(page, state)
scrollToBottom(scrollView)
scrollLogsToBottom(scrollViews)
if (model.isTextSettingEditorActive) {
focusSystemNameField(page)
}
+40 -13
View File
@@ -5,30 +5,57 @@
<ActionItem text="{{ showLogMenuLabel }}" tap="{{ onToggleLogTap }}" android.position="popup" />
</ActionBar>
<GridLayout rows="auto,*">
<StackLayout row="0" visibility="{{ displayPreviewVisibility }}">
<Image src="{{ displayPreview }}" stretch="fill" height="{{ displayPreviewHeight }}" />
</StackLayout>
<GridLayout row="1" rows="auto,auto,auto,*" class="p-20">
<GridLayout>
<GridLayout rows="auto,*" visibility="{{ portraitLayoutVisibility }}">
<StackLayout row="0" visibility="{{ displayPreviewVisibility }}">
<Image src="{{ displayPreview }}" stretch="fill" height="{{ displayPreviewHeight }}" />
</StackLayout>
<GridLayout row="1" rows="auto,auto,auto,*" class="p-20">
<StackLayout row="0" class="m-b-8" visibility="{{ evenAppConflictWarningVisibility }}">
<Label text="{{ evenAppConflictMessage }}" textWrap="true" />
<Button text="Open Even app settings" tap="{{ onOpenEvenAppSettingsTap }}" class="-primary" />
</StackLayout>
<StackLayout row="1">
<GridLayout rows="auto,auto,auto" columns="*,*,*" class="m-t-8">
<Button row="0" col="1" text="Swipe up" tap="{{ onSyntheticUpTap }}" />
<Button row="1" col="0" text="Double-click" tap="{{ onSyntheticLeftTap }}" />
<Button row="1" col="2" text="Click" tap="{{ onSyntheticRightTap }}" />
<Button row="2" col="1" text="Swipe down" tap="{{ onSyntheticDownTap }}" />
</GridLayout>
</StackLayout>
<Label row="2" text="{{ status }}" class="m-t-16" textWrap="true" />
<ScrollView id="logScrollView" row="3" class="m-t-16" visibility="{{ showLogVisibility }}">
<Label text="{{ log }}" textWrap="true" verticalAlignment="top" />
</ScrollView>
</GridLayout>
</GridLayout>
<GridLayout rows="auto,*" class="p-20" visibility="{{ landscapeLayoutVisibility }}">
<StackLayout row="0" class="m-b-8" visibility="{{ evenAppConflictWarningVisibility }}">
<Label text="{{ evenAppConflictMessage }}" textWrap="true" />
<Button text="Open Even app settings" tap="{{ onOpenEvenAppSettingsTap }}" class="-primary" />
</StackLayout>
<StackLayout row="1">
<GridLayout rows="auto,auto,auto" columns="*,*,*" class="m-t-8">
<GridLayout row="1" rows="auto,auto,*" columns="*,260">
<StackLayout row="0" col="0" visibility="{{ displayPreviewVisibility }}" horizontalAlignment="left">
<Image
src="{{ displayPreview }}"
stretch="fill"
width="{{ landscapeDisplayPreviewWidth }}"
height="{{ landscapeDisplayPreviewHeight }}" />
</StackLayout>
<GridLayout row="0" col="1" rows="auto,auto,auto" columns="*,*,*" class="m-l-16">
<Button row="0" col="1" text="Swipe up" tap="{{ onSyntheticUpTap }}" />
<Button row="1" col="0" text="Double-click" tap="{{ onSyntheticLeftTap }}" />
<Button row="1" col="2" text="Click" tap="{{ onSyntheticRightTap }}" />
<Button row="2" col="1" text="Swipe down" tap="{{ onSyntheticDownTap }}" />
</GridLayout>
</StackLayout>
<Label row="2" text="{{ status }}" class="m-t-16" textWrap="true" />
<ScrollView id="logScrollView" row="3" class="m-t-16" visibility="{{ showLogVisibility }}">
<Label text="{{ log }}" textWrap="true" verticalAlignment="top" />
</ScrollView>
<Label row="1" col="0" colSpan="2" text="{{ status }}" class="m-t-16" textWrap="true" />
<ScrollView id="logScrollViewLandscape" row="2" col="0" colSpan="2" class="m-t-16" visibility="{{ showLogVisibility }}">
<Label text="{{ log }}" textWrap="true" verticalAlignment="top" />
</ScrollView>
</GridLayout>
</GridLayout>
<StackLayout
row="0"
class="system-name-modal"
visibility="{{ textSettingEditorVisibility }}"
horizontalAlignment="stretch"
+42 -1
View File
@@ -1,10 +1,13 @@
import { Frame, ImageSource, Observable, Screen } from "@nativescript/core";
import { Application, Frame, ImageSource, Observable, Screen } from "@nativescript/core";
import { dashboardController, type TextSettingEditorKind } from "../g2/dashboard-controller";
type LayoutOrientation = "portrait" | "landscape";
export class MainViewModel extends Observable {
private _status = "Disconnected.";
private _log = "";
private _displayPreview: ImageSource | null = null;
private _layoutOrientation: LayoutOrientation = this.readLayoutOrientation();
private _systemCardName = "Faceclaw";
private _editingSystemCardName = false;
private _activeTextSettingEditorKind: TextSettingEditorKind | null = null;
@@ -79,6 +82,36 @@ export class MainViewModel extends Observable {
return Screen.mainScreen.widthDIPs / 2;
}
get landscapeDisplayPreviewWidth(): number {
const screenWidth = Screen.mainScreen.widthDIPs;
const sidePanelWidth = 260;
return Math.max(240, Math.floor(screenWidth - sidePanelWidth - 56));
}
get landscapeDisplayPreviewHeight(): number {
return Math.floor(this.landscapeDisplayPreviewWidth / 2);
}
get portraitLayoutVisibility(): "visible" | "collapse" {
return this._layoutOrientation === "portrait" ? "visible" : "collapse";
}
get landscapeLayoutVisibility(): "visible" | "collapse" {
return this._layoutOrientation === "landscape" ? "visible" : "collapse";
}
refreshLayoutMetrics(): void {
const nextOrientation = this.readLayoutOrientation();
if (this._layoutOrientation !== nextOrientation) {
this._layoutOrientation = nextOrientation;
this.notifyPropertyChange("portraitLayoutVisibility", this.portraitLayoutVisibility);
this.notifyPropertyChange("landscapeLayoutVisibility", this.landscapeLayoutVisibility);
}
this.notifyPropertyChange("displayPreviewHeight", this.displayPreviewHeight);
this.notifyPropertyChange("landscapeDisplayPreviewWidth", this.landscapeDisplayPreviewWidth);
this.notifyPropertyChange("landscapeDisplayPreviewHeight", this.landscapeDisplayPreviewHeight);
}
get showLog(): boolean {
return this._showLog;
}
@@ -287,6 +320,14 @@ export class MainViewModel extends Observable {
this.log = this.log ? `${this.log}\n[${stamp}] ${line}` : `[${stamp}] ${line}`;
}
private readLayoutOrientation(): LayoutOrientation {
const applicationOrientation = Application.orientation();
if (applicationOrientation === "landscape" || applicationOrientation === "portrait") {
return applicationOrientation;
}
return Screen.mainScreen.widthDIPs > Screen.mainScreen.heightDIPs ? "landscape" : "portrait";
}
private formatError(error: unknown): string {
const raw = (error as Error)?.message ?? String(error);
const sanitized = raw.replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
+72
View File
@@ -0,0 +1,72 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const COLOR_VALUE_COUNT = 256;
const COLOR_VALUE_STEP = 1;
const SWATCH_COUNT = COLOR_VALUE_COUNT / COLOR_VALUE_STEP;
const GRID_COLUMNS = 16;
const GRID_ROWS = Math.ceil(SWATCH_COUNT / GRID_COLUMNS);
const CELL_WIDTH = Math.floor(G2_LENS_WIDTH / GRID_COLUMNS);
//const CELL_HEIGHT = Math.floor(G2_LENS_HEIGHT / GRID_ROWS);
const CELL_HEIGHT = 30;
const GRID_X = Math.floor((G2_LENS_WIDTH - CELL_WIDTH * GRID_COLUMNS) / 2);
const LABEL_Y_OFFSET = 0;
const SWATCH_Y_OFFSET = 12;
const SWATCH_WIDTH = 24;
const SWATCH_HEIGHT = 16;
export class ScreenTestLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
for (let value = 0; value < COLOR_VALUE_COUNT; value += COLOR_VALUE_STEP) {
const slot = value / COLOR_VALUE_STEP;
const col = slot % GRID_COLUMNS;
const row = Math.floor(slot / GRID_COLUMNS);
const cellX = GRID_X + col * CELL_WIDTH;
const cellY = row * CELL_HEIGHT;
const label = String(value);
const labelX = cellX + Math.max(0, Math.floor((CELL_WIDTH - ctx.font.measureText(label)) / 2));
const swatchX = cellX + Math.floor((CELL_WIDTH - SWATCH_WIDTH) / 2);
image.drawText(ctx.font, labelX, cellY + LABEL_Y_OFFSET, label, 185);
fillSwatch(image, swatchX, cellY + SWATCH_Y_OFFSET, SWATCH_WIDTH, SWATCH_HEIGHT, value);
}
return image;
}
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
if (event.type === "double-click") {
ctx.stack.pop();
}
}
}
function fillSwatch(image: GrayImage, x: number, y: number, width: number, height: number, value: number): void {
// Values will ultimately be rounded up to the nearest multiple of 16 (since they're transmitted as 4-bit)
// grayscale). So, for intermediate values, dither.
if (value % 16 !== 0) {
const baseValue = value & 0xf0;
const fillFraction = value & 0x0f;
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const ditheredValue = ditherPattern(j, i, fillFraction)
image.setPixel(x + j, y + i, ditheredValue ? baseValue + 16 : baseValue);
}
}
} else {
image.fillRect(x, y, width, height, value);
}
}
function ditherPattern(x: number, y: number, fillFraction: number): number {
const pattern = [
[0, 8, 2, 10],
[12, 4, 14, 6],
[3, 11, 1, 9],
[15, 7, 13, 5],
];
const patternValue = pattern[y % 4][x % 4];
return (fillFraction > patternValue) ? 1 : 0;
}
+1 -1
View File
@@ -379,7 +379,7 @@ function drawNightscoutBasalOverlay(
const leftX = timeToGraphX(bounds, windowStartMs, startMs);
const rightX = timeToGraphX(bounds, windowStartMs, endMs);
const height = Math.max(1, Math.round((event.rate / maxRate) * overlayHeight));
image.fillRect(leftX, bottomY - height + 1, Math.max(1, rightX - leftX + 1), height, 18);
image.fillRect(leftX, bottomY - height + 1, Math.max(1, rightX - leftX + 1), height, 15);
}
}
+7
View File
@@ -53,6 +53,7 @@ import { StopwatchLayer } from "./apps/stopwatch";
import { NotificationsListLayer } from "./notifications";
import { TranscribeLayer } from "./apps/transcribe";
import { TelepromptLayer } from "./apps/teleprompt";
import { ScreenTestLayer } from "./apps/screen-test";
type DashboardCardId = "system" | DashboardSlotId;
export type DashboardBatteryLevels = {
@@ -532,6 +533,12 @@ function createAppsMenuLayer(): MenuLayer {
layer.start(ctx);
},
},
{
label: "Screen test",
onSelect: (ctx) => {
ctx.stack.push(new ScreenTestLayer());
},
},
],
TOP_LEFT_MENU_LAYOUT,
);
+5 -3
View File
@@ -10,8 +10,10 @@ const DEFAULT_MENU_HEIGHT = 128;
const MENU_TITLE_HEIGHT = 16;
const MENU_ROW_HEIGHT = 20;
const MENU_HIGHLIGHT_Y_OFFSET = 0;
const MENU_TOGGLE_SWITCH_Y_OFFSET = -1;
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;
export type MenuLayout = {
x: number;
@@ -104,8 +106,8 @@ export class MenuLayer implements Layer {
const y = bodyY + index * MENU_ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
image.fillRoundedRect(x + 12, y + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, 18);
image.drawRoundedRect(x + 12, y + MENU_HIGHLIGHT_Y_OFFSET, width - 24, MENU_HIGHLIGHT_HEIGHT, 45);
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);
}
if (item.render) {
item.render({