diff --git a/packages/client-runtime/src/device/phoneViewer.test.ts b/packages/client-runtime/src/device/phoneViewer.test.ts index 9d8280ca9acd3..cd792075876b7 100644 --- a/packages/client-runtime/src/device/phoneViewer.test.ts +++ b/packages/client-runtime/src/device/phoneViewer.test.ts @@ -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(); @@ -91,7 +91,7 @@ afterEach(() => { models.pending.length = 0; }); -function fixture(profile?: DeviceShapeProfile) { +function fixture(profile?: DeviceShapeProfile, foldAngle?: number) { const pending = new Map(); let id = 0; let now = 0; @@ -111,6 +111,7 @@ function fixture(profile?: DeviceShapeProfile) { onUnavailable, onFramingAspect, ...(profile ? { profile } : {}), + ...(foldAngle === undefined ? {} : { foldAngle }), }); const draw = (time = now) => { now = time; @@ -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(); +}); diff --git a/packages/client-runtime/src/device/phoneViewer.ts b/packages/client-runtime/src/device/phoneViewer.ts index af45745f8e484..73d8bc07ccc09 100644 --- a/packages/client-runtime/src/device/phoneViewer.ts +++ b/packages/client-runtime/src/device/phoneViewer.ts @@ -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> | null = null; let modelSource = options.model ?? null; @@ -110,6 +108,9 @@ 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()); const visibleFoldAngle = (fallback: number) => { if (!foldTurn) return fallback; const progress = Math.min(1, (performance.now() - foldTurn.startedAt) / ANDROID_FOLD_TURN_MS); @@ -117,9 +118,10 @@ export function createPhoneViewer(options: { return foldTurn.from + (foldTurn.to - foldTurn.from) * eased; }; let phone: ReturnType | ReturnType = - 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(); @@ -238,17 +240,21 @@ 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); @@ -256,7 +262,10 @@ export function createPhoneViewer(options: { } 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), @@ -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; @@ -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(); @@ -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()) {