Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ interface Spec extends TurboModule {
viewportOffsetY?: number,
) => RootTag;
stopSurface: (surfaceId: RootTag) => void;
updateSurfaceConstraints: (
surfaceId: RootTag,
viewportWidth: number,
viewportHeight: number,
devicePixelRatio: number,
) => void;
enqueueNativeEvent: (
shadowNode: unknown /* ShadowNode */,
type: string,
Expand Down
4 changes: 4 additions & 0 deletions private/react-native-fantom/__docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ values. If another root is still alive and the new metrics differ from the
current global dimensions, Fantom applies the new values and emits a warning.
Call `root.destroy()` when a root is no longer needed.

`root.updateSurfaceConstraints(width, height)` changes native root constraints
without rendering React or flushing pending work. Use it to test root and
relative child layout when a native host resizes the surface.

Each root also uses its `fontScale` for native text and text-input measurement,
including line heights and wrapping. `allowFontScaling={false}` opts a component
out, and `maxFontSizeMultiplier` caps its scale. Existing roots retain their own
Expand Down
65 changes: 65 additions & 0 deletions private/react-native-fantom/src/__tests__/Fantom-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,71 @@ function getActualViewportDimensions(root: Root): {
}

describe('Fantom', () => {
describe('root.updateSurfaceConstraints', () => {
it('resizes the root and percentage-sized descendants without another React render', () => {
const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 80});
const nodeRef = createRef<HostInstance>();
Fantom.runTask(() => {
root.render(
<View ref={nodeRef} style={{width: '50%', height: '25%'}} />,
);
});
const node = nullthrows(nodeRef.current);
Fantom.runTask(() => {
const documentElement = nullthrows(root.document.documentElement);
expect(documentElement.getBoundingClientRect().width).toBe(100);
expect(node.getBoundingClientRect().width).toBe(50);
});

root.updateSurfaceConstraints(200, 160);
Fantom.runTask(() => {
const documentElement = nullthrows(root.document.documentElement);
expect(documentElement.getBoundingClientRect().width).toBe(200);
expect(documentElement.getBoundingClientRect().height).toBe(160);
expect(node.getBoundingClientRect().width).toBe(100);
expect(node.getBoundingClientRect().height).toBe(40);
expect(nodeRef.current).toBe(node);
});

root.updateSurfaceConstraints(80, 40);
Fantom.runTask(() => {
const documentElement = nullthrows(root.document.documentElement);
expect(documentElement.getBoundingClientRect().width).toBe(80);
expect(documentElement.getBoundingClientRect().height).toBe(40);
expect(node.getBoundingClientRect().width).toBe(40);
expect(node.getBoundingClientRect().height).toBe(10);
});
root.destroy();
});

it('does not flush pending JS work when updating native constraints', () => {
const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 80});
Fantom.runTask(() => root.render(<View style={{flex: 1}} />));
let didRunTask = false;
Fantom.scheduleTask(() => {
didRunTask = true;
expect(
nullthrows(root.document.documentElement).getBoundingClientRect()
.width,
).toBe(200);
});

root.updateSurfaceConstraints(200, 160);
expect(didRunTask).toBe(false);
Fantom.runWorkLoop();
expect(didRunTask).toBe(true);
root.destroy();
});

it('rejects resizing a destroyed root', () => {
const root = Fantom.createRoot();
root.destroy();
expect(() => root.updateSurfaceConstraints(200, 160)).toThrow(
'Cannot resize a destroyed root.',
);
});
});

describe('scheduleTask', () => {
it('does not run task immediately', () => {
let didRun = false;
Expand Down
16 changes: 16 additions & 0 deletions private/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ class Root {
return NativeFantom.takeMountingManagerLogs(this.#surfaceId);
}

/**
* Changes the root's native surface constraints without rendering React,
* updating Dimensions, or flushing pending work.
*/
updateSurfaceConstraints(width: number, height: number): void {
if (this.#destroyed) {
throw new Error('Cannot resize a destroyed root.');
}
NativeFantom.updateSurfaceConstraints(
this.#surfaceId,
width,
height,
this.#devicePixelRatio,
);
}

destroy() {
// TODO: check for leaks.
NativeFantom.stopSurface(this.#surfaceId);
Expand Down
13 changes: 13 additions & 0 deletions private/react-native-fantom/tester/src/NativeFantom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ void NativeFantom::stopSurface(jsi::Runtime& /*runtime*/, SurfaceId surfaceId) {
appDelegate_.stopSurface(surfaceId);
}

void NativeFantom::updateSurfaceConstraints(
jsi::Runtime& /*runtime*/,
SurfaceId surfaceId,
double viewportWidth,
double viewportHeight,
double devicePixelRatio) {
appDelegate_.updateSurfaceConstraints(
surfaceId,
static_cast<float>(viewportWidth),
static_cast<float>(viewportHeight),
static_cast<float>(devicePixelRatio));
}

void NativeFantom::produceFramesForDuration(
jsi::Runtime& /*runtime*/,
double milliseconds) {
Expand Down
7 changes: 7 additions & 0 deletions private/react-native-fantom/tester/src/NativeFantom.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class NativeFantom : public NativeFantomCxxSpec<NativeFantom> {

void stopSurface(jsi::Runtime &runtime, SurfaceId surfaceId);

void updateSurfaceConstraints(
jsi::Runtime &runtime,
SurfaceId surfaceId,
double viewportWidth,
double viewportHeight,
double devicePixelRatio);

void produceFramesForDuration(jsi::Runtime &runtime, double milliseconds);

void flushMessageQueue(jsi::Runtime &runtime);
Expand Down
Loading