Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions packages/client-runtime/src/device/phoneViewer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ vi.mock("./modelScene.ts", async () => {
};
});

import { Group, Quaternion } from "three";
import { Box3, Group, Quaternion, Vector3 } from "three";
import { createPhoneViewer } from "./phoneViewer.ts";
import { type DeviceShapeProfile } from "./shapeProfile.ts";
import { ANDROID_PHONE_SHAPE, type DeviceShapeProfile } from "./shapeProfile.ts";

afterEach(() => {
vi.restoreAllMocks();
Expand All @@ -91,7 +91,7 @@ afterEach(() => {
models.pending.length = 0;
});

function fixture(profile?: DeviceShapeProfile) {
function fixture(profile?: DeviceShapeProfile, foldAngle?: number) {
const pending = new Map<number, FrameRequestCallback>();
let id = 0;
let now = 0;
Expand All @@ -111,6 +111,7 @@ function fixture(profile?: DeviceShapeProfile) {
onUnavailable,
onFramingAspect,
...(profile ? { profile } : {}),
...(foldAngle === undefined ? {} : { foldAngle }),
});
const draw = (time = now) => {
now = time;
Expand Down Expand Up @@ -152,3 +153,43 @@ it("rejects incompatible accessories and releases an accessory that finishes aft
expect(dispose).toHaveBeenCalledOnce();
viewer.dispose();
});

it("uses the fold model only for an inner display frame, whatever the hinge angle", () => {
const { viewer, draw, source, state } = fixture(ANDROID_PHONE_SHAPE, 0);
let time = 0;
const shown = () => {
draw((time += 1000));
const phone = state.frames.at(-1)!.phone!;
const size = new Box3().setFromObject(phone).getSize(new Vector3());
return {
fold: phone.getObjectByName("continuous-inner-screen") !== undefined,
fitsFrame: Math.abs(Math.log(size.x / size.y / (source.width / source.height))) < 0.1,
};
};
const showFrame = (width: number, height: number) => {
source.width = width;
source.height = height;
viewer.setScreen({
width,
height,
orientation: width > height ? "landscape_left" : "portrait",
});
viewer.frameUpdated();
return shown();
};
const phoneModel = { fold: false, fitsFrame: true };
expect(shown()).toEqual(phoneModel);
viewer.setFoldAngle(180);
expect(shown()).toEqual(phoneModel);
viewer.frameUpdated();
expect(shown()).toEqual(phoneModel);
expect(showFrame(2076, 2152)).toEqual({ fold: true, fitsFrame: true });
viewer.setFoldAngle(0);
expect(shown().fold).toBe(true);
expect(showFrame(1920, 1200)).toEqual(phoneModel);
viewer.setFoldAngle(180);
expect(shown()).toEqual(phoneModel);
expect(showFrame(2076, 2152)).toEqual({ fold: true, fitsFrame: true });
expect(showFrame(1080, 2400)).toEqual(phoneModel);
viewer.dispose();
});
40 changes: 28 additions & 12 deletions packages/client-runtime/src/device/phoneViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ export function createPhoneViewer(options: {
let layout = phoneDisplayLayout(screen, options.source.width, options.source.height);
let profile = options.profile ?? IOS_PHONE_SHAPE;
let foldAngle = options.foldAngle ?? null;
let orientationAngle =
foldAngle !== null && profile.id.startsWith("android") ? 0 : layout.rotation;
let orientationTurn: { from: number; to: number; startedAt: number } | null = null;
let imported: Awaited<ReturnType<typeof loadDeviceModel>> | null = null;
let modelSource = options.model ?? null;
Expand All @@ -110,16 +108,20 @@ export function createPhoneViewer(options: {
let foldAspect = isFoldInnerAspect(rawAspect()) ? rawAspect() : DEFAULT_FOLD_INNER_ASPECT;
const createFoldScene = (angle: number, displayLayout = layout) =>
createAndroidFoldScene(texture, displayLayout, angle, foldAspect);
// Only an inner display frame fits the fold body; the cover screen would stretch any other frame.
const fitsFold = (nextProfile = profile) =>
nextProfile.id.startsWith("android") && isFoldInnerAspect(rawAspect());
Comment thread
shivamhwp marked this conversation as resolved.
Resolved
const visibleFoldAngle = (fallback: number) => {
if (!foldTurn) return fallback;
const progress = Math.min(1, (performance.now() - foldTurn.startedAt) / ANDROID_FOLD_TURN_MS);
const eased = progress * progress * (3 - 2 * progress);
return foldTurn.from + (foldTurn.to - foldTurn.from) * eased;
};
let phone: ReturnType<typeof createPhoneScene> | ReturnType<typeof createAndroidFoldScene> =
foldAngle !== null && profile.id.startsWith("android")
foldAngle !== null && fitsFold()
? createFoldScene(foldAngle)
: createPhoneScene(texture, layout, profile);
let orientationAngle = "setAngle" in phone ? 0 : layout.rotation;
scene.add(phone.root);
let disposed = false;
const rest = new Quaternion();
Expand Down Expand Up @@ -238,25 +240,32 @@ export function createPhoneViewer(options: {
const frameAspect = rawAspect();
const innerChanged = isFoldInnerAspect(frameAspect) && frameAspect !== foldAspect;
if (innerChanged) foldAspect = frameAspect;
if (!imported && "setAngle" in phone && innerChanged) {
const angle = visibleFoldAngle(foldAngle ?? 180);
const wasFold = "setAngle" in phone;
const foldTo = !imported && foldAngle !== null && fitsFold(nextProfile) ? foldAngle : null;
if (foldTo !== null && (!wasFold || innerChanged)) {
const angle = wasFold ? visibleFoldAngle(foldTo) : foldTo;
if (!wasFold) foldTurn = null;
scene.remove(phone.root);
phone.dispose();
phone = createFoldScene(angle, next);
scene.add(phone.root);
} else if (
!imported &&
!("setAngle" in phone) &&
(nextProfile !== profile || next.aspect !== layout.aspect)
foldTo === null &&
(wasFold || nextProfile !== profile || next.aspect !== layout.aspect)
) {
foldTurn = null;
scene.remove(phone.root);
phone.dispose();
phone = createPhoneScene(texture, next, nextProfile);
scene.add(phone.root);
} else {
phone.setDisplay(texture, next);
}
if (next.rotation !== layout.rotation) {
if ("setAngle" in phone !== wasFold) {
orientationTurn = null;
orientationAngle = wasFold ? next.rotation : 0;
} else if (next.rotation !== layout.rotation) {
if (nextProfile.id.startsWith("android") && !("setAngle" in phone) && !reducedMotion()) {
const difference = Math.atan2(
Math.sin(next.rotation - orientationAngle),
Expand Down Expand Up @@ -287,7 +296,7 @@ export function createPhoneViewer(options: {
? null
: model
? createImportedPhoneScene(model.asset, texture, layout)
: foldAngle !== null && profile.id.startsWith("android")
: foldAngle !== null && fitsFold()
? createFoldScene(foldAngle)
: createPhoneScene(texture, layout, profile);
foldTurn = null;
Expand All @@ -301,6 +310,8 @@ export function createPhoneViewer(options: {
if (imported) phone.orientation.add(accessory.asset);
else accessory.asset.removeFromParent();
}
orientationTurn = null;
orientationAngle = "setAngle" in phone ? 0 : layout.rotation;
applyPose();
fit(true);
scheduler.invalidate();
Expand Down Expand Up @@ -350,16 +361,21 @@ export function createPhoneViewer(options: {
const previous = foldAngle;
foldAngle = next;
if (imported) return;
if (next === null || !("setAngle" in phone)) {
const swap = (nextPhone: typeof phone) => {
scene.remove(phone.root);
phone.dispose();
phone = next === null ? createPhoneScene(texture, layout, profile) : createFoldScene(next);
phone = nextPhone;
scene.add(phone.root);
orientationTurn = null;
orientationAngle = next === null ? layout.rotation : 0;
orientationAngle = "setAngle" in phone ? 0 : layout.rotation;
foldTurn = null;
applyPose();
fit(true);
};
if (next === null || !fitsFold()) {
if ("setAngle" in phone) swap(createPhoneScene(texture, layout, profile));
} else if (!("setAngle" in phone)) {
swap(createFoldScene(next));
} else {
const from = visibleFoldAngle(previous ?? next);
if (reducedMotion()) {
Expand Down
Loading