Skip to content

Add Component Highlighting to Profiler DevTools #17934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -15,9 +15,12 @@ type Props = {|
color: string,
height: number,
isDimmed?: boolean,
isHovered?: boolean,
label: string,
onClick: (event: SyntheticMouseEvent<*>) => mixed,
onDoubleClick?: (event: SyntheticMouseEvent<*>) => mixed,
onMouseEnter?: (event: SyntheticMouseEvent<*>) => mixed,
onMouseLeave?: (event: SyntheticMouseEvent<*>) => mixed,
placeLabelAboveNode?: boolean,
textStyle?: Object,
width: number,
Expand All @@ -31,14 +34,24 @@ export default function ChartNode({
color,
height,
isDimmed = false,
isHovered = false,
label,
onClick,
onDoubleClick,
onMouseEnter,
onMouseLeave,
textStyle,
width,
x,
y,
}: Props) {
let opacity = 1;
if (isHovered) {
opacity = 0.75;
} else if (isDimmed) {
opacity = 0.5;
}

return (
<g className={styles.Group} transform={`translate(${x},${y})`}>
<title>{label}</title>
Expand All @@ -48,9 +61,11 @@ export default function ChartNode({
fill={color}
onClick={onClick}
onDoubleClick={onDoubleClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
className={styles.Rect}
style={{
opacity: isDimmed ? 0.5 : 1,
opacity,
}}
/>
{width >= minWidthToDisplay && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {ProfilerContext} from './ProfilerContext';
import NoCommitData from './NoCommitData';
import CommitFlamegraphListItem from './CommitFlamegraphListItem';
import {scale} from './utils';
import {StoreContext} from '../context';
import {BridgeContext, StoreContext} from '../context';
import {SettingsContext} from '../Settings/SettingsContext';

import styles from './CommitFlamegraph.css';
Expand All @@ -24,6 +24,7 @@ import type {CommitTree} from './types';

export type ItemData = {|
chartData: ChartData,
onElementMouseEnter: (id: number) => void,
scaleX: (value: number, fallbackValue: number) => number,
selectedChartNode: ChartNode | null,
selectedChartNodeIndex: number,
Expand Down Expand Up @@ -93,6 +94,8 @@ type Props = {|
function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
const {lineHeight} = useContext(SettingsContext);
const {selectFiber, selectedFiberID} = useContext(ProfilerContext);
const store = useContext(StoreContext);
const bridge = useContext(BridgeContext);

const selectedChartNodeIndex = useMemo<number>(() => {
if (selectedFiberID === null) {
Expand All @@ -115,9 +118,36 @@ function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
return null;
}, [chartData, selectedFiberID, selectedChartNodeIndex]);

const highlightNativeElement = useCallback(
(id: number) => {
const element = store.getElementByID(id);
const rendererID = store.getRendererIDForElement(id);
if (element !== null && rendererID !== null) {
bridge.send('highlightNativeElement', {
displayName: element.displayName,
hideAfterTimeout: false,
id,
openNativeElementsPanel: false,
rendererID,
scrollIntoView: false,
});
}
},
[store, bridge],
);

// Highlight last hovered element.
const handleElementMouseEnter = useCallback(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@M-Izadmehr, can you please explain why you wrap in another useCallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done:)
I removed the useless useCallback.

id => {
highlightNativeElement(id);
},
[highlightNativeElement],
);

Copy link

@strdr4605 strdr4605 Jan 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is also repeated in packages/react-devtools-shared/src/devtools/views/Profiler/CommitRanked.js and also in
packages/react-devtools-shared/src/devtools/views/Components/Tree.js.

Maybe better to create a separate hook file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, Yeah I absolutely agree, I will try to use a hook for the highlighting functionality:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, now I extracted the highlighting logic to the hooks file, and created a hook called useNativeElementHighlighter, this hook exposes two methods:

  1. highlightNativeElement: method used to highlight an element using its id
  2. clearNativeElementHighlight: method used to clear highlighting of elements

const itemData = useMemo<ItemData>(
() => ({
chartData,
onElementMouseEnter: handleElementMouseEnter,
scaleX: scale(
0,
selectedChartNode !== null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import React, {Fragment, memo, useCallback, useContext} from 'react';
import React, {Fragment, memo, useCallback, useContext, useState} from 'react';
import {areEqual} from 'react-window';
import {barWidthThreshold} from './constants';
import {getGradientColor} from './utils';
Expand All @@ -24,8 +24,11 @@ type Props = {
};

function CommitFlamegraphListItem({data, index, style}: Props) {
const [isHovered, setIsHovered] = useState(false);

const {
chartData,
onElementMouseEnter,
scaleX,
selectedChartNode,
selectedChartNodeIndex,
Expand All @@ -43,6 +46,18 @@ function CommitFlamegraphListItem({data, index, style}: Props) {
[selectFiber],
);

const handleMouseEnter = (id: number) => {
setIsHovered(true);

if (id !== null) {
onElementMouseEnter(id);
}
};

const handleMouseLeave = (id: number) => {
setIsHovered(false);
};

// List items are absolutely positioned using the CSS "top" attribute.
// The "left" value will always be 0.
// Since height is fixed, and width is based on the node's duration,
Expand Down Expand Up @@ -101,9 +116,12 @@ function CommitFlamegraphListItem({data, index, style}: Props) {
color={color}
height={lineHeight}
isDimmed={index < selectedChartNodeIndex}
isHovered={isHovered}
key={id}
label={label}
onClick={event => handleClick(event, id, name)}
onMouseEnter={() => handleMouseEnter(id)}
onMouseLeave={() => handleMouseLeave(id)}
textStyle={{color: textColor}}
width={nodeWidth}
x={nodeOffset - selectedNodeOffset}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {ProfilerContext} from './ProfilerContext';
import NoCommitData from './NoCommitData';
import CommitRankedListItem from './CommitRankedListItem';
import {scale} from './utils';
import {StoreContext} from '../context';
import {BridgeContext, StoreContext} from '../context';
import {SettingsContext} from '../Settings/SettingsContext';

import styles from './CommitRanked.css';
Expand All @@ -24,6 +24,7 @@ import type {CommitTree} from './types';

export type ItemData = {|
chartData: ChartData,
onElementMouseEnter: (id: number) => void,
scaleX: (value: number, fallbackValue: number) => number,
selectedFiberID: number | null,
selectedFiberIndex: number,
Expand Down Expand Up @@ -91,15 +92,44 @@ type Props = {|
function CommitRanked({chartData, commitTree, height, width}: Props) {
const {lineHeight} = useContext(SettingsContext);
const {selectedFiberID, selectFiber} = useContext(ProfilerContext);
const store = useContext(StoreContext);
const bridge = useContext(BridgeContext);

const selectedFiberIndex = useMemo(
() => getNodeIndex(chartData, selectedFiberID),
[chartData, selectedFiberID],
);

const highlightNativeElement = useCallback(
(id: number) => {
const element = store.getElementByID(id);
const rendererID = store.getRendererIDForElement(id);
if (element !== null && rendererID !== null) {
bridge.send('highlightNativeElement', {
displayName: element.displayName,
hideAfterTimeout: false,
id,
openNativeElementsPanel: false,
rendererID,
scrollIntoView: false,
});
}
},
[store, bridge],
);

// Highlight last hovered element.
const handleElementMouseEnter = useCallback(
id => {
highlightNativeElement(id);
},
[highlightNativeElement],
);

const itemData = useMemo<ItemData>(
() => ({
chartData,
onElementMouseEnter: handleElementMouseEnter,
scaleX: scale(0, chartData.nodes[selectedFiberIndex].value, 0, width),
selectedFiberID,
selectedFiberIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import React, {memo, useCallback, useContext} from 'react';
import React, {memo, useCallback, useContext, useState} from 'react';
import {areEqual} from 'react-window';
import {minBarWidth} from './constants';
import {getGradientColor} from './utils';
Expand All @@ -24,7 +24,15 @@ type Props = {
};

function CommitRankedListItem({data, index, style}: Props) {
const {chartData, scaleX, selectedFiberIndex, selectFiber, width} = data;
const [isHovered, setIsHovered] = useState(false);
const {
chartData,
onElementMouseEnter,
scaleX,
selectedFiberIndex,
selectFiber,
width,
} = data;

const node = chartData.nodes[index];

Expand All @@ -38,6 +46,18 @@ function CommitRankedListItem({data, index, style}: Props) {
[node, selectFiber],
);

const handleMouseEnter = (id: number) => {
setIsHovered(true);

if (id !== null) {
onElementMouseEnter(id);
}
};

const handleMouseLeave = (id: number) => {
setIsHovered(false);
};

// List items are absolutely positioned using the CSS "top" attribute.
// The "left" value will always be 0.
// Since height is fixed, and width is based on the node's duration,
Expand All @@ -49,9 +69,12 @@ function CommitRankedListItem({data, index, style}: Props) {
color={getGradientColor(node.value / chartData.maxValue)}
height={lineHeight}
isDimmed={index < selectedFiberIndex}
isHovered={isHovered}
key={node.id}
label={node.label}
onClick={handleClick}
onMouseEnter={() => handleMouseEnter(node.id)}
onMouseLeave={() => handleMouseLeave(node.id)}
width={Math.max(minBarWidth, scaleX(node.value, width))}
x={0}
y={top}
Expand Down