-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3201] Add support for tools on multiple diagram elements (hide, fade…
… and pin) Bug: #3201 Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
- Loading branch information
Showing
6 changed files
with
247 additions
and
7 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
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
117 changes: 117 additions & 0 deletions
117
...rams/frontend/sirius-components-diagrams/src/renderer/palette/group-tool/GroupPalette.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,117 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
import { Paper } from '@material-ui/core'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import Tooltip from '@material-ui/core/Tooltip'; | ||
import TonalityIcon from '@material-ui/icons/Tonality'; | ||
import VisibilityOffIcon from '@material-ui/icons/VisibilityOff'; | ||
import { memo, useCallback, useState } from 'react'; | ||
import { useOnSelectionChange } from 'reactflow'; | ||
import { GroupPaletteProps } from './GroupPalette.types'; | ||
import { ContextualPaletteStyleProps } from '../Palette.types'; | ||
import { PalettePortal } from '../PalettePortal'; | ||
import { useHideDiagramElements } from '../../hide/useHideDiagramElements'; | ||
import { useDiagramElementPalette } from '../useDiagramElementPalette'; | ||
import { useFadeDiagramElements } from '../../fade/useFadeDiagramElements'; | ||
import { PinIcon } from '../../../icons/PinIcon'; | ||
import { usePinDiagramElements } from '../../pin/usePinDiagramElements'; | ||
|
||
const usePaletteStyle = makeStyles((theme) => ({ | ||
palette: { | ||
border: `1px solid ${theme.palette.divider}`, | ||
borderRadius: '2px', | ||
zIndex: 2, | ||
position: 'fixed', | ||
display: 'flex', | ||
alignItems: 'center', | ||
}, | ||
paletteContent: { | ||
display: 'grid', | ||
gridTemplateColumns: ({ toolCount }: ContextualPaletteStyleProps) => `repeat(${Math.min(toolCount, 10)}, 36px)`, | ||
gridTemplateRows: '28px', | ||
gridAutoRows: '28px', | ||
placeItems: 'center', | ||
}, | ||
toolIcon: { | ||
color: theme.palette.text.primary, | ||
}, | ||
})); | ||
|
||
export const GroupPalette = memo(({ x, y, isOpened }: GroupPaletteProps) => { | ||
const { hideDiagramElements } = useHideDiagramElements(); | ||
const { fadeDiagramElements } = useFadeDiagramElements(); | ||
const { pinDiagramElements } = usePinDiagramElements(); | ||
const { hideDiagramElementPalette } = useDiagramElementPalette(); | ||
const [selectedElementIds, setSelectedElementIds] = useState<string[]>([]); | ||
|
||
const onChange = useCallback(({ nodes }) => { | ||
if (nodes.filter((node) => node.selected).length > 1) { | ||
setSelectedElementIds(nodes.filter((node) => node.selected).map((node) => node.id)); | ||
} else { | ||
setSelectedElementIds([]); | ||
} | ||
}, []); | ||
useOnSelectionChange({ | ||
onChange, | ||
}); | ||
|
||
const toolCount = 3; | ||
const classes = usePaletteStyle({ toolCount }); | ||
|
||
const shouldRender = selectedElementIds.length > 1 && isOpened && x && y; | ||
if (!shouldRender) { | ||
return null; | ||
} | ||
hideDiagramElementPalette(); | ||
|
||
return ( | ||
<PalettePortal> | ||
<Paper className={classes.palette} style={{ position: 'absolute', left: x, top: y }} data-testid="GroupPalette"> | ||
<div className={classes.paletteContent}> | ||
<Tooltip title="Hide elements"> | ||
<IconButton | ||
className={classes.toolIcon} | ||
size="small" | ||
aria-label="hide elements" | ||
onClick={() => hideDiagramElements(selectedElementIds, true)} | ||
data-testid="Hide-elements"> | ||
<VisibilityOffIcon fontSize="small" /> | ||
</IconButton> | ||
</Tooltip> | ||
<Tooltip title="Fade elements"> | ||
<IconButton | ||
className={classes.toolIcon} | ||
size="small" | ||
aria-label="Fade elements" | ||
onClick={() => fadeDiagramElements(selectedElementIds, true)} | ||
data-testid="Fade-elements"> | ||
<TonalityIcon fontSize="small" /> | ||
</IconButton> | ||
</Tooltip> | ||
<Tooltip title="Pin element"> | ||
<IconButton | ||
className={classes.toolIcon} | ||
size="small" | ||
aria-label="Pin element" | ||
onClick={() => pinDiagramElements(selectedElementIds, true)} | ||
data-testid="Pin-element"> | ||
<PinIcon fontSize="small" /> | ||
</IconButton> | ||
</Tooltip> | ||
</div> | ||
</Paper> | ||
</PalettePortal> | ||
); | ||
}); |
19 changes: 19 additions & 0 deletions
19
...frontend/sirius-components-diagrams/src/renderer/palette/group-tool/GroupPalette.types.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,19 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
export interface GroupPaletteProps { | ||
x?: number; | ||
y?: number; | ||
isOpened: boolean; | ||
refreshEventPayloadId: string; | ||
} |
45 changes: 45 additions & 0 deletions
45
...s/frontend/sirius-components-diagrams/src/renderer/palette/group-tool/useGroupPalette.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,45 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
import { useCallback, useState } from 'react'; | ||
import { XYPosition, useStoreApi } from 'reactflow'; | ||
import { UseGroupPaletteValue, UseGroupPaletteState } from './useGroupPalette.types'; | ||
|
||
const computePalettePosition = (event: MouseEvent | React.MouseEvent, bounds?: DOMRect): XYPosition => { | ||
return { | ||
x: event.clientX - (bounds?.left ?? 0), | ||
y: event.clientY - (bounds?.top ?? 0), | ||
}; | ||
}; | ||
|
||
export const useGroupPalette = (): UseGroupPaletteValue => { | ||
const [state, setState] = useState<UseGroupPaletteState>({ position: null, isOpened: false }); | ||
|
||
const store = useStoreApi(); | ||
|
||
const onDiagramGroupElementClick = useCallback((event: React.MouseEvent<Element, MouseEvent>) => { | ||
const { domNode } = store.getState(); | ||
const element = domNode?.getBoundingClientRect(); | ||
const palettePosition = computePalettePosition(event, element); | ||
setState((prevState) => ({ ...prevState, position: palettePosition, isOpened: true })); | ||
}, []); | ||
|
||
const hideGroupPalette = () => { | ||
setState((prevState) => ({ ...prevState, position: null, isOpened: false })); | ||
}; | ||
return { | ||
position: state.position, | ||
isOpened: state.isOpened, | ||
hideGroupPalette, | ||
onDiagramGroupElementClick, | ||
}; | ||
}; |
26 changes: 26 additions & 0 deletions
26
...ntend/sirius-components-diagrams/src/renderer/palette/group-tool/useGroupPalette.types.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,26 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Obeo. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
import { XYPosition } from 'reactflow'; | ||
|
||
export interface UseGroupPaletteValue { | ||
position: XYPosition | null; | ||
isOpened: boolean; | ||
hideGroupPalette: () => void; | ||
onDiagramGroupElementClick: (event: React.MouseEvent<Element, MouseEvent>) => void; | ||
} | ||
|
||
export interface UseGroupPaletteState { | ||
position: XYPosition | null; | ||
isOpened: boolean; | ||
} |