Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/components/ChatDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { showError } from '../lib/errors';
import { createSocket } from '../lib/socket';
import { getTheme } from '../lib/theme';
import { copyToClipboard, showToast } from '../lib/toast';
import Menu, { type MenuItem } from './designer/Menu';
import {
CopyIcon,
DownloadIcon,
Expand All @@ -40,6 +39,7 @@ import {
SparklesIcon,
} from './Icons';
import ImageLightbox from './ImageLightbox';
import Menu, { type MenuItem } from './Menu';
import WidgetDialog from './WidgetDialog';

type Segment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from 'react';
import { createPortal } from 'react-dom';

import { MenuIcon } from '../Icons';
import { MenuIcon } from './Icons';

export interface MenuItem {
// A heading above a run of items, rather than an item itself.
Expand Down
130 changes: 130 additions & 0 deletions frontend/src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* A right-aligned row of actions that never wraps and never jumps.
*
* When the actions don't all fit, the leftmost ones that aren't pinned move into a "more" menu
* that takes their place at the start of the row, rather than the row wrapping onto a second line or squeezing its
* neighbours. Which actions fit depends only on the room available and on which actions
* exist. It never depends on their state: a button going disabled, busy or enabled keeps its
* width (see BusyLabel), so Save lighting up can't move a thing.
*
* Every action is also rendered in an invisible measuring row, so each button's natural width
* is known even while it sits in the menu, and the decision can be made in one layout pass.
*/

import { ReactNode, useCallback, useLayoutEffect, useRef, useState } from 'react';

import BusyLabel from './BusyLabel';
import { MenuIcon } from './Icons';
import Menu, { type MenuItem } from './Menu';
import { overflowCount } from './toolbarOverflow';

export interface ToolbarAction {
id: string;
label: string;
icon: ReactNode;
title?: string;
onClick: () => void;
disabled?: boolean;
busy?: boolean;
// The label while busy, which the button reserves room for at all times.
busyLabel?: string;
// The filled, primary button rather than a secondary one.
primary?: boolean;
// Never moves into the menu.
pinned?: boolean;
}

// An action's button. The ruler's copy has no title, so it is never found or announced.
function ActionButton(props: Readonly<{ action: ToolbarAction; ruler?: boolean }>) {
const { action } = props;
return (
<button
type="button"
className={action.primary ? 'btn btn-small' : 'btn btn-secondary btn-small'}
title={props.ruler ? undefined : (action.title ?? action.label)}
disabled={action.disabled || action.busy}
onClick={action.onClick}
>
{action.icon}
{action.busyLabel ? (
<BusyLabel busy={!!action.busy} label={action.label} busyLabel={action.busyLabel} />
) : (
action.label
)}
</button>
);
}

export default function Toolbar(
props: Readonly<{
actions: ToolbarAction[];
// The title of the "more" menu's trigger.
moreTitle?: string;
}>,
) {
const { actions } = props;
const rowRef = useRef<HTMLDivElement>(null);
const measureRef = useRef<HTMLDivElement>(null);
const [hidden, setHidden] = useState(0);

/*
* Only which actions exist, and which are pinned, can change the outcome, so that's all
* re-measuring is keyed on.
*/
const shape = actions.map(action => (action.pinned ? '!' : '') + action.id).join('|');

const measure = useCallback(() => {
const row = rowRef.current;
const ruler = measureRef.current;
if (!row || !ruler) {
return;
}
const widths = Array.from(ruler.children, child => (child as HTMLElement).offsetWidth);
const triggerWidth = widths.pop() ?? 0;
const pinned = shape.split('|').map(key => key.startsWith('!'));
setHidden(overflowCount(pinned, widths, triggerWidth, row.clientWidth));
}, [shape]);

useLayoutEffect(() => {
measure();
const row = rowRef.current;
if (!row) {
return;
}
const observer = new ResizeObserver(() => measure());
observer.observe(row);
return () => observer.disconnect();
}, [measure]);

const unpinned = actions.filter(action => !action.pinned);
const overflowed = new Set(unpinned.slice(0, hidden).map(action => action.id));
const items: MenuItem[] = actions
.filter(action => overflowed.has(action.id))
.map(action => ({
label: action.busy && action.busyLabel ? action.busyLabel : action.label,
icon: action.icon,
hint: action.title,
disabled: action.disabled || action.busy,
onClick: action.onClick,
}));
const moreTitle = props.moreTitle ?? 'More actions';

return (
<div className="overflow-toolbar" ref={rowRef}>
<div className="overflow-toolbar-measure" ref={measureRef} aria-hidden="true" inert>
{actions.map(action => (
<ActionButton key={action.id} action={action} ruler />
))}
<button type="button" className="btn btn-secondary btn-small">
<MenuIcon />
</button>
</div>
{items.length > 0 && <Menu title={moreTitle} items={items} />}
{actions
.filter(action => !overflowed.has(action.id))
.map(action => (
<ActionButton key={action.id} action={action} />
))}
</div>
);
}
2 changes: 1 addition & 1 deletion frontend/src/components/designer/DesignerMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Menu from './Menu';
import Menu from '../Menu';
import { designerMenuItems, DesignerMenuOptions } from './menuItems';

// The "more" menu in the toolbar, built from the designer's current state.
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/designer/menuItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* The items of the Web Designer's "more" menu, built from its current state.
*/

import type { MenuItem } from '../Menu';
import { canonicalUrl, companionFile, WEB_ROOT } from './designerFiles';
import { VIEWPORTS } from './layout';
import type { MenuItem } from './Menu';

// What the menu is built from.
export interface DesignerMenuOptions {
Expand Down
149 changes: 86 additions & 63 deletions frontend/src/components/files/IdeHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { copyToClipboard } from '../../lib/toast';
import BusyLabel from '../BusyLabel';
import { BracesIcon, CopyIcon, DownloadIcon, PlayIcon, SaveIcon, SparkIcon } from '../Icons';
import Toolbar, { type ToolbarAction } from '../Toolbar';
import { endpointOf } from './helpers';

/*
* The Hyper IDE's header — the title, the open file's path and the toolbar
* The Hyper IDE's header — the title, the open file's name and the toolbar
* acting on that file.
*/
export default function IdeHeader(
Expand All @@ -22,6 +22,72 @@ export default function IdeHeader(
}>,
) {
const { selectedFile, dirty, saving, generating, hasSelection } = props;
const hyperlambda = selectedFile.endsWith('.hl');

/*
* Listed left to right. When the row runs out of room the leftmost go into the "more" menu
* first, so the occasional actions leave before the frequent ones, and Save never does.
*/
const actions: ToolbarAction[] = [];
if (selectedFile) {
actions.push(
{
id: 'copy',
label: 'Copy path',
icon: <CopyIcon />,
onClick: () => void copyToClipboard(selectedFile, 'The path'),
},
{
id: 'download',
label: 'Download',
title: 'Download file',
icon: <DownloadIcon />,
onClick: () => void props.onDownload(),
},
);
}
if (endpointOf(selectedFile)) {
actions.push({
id: 'openapi',
label: 'OpenAPI',
title: 'OpenAPI specification for this endpoint',
icon: <BracesIcon />,
onClick: () => void props.onOpenApi(selectedFile),
});
}
if (hyperlambda) {
actions.push(
{
id: 'generate',
label: 'Generate',
busyLabel: 'Generating…',
title: 'Run the selected text through the Hyperlambda generator',
icon: <SparkIcon />,
busy: generating,
disabled: !hasSelection,
onClick: () => void props.onGenerate(),
},
{
id: 'execute',
label: 'Execute',
title: 'Execute this Hyperlambda',
icon: <PlayIcon />,
onClick: () => void props.onExecute(),
},
);
}
actions.push({
id: 'save',
label: 'Save',
busyLabel: 'Saving…',
icon: <SaveIcon />,
primary: true,
pinned: true,
busy: saving,
disabled: !selectedFile || !dirty,
onClick: () => void props.onSave(),
});

return (
<div
className="page-header ide-header"
Expand All @@ -36,68 +102,25 @@ export default function IdeHeader(
*/}
{!selectedFile && <p>Browse and edit any file on your server</p>}
</div>
<span className="spacer" style={{ flex: 1 }} />
<span className="mono ide-path">
{selectedFile || 'No file open'}
{dirty ? ' •' : ''}
</span>
{selectedFile && (
<>
<button
className="btn btn-secondary btn-small"
title="Copy path"
onClick={() => {
void copyToClipboard(selectedFile, 'The path');
}}
>
<CopyIcon />
Copy path
</button>
<button
className="btn btn-secondary btn-small"
title="Download file"
onClick={props.onDownload}
>
<DownloadIcon />
Download
</button>
</>
)}
{endpointOf(selectedFile) && (
<button
className="btn btn-secondary btn-small"
title="OpenAPI specification for this endpoint"
onClick={() => props.onOpenApi(selectedFile)}
{/*
* The full path, since the tab already names the file. When it doesn't fit it loses its
* start rather than its end, so the folders nearest the file stay readable. The unsaved
* marker always occupies its slot, only shown or hidden, so editing the file changes the
* width of nothing.
*/}
<span className="mono ide-path" title={selectedFile || undefined}>
<span className="ide-path-text">
<bdi dir="ltr">{selectedFile || 'No file open'}</bdi>
</span>
<span
className={dirty ? 'ide-dirty is-dirty' : 'ide-dirty'}
title={dirty ? 'Unsaved changes' : undefined}
aria-hidden={!dirty}
>
<BracesIcon />
OpenAPI
</button>
)}
{selectedFile.endsWith('.hl') && (
<button
className="btn btn-secondary btn-small"
title="Run the selected text through the Hyperlambda generator"
onClick={props.onGenerate}
disabled={generating || !hasSelection}
>
<SparkIcon />
<BusyLabel busy={generating} label="Generate" busyLabel="Generating…" />
</button>
)}
{selectedFile.endsWith('.hl') && (
<button className="btn btn-secondary btn-small" onClick={props.onExecute}>
<PlayIcon />
Execute
</button>
)}
<button
className="btn btn-small"
onClick={props.onSave}
disabled={!selectedFile || !dirty || saving}
>
<SaveIcon />
<BusyLabel busy={saving} label="Save" busyLabel="Saving…" />
</button>
•
</span>
</span>
<Toolbar actions={actions} moreTitle="More file actions" />
</div>
);
}
Loading
Loading