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 Feb 2, 2023
1 parent ee85098 commit 1333c95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 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
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export default class Agent extends EventEmitter<{
stopInspectingNative: [],
shutdown: [],
traceUpdates: [Set<NativeType>],
drawTraceUpdates: [Array<NativeType>],
disableTraceUpdates: [],
}> {
_bridge: BackendBridge;
_isProfiling: boolean = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {Data} from './index';
import type {Rect} from '../utils';
import type {NativeType} from '../../types';
import type Agent from '../../agent';

const OUTLINE_COLOR = '#f0f0f0';

Expand All @@ -29,7 +30,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 +51,24 @@ 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);
}
});
}

function iterateNodes(
nodeToData: Map<NativeType, Data>,
execute: (rect: Rect | null, color: string, node: NativeType) => void,
) {
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 +97,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 1333c95

Please sign in to comment.