Skip to content

Commit

Permalink
Support highlights for React Native apps in dev tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancat committed Jan 27, 2023
1 parent ee85098 commit f3ad5a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/react-devtools-core/src/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ function initialize(socket: WebSocket) {
store = new Store(bridge, {
checkBridgeProtocolCompatibility: true,
supportsNativeInspection: true,
supportsTraceUpdates: true,
});

log('Connected');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {Data} from './index';
import type {Rect} from '../utils';
import type {NativeType} from '../../types';

import Agent from 'react-devtools-shared/src/backend/agent';

const OUTLINE_COLOR = '#f0f0f0';

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

let canvas: HTMLCanvasElement | null = null;

export function draw(nodeToData: Map<NativeType, Data>): void {
export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
if (window.document == null) {
const nodesToDraw = [];
iterateNodes(nodeToData, (_, color, node) => {
nodesToDraw.push({ node, color });
});

agent.emit('drawTraceUpdates', nodesToDraw);
return;
}

if (canvas === null) {
initialize();
}
Expand All @@ -40,17 +52,21 @@ export function draw(nodeToData: Map<NativeType, Data>): void {

const context = canvasFlow.getContext('2d');
context.clearRect(0, 0, canvasFlow.width, canvasFlow.height);

nodeToData.forEach(({count, rect}) => {
iterateNodes(nodeToData, (rect, color) => {
if (rect !== null) {
const colorIndex = Math.min(COLORS.length - 1, count - 1);
const color = COLORS[colorIndex];

drawBorder(context, rect, color);
drawBorder(context, rect, color)
}
});
}

function iterateNodes(nodeToData: Map<NativeType, Data>, execute: (rect: Rect, color: String) => void, node: NativeType) {
nodeToData.forEach(({count, rect}, node) => {
const colorIndex = Math.min(COLORS.length - 1, count - 1);
const color = COLORS[colorIndex];
execute(rect, color, node);
});
}

function drawBorder(
context: CanvasRenderingContext2D,
rect: Rect,
Expand Down Expand Up @@ -79,7 +95,12 @@ function drawBorder(
context.setLineDash([0]);
}

export function destroy(): void {
export function destroy(agent: Agent): void {
if (window.document == null) {
agent.emit('disableTraceUpdates');
return;
}

if (canvas !== null) {
if (canvas.parentNode != null) {
canvas.parentNode.removeChild(canvas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function toggleEnabled(value: boolean): void {
redrawTimeoutID = null;
}

destroyCanvas();
destroyCanvas(agent);
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ function prepareToDraw(): void {
}
});

draw(nodeToData);
draw(nodeToData, agent);

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

0 comments on commit f3ad5a8

Please sign in to comment.