Skip to content

Commit 191ff2e

Browse files
authored
Show the marker keys instead of the labels in marker tooltips when alt is pressed. (#5625)
1 parent c176744 commit 191ff2e

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/components/tooltip/Marker.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
formatTimestamp,
1111
} from 'firefox-profiler/utils/format-numbers';
1212
import explicitConnect from 'firefox-profiler/utils/connect';
13+
import { useAltKey } from 'firefox-profiler/hooks/useAltKey';
1314
import {
1415
getCategories,
1516
getMarkerSchemaByName,
@@ -88,6 +89,7 @@ type OwnProps = {
8889
readonly restrictHeightWidth: boolean;
8990
// Optional callback for when a stack frame is clicked in the backtrace.
9091
readonly onStackFrameClick?: (stackIndex: IndexIntoStackTable) => void;
92+
readonly showKeys?: boolean;
9193
};
9294

9395
type StateProps = {
@@ -275,8 +277,10 @@ class MarkerTooltipContents extends React.PureComponent<Props> {
275277
continue;
276278
}
277279

280+
// When Alt is pressed (showKeys is true), display the field key instead of label
281+
const displayLabel = this.props.showKeys ? key : label || key;
278282
details.push(
279-
<TooltipDetail key={schema.name + '-' + key} label={label || key}>
283+
<TooltipDetail key={schema.name + '-' + key} label={displayLabel}>
280284
{formatMarkupFromMarkerSchema(
281285
schema.name,
282286
format,
@@ -554,7 +558,7 @@ class MarkerTooltipContents extends React.PureComponent<Props> {
554558
}
555559
}
556560

557-
export const TooltipMarker = explicitConnect<
561+
const ConnectedMarkerTooltipContents = explicitConnect<
558562
OwnProps,
559563
StateProps,
560564
DispatchProps
@@ -579,3 +583,9 @@ export const TooltipMarker = explicitConnect<
579583
mapDispatchToProps: { changeMarkersSearchString },
580584
component: MarkerTooltipContents,
581585
});
586+
587+
// Wrapper component that provides the Alt key state
588+
export function TooltipMarker(props: OwnProps) {
589+
const showKeys = useAltKey();
590+
return <ConnectedMarkerTooltipContents {...props} showKeys={showKeys} />;
591+
}

src/hooks/useAltKey.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import * as React from 'react';
6+
7+
// Global state for Alt key tracking
8+
let isAltPressed = false;
9+
const listeners = new Set<(pressed: boolean) => void>();
10+
11+
// Initialize global event listeners once
12+
if (typeof window !== 'undefined') {
13+
const handleKeyDown = (event: KeyboardEvent) => {
14+
if (event.altKey && !isAltPressed) {
15+
isAltPressed = true;
16+
listeners.forEach((listener) => listener(true));
17+
}
18+
};
19+
20+
const handleKeyUp = (event: KeyboardEvent) => {
21+
if (!event.altKey && isAltPressed) {
22+
isAltPressed = false;
23+
listeners.forEach((listener) => listener(false));
24+
}
25+
};
26+
27+
const handleBlur = () => {
28+
// Reset Alt state when window loses focus
29+
if (isAltPressed) {
30+
isAltPressed = false;
31+
listeners.forEach((listener) => listener(false));
32+
}
33+
};
34+
35+
window.addEventListener('keydown', handleKeyDown);
36+
window.addEventListener('keyup', handleKeyUp);
37+
window.addEventListener('blur', handleBlur);
38+
}
39+
40+
/**
41+
* Custom hook that tracks whether the Alt key is currently pressed.
42+
* Returns true when Alt is pressed, false otherwise.
43+
* The state is global and shared across all component instances.
44+
*/
45+
export function useAltKey(): boolean {
46+
const [altPressed, setAltPressed] = React.useState(isAltPressed);
47+
48+
React.useEffect(() => {
49+
// Set initial state
50+
setAltPressed(isAltPressed);
51+
52+
// Subscribe to changes
53+
const listener = (pressed: boolean) => {
54+
setAltPressed(pressed);
55+
};
56+
listeners.add(listener);
57+
58+
return () => {
59+
listeners.delete(listener);
60+
};
61+
}, []);
62+
63+
return altPressed;
64+
}

0 commit comments

Comments
 (0)