Center the phone screen preview

This commit is contained in:
James Babcock committed 2026-10-12 17:12:36 -07:00
1 parent f6201d8e41
commit 96e3bd95f7
18 files changed
+311 -24

No files matched your search

+13 -6
View File
@@ -46,6 +46,7 @@ const MIRROR_TOUCH_GESTURES: Record<Exclude<MirrorTouchKind, "tap">, WearRemoteI
"swipe-right": "swipe-right",
};
import { GlanceHost } from "./glance-host";
import { mirrorToPanelFraction, phonePreviewCrop } from "./phone-preview-crop";
import { type GlanceEvent } from "./glance-state";
import { isPreviewOnlyMode } from "../phone-ui/onboarding-state";
import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from "../apps/onboarding/onboarding-progress";
@@ -69,7 +70,7 @@ import { ALL_APPS } from "../apps/all-apps";
import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition";
import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window";
import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence";
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry";
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry";
import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers";
import { type KeyboardInputSession } from "../ui/shell/keyboard-input";
import { appSwitcherPositionSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
@@ -273,6 +274,8 @@ class DashboardController {
private extractedFontsConfirmed = false;
private screenRecordingActive = false;
private displayPreview: ImageSource | null = createInitialDisplayPreview();
/** The screen area displayPreview shows (Preview scaling), or null for the whole panel. */
private displayPreviewCrop: ScreenRect | null = null;
private silentMode = false;
// Brightness value last pushed to the glasses; see pushBrightness.
private lastPushedBrightness: string | null = null;
@@ -458,8 +461,9 @@ class DashboardController {
this.applyAppLayoutsIfChanged();
this.syncAssistantBridgeIfChanged();
this.syncLockScreenSettingIfChanged();
// A Preview color change should show on the mirror at once, not at the
// next composited frame (cheap: floor-limited, dedup'd on the Java side).
// A Preview color or scaling change should show on the mirror at once,
// not at the next composited frame (cheap: floor-limited, dedup'd on
// the Java side).
this.schedulePreviewUpdate();
});
// Connect to the external agent bridge at boot if configured; the
@@ -1931,8 +1935,9 @@ class DashboardController {
await this.injectSyntheticRingInput(MIRROR_TOUCH_GESTURES[kind], "watch");
return;
}
const x = Math.round(Math.min(1, Math.max(0, nx)) * G2_LENS_WIDTH);
const y = Math.round(Math.min(1, Math.max(0, ny)) * G2_LENS_HEIGHT);
const at = mirrorToPanelFraction(this.displayPreviewCrop, nx, ny);
const x = Math.round(Math.min(1, Math.max(0, at.nx)) * G2_LENS_WIDTH);
const y = Math.round(Math.min(1, Math.max(0, at.ny)) * G2_LENS_HEIGHT);
const foreground = shell.foregroundWindow();
const stripShown = sidebarStripVisible(shell.getFocus(), foreground?.appId);
this.appendLog(`mirror tap at ${x},${y}`);
@@ -2883,8 +2888,10 @@ class DashboardController {
const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager;
if (!power?.isInteractive()) return;
}
const preview = display.getCompositePreview(previewColorSetting.get() === "green");
const crop = phonePreviewCrop(this.glassesLocked, this.glance.screenRect());
const preview = display.getCompositePreview(previewColorSetting.get() === "green", crop);
if (preview) {
this.displayPreviewCrop = crop;
this.setDisplayPreview(preview);
}
}
+9 -1
View File
@@ -12,7 +12,7 @@ import { prepareFrameDraws } from "../graphics/glyph-wire";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
import * as frameTimings from "../native/frame-timings";
import { type DisplayTarget } from "../native/preview-display";
import { minWindowTop } from "../ui/shell/geometry";
import { minWindowTop, type ScreenRect } from "../ui/shell/geometry";
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
import { type GlanceBoardInstance, type GlanceboardProvider } from "../apps/app-definition";
import { GLANCE_HIDDEN, glanceEventForGesture, reduceGlance, type GlanceEvent, type GlanceState } from "./glance-state";
@@ -59,6 +59,8 @@ export class GlanceHost {
private rendering = false;
private renderPending = false;
private nextRenderWantsFreshData = false;
/** Where the last render put the board on screen. */
private boardRect: ScreenRect | null = null;
constructor(private readonly options: GlanceHostOptions) {}
@@ -66,6 +68,11 @@ export class GlanceHost {
return this.state.visible;
}
/** The board's area of the screen while it shows (for the phone mirror's Scaled preview), else null. */
screenRect(): ScreenRect | null {
return this.state.visible ? this.boardRect : null;
}
/** Whether sleep-time gestures should reach the board at all. */
isEnabled(): boolean {
return this.options.getProvider()?.isEnabled() ?? false;
@@ -249,6 +256,7 @@ export class GlanceHost {
const x = Math.max(0, ((G2_LENS_WIDTH - boardImage.width) / 2) | 0);
const y = Math.max(0, Math.min(G2_LENS_HEIGHT - boardImage.height, minWindowTop()));
boardImage.composeInto(full, x, y);
this.boardRect = { x, y, width: boardImage.width, height: boardImage.height };
return full;
}),
);
+9 -3
View File
@@ -43,7 +43,8 @@ import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/g
import { noopLayerActions, type LayerActions } from '../ui/layers'
import { TextViewerLayer } from '../apps/files/text-viewer'
import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell'
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible } from '../ui/shell/geometry'
import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect } from '../ui/shell/geometry'
import { mirrorToPanelFraction, phonePreviewCrop } from './phone-preview-crop'
import { onAnySettingChanged,
previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences, getStringSettingById,
nightscoutSiteUrlSetting, nightscoutApiTokenSetting, voiceProviderSetting, elevenLabsApiKeySetting,
@@ -116,6 +117,8 @@ export class IosPreviewController {
appendLog: message => this.logBluetooth(message),
})
private previewTimer: ReturnType<typeof setTimeout> | null = null
/** The screen area the mirror's image shows (Preview scaling), or null for the whole panel. */
private previewCrop: ScreenRect | null = null
private clockTimer: ReturnType<typeof setInterval> | null = null
private offSettings: (() => void) | null = null
private lastBrightness: string | null = null
@@ -506,8 +509,10 @@ export class IosPreviewController {
this.previewTimer = null
if (!this.active) return
try {
const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green')
const crop = phonePreviewCrop(this.glassesLocked, this.glance.screenRect())
const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green', crop)
if (!image) return
this.previewCrop = crop
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
} catch (error) { this.fail(error) }
@@ -561,7 +566,8 @@ export class IosPreviewController {
await shell.receiveInput(event)
this.requestShellRender()
}
private async mirrorTap(nx: number, ny: number): Promise<void> {
private async mirrorTap(mirrorNx: number, mirrorNy: number): Promise<void> {
const { nx, ny } = mirrorToPanelFraction(this.previewCrop, mirrorNx, mirrorNy)
const x = Math.max(0, Math.min(639, Math.floor(nx * 640)))
const y = Math.max(0, Math.min(479, Math.floor(ny * 480)))
const window = shell.foregroundWindow()
+33
View File
@@ -0,0 +1,33 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
import { previewScalingSetting } from "../ui/dashboard-settings";
import { phonePreviewRect, usedScreenRect, type ScreenRect } from "../ui/shell/geometry";
import { shell } from "../ui/shell/shell";
/**
* The screen area the phone's mirror shows (Settings > Phone display >
* Preview scaling), or null for the whole panel. Scaled zooms to the
* Glanceboard while it shows, else to the area the shell uses around the
* foreground window, centred at the panel's aspect. A locked display gets
* the whole panel: its message box is centred in the panel, not the band.
*/
export function phonePreviewCrop(locked: boolean, glanceRect: ScreenRect | null): ScreenRect | null {
if (locked || previewScalingSetting.get() === "raw") return null;
const window = shell.foregroundWindow();
const crop = phonePreviewRect(glanceRect ?? usedScreenRect(window?.heightMode ?? "min", window?.appId));
const wholePanel = crop.x === 0 && crop.y === 0 && crop.width === G2_LENS_WIDTH && crop.height === G2_LENS_HEIGHT;
return wholePanel ? null : crop;
}
/**
* A point on the mirror, as fractions of the mirror's width and height,
* as fractions of the panel's, given the crop the mirror is showing.
* Points in the black margins past the panel's edges come out below 0 or
* above 1.
*/
export function mirrorToPanelFraction(crop: ScreenRect | null, nx: number, ny: number): { nx: number; ny: number } {
if (!crop) return { nx, ny };
return {
nx: (crop.x + nx * crop.width) / G2_LENS_WIDTH,
ny: (crop.y + ny * crop.height) / G2_LENS_HEIGHT,
};
}