-
-
Notifications
You must be signed in to change notification settings - Fork 18
Move TopBar context menu to React (BL-15901) #7760
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
211 changes: 211 additions & 0 deletions
211
src/BloomBrowserUI/react_components/TopBar/TopBarContextMenu.tsx
This file contains hidden or 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,211 @@ | ||
| import * as React from "react"; | ||
| import { css } from "@emotion/react"; | ||
| import { getBoolean, post, postBoolean, postJson } from "../../utils/bloomApi"; | ||
| import Menu from "@mui/material/Menu"; | ||
| import Divider from "@mui/material/Divider"; | ||
| import { | ||
| LocalizableMenuItem, | ||
| LocalizableSelectableMenuItem, | ||
| } from "../localizableMenuItem"; | ||
| import { useMountEffect } from "../../utils/useMountEffect"; | ||
|
|
||
| interface ITopBarContextMenuPoint { | ||
| clientX: number; | ||
| clientY: number; | ||
| } | ||
|
|
||
| interface ITopBarContextMenuItem { | ||
| label: string; | ||
| enabled?: boolean; | ||
| selected?: boolean; | ||
| onClick?: () => void; | ||
| } | ||
|
|
||
| export const TopBarContextMenu: React.FunctionComponent<{ | ||
| targetRef: React.RefObject<HTMLDivElement>; | ||
| }> = (props) => { | ||
| const [menuPoint, setMenuPoint] = React.useState<ITopBarContextMenuPoint>(); | ||
| const [currentlyMeasuring, setCurrentlyMeasuring] = React.useState(false); | ||
| const [alwaysMeasurePerformance, setAlwaysMeasurePerformance] = | ||
| React.useState(false); | ||
| const [isMeddlingWithNewFiles, setIsMeddlingWithNewFiles] = | ||
| React.useState(false); | ||
|
|
||
| const onClose = React.useCallback(() => { | ||
| setMenuPoint(undefined); | ||
| }, []); | ||
|
|
||
| useMountEffect(() => { | ||
|
andrew-polk marked this conversation as resolved.
|
||
| // CurrentlyMeasuring is only turned on from this menu, so local state can own it | ||
| // once we establish the initial persisted setting. (No need to ask the back end.) | ||
| getBoolean("app/alwaysMeasurePerformance", (value) => { | ||
| setAlwaysMeasurePerformance(value); | ||
| if (value) { | ||
| setCurrentlyMeasuring(true); | ||
| } | ||
| }); | ||
| // Even though this is only controlled from this menu, meddling could be turned on | ||
| // and then Bloom could restart or reload a collection. So we do need to ask | ||
| // the back end for the current value. | ||
| getBoolean("app/isMeddlingWithNewFiles", (value) => { | ||
| setIsMeddlingWithNewFiles(value); | ||
| }); | ||
|
|
||
| const target = props.targetRef.current; | ||
| if (!target) { | ||
| return; | ||
| } | ||
|
|
||
| const handleTopBarContextMenu = (event: MouseEvent) => { | ||
| // Don't block the Ctrl+RightClick context menu | ||
| if (event.ctrlKey) { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
|
|
||
| setMenuPoint({ | ||
| clientX: event.clientX, | ||
| clientY: event.clientY, | ||
| }); | ||
| }; | ||
|
|
||
| target.addEventListener("contextmenu", handleTopBarContextMenu); | ||
|
|
||
| return () => { | ||
| target.removeEventListener("contextmenu", handleTopBarContextMenu); | ||
| }; | ||
| }); | ||
|
|
||
| function handleResizeWindow(width: number, height: number) { | ||
| postJson("app/resizeWindow", { | ||
| width, | ||
| height, | ||
| }); | ||
| } | ||
|
|
||
| const menuItems = React.useMemo<ITopBarContextMenuItem[]>(() => { | ||
| return [ | ||
| { | ||
| label: "1024 x 586 Low-end netbook with windows Task bar", | ||
| onClick: () => { | ||
| handleResizeWindow(1024, 586); | ||
| }, | ||
| }, | ||
| { label: "-" }, | ||
| { | ||
| label: "800 x 600", | ||
| onClick: () => { | ||
| handleResizeWindow(800, 600); | ||
| }, | ||
| }, | ||
| { | ||
| label: "1024 x 600", | ||
| onClick: () => { | ||
| handleResizeWindow(1024, 600); | ||
| }, | ||
| }, | ||
| { | ||
| label: "1024 x 768", | ||
| onClick: () => { | ||
| handleResizeWindow(1024, 768); | ||
| }, | ||
| }, | ||
| { label: "-" }, | ||
| { | ||
| label: "Always Measure Performance", | ||
| selected: alwaysMeasurePerformance, | ||
| onClick: () => { | ||
| const newValue = !alwaysMeasurePerformance; | ||
| postBoolean("app/alwaysMeasurePerformance", newValue); | ||
| setAlwaysMeasurePerformance(newValue); | ||
| if (newValue) { | ||
| setCurrentlyMeasuring(true); | ||
| } | ||
| }, | ||
| }, | ||
| { | ||
| label: currentlyMeasuring | ||
| ? "Currently Measuring Performance" | ||
| : "Start Measuring Performance", | ||
| enabled: !currentlyMeasuring, | ||
| onClick: () => { | ||
| post("app/startMeasuringPerformance"); | ||
| setCurrentlyMeasuring(true); | ||
| }, | ||
| }, | ||
| { | ||
| label: "Show Performance Page", | ||
| enabled: currentlyMeasuring, | ||
| onClick: () => { | ||
| post("app/showPerformancePage"); | ||
| }, | ||
| }, | ||
| { | ||
| label: isMeddlingWithNewFiles | ||
| ? "Stop Meddling with New Files" | ||
| : "Meddle with New Files", | ||
| onClick: () => { | ||
| const newValue = !isMeddlingWithNewFiles; | ||
| postBoolean("app/isMeddlingWithNewFiles", newValue); | ||
| setIsMeddlingWithNewFiles(newValue); | ||
| }, | ||
| }, | ||
| ]; | ||
| }, [alwaysMeasurePerformance, currentlyMeasuring, isMeddlingWithNewFiles]); | ||
|
|
||
| return ( | ||
| <Menu | ||
| keepMounted={true} | ||
| open={!!menuPoint} | ||
| onClose={onClose} | ||
| anchorReference="anchorPosition" | ||
| anchorPosition={ | ||
| menuPoint | ||
| ? { | ||
| top: menuPoint.clientY, | ||
| left: menuPoint.clientX, | ||
| } | ||
| : undefined | ||
| } | ||
| slotProps={{ | ||
| paper: { | ||
| css: css` | ||
| min-width: 220px; | ||
| max-width: 440px; | ||
| `, | ||
| }, | ||
| }} | ||
| > | ||
| {menuItems.map((item, index) => { | ||
| if (item.label === "-") { | ||
| return <Divider key={`separator-${index}`} />; | ||
| } | ||
|
|
||
| const commonProps = { | ||
| key: `${item.label ?? index}`, | ||
| english: item.label, | ||
| l10nId: null, | ||
| onClick: () => { | ||
| item.onClick?.(); | ||
| onClose(); | ||
| }, | ||
| disabled: item.enabled === false, | ||
| }; | ||
|
|
||
| return item.selected !== undefined ? ( | ||
| <LocalizableSelectableMenuItem | ||
| {...commonProps} | ||
| selected={item.selected} | ||
| /> | ||
| ) : ( | ||
| <LocalizableMenuItem | ||
| {...commonProps} | ||
| hasLeadingIconSpace={true} | ||
| /> | ||
| ); | ||
| })} | ||
| </Menu> | ||
| ); | ||
| }; | ||
This file contains hidden or 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,6 @@ | ||
| import { useEffect } from "react"; | ||
|
|
||
| export function useMountEffect(effect: () => void | (() => void)) { | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| useEffect(effect, []); | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.