useViewSize
The plugin view's size, pixel density and safe area, re-rendering the component when the user resizes the editor.
function useViewSize(): ViewSize;
interface ViewSize {
readonly width: number; // logical pixels
readonly height: number; // logical pixels
readonly scale: number; // device pixels per logical pixel
readonly safeArea: LayoutRect; // the part always shown, in logical pixels
}useViewSize() returns the size of the plugin view and re-renders the
component when it changes: when the user resizes a
resizable editor, when a web host's embedding
iframe changes size, or when the view moves to a display of another density.
An editor with resize: 'scale' keeps its width and height as the user
resizes it; only scale changes, unless it covers a window of another
shape (fit: 'cover'), where width and height grow and safeArea says
where its configured size is.
It does not re-render for an unchanged size, and it does not poll: the
runtime tells it of a change, once.
import { Text, useViewSize, View } from '@soundor/react';
// The view's size in logical pixels, re-rendered when it changes. Below 480
// the two controls stack; above, they sit side by side. Here the view is the
// page's iframe: make the window narrower to see it switch.
export default function ViewSize() {
const { width, height, scale } = useViewSize();
const compact = width < 480;
return (
<View style={{ alignSelf: 'stretch', gap: 12 }}>
<Text style={{ color: '#8b8f98', fontSize: 13 }}>
{`${Math.round(width)} × ${Math.round(height)} · scale ${scale}`}
</Text>
<View style={{ flexDirection: compact ? 'column' : 'row', gap: 8 }}>
{['Gain', 'Tone'].map((label) => (
<View
key={label}
style={{
flex: compact ? 0 : 1,
padding: 16,
borderRadius: 8,
backgroundColor: '#24262c',
}}
>
<Text style={{ color: '#e6e7ea', fontSize: 15 }}>{label}</Text>
</View>
))}
</View>
</View>
);
}view-size.tsxSourceimport { Text, useViewSize, View } from '@soundor/react';
function Controls() {
const { width, height } = useViewSize();
const compact = width < 640;
return (
<View style={{ flex: 1, flexDirection: compact ? 'column' : 'row' }}>
<Text>{`${width} × ${height}`}</Text>
<Fader />
<Knob />
</View>
);
}The same component runs in the VST3 runtime and in the browser, with no check for which one it is in.
Logical and device pixels
width and height are logical pixels: the units of styles, layout and
pointer positions, the same at any display density. scale is how many
device pixels there are per logical pixel: 2 on a typical high-density
display. It also counts any scaling a host applies: a web host showing a
fixed-size view at half its size in a small page reports half the
display's density. Draw a canvas at width * scale pixels for a sharp
picture; lay out with width and height.
The safe area
safeArea is the whole view, except in a resizable editor with
fit: 'cover'
in a window it was not made for (larger than its maximum, or of another
shape when it scales): there the view covers the window and safeArea is
the part it was made for, in its middle. Keep the content in it and decorate the rest:
function App() {
const { width, height, safeArea } = useViewSize();
return (
<View style={{ width, height, backgroundColor: '#14161c' }}>
<View
style={{
position: 'absolute',
left: safeArea.x,
top: safeArea.y,
width: safeArea.width,
height: safeArea.height,
}}
>
<Controls />
</View>
</View>
);
}The editor's own size, in its pixels, is
editorSize().
Flexbox first
Most layouts need no size at all: a view with flex: 1 fills the editor and
follows it as it is resized, flexWrap: 'wrap' reflows a row, and
percentages scale with their parent. Use useViewSize() for what flexbox
cannot say: a different arrangement past a breakpoint, a control shown only
when there is room, or a canvas's drawing buffer.
In each runtime
- VST3: the size of the editor, from the moment the host gives it one
(before that, briefly, 0×0). A resize reaches the hook on the next tick of
the editor's 60 Hz timer, before the frame laid out at the new size is
drawn. After a reload in
soundor dev, the new runtime's hooks start at the editor's current size. - Web: the size of the plugin view: the editor's size in the
demoanddevpresentations (resizable with its handle), and the iframe's in?embed, following the iframe as the parent page resizes it. - Tests: the editor's size from the project's config (800×600 without
one), all of it safe;
testView.resize()changes it, the safe area included.
It reads soundor:ui's viewSize() and
subscribeViewSize(), which code outside React can use directly.