Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ as a fill is invisible against at least one other (the light ladder is 1.04:1 en
- **Inactive selection (unfocused pane):** `rgba(95,90,76,.04)` / `rgba(233,228,216,.04)` — the active tab of an
unfocused dock group
- **Skeleton:** `rgba(95,90,76,.10)` / `rgba(233,228,216,.10)` — a mark, not a ground
- **Drop target:** info wash + dashed info line
- **Badge:** primary fill, inverse text; semantic badges use their quad
- **Scrollbar thumb:** border at rest → strong on hover → disabled ink on drag

Expand Down
5 changes: 3 additions & 2 deletions apps/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ function createWindow(background: boolean) {
});
// Without this, a link the renderer opens (a terminal URL) becomes a bare BrowserWindow.
main.webContents.setWindowOpenHandler(({ url }) => {
const protocol = URL.parse(url)?.protocol;
if (protocol === 'http:' || protocol === 'https:') {
const target = URL.parse(url);
const own = target?.protocol === home.protocol && target.host === home.host;
if (!own && (target?.protocol === 'http:' || target?.protocol === 'https:')) {
void shell.openExternal(url);
}

Expand Down
6 changes: 6 additions & 0 deletions apps/web/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
}
}
},
"overrides": [
{
"includes": ["**/vite.config.ts", "**/vitest.config.ts", "**/vite/**/*"],
"linter": { "rules": { "style": { "noRestrictedImports": "off" } } }
}
],
"javascript": {
"formatter": {
"quoteStyle": "single",
Expand Down
48 changes: 48 additions & 0 deletions apps/web/src/components/drag.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DOMRectangle } from '@dnd-kit/dom/utilities';
import { afterEach, describe, expect, test } from 'vitest';
import { CENTERED } from './drag';

const boxes: Element[] = [];

function shape(left: number, top: number, width: number, height: number) {
const element = document.createElement('div');
element.style.cssText = `position: fixed; left: ${left}px; top: ${top}px; width: ${width}px; height: ${height}px;`;
document.body.append(element);
boxes.push(element);
return new DOMRectangle(element);
}

// Matches a droppable against a dragged item, each with the given shape.
const match = (dragged: DOMRectangle | undefined, droppable: DOMRectangle) =>
CENTERED({
dragOperation: { shape: dragged === undefined ? null : { current: dragged } },
droppable: { id: 'droppable', shape: droppable },
} as unknown as Parameters<typeof CENTERED>[0]);

afterEach(() => {
for (const element of boxes.splice(0)) {
element.remove();
}
});

describe('CENTERED', () => {
test('matches droppable containing dragged centre', () => {
expect(match(shape(150, 100, 100, 20), shape(190, 90, 100, 40))?.id).toBe('droppable');
});

test('returns null for droppable that dragged item only overlaps', () => {
expect(match(shape(100, 100, 100, 20), shape(190, 90, 100, 40))).toBeNull();
});

test('returns null without dragged shape', () => {
expect(match(undefined, shape(190, 90, 100, 40))).toBeNull();
});

test('ranks droppable with nearer centre higher', () => {
const dragged = shape(150, 100, 100, 20);
const near = match(dragged, shape(150, 90, 100, 40));
const far = match(dragged, shape(0, 0, 1000, 1000));

expect(near?.value).toBeGreaterThan(far?.value ?? Number.POSITIVE_INFINITY);
});
});
48 changes: 48 additions & 0 deletions apps/web/src/components/drag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
type CollisionDetector,
CollisionPriority,
CollisionType,
type DragOperation,
Modifier,
} from '@dnd-kit/abstract';
import { AutoScroller, Feedback, PointerActivationConstraints, PointerSensor, PreventSelection } from '@dnd-kit/dom';

// Moves the dragged item by whole device pixels. The pointer reports fractions of a pixel, which blurs the item.
class SnapToPixels extends Modifier {
override apply({ shape, transform }: DragOperation) {
const ratio = window.devicePixelRatio;
const { left = 0, top = 0 } = shape?.initial.boundingRectangle ?? {};
return {
x: Math.round((left + transform.x) * ratio) / ratio - left,
y: Math.round((top + transform.y) * ratio) / ratio - top,
};
}
}

// Matches by the dragged item's centre.
export const CENTERED: CollisionDetector = ({ dragOperation, droppable }) => {
const centre = dragOperation.shape?.current.center;
if (centre === undefined || droppable.shape?.containsPoint(centre) !== true) {
return null;
}

const { x, y } = droppable.shape.center;
return {
id: droppable.id,
value: 1 / Math.hypot(x - centre.x, y - centre.y),
type: CollisionType.PointerIntersection,
priority: CollisionPriority.High,
};
};

export const DRAG_MODIFIERS = [SnapToPixels];

export const DRAG_PLUGINS = [AutoScroller, Feedback, PreventSelection];

export const DRAG_INSTANT_PLUGINS = [Feedback.configure({ dropAnimation: null }), PreventSelection];

export const DRAG_SENSORS = [
PointerSensor.configure({
activationConstraints: () => [new PointerActivationConstraints.Distance({ value: 5 })],
}),
];
219 changes: 219 additions & 0 deletions apps/web/src/dock/dock-manager.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Panel, TerminalPanel } from '@elsewise/frontend-plugin';
import { QueryClient } from '@tanstack/react-query';
import { describe, expect, test, vi } from 'vitest';
import { ProjectManager } from '@/projects/project-manager';
Expand Down Expand Up @@ -304,3 +305,221 @@ describe('activate', () => {
expect(dock.activePanel).toEqual(FILE);
});
});

describe('sizes', () => {
const A = { ...TERMINAL, id: 'terminal:a', session: { ...WORKSPACE, session: 'a' } };
const B = { ...TERMINAL, id: 'terminal:b', session: { ...WORKSPACE, session: 'b' } };
const C = { ...TERMINAL, id: 'terminal:c', session: { ...WORKSPACE, session: 'c' } };

// A dock whose row has the files 500 wide, a 120 wide, b 150 wide and the file tree.
async function mountRow() {
const mounted = await mount();
const { dockview } = mounted;
dockview.getPanel(TERMINAL.id)?.api.close();

const add = (panel: TerminalPanel, reference: Panel, direction: 'right' | 'below') =>
dockview.addPanel({
id: panel.id,
component: 'terminal',
params: { session: panel.session.session },
position: { referencePanel: reference.id, direction },
});
const width = (panel: Panel) => dockview.getPanel(panel.id)?.group.api.width;
const height = (panel: Panel) => dockview.getPanel(panel.id)?.group.api.height;
const resize = async (panel: Panel, size: { width?: number; height?: number }) => {
dockview.getPanel(panel.id)?.group.api.setSize(size);
await new Promise((resolve) => setTimeout(resolve));
};

add(A, FILE, 'right');
add(B, A, 'right');
await resize(FILE, { width: 500 });
await resize(A, { width: 120 });
await resize(B, { width: 150 });
return { ...mounted, add, width, height, resize, fileTree: width(FILE_TREE) };
}

test('gives width of closed group to previous group', async () => {
const { dock, width, fileTree } = await mountRow();

await dock.close(A);

expect([width(FILE), width(B), width(FILE_TREE)]).toEqual([620, 150, fileTree]);
});

test('gives width of closed first group to next group', async () => {
const { dock, width, fileTree } = await mountRow();
await dock.close(OTHER);

await dock.close(FILE);

expect([width(A), width(B), width(FILE_TREE)]).toEqual([620, 150, fileTree]);
});

test('gives width of closed group to tab group over tabless panel', async () => {
const { dock, width, resize } = await mountRow();
dock.move(FILE_TREE, { edge: 'left' });
await resize(FILE_TREE, { width: 300 });
await resize(FILE, { width: 400 });
await resize(A, { width: 120 });
const b = width(B);
await dock.close(OTHER);

await dock.close(FILE);

expect([width(FILE_TREE), width(A), width(B)]).toEqual([300, 520, b]);
});

test('keeps equal groups equal when group closes', async () => {
const { dock, dockview, width, resize } = await mountRow();
const quarter = dockview.width / 4;
await resize(FILE, { width: quarter });
await resize(A, { width: quarter });
await resize(B, { width: quarter });

await dock.close(A);

const widths = [width(FILE) ?? 0, width(B) ?? 0, width(FILE_TREE) ?? 0];
expect(Math.max(...widths) - Math.min(...widths)).toBeLessThanOrEqual(2);
});

test('gives height of closed group to previous group', async () => {
const { dock, add, height, resize } = await mountRow();
add(C, B, 'below');
add(TERMINAL, C, 'below');
await resize(B, { height: 400 });
await resize(C, { height: 100 });

await dock.close(C);

expect([height(B), height(TERMINAL)]).toEqual([500, 200]);
});

test('keeps width of stack beside closed group', async () => {
const { dock, add, width, fileTree } = await mountRow();
add(C, B, 'below');

await dock.close(A);

expect([width(FILE), width(B), width(C), width(FILE_TREE)]).toEqual([620, 150, 150, fileTree]);
});

test('keeps widths of other groups when last tab moves to another group', async () => {
const { dock, width, fileTree } = await mountRow();

dock.move(A, { tab: 'last', with: B });

expect([width(FILE), width(B), width(FILE_TREE)]).toEqual([620, 150, fileTree]);
});

test('keeps widths of other groups when last tab moves to group of its own', async () => {
const { dock, width, fileTree } = await mountRow();

dock.move(A, { group: 'below', of: B });

expect([width(FILE), width(A), width(B), width(FILE_TREE)]).toEqual([620, 150, 150, fileTree]);
});

test('keeps widths of other groups when last tab moves along bottom edge', async () => {
const { dock, width, fileTree } = await mountRow();

dock.move(A, { edge: 'bottom' });

expect([width(FILE), width(B), width(FILE_TREE)]).toEqual([620, 150, fileTree]);
});

test('shrinks other groups alike when last tab moves along left edge', async () => {
const { dock, dockview, width, fileTree } = await mountRow();

dock.move(A, { edge: 'left' });

const scale = (dockview.width - (width(A) ?? 0)) / dockview.width;
expect(width(FILE)).toBeCloseTo(620 * scale, -1);
expect(width(B)).toBeCloseTo(150 * scale, -1);
expect(width(FILE_TREE)).toBeCloseTo((fileTree ?? 0) * scale, -1);
});

test('restores widths before next frame', async () => {
const { dock, width } = await mountRow();
const frame = new Promise((resolve) => requestAnimationFrame(() => resolve([width(FILE), width(B)])));

dock.move(A, { tab: 'last', with: B });

await expect(frame).resolves.toEqual([620, 150]);
});
});

describe('placeholder', () => {
const PLACEHOLDER = { id: 'placeholder', type: 'placeholder', tab: true, closable: false } as const;
const LAYOUTS = ['one tool', 'stacked tools', 'tools in grid', 'stacked tools on left'] as const;

// A dock without a tab, whose tools are 266 wide beside the placeholder.
async function mountEmpty(layout: (typeof LAYOUTS)[number]) {
const mounted = await mount();
const { dock, dockview } = mounted;
const tool = (id: string, referencePanel: string, direction: 'below' | 'right') =>
dockview.addPanel({
id,
component: 'fileTree',
tabComponent: 'fileTree',
position: { referencePanel, direction },
});
if (layout !== 'one tool') {
tool('tool:c', FILE_TREE.id, 'below');
}
if (layout === 'tools in grid') {
tool('tool:b', FILE_TREE.id, 'right');
tool('tool:d', 'tool:c', 'right');
dockview.getPanel(FILE_TREE.id)?.group.api.setSize({ width: 100 });
}
if (layout === 'stacked tools on left') {
dock.move(FILE, { edge: 'right', width: 798 });
dock.move(OTHER, { tab: 'last', with: FILE });
dock.move(TERMINAL, { tab: 'last', with: FILE });
}
await new Promise((resolve) => setTimeout(resolve));

const sizes = () =>
Object.fromEntries(dockview.groups.map((group) => [group.panels[0]?.id, [group.api.width, group.api.height]]));
const tools = Object.fromEntries(
Object.entries(sizes()).filter(([id]) => id === FILE_TREE.id || id.startsWith('tool:')),
);
for (const panel of [FILE, OTHER, TERMINAL]) {
await dock.close(panel);
}
return { ...mounted, sizes, tools };
}

test.each(LAYOUTS)('takes place of last tab panel beside %s', async (layout) => {
const { dock, sizes, tools } = await mountEmpty(layout);

expect(sizes()).toEqual({ ...tools, [PLACEHOLDER.id]: [798, 700] });
expect(dock.activeTabPanel).toEqual(PLACEHOLDER);
});

test.each(LAYOUTS)('leaves its place to tab panel beside %s', async (layout) => {
const { dock, sizes, tools } = await mountEmpty(layout);

const panel = dock.addFile('src/new.ts', { tab: 'last', with: PLACEHOLDER });

expect(sizes()).toEqual({ ...tools, [panel?.id ?? '']: [798, 700] });
expect(dock.get(PLACEHOLDER.id)).toBeNull();
});

test('closes when tab panel opens in group of its own', async () => {
const { dock } = await mountEmpty('one tool');

const panel = dock.addFile('src/new.ts', { edge: 'bottom' });

expect(dock.panels).toEqual([FILE_TREE, panel]);
});

test('stays while tab panel is open', async () => {
const { dock } = await mount();

await dock.close(OTHER);
await dock.close(TERMINAL);

expect(dock.get(PLACEHOLDER.id)).toBeNull();
});
});
Loading
Loading