88 */
99
1010import * as React from 'react' ;
11- import { Fragment , useContext } from 'react' ;
11+ import { Fragment , useContext , useEffect , useRef } from 'react' ;
1212import { ModalDialog } from '../ModalDialog' ;
1313import { ProfilerContext } from './ProfilerContext' ;
1414import TabBar from '../TabBar' ;
@@ -38,6 +38,8 @@ import {TimelineContext} from 'react-devtools-timeline/src/TimelineContext';
3838import styles from './Profiler.css' ;
3939
4040function Profiler ( _ : { } ) {
41+ const profilerRef = useRef < HTMLDivElement | null > ( null ) ;
42+
4143 const {
4244 didRecordCommits,
4345 isProcessingData,
@@ -47,6 +49,8 @@ function Profiler(_: {}) {
4749 selectedTabID,
4850 selectTab,
4951 supportsProfiling,
52+ startProfiling,
53+ stopProfiling,
5054 } = useContext ( ProfilerContext ) ;
5155
5256 const { file : timelineTraceEventData , searchInputContainerRef} =
@@ -56,6 +60,34 @@ function Profiler(_: {}) {
5660
5761 const isLegacyProfilerSelected = selectedTabID !== 'timeline' ;
5862
63+ // Cmd+E to start/stop profiler recording
64+ useEffect ( ( ) => {
65+ const div = profilerRef . current ;
66+ if ( div === null ) {
67+ return ;
68+ }
69+
70+ const ownerWindow = div . ownerDocument . defaultView ;
71+ const isMac = navigator . platform . toUpperCase ( ) . indexOf ( 'MAC' ) >= 0 ;
72+ const handleKeyDown = ( event : KeyboardEvent ) => {
73+ const correctModifier = isMac ? event . metaKey : event . ctrlKey ;
74+ if ( correctModifier && event . key === 'e' ) {
75+ if ( isProfiling ) {
76+ stopProfiling ( ) ;
77+ } else {
78+ startProfiling ( ) ;
79+ }
80+ event . preventDefault ( ) ;
81+ event . stopPropagation ( ) ;
82+ }
83+ } ;
84+
85+ ownerWindow . addEventListener ( 'keydown' , handleKeyDown ) ;
86+ return ( ) => {
87+ ownerWindow . removeEventListener ( 'keydown' , handleKeyDown ) ;
88+ } ;
89+ } , [ isProfiling , startProfiling , stopProfiling ] ) ;
90+
5991 let view = null ;
6092 if ( didRecordCommits || selectedTabID === 'timeline' ) {
6193 switch ( selectedTabID ) {
@@ -112,7 +144,7 @@ function Profiler(_: {}) {
112144
113145 return (
114146 < SettingsModalContextController >
115- < div className = { styles . Profiler } >
147+ < div ref = { profilerRef } className = { styles . Profiler } >
116148 < div className = { styles . LeftColumn } >
117149 < div className = { styles . Toolbar } >
118150 < RecordToggle disabled = { ! supportsProfiling } />
0 commit comments