Skip to content

Commit f3ad5a8

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

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

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: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {Data} from './index';
1111
import type {Rect} from '../utils';
1212
import type {NativeType} from '../../types';
1313

14+
import Agent from 'react-devtools-shared/src/backend/agent';
15+
1416
const OUTLINE_COLOR = '#f0f0f0';
1517

1618
// Note these colors are in sync with DevTools Profiler chart colors.
@@ -29,7 +31,17 @@ const COLORS = [
2931

3032
let canvas: HTMLCanvasElement | null = null;
3133

32-
export function draw(nodeToData: Map<NativeType, Data>): void {
34+
export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
35+
if (window.document == null) {
36+
const nodesToDraw = [];
37+
iterateNodes(nodeToData, (_, color, node) => {
38+
nodesToDraw.push({ node, color });
39+
});
40+
41+
agent.emit('drawTraceUpdates', nodesToDraw);
42+
return;
43+
}
44+
3345
if (canvas === null) {
3446
initialize();
3547
}
@@ -40,17 +52,21 @@ export function draw(nodeToData: Map<NativeType, Data>): void {
4052

4153
const context = canvasFlow.getContext('2d');
4254
context.clearRect(0, 0, canvasFlow.width, canvasFlow.height);
43-
44-
nodeToData.forEach(({count, rect}) => {
55+
iterateNodes(nodeToData, (rect, color) => {
4556
if (rect !== null) {
46-
const colorIndex = Math.min(COLORS.length - 1, count - 1);
47-
const color = COLORS[colorIndex];
48-
49-
drawBorder(context, rect, color);
57+
drawBorder(context, rect, color)
5058
}
5159
});
5260
}
5361

62+
function iterateNodes(nodeToData: Map<NativeType, Data>, execute: (rect: Rect, color: String) => void, node: NativeType) {
63+
nodeToData.forEach(({count, rect}, node) => {
64+
const colorIndex = Math.min(COLORS.length - 1, count - 1);
65+
const color = COLORS[colorIndex];
66+
execute(rect, color, node);
67+
});
68+
}
69+
5470
function drawBorder(
5571
context: CanvasRenderingContext2D,
5672
rect: Rect,
@@ -79,7 +95,12 @@ function drawBorder(
7995
context.setLineDash([0]);
8096
}
8197

82-
export function destroy(): void {
98+
export function destroy(agent: Agent): void {
99+
if (window.document == null) {
100+
agent.emit('disableTraceUpdates');
101+
return;
102+
}
103+
83104
if (canvas !== null) {
84105
if (canvas.parentNode != null) {
85106
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)