Debian Codegraph @

ivette/src/dome/renderer/layout/splitters.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 /* eslint-disable @typescript-eslint/explicit-function-return-type */
24
25 // --------------------------------------------------------------------------
26 // --- Splitters
27 // --------------------------------------------------------------------------
28
29 /**
30 @packageDocumentation
31 @module dome/layout/splitters
32 */
33
34 import * as React from 'react';
35 import * as Dome from 'dome';
36 import * as Utils from 'dome/misc/utils';
37 import { DraggableCore, DraggableEventHandler } from 'react-draggable';
38 import { AutoSizer, Size } from 'react-virtualized';
39
40 // --------------------------------------------------------------------------
41 // --- Splitter
42 // --------------------------------------------------------------------------
43
44 export interface SplitterBaseProps {
45 /** Window settings to store the splitter position. */
46 settings?: string;
47 /** Ratio or size depending of the splitter:
48 - for 'horizontal' or 'vertical' split, ratio between the two panels;
49 - for other splits, size of the foldable component. */
50 defaultPosition?: number;
51 /** Minimal margin from container edges (minimum `32`). */
52 margin?: number;
53 /** Splitter children components. */
54 children: [JSX.Element, JSX.Element];
55 }
56
57 export interface SplitterFoldProps extends SplitterBaseProps {
58 /**
59 Visibility of the foldable component.
60 Only applies to left, right, top and bottom layout.
61 */
62 unfold?: boolean;
63 }
64
65 export enum Direction {
66 /** Horizontal split. */
67 HORIZONTAL,
68 /** Vertical split. */
69 VERTICAL,
70 /** Stacked, foldable left component. */
71 LEFT,
72 /** Stacked, foldable right component. */
73 RIGHT,
74 /** Stacked, foldable top component. */
75 TOP,
76 /** Stacked, foldable bottom component. */
77 BOTTOM,
78 }
79
80 export interface SplitterDirProps extends SplitterFoldProps {
81 /** Direction of the splitter. */
82 direction: Direction;
83 }
84
85 /* --------------------------------------------------------------------------*/
86 /* --- Splitter Layout ---*/
87 /* --------------------------------------------------------------------------*/
88
89 type Layout = {
90 hsplit: boolean;
91 foldA: boolean;
92 foldB: boolean;
93 };
94
95 const PANEL = 'dome-container';
96 const DRAGGING = 'dome-color-dragging';
97 const DRAGZONE = 'dome-color-dragzone';
98
99 const CONTAINER = 'dome-xSplitter-container';
100 const NOCURSOR = 'dome-xSplitter-no-cursor';
101 const HCURSOR = 'dome-xSplitter-h-cursor';
102 const VCURSOR = 'dome-xSplitter-v-cursor';
103
104 const HIDDEN = 'dome-xSplitter-hidden';
105 const HFLEX = 'dome-xSplitter-hflex';
106 const VFLEX = 'dome-xSplitter-vflex';
107
108 const BLOCK = 'dome-xSplitter-block';
109 const HPANE = 'dome-xSplitter-hpane';
110 const VPANE = 'dome-xSplitter-vpane';
111 const HFOLD = 'dome-xSplitter-hfold';
112 const VFOLD = 'dome-xSplitter-vfold';
113 const HLINE = 'dome-xSplitter-hline';
114 const VLINE = 'dome-xSplitter-vline';
115 const HANDLE = '.dome-xSplitter-grab';
116 const HGRAB = 'dome-xSplitter-grab dome-xSplitter-hgrab';
117 const VGRAB = 'dome-xSplitter-grab dome-xSplitter-vgrab';
118 const HPOSA = 'dome-xSplitter-hpos-A';
119 const VPOSA = 'dome-xSplitter-vpos-A';
120 const HPOSB = 'dome-xSplitter-hpos-B';
121 const VPOSB = 'dome-xSplitter-vpos-B';
122 const HPOSR = 'dome-xSplitter-hline dome-xSplitter-hpos-R';
123 const VPOSR = 'dome-xSplitter-vline dome-xSplitter-vpos-R';
124
125 type CSS = {
126 container: string;
127 sideA: string;
128 sideB: string;
129 split: string;
130 };
131
132 const getFlexCSS = (hsplit: boolean, fold: boolean) => (
133 hsplit ? (fold ? HFOLD : HPANE) : (fold ? VFOLD : VPANE)
134 );
135
136 const getCSS = (
137 unfold: boolean,
138 dragged: boolean,
139 { hsplit, foldA, foldB }: Layout,
140 ): CSS => {
141 // FOLDED
142 if (!unfold) return {
143 container: BLOCK,
144 sideA: foldA ? HIDDEN : BLOCK,
145 split: HIDDEN,
146 sideB: foldB ? HIDDEN : BLOCK,
147 };
148 // DRAGGED
149 if (dragged) return {
150 container: BLOCK,
151 sideA: hsplit ? HPOSA : VPOSA,
152 split: hsplit ? HPOSR : VPOSR,
153 sideB: hsplit ? HPOSB : VPOSB,
154 };
155 // FLEX
156 return {
157 container: hsplit ? HFLEX : VFLEX,
158 sideA: getFlexCSS(hsplit, foldA),
159 split: hsplit ? HLINE : VLINE,
160 sideB: getFlexCSS(hsplit, foldB),
161 };
162 };
163
164 /* --------------------------------------------------------------------------*/
165 /* --- Sizing Utility Engine ---*/
166 /* --------------------------------------------------------------------------*/
167
168 type Dragging = undefined | {
169 position: number;
170 anchor: number;
171 offset: number;
172 };
173
174 function getPositionFromSettings(
175 dragging: Dragging,
176 L: Layout,
177 S: number,
178 D: number,
179 ): number {
180 if (dragging) return dragging.position;
181 if (L.foldA) return S;
182 if (L.foldB) return D - S;
183 return D * S;
184 }
185
186 function getSettingsFromPosition(L: Layout, P: number, D: number): number {
187 if (L.foldA) return P;
188 if (L.foldB) return D - P;
189 return P / D;
190 }
191
192 const inRange = (M: number, D: number, P: number) => (
193 D < M ? D / 2 : Math.min(Math.max(P, M), D - M)
194 );
195
196 /* --------------------------------------------------------------------------*/
197 /* --- Splitter Engine ---*/
198 /* --------------------------------------------------------------------------*/
199
200 interface SplitterLayoutProps extends SplitterFoldProps { layout: Layout }
201 interface SplitterEngineProps extends SplitterLayoutProps { size: Size }
202
203 function SplitterEngine(props: SplitterEngineProps) {
204 const defaultPosition = props.defaultPosition ?? 0;
205 const [settings, setSettings] =
206 Dome.useNumberSettings(props.settings, defaultPosition);
207 const [dragging, setDragging] = React.useState<Dragging>(undefined);
208 const { size, margin = 32, layout } = props;
209 const { hsplit } = layout;
210 const M = Math.max(margin, 32);
211 const D = hsplit ? size.width : size.height;
212 const { unfold = true } = props;
213 const [A, B] = props.children;
214 const dragged = settings > 0 || dragging !== undefined;
215 const css = getCSS(unfold, dragged, layout);
216 const cursor = dragging ? (hsplit ? HCURSOR : VCURSOR) : NOCURSOR;
217 const container = Utils.classes(css.container, cursor);
218 const sideA = Utils.classes(css.sideA, PANEL);
219 const sideB = Utils.classes(css.sideB, PANEL);
220 const dragger = Utils.classes(
221 hsplit ? HGRAB : VGRAB,
222 dragging ? DRAGGING : DRAGZONE,
223 );
224
225 let styleA: undefined | React.CSSProperties;
226 let styleB: undefined | React.CSSProperties;
227 let styleR: undefined | React.CSSProperties;
228
229 if (unfold && dragged) {
230 const P = getPositionFromSettings(dragging, layout, settings, D);
231 const X = dragging ? dragging.offset - dragging.anchor : 0;
232 const Q = inRange(M, D, P + X);
233 styleA = hsplit ? { width: Q } : { height: Q };
234 styleR = hsplit ? { left: Q } : { top: Q };
235 styleB = hsplit ? { left: Q + 1 } : { top: Q + 1 };
236 }
237
238 const onStart: DraggableEventHandler =
239 (_evt, data) => {
240 const startPos = hsplit ? data.node.offsetLeft : data.node.offsetTop;
241 const anchor = hsplit ? data.x : data.y;
242 setDragging({ position: startPos, offset: anchor, anchor });
243 };
244
245 const onDrag: DraggableEventHandler =
246 (_evt, data) => {
247 if (dragging) {
248 const offset = hsplit ? data.x : data.y;
249 setDragging({ ...dragging, offset });
250 }
251 };
252
253 const onStop: DraggableEventHandler =
254 (evt, _data) => {
255 if (evt.metaKey || evt.altKey || evt.ctrlKey) {
256 setSettings(defaultPosition);
257 } else if (unfold && dragging) {
258 const offsetPos = dragging.position + dragging.offset - dragging.anchor;
259 const newPos = inRange(M, D, offsetPos);
260 setSettings(getSettingsFromPosition(layout, newPos, D));
261 }
262 setDragging(undefined);
263 };
264
265 return (
266 <div
267 key="container"
268 className={container}
269 style={props.size}
270 >
271 <div
272 key="sideA"
273 className={sideA}
274 style={styleA}
275 >
276 {A}
277 </div>
278 <DraggableCore
279 handle={HANDLE}
280 onStart={onStart}
281 onDrag={onDrag}
282 onStop={onStop}
283 >
284 <div
285 key="split"
286 className={css.split}
287 style={styleR}
288 >
289 <div className={dragger} />
290 </div>
291 </DraggableCore>
292 <div
293 key="sideB"
294 className={sideB}
295 style={styleB}
296 >
297 {B}
298 </div>
299 </div>
300 );
301 }
302
303 const SplitterLayout = (props: SplitterLayoutProps) => (
304 <div className={CONTAINER}>
305 <AutoSizer>
306 {(size: Size) => (
307 <SplitterEngine size={size} {...props} />
308 )}
309 </AutoSizer>
310 </div>
311 );
312
313 // --------------------------------------------------------------------------
314 // --- Short Cuts
315 // --------------------------------------------------------------------------
316
317 const HLayout = { hsplit: true, foldA: false, foldB: false };
318 const LLayout = { hsplit: true, foldA: true, foldB: false };
319 const RLayout = { hsplit: true, foldA: false, foldB: true };
320 const VLayout = { hsplit: false, foldA: false, foldB: false };
321 const TLayout = { hsplit: false, foldA: true, foldB: false };
322 const BLayout = { hsplit: false, foldA: false, foldB: true };
323
324 const getLayout = (d: Direction): Layout => {
325 switch (d) {
326 case Direction.HORIZONTAL: return HLayout;
327 case Direction.LEFT: return LLayout;
328 case Direction.RIGHT: return RLayout;
329 case Direction.VERTICAL: return VLayout;
330 case Direction.TOP: return TLayout;
331 case Direction.BOTTOM: return BLayout;
332 }
333 };
334
335 /** Splitter with specified direction.
336 @category Base Component
337 */
338 export function Splitter(props: SplitterDirProps) {
339 const { direction, ...others } = props;
340 const layout = getLayout(direction);
341 return <SplitterLayout layout={layout} {...others} />;
342 }
343
344 /** Horizontal Splitter. */
345 export function HSplit(props: SplitterBaseProps) {
346 return <SplitterLayout layout={HLayout} {...props} />;
347 }
348
349 /** Vertical Splitter. */
350 export function VSplit(props: SplitterBaseProps) {
351 return <SplitterLayout layout={VLayout} {...props} />;
352 }
353
354 /** Horizontal Splitter with stacked and foldable left element. */
355 export function LSplit(props: SplitterFoldProps) {
356 return <SplitterLayout layout={LLayout} {...props} />;
357 }
358
359 /** Horizontal Splitter with stacked and foldable right element. */
360 export function RSplit(props: SplitterFoldProps) {
361 return <SplitterLayout layout={RLayout} {...props} />;
362 }
363
364 /** Vertical Splitter with stacked and foldable top element. */
365 export function TSplit(props: SplitterFoldProps) {
366 return <SplitterLayout layout={TLayout} {...props} />;
367 }
368
369 /** Vertical Splitter with stacked and foldable bottom element. */
370 export function BSplit(props: SplitterFoldProps) {
371 return <SplitterLayout layout={BLayout} {...props} />;
372 }
373
374 // --------------------------------------------------------------------------