Debian Codegraph @

ivette/src/dome/renderer/frame/sidebars.tsx

raw
1 /* ************************************************************************ */
2 /* */
3 /* This file is part of Frama-C. */
4 /* */
5 /* Copyright (C) 2007-2022 */
6 /* CEA (Commissariat à l'énergie atomique et aux énergies */
7 /* alternatives) */
8 /* */
9 /* you can redistribute it and/or modify it under the terms of the GNU */
10 /* Lesser General Public License as published by the Free Software */
11 /* Foundation, version 2.1. */
12 /* */
13 /* It is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU Lesser General Public License for more details. */
17 /* */
18 /* See the GNU Lesser General Public License version 2.1 */
19 /* for more details (enclosed in the file licenses/LGPLv2.1). */
20 /* */
21 /* ************************************************************************ */
22
23 // --------------------------------------------------------------------------
24 // --- SideBars
25 // --------------------------------------------------------------------------
26
27 /**
28 @packageDocumentation
29 @module dome/frame/sidebars
30 */
31
32 import React from 'react';
33 import { useFlipSettings } from 'dome';
34 import { Badge } from 'dome/controls/icons';
35 import { Label } from 'dome/controls/labels';
36 import { classes } from 'dome/misc/utils';
37 import { Hbox, Hfill } from 'dome/layout/boxes';
38 import { IconButton, IconButtonProps } from 'dome/controls/buttons';
39
40 import './style.css';
41
42 // --------------------------------------------------------------------------
43 // --- SideBar Container
44 // --------------------------------------------------------------------------
45
46 export interface SideBarProps {
47 className?: string;
48 style?: React.CSSProperties;
49 children?: React.ReactNode;
50 }
51
52 /**
53 Container for sidebar items.
54 */
55 export function SideBar(props: SideBarProps): JSX.Element {
56 const className = classes(
57 'dome-xSideBar',
58 'dome-color-frame',
59 props.className,
60 );
61 return (
62 <div className={className} style={props.style}>
63 {props.children}
64 </div>
65 );
66 }
67
68 // --------------------------------------------------------------------------
69 // --- Badges Specifications
70 // --------------------------------------------------------------------------
71
72 export type BadgeElt = undefined | null | string | number | React.ReactNode;
73 export type Badges = BadgeElt | BadgeElt[];
74
75 const makeBadgeElt = (elt: BadgeElt, index: number): React.ReactNode => {
76 if (elt === undefined || elt === null) return null;
77 switch (typeof (elt)) {
78 case 'number':
79 case 'string':
80 return <Badge value={elt} key={`item#${index}`} />;
81 default:
82 return elt;
83 }
84 };
85
86 const makeBadge = (elt: Badges): React.ReactNode => {
87 if (Array.isArray(elt))
88 return elt.map(makeBadgeElt);
89 return makeBadgeElt(elt, 0);
90 };
91
92 // --------------------------------------------------------------------------
93 // --- SideBar Section
94 // --------------------------------------------------------------------------
95
96 export interface SectionProps {
97 /** Section label. */
98 label: string;
99 /** Section tooltip description. */
100 title?: string;
101 /** Hide/Show window settings. */
102 settings?: string;
103 /** Controlled Fold/Unfold state. */
104 unfold?: boolean;
105 /** Initial unfold state (default is `true`). */
106 defaultUnfold?: boolean;
107 /** Enabled sections are made visible. */
108 enabled?: boolean;
109 /** Disabled sections are made unvisible. */
110 disabled?: boolean;
111 /** Badge summary (only visible when folded). */
112 summary?: Badges;
113 /** Additional controls, (only visible when unfolded). */
114 rightButtonProps?: IconButtonProps;
115 /** Section contents. */
116 children?: React.ReactNode;
117 /** Additionnal CSS class. */
118 className?: string;
119 }
120
121 /**
122 Sidebar Section.
123
124 Unless specified, sections can be hidden on click.
125 When items in the section have badge(s)
126 it is highly recommended to provide a badge summary to be displayed
127 when the content is hidden.
128
129 Sections with no items are not displayed.
130 */
131 export function Section(props: SectionProps): JSX.Element | null {
132 const { settings, defaultUnfold, unfold } = props;
133 const [state, flipState] = useFlipSettings(settings, defaultUnfold);
134 const icon = state ? 'TRIANGLE.DOWN' : 'TRIANGLE.RIGHT';
135
136 const { enabled = true, disabled = false, children } = props;
137 if (disabled || !enabled || React.Children.count(children) === 0) return null;
138
139 const visible = unfold ?? state;
140 const maxHeight = visible ? 'max-content' : 0;
141 const { rightButtonProps: iconProps } = props;
142 const className = `dome-xSideBarSection-filterButton ${iconProps?.className}`;
143 const rightButton =
144 iconProps ? <IconButton {...iconProps} className={className}/> : undefined;
145
146 return (
147 <div className={`dome-xSideBarSection ${props.className}`}>
148 <Hbox>
149 <Label
150 className='dome-xSideBarSection-title dome-color-frame'
151 title={props.title}
152 label={props.label}
153 icon={icon}
154 onClick={flipState}
155 />
156 <Hfill />
157 {visible ? rightButton : makeBadge(props.summary)}
158 </Hbox>
159 <div className='dome-xSideBarSection-content' style={{ maxHeight }}>
160 {children}
161 </div>
162 </div>
163 );
164 }
165
166 // --------------------------------------------------------------------------
167 // --- SideBar Items
168 // --------------------------------------------------------------------------
169
170 export interface ItemProps {
171 /** Item icon. */
172 icon?: string;
173 /** Item label. */
174 label?: string;
175 /** Item tooltip text. */
176 title?: string;
177 /** Badge. */
178 badge?: Badges;
179 /** Enabled item. */
180 enabled?: boolean;
181 /** Disabled item (dimmed). */
182 disabled?: boolean;
183 /** Selection state. */
184 selected?: boolean;
185 /** Selection callback. */
186 onSelection?: () => void;
187 /** Right-click callback. */
188 onContextMenu?: () => void;
189 /** Additional class. */
190 className?: string;
191 /** Additional styles. */
192 style?: React.CSSProperties;
193 /** Other item elements. */
194 children?: React.ReactNode;
195 }
196
197 /** Sidebar Items. */
198 export function Item(props: ItemProps): JSX.Element {
199 const { selected = false, disabled = false, enabled = true } = props;
200 const isDisabled = disabled || !enabled;
201 const ref = React.useRef<HTMLDivElement>(null);
202 const [clicked, setClicked] = React.useState(false);
203 const onSelection = isDisabled ? undefined : props.onSelection;
204 const onClick =
205 onSelection ? () => { setClicked(true); onSelection(); } : undefined;
206 const onContextMenu = isDisabled ? undefined : props.onContextMenu;
207 const className = classes(
208 'dome-xSideBarItem',
209 selected ? 'dome-active' : 'dome-inactive',
210 isDisabled && 'dome-disabled',
211 props.className,
212 );
213
214 React.useLayoutEffect(() => {
215 if (!clicked && selected) {
216 ref?.current?.scrollIntoView({
217 behavior: 'auto',
218 block: 'center',
219 });
220 }
221 if (!selected && clicked)
222 setClicked(false);
223 }, [clicked, selected]);
224
225 return (
226 <div
227 ref={ref}
228 className={className}
229 style={props.style}
230 title={props.title}
231 onContextMenu={onContextMenu}
232 onClick={onClick}
233 >
234 <Label icon={props.icon} label={props.label} />
235 {props.children}
236 {makeBadge(props.badge)}
237 </div>
238 );
239 }
240
241 // --------------------------------------------------------------------------