Skip to content

Commit c6b4b02

Browse files
committed
Support highlights for React Native apps in dev tools
1 parent ee85098 commit c6b4b02

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

packages/react-devtools-core/src/standalone.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ function initialize(socket: WebSocket) {
262262
store = new Store(bridge, {
263263
checkBridgeProtocolCompatibility: true,
264264
supportsNativeInspection: true,
265+
supportsTraceUpdates: true,
265266
});
266267

267268
log('Connected');

packages/react-devtools-shared/src/backend/views/TraceUpdates/canvas.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {Data} from './index';
1111
import type {Rect} from '../utils';
1212
import type {NativeType} from '../../types';
13+
import type Agent from '../../agent';
1314

1415
const OUTLINE_COLOR = '#f0f0f0';
1516

@@ -29,7 +30,17 @@ const COLORS = [
2930

3031
let canvas: HTMLCanvasElement | null = null;
3132

32-
export function draw(nodeToData: Map<NativeType, Data>): void {
33+
export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
34+
if (window.document == null) {
35+
const nodesToDraw = [];
36+
iterateNodes(nodeToData, (_, color, node) => {
37+
nodesToDraw.push({node, color});
38+
});
39+
40+
agent.emit('drawTraceUpdates', nodesToDraw);
41+
return;
42+
}
43+
3344
if (canvas === null) {
3445
initialize();
3546
}
@@ -40,17 +51,25 @@ export function draw(nodeToData: Map<NativeType, Data>): void {
4051

4152
const context = canvasFlow.getContext('2d');
4253
context.clearRect(0, 0, canvasFlow.width, canvasFlow.height);
43-
44-
nodeToData.forEach(({count, rect}) => {
54+
iterateNodes(nodeToData, (rect, color) => {
4555
if (rect !== null) {
46-
const colorIndex = Math.min(COLORS.length - 1, count - 1);
47-
const color = COLORS[colorIndex];
48-
4956
drawBorder(context, rect, color);
5057
}
5158
});
5259
}
5360

61+
function iterateNodes(
62+
nodeToData: Map<NativeType, Data>,
63+
execute: (rect: Rect, color: String) => void,
64+
node: NativeType,
65+
) {
66+
nodeToData.forEach(({count, rect}, node) => {
67+
const colorIndex = Math.min(COLORS.length - 1, count - 1);
68+
const color = COLORS[colorIndex];
69+
execute(rect, color, node);
70+
});
71+
}
72+
5473
function drawBorder(
5574
context: CanvasRenderingContext2D,
5675
rect: Rect,
@@ -79,7 +98,12 @@ function drawBorder(
7998
context.setLineDash([0]);
8099
}
81100

82-
export function destroy(): void {
101+
export function destroy(agent: Agent): void {
102+
if (window.document == null) {
103+
agent.emit('disableTraceUpdates');
104+
return;
105+
}
106+
83107
if (canvas !== null) {
84108
if (canvas.parentNode != null) {
85109
canvas.parentNode.removeChild(canvas);

packages/react-devtools-shared/src/backend/views/TraceUpdates/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function toggleEnabled(value: boolean): void {
6666
redrawTimeoutID = null;
6767
}
6868

69-
destroyCanvas();
69+
destroyCanvas(agent);
7070
}
7171
}
7272

@@ -126,7 +126,7 @@ function prepareToDraw(): void {
126126
}
127127
});
128128

129-
draw(nodeToData);
129+
draw(nodeToData, agent);
130130

131131
if (earliestExpiration !== Number.MAX_VALUE) {
132132
redrawTimeoutID = setTimeout(prepareToDraw, earliestExpiration - now);

0 commit comments

Comments
 (0)