osr context menu
This commit is contained in:
parent
f29e1849f3
commit
b8098c8ff1
2 changed files with 102 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ import { AuthProvider } from "../context/Auth";
|
|||
import { exitEditMode } from "../redux/thunks";
|
||||
import { loadLastSession, saveCurrentSession } from "../redux/thunks/session";
|
||||
import { incrementFreeTrialCount } from "../util/freeTrial";
|
||||
import OSRContextMenu from "./OSRContextMenu";
|
||||
|
||||
const LayoutTopDiv = styled(CustomScrollbarDiv)`
|
||||
height: 100%;
|
||||
|
|
@ -261,6 +262,7 @@ const Layout = () => {
|
|||
return (
|
||||
<AuthProvider>
|
||||
<LayoutTopDiv>
|
||||
<OSRContextMenu />
|
||||
<div
|
||||
style={{
|
||||
scrollbarGutter: "stable both-edges",
|
||||
|
|
|
|||
100
gui/src/components/OSRContextMenu.tsx
Normal file
100
gui/src/components/OSRContextMenu.tsx
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import React, { useContext, useEffect, useState } from "react";
|
||||
import useIsOSREnabled from "../hooks/useIsOSREnabled";
|
||||
import { IdeMessengerContext } from "../context/IdeMessenger";
|
||||
|
||||
const OSRContextMenu = () => {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
||||
const [position, setPosition] = useState<{
|
||||
top?: number;
|
||||
left?: number;
|
||||
bottom?: number;
|
||||
right?: number;
|
||||
} | null>(null);
|
||||
const menuRef = React.useRef<HTMLDivElement>(null);
|
||||
|
||||
const isOSREnabled = useIsOSREnabled();
|
||||
useEffect(() => {
|
||||
function leaveWindowHandler(event: MouseEvent) {
|
||||
setPosition(null);
|
||||
}
|
||||
function contextMenuHandler(event: MouseEvent) {
|
||||
event.preventDefault();
|
||||
}
|
||||
function rightClickHandler(event: MouseEvent) {
|
||||
if (!menuRef.current?.contains(event.target as Node)) {
|
||||
setPosition(null);
|
||||
}
|
||||
if (event.button === 2) {
|
||||
event.preventDefault();
|
||||
|
||||
// always open towards inside of window from click
|
||||
const toRight = event.clientX > window.innerWidth / 2;
|
||||
const toBottom = event.clientY > window.innerHeight / 2;
|
||||
if (toRight) {
|
||||
if (toBottom) {
|
||||
setPosition({
|
||||
bottom: window.innerHeight - event.clientY,
|
||||
right: window.innerWidth - event.clientX,
|
||||
});
|
||||
} else {
|
||||
setPosition({
|
||||
top: event.clientY,
|
||||
right: window.innerWidth - event.clientX,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (toBottom) {
|
||||
setPosition({
|
||||
bottom: window.innerHeight - event.clientY,
|
||||
left: event.clientX,
|
||||
});
|
||||
} else {
|
||||
setPosition({
|
||||
top: event.clientY,
|
||||
left: event.clientX,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isOSREnabled) {
|
||||
document.addEventListener("mouseleave", leaveWindowHandler);
|
||||
document.addEventListener("contextmenu", contextMenuHandler);
|
||||
document.addEventListener("mousedown", rightClickHandler);
|
||||
}
|
||||
|
||||
return () => {
|
||||
document.removeEventListener("mouseleave", leaveWindowHandler);
|
||||
document.removeEventListener("contextmenu", contextMenuHandler);
|
||||
document.removeEventListener("mousedown", rightClickHandler);
|
||||
};
|
||||
}, [isOSREnabled, menuRef]);
|
||||
|
||||
if (!position) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<div
|
||||
className="bg-vsc-editor-background absolute overflow-hidden rounded-md border border-solid border-gray-500"
|
||||
style={{
|
||||
...position,
|
||||
zIndex: 1000,
|
||||
}}
|
||||
ref={menuRef}
|
||||
>
|
||||
<div
|
||||
className="cursor-pointer px-2 py-1 hover:opacity-90"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
ideMessenger.post("toggleDevTools", undefined);
|
||||
}}
|
||||
>
|
||||
Toggle Dev Tools
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default OSRContextMenu;
|
||||
Loading…
Add table
Reference in a new issue