Skip to content

Commit

Permalink
Fix issue where scene context menu "Set Starting Scene Direction" inp…
Browse files Browse the repository at this point in the history
…ut wasn't working as expected
  • Loading branch information
chrismaltby committed Oct 15, 2024
1 parent 05026d3 commit d911e5c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
17 changes: 11 additions & 6 deletions src/components/ui/lists/EntityListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ type EntityListItemProps<T extends EntityListItemData> = {
collapsable?: boolean;
onToggleCollapse?: () => void;
onContextMenu?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
renderContextMenu?: (item: T) => JSX.Element[] | undefined;
renderContextMenu?: (
item: T,
closeMenu: () => void
) => JSX.Element[] | undefined;
renderLabel?: (item: T) => React.ReactNode;
} & (
| {
Expand Down Expand Up @@ -111,22 +114,24 @@ export const EntityListItem = <T extends EntityListItemData>({
y: number;
menu: JSX.Element[];
}>();

const onContextMenuClose = useCallback(() => {
setContextMenu(undefined);
}, []);

const onContextMenu = useCallback(
(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
if (!renderContextMenu) {
return;
}
const menu = renderContextMenu(item);
const menu = renderContextMenu(item, onContextMenuClose);
if (!menu) {
return;
}
setContextMenu({ x: e.pageX, y: e.pageY, menu });
},
[item, renderContextMenu]
[item, renderContextMenu, onContextMenuClose]
);
const onContextMenuClose = useCallback(() => {
setContextMenu(undefined);
}, []);

const onRenameComplete = useCallback(
(newValue: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
export const Menu = forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>((props, outerRef) => <StyledMenu ref={outerRef} {...props} />);
>((props, outerRef) => <StyledMenu ref={outerRef} role="menu" {...props} />);

export interface MenuItemProps extends React.HTMLAttributes<HTMLDivElement> {
readonly "data-index"?: number;
Expand Down
3 changes: 2 additions & 1 deletion src/components/world/NavigatorScenes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export const NavigatorScenes: FC<NavigatorScenesProps> = ({
}, []);

const renderContextMenu = useCallback(
(item: SceneNavigatorItem) => {
(item: SceneNavigatorItem, onClose: () => void) => {
if (item.type === "scene") {
return renderSceneContextMenu({
dispatch,
Expand All @@ -240,6 +240,7 @@ export const NavigatorScenes: FC<NavigatorScenesProps> = ({
hoverX: 0,
hoverY: 0,
onRename: () => setRenameId(item.id),
onClose,
});
} else if (item.type === "actor") {
return renderActorContextMenu({
Expand Down
10 changes: 6 additions & 4 deletions src/components/world/SceneView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ const SceneView = memo(
menu: JSX.Element[];
}>();

const onContextMenuClose = useCallback(() => {
setContextMenu(undefined);
}, []);

const renderContextMenu = useCallback(() => {
return renderSceneContextMenu({
dispatch,
Expand All @@ -582,6 +586,7 @@ const SceneView = memo(
startDirection,
hoverX,
hoverY,
onClose: onContextMenuClose,
});
}, [
dispatch,
Expand All @@ -591,6 +596,7 @@ const SceneView = memo(
sceneSelectionIds,
startDirection,
startSceneId,
onContextMenuClose,
]);

const onContextMenu = useCallback(
Expand All @@ -610,10 +616,6 @@ const SceneView = memo(
[renderContextMenu, tool]
);

const onContextMenuClose = useCallback(() => {
setContextMenu(undefined);
}, []);

const onToggleSelection = useCallback(() => {
dispatch(editorActions.toggleSceneSelectedId(id));
}, [dispatch, id]);
Expand Down
10 changes: 7 additions & 3 deletions src/components/world/renderSceneContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface SceneContextMenuProps {
hoverX: number;
hoverY: number;
onRename?: () => void;
onClose?: () => void;
}

const renderSceneContextMenu = ({
Expand All @@ -28,6 +29,7 @@ const renderSceneContextMenu = ({
startDirection,
hoverX,
hoverY,
onClose,
}: SceneContextMenuProps) => {
return [
<MenuSection key="label" style={{ paddingRight: 10, marginBottom: 5 }}>
Expand Down Expand Up @@ -60,7 +62,7 @@ const renderSceneContextMenu = ({
<div key={color} style={{ marginRight: color === "gray" ? 0 : 5 }}>
<LabelButton
color={color}
onClick={() =>
onClick={() => {
dispatch(
additionalSceneIds.length > 1
? entitiesActions.editScenes(
Expand All @@ -77,8 +79,9 @@ const renderSceneContextMenu = ({
labelColor: color,
},
})
)
}
);
onClose?.();
}}
/>
</div>
)
Expand Down Expand Up @@ -114,6 +117,7 @@ const renderSceneContextMenu = ({
startY: hoverY,
})
);
onClose?.();
}}
/>
</div>
Expand Down

0 comments on commit d911e5c

Please sign in to comment.