aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/TopBar.tsx
blob: 5c9bfc0a731a785e334ca8862f95c59d3b63edff (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import BookIcon from '@mui/icons-material/Book';
import GitHubIcon from '@mui/icons-material/GitHub';
import HomeIcon from '@mui/icons-material/Home';
import AppBar from '@mui/material/AppBar';
import IconButton from '@mui/material/IconButton';
import Stack from '@mui/material/Stack';
import Toolbar from '@mui/material/Toolbar';
import Tooltip from '@mui/material/Tooltip';
import Typography from '@mui/material/Typography';
import { styled, useTheme } from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';
import { throttle } from 'lodash-es';
import { observer } from 'mobx-react-lite';
import { useEffect, useMemo, useState } from 'react';

import PaneButtons from './PaneButtons';
import { useRootStore } from './RootStoreProvider';
import ToggleDarkModeButton from './ToggleDarkModeButton';
import GenerateButton from './editor/GenerateButton';

function useWindowControlsOverlayVisible(): boolean {
  const [windowControlsOverlayVisible, setWindowControlsOverlayVisible] =
    useState(false);
  const updateWindowControlsOverlayVisible = useMemo(
    () =>
      throttle(
        ({ visible }: WindowControlsOverlayGeometryChangeEvent) =>
          setWindowControlsOverlayVisible(visible),
        250,
      ),
    [],
  );
  useEffect(() => {
    if ('windowControlsOverlay' in navigator) {
      const { windowControlsOverlay } = navigator;
      setWindowControlsOverlayVisible(windowControlsOverlay.visible);
      windowControlsOverlay.addEventListener(
        'geometrychange',
        updateWindowControlsOverlayVisible,
      );
      return () => {
        windowControlsOverlay.removeEventListener(
          'geometrychange',
          updateWindowControlsOverlayVisible,
        );
        updateWindowControlsOverlayVisible.cancel();
      };
    }
    // Nothing to clean up if `windowControlsOverlay` is unsupported.
    return () => {};
  }, [updateWindowControlsOverlayVisible]);
  return windowControlsOverlayVisible;
}

function RefineryIcon({ size }: { size: number }): JSX.Element {
  const theme = useTheme();
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      width={size}
      height={size}
      viewBox="0 0 512 515"
    >
      <path
        d="M447.98 179.335c-139.95-9.583-301.272-50.91-384-147.336v46.117C98.45 129.623 209.442 178.137 294.243 199.1c-84.796 20.963-195.791 69.476-230.265 120.985v46.117c82.73-96.422 244.053-137.752 384.002-147.334z"
        fill={theme.palette.text.primary}
      />
      <path
        d="M447.98 296.729c-113.755 4.192-287.485 40.727-384 136.557v46.716c95.14-103.612 279.898-137.754 384-143.745z"
        fill={theme.palette.primary.main}
      />
    </svg>
  );
}

const DevModeBadge = styled('div')(({ theme }) => ({
  ...theme.typography.button,
  display: 'inline-block',
  padding: `0 ${theme.shape.borderRadius}px`,
  background: theme.palette.text.primary,
  color: theme.palette.outer.background,
  borderRadius: theme.shape.borderRadius,
}));

const FileName = styled('span', {
  shouldForwardProp: (prop) => prop !== 'unsavedChanges',
})<{ unsavedChanges: boolean }>(({ theme, unsavedChanges }) => ({
  marginLeft: theme.spacing(1),
  fontWeight: theme.typography.fontWeightLight,
  fontSize: '1.25rem',
  lineHeight: '1.6rem',
  color: unsavedChanges
    ? theme.palette.text.primary
    : theme.palette.text.secondary,
}));

export default observer(function TopBar(): JSX.Element {
  const { editorStore, themeStore } = useRootStore();
  const overlayVisible = useWindowControlsOverlayVisible();
  const { breakpoints } = useTheme();
  const medium = useMediaQuery(breakpoints.up('sm'));
  const large = useMediaQuery(breakpoints.up('md'));
  const veryLarge = useMediaQuery(breakpoints.up('lg'));

  return (
    <AppBar
      position="static"
      elevation={0}
      color="transparent"
      sx={(theme) => ({
        background: theme.palette.outer.background,
        borderBottom: `1px solid ${theme.palette.outer.border}`,
        appRegion: 'drag',
        '.MuiButtonBase-root': {
          appRegion: 'no-drag',
        },
      })}
    >
      <Toolbar
        sx={{
          ...(overlayVisible
            ? {
                marginLeft: 'env(titlebar-area-x, 0)',
                marginTop: 'env(titlebar-area-y, 0)',
                width: 'env(titlebar-area-width, 100%)',
                minHeight: 'env(titlebar-area-height, auto)',
              }
            : {
                minHeight: 'auto',
              }),
          py: 0.5,
        }}
      >
        <RefineryIcon size={32} />
        <Typography variant="h6" component="h1" pl={1}>
          Refinery {import.meta.env.DEV && <DevModeBadge>Dev</DevModeBadge>}
        </Typography>
        {large && editorStore?.simpleName !== undefined && (
          <FileName unsavedChanges={editorStore.unsavedChanges}>
            {editorStore.simpleName}
          </FileName>
        )}
        <Stack direction="row" alignItems="center" flexGrow={1} marginLeft={1}>
          {medium && !large && (
            <PaneButtons themeStore={themeStore} hideLabel />
          )}
        </Stack>
        {large && (
          <Stack
            direction="row"
            alignItems="center"
            sx={{
              position: 'absolute',
              top: 0,
              bottom: 0,
              left: '50%',
              transform: 'translateX(-50%)',
            }}
          >
            <PaneButtons themeStore={themeStore} />
          </Stack>
        )}
        <Stack direction="row" marginLeft={1} gap={1} alignItems="center">
          {large && (
            <Stack direction="row" alignItems="center">
              <Tooltip title="Refinery home page">
                <IconButton
                  href="https://refinery.tools/"
                  target="_blank"
                  color="inherit"
                >
                  <HomeIcon />
                </IconButton>
              </Tooltip>
              <Tooltip title="Refinery documentation">
                <IconButton
                  href="https://refinery.tools/learn/"
                  target="_blank"
                  color="inherit"
                >
                  <BookIcon />
                </IconButton>
              </Tooltip>
              <Tooltip title="Check us out at GitHub">
                <IconButton
                  href="https://github.com/graphs4value/refinery"
                  target="_blank"
                  color="inherit"
                >
                  <GitHubIcon />
                </IconButton>
              </Tooltip>
            </Stack>
          )}
          <GenerateButton editorStore={editorStore} hideWarnings={!veryLarge} />
          <ToggleDarkModeButton />
        </Stack>
      </Toolbar>
    </AppBar>
  );
});