Skip to content

Commit 8c9d948

Browse files
fix(devices): keep the device aspect ratio and shrink height when the panel is narrow
Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 3edb3c8 commit 8c9d948

1 file changed

Lines changed: 60 additions & 12 deletions

File tree

‎apps/web/src/components/device/DeviceStreamView.tsx‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ export function DeviceStreamView(props: {
105105
props.visible,
106106
]);
107107

108+
// Displayed aspect ratio (width / height) of the device as the user sees it.
108109
const aspect = useMemo(() => {
109-
if (!screen) return props.platform === "ios" ? "9 / 19.5" : "9 / 20";
110+
if (!screen) return props.platform === "ios" ? 9 / 19.5 : 9 / 20;
110111
const landscape =
111112
screen.orientation === "landscape_left" || screen.orientation === "landscape_right";
112113
const w = landscape
@@ -115,9 +116,39 @@ export function DeviceStreamView(props: {
115116
const h = landscape
116117
? Math.min(screen.width, screen.height)
117118
: Math.max(screen.width, screen.height);
118-
return `${w} / ${h}`;
119+
return w / h;
119120
}, [props.platform, screen]);
120121

122+
// The frame is the largest box at `aspect` that fits the container, so a
123+
// narrow panel shows a shorter phone rather than a squeezed one. CSS
124+
// `aspect-ratio` alone cannot do this: with the height pinned to 100% the
125+
// width clamp wins and distorts the drawn frame.
126+
const hostRef = useRef<HTMLDivElement | null>(null);
127+
const [host, setHost] = useState({ width: 0, height: 0 });
128+
useEffect(() => {
129+
const element = hostRef.current;
130+
if (!element) return;
131+
const update = () => {
132+
const rect = element.getBoundingClientRect();
133+
setHost((current) =>
134+
current.width === rect.width && current.height === rect.height
135+
? current
136+
: { width: rect.width, height: rect.height },
137+
);
138+
};
139+
update();
140+
const observer = new ResizeObserver(update);
141+
observer.observe(element);
142+
return () => observer.disconnect();
143+
}, []);
144+
const frame = useMemo(() => {
145+
if (host.width === 0 || host.height === 0) return { width: 0, height: 0 };
146+
const byHeight = { width: host.height * aspect, height: host.height };
147+
return byHeight.width <= host.width
148+
? byHeight
149+
: { width: host.width, height: host.width / aspect };
150+
}, [aspect, host]);
151+
121152
// serve-sim streams the raw framebuffer; rotate the display for a device
122153
// that reports landscape while its frames stay portrait.
123154
const rotation = useMemo(() => {
@@ -134,6 +165,26 @@ export function DeviceStreamView(props: {
134165
}
135166
}, [props.platform, screen]);
136167

168+
// A sideways rotation draws the raw portrait frame into a landscape box:
169+
// the media element takes the transposed size and is rotated about the
170+
// box's center.
171+
const sideways = rotation === 90 || rotation === -90;
172+
const mediaStyle: React.CSSProperties = sideways
173+
? {
174+
width: frame.height,
175+
height: frame.width,
176+
left: (frame.width - frame.height) / 2,
177+
top: (frame.height - frame.width) / 2,
178+
transform: `rotate(${rotation}deg)`,
179+
}
180+
: {
181+
width: frame.width,
182+
height: frame.height,
183+
left: 0,
184+
top: 0,
185+
...(rotation ? { transform: `rotate(${rotation}deg)` } : {}),
186+
};
187+
137188
const pointerActive = useRef(false);
138189
const normalizedPoint = (event: React.PointerEvent<HTMLElement>) => {
139190
const rect = event.currentTarget.getBoundingClientRect();
@@ -147,6 +198,7 @@ export function DeviceStreamView(props: {
147198

148199
return (
149200
<div
201+
ref={hostRef}
150202
className="relative flex size-full items-center justify-center overflow-hidden bg-black/90 outline-none"
151203
tabIndex={0}
152204
role="application"
@@ -161,12 +213,8 @@ export function DeviceStreamView(props: {
161213
}}
162214
>
163215
<div
164-
className="relative max-h-full max-w-full select-none"
165-
style={{
166-
aspectRatio: aspect,
167-
height: rotation === 0 || rotation === 180 ? "100%" : undefined,
168-
width: rotation === 90 || rotation === -90 ? "100%" : undefined,
169-
}}
216+
className="relative select-none"
217+
style={{ width: frame.width, height: frame.height }}
170218
onPointerDown={(event) => {
171219
event.currentTarget.setPointerCapture(event.pointerId);
172220
(event.currentTarget.parentElement as HTMLElement | null)?.focus();
@@ -194,17 +242,17 @@ export function DeviceStreamView(props: {
194242
>
195243
<canvas
196244
ref={canvasRef}
197-
className={cn("size-full", mjpegUrl && "hidden")}
198-
style={rotation ? { transform: `rotate(${rotation}deg)` } : undefined}
245+
className={cn("absolute", mjpegUrl && "hidden")}
246+
style={mediaStyle}
199247
/>
200248
{mjpegUrl ? (
201249
<img
202250
key={mjpegGeneration}
203251
src={mjpegUrl}
204252
alt=""
205253
draggable={false}
206-
className="size-full object-contain"
207-
style={rotation ? { transform: `rotate(${rotation}deg)` } : undefined}
254+
className="absolute object-contain"
255+
style={mediaStyle}
208256
/>
209257
) : null}
210258
</div>

0 commit comments

Comments
 (0)