aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/Refinery.tsx
blob: 099646f0490420c989cab67892459314f7f4e73f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import MoreHorizIcon from '@mui/icons-material/MoreHoriz';
import MoreVertIcon from '@mui/icons-material/MoreVert';
import Box from '@mui/material/Box';
import Grow from '@mui/material/Grow';
import Stack from '@mui/material/Stack';
import { alpha, useTheme } from '@mui/material/styles';
import { SnackbarProvider } from 'notistack';
import { memo, useRef, useState } from 'react';
import { useResizeDetector } from 'react-resize-detector';

import TopBar from './TopBar';
import UpdateNotification from './UpdateNotification';
import EditorPane from './editor/EditorPane';
import GraphPane from './graph/GraphPane';

const DirectionalSplitPane = memo(function SplitPanel({
  horizontalSplit,
}: {
  horizontalSplit: boolean;
}): JSX.Element {
  const theme = useTheme();
  const stackRef = useRef<HTMLDivElement>(null);
  const sliderRef = useRef<HTMLDivElement>(null);
  const [resizing, setResizing] = useState(false);
  const [fraction, setFraction] = useState(0.5);

  const direction = horizontalSplit ? 'column' : 'row';
  const axis = horizontalSplit ? 'height' : 'width';
  const primarySize = `calc(${fraction * 100}% - 0.5px)`;
  const secondarySize = `calc(${(1 - fraction) * 100}% - 0.5px)`;

  return (
    <Stack direction={direction} height="100%" overflow="hidden" ref={stackRef}>
      <Box {...{ [axis]: primarySize }}>
        <EditorPane />
      </Box>
      <Box
        sx={{
          overflow: 'visible',
          position: 'relative',
          [axis]: '0px',
          display: 'flex',
          flexDirection: direction,
          [horizontalSplit
            ? 'borderBottom'
            : 'borderRight']: `1px solid ${theme.palette.outer.border}`,
        }}
      >
        <Box
          ref={sliderRef}
          sx={{
            display: 'flex',
            position: 'absolute',
            [axis]: theme.spacing(2),
            ...(horizontalSplit
              ? {
                  top: theme.spacing(-1),
                  left: 0,
                  right: 0,
                  transform: 'translateY(0.5px)',
                }
              : {
                  left: theme.spacing(-1),
                  top: 0,
                  bottom: 0,
                  transform: 'translateX(0.5px)',
                }),
            zIndex: 999,
            alignItems: 'center',
            justifyContent: 'center',
            color: theme.palette.text.secondary,
            cursor: horizontalSplit ? 'ns-resize' : 'ew-resize',
            '.MuiSvgIcon-root': {
              opacity: resizing ? 1 : 0,
            },
            ...(resizing
              ? {
                  background: alpha(
                    theme.palette.text.primary,
                    theme.palette.action.activatedOpacity,
                  ),
                }
              : {
                  '&:hover': {
                    background: alpha(
                      theme.palette.text.primary,
                      theme.palette.action.hoverOpacity,
                    ),
                    '.MuiSvgIcon-root': {
                      opacity: 1,
                    },
                  },
                }),
          }}
          onPointerDown={(event) => {
            if (event.button !== 0) {
              return;
            }
            sliderRef.current?.setPointerCapture(event.pointerId);
            setResizing(true);
          }}
          onPointerUp={(event) => {
            if (event.button !== 0) {
              return;
            }
            sliderRef.current?.releasePointerCapture(event.pointerId);
            setResizing(false);
          }}
          onPointerMove={(event) => {
            if (!resizing) {
              return;
            }
            const container = stackRef.current;
            if (container === null) {
              return;
            }
            const rect = container.getBoundingClientRect();
            const newFraction = horizontalSplit
              ? (event.clientY - rect.top) / rect.height
              : (event.clientX - rect.left) / rect.width;
            setFraction(Math.min(0.9, Math.max(0.1, newFraction)));
          }}
          onDoubleClick={() => setFraction(0.5)}
        >
          {horizontalSplit ? <MoreHorizIcon /> : <MoreVertIcon />}
        </Box>
      </Box>
      <Box {...{ [axis]: secondarySize }}>
        <GraphPane />
      </Box>
    </Stack>
  );
});

function SplitPane(): JSX.Element {
  const { ref, width, height } = useResizeDetector();
  const horizontalSplit =
    width !== undefined && height !== undefined && height > width;

  return (
    <Box height="100%" overflow="auto" ref={ref}>
      <DirectionalSplitPane horizontalSplit={horizontalSplit} />
    </Box>
  );
}

export default function Refinery(): JSX.Element {
  return (
    <SnackbarProvider TransitionComponent={Grow}>
      <UpdateNotification />
      <Stack direction="column" height="100%" overflow="auto">
        <TopBar />
        <SplitPane />
      </Stack>
    </SnackbarProvider>
  );
}