-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add basic context menu with 'Reset View' button also increase BAR_HEIGHT to make it easier to click
- Loading branch information
Showing
14 changed files
with
213 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+570 Bytes
(100%)
cypress/snapshots/basic.ts/pyroscope.server.alloc_objects-flamegraph.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+493 Bytes
(100%)
cypress/snapshots/basic.ts/pyroscope.server.alloc_space-flamegraph.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+426 Bytes
(100%)
cypress/snapshots/basic.ts/pyroscope.server.cpu-flamegraph.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+635 Bytes
(100%)
cypress/snapshots/basic.ts/pyroscope.server.inuse_objects-flamegraph.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+603 Bytes
(100%)
cypress/snapshots/basic.ts/pyroscope.server.inuse_space-flamegraph.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+96 Bytes
(100%)
cypress/snapshots/e2e.ts/e2e-comparison-diff-flamegraph.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+84 Bytes
(100%)
cypress/snapshots/e2e.ts/e2e-comparison-flamegraph-left.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+116 Bytes
(100%)
cypress/snapshots/e2e.ts/e2e-comparison-flamegraph-right.snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions
72
webapp/javascript/components/FlameGraph/FlameGraphComponent/ContextMenu.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable react/jsx-props-no-spreading */ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { MenuItem } from '@szhsin/react-menu'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import ContextMenu, { ContextMenuProps } from './ContextMenu'; | ||
|
||
const { queryByRole, queryAllByRole } = screen; | ||
|
||
function TestCanvas(props: Omit<ContextMenuProps, 'canvasRef'>) { | ||
const canvasRef = React.useRef(); | ||
|
||
return ( | ||
<> | ||
<canvas data-testid="canvas" ref={canvasRef} /> | ||
<ContextMenu data-testid="contextmenu" canvasRef={canvasRef} {...props} /> | ||
</> | ||
); | ||
} | ||
|
||
describe('ContextMenu', () => { | ||
it('works', () => { | ||
let hasBeenClicked = false; | ||
|
||
const xyToMenuItems = () => { | ||
return [ | ||
<MenuItem | ||
key="test" | ||
onClick={() => { | ||
hasBeenClicked = true; | ||
}} | ||
> | ||
Test | ||
</MenuItem>, | ||
] as unknown[] as typeof MenuItem[]; // nasty conversion | ||
}; | ||
|
||
render(<TestCanvas xyToMenuItems={xyToMenuItems} />); | ||
|
||
expect(queryByRole('menu')).not.toBeInTheDocument(); | ||
|
||
// trigger a right click | ||
userEvent.click(screen.getByTestId('canvas'), { buttons: 2 }); | ||
|
||
expect(queryByRole('menu')).toBeVisible(); | ||
expect(queryAllByRole('menuitem')).toHaveLength(1); | ||
|
||
userEvent.click(queryByRole('menuitem')); | ||
expect(hasBeenClicked).toBe(true); | ||
}); | ||
|
||
it('shows different items depending on the clicked node', () => { | ||
const xyToMenuItems = jest.fn(); | ||
|
||
render(<TestCanvas xyToMenuItems={xyToMenuItems} />); | ||
|
||
expect(queryByRole('menu')).not.toBeInTheDocument(); | ||
|
||
// trigger a right click | ||
xyToMenuItems.mockReturnValueOnce([<MenuItem key="1">1</MenuItem>]); | ||
userEvent.click(screen.getByTestId('canvas'), { buttons: 2 }); | ||
expect(queryAllByRole('menuitem')).toHaveLength(1); | ||
|
||
xyToMenuItems.mockReturnValueOnce([ | ||
<MenuItem key="1">1</MenuItem>, | ||
<MenuItem key="2">2</MenuItem>, | ||
]); | ||
userEvent.click(screen.getByTestId('canvas'), { buttons: 2 }); | ||
expect(queryAllByRole('menuitem')).toHaveLength(2); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
webapp/javascript/components/FlameGraph/FlameGraphComponent/ContextMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import { | ||
ControlledMenu, | ||
useMenuState, | ||
MenuItem, | ||
SubMenu, | ||
} from '@szhsin/react-menu'; | ||
|
||
// even though the library support many different types | ||
// we only support these | ||
type SupportedItems = typeof MenuItem | typeof SubMenu; | ||
|
||
type xyToMenuItems = (x: number, y: number) => SupportedItems[]; | ||
|
||
export interface ContextMenuProps { | ||
canvasRef: React.RefObject<HTMLCanvasElement>; | ||
|
||
// The menu should be built dynamically | ||
// Based on the cell's contents | ||
xyToMenuItems: xyToMenuItems; | ||
} | ||
|
||
export default function ContextMenu(props: ContextMenuProps) { | ||
const { toggleMenu, openMenu, closeMenu, ...menuProps } = useMenuState(false); | ||
const [anchorPoint, setAnchorPoint] = React.useState({ x: 0, y: 0 }); | ||
const { canvasRef } = props; | ||
const [menuItems, setMenuItems] = React.useState<SupportedItems[]>([]); | ||
|
||
const onContextMenu = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
|
||
const items = props.xyToMenuItems(e.clientX, e.clientY); | ||
setMenuItems(items); | ||
|
||
// TODO | ||
// if the menu becomes too large, it may overflow to outside the screen | ||
const x = e.clientX; | ||
const y = e.clientY + 20; | ||
|
||
setAnchorPoint({ x, y }); | ||
openMenu(); | ||
}; | ||
|
||
React.useEffect(() => { | ||
closeMenu(); | ||
|
||
// use closure to "cache" the current canvas reference | ||
// so that when cleaning up, it points to a valid canvas | ||
// (otherwise it would be null) | ||
const canvasEl = canvasRef.current; | ||
if (!canvasEl) { | ||
return () => {}; | ||
} | ||
|
||
// watch for mouse events on the bar | ||
canvasEl.addEventListener('contextmenu', onContextMenu); | ||
|
||
return () => { | ||
canvasEl.removeEventListener('contextmenu', onContextMenu); | ||
}; | ||
}, []); | ||
return ( | ||
<ControlledMenu | ||
menuItemFocus={menuProps.menuItemFocus} | ||
isMounted={menuProps.isMounted} | ||
isOpen={menuProps.isOpen} | ||
anchorPoint={anchorPoint} | ||
onClose={closeMenu} | ||
> | ||
{menuItems} | ||
</ControlledMenu> | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
webapp/javascript/components/FlameGraph/FlameGraphComponent/constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const PX_PER_LEVEL = 22; | ||
export const COLLAPSE_THRESHOLD = 5; | ||
export const LABEL_THRESHOLD = 20; | ||
export const GAP = 0.5; | ||
export const BAR_HEIGHT = PX_PER_LEVEL - GAP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters