Improve workspace navigation, search, and tab actions

This commit is contained in:
Knotty Wyvern
2026-09-29 07:48:24 -07:00
parent bdc15e952d
commit 43106ed9c7
34 changed files with 1798 additions and 100 deletions
+219 -8
View File
@@ -1,4 +1,8 @@
<script lang="ts">
import { onDestroy, tick } from "svelte";
import { actionMenu, type ActionMenuItem } from "$lib/ui/actionMenu";
import { actionIcons } from "$lib/ui/actionIcons";
import { tabsToClose, type TabCloseScope } from "$lib/workspace/tabActions";
import type { OpenBuffer } from "$lib/workspace/types";
type Props = {
@@ -6,9 +10,175 @@
activePath: string | null;
onSelect: (relPath: string) => void;
onClose: (relPath: string) => void;
onMove: (relPath: string, index: number) => void;
onCloseTabs: (relPath: string, scope: TabCloseScope) => void;
onPin: (relPath: string) => void;
fileActions: (tab: OpenBuffer) => ActionMenuItem[];
};
let { tabs, activePath, onSelect, onClose }: Props = $props();
let { tabs, activePath, onSelect, onClose, onMove, onCloseTabs, onPin, fileActions }: Props = $props();
let bar: HTMLDivElement;
let dragged = $state<string | null>(null);
let dropBefore = $state<string | null>(null);
let dropAfter = $state<string | null>(null);
let announcement = $state("");
let cancelDrag = () => {};
let suppressClick = false;
let menu: HTMLElement | undefined;
let menuAnchor: HTMLButtonElement | undefined;
let menuPath = $state<string | null>(null);
$effect(() => {
if (menuPath && !tabs.some(tab => tab.relPath === menuPath)) closeMenu();
});
function tabButton(path: string) {
return [...bar.querySelectorAll<HTMLButtonElement>('[role="tab"]')].find(button => button.dataset.path === path);
}
$effect(() => {
const path = activePath;
if (path) void tick().then(() => tabButton(path)?.scrollIntoView({ block: "nearest", inline: "nearest" }));
});
async function move(path: string, index: number) {
onMove(path, index);
await tick();
tabButton(path)?.focus();
const position = tabs.findIndex(tab => tab.relPath === path);
if (position >= 0) announcement = `${tabs[position].name}, position ${position + 1} of ${tabs.length}`;
}
function closeMenu(restoreFocus = false) {
menu?.remove();
menu = undefined;
menuPath = null;
if (restoreFocus) menuAnchor?.focus();
}
function showMenu(button: HTMLButtonElement, path: string, x: number, y: number) {
cancelDrag();
closeMenu();
menuAnchor = button;
const index = tabs.findIndex(tab => tab.relPath === path);
if (index < 0) return;
menuPath = path;
const run = (action: () => void) => () => { closeMenu(true); action(); };
const closing = (label: string, scope: TabCloseScope): ActionMenuItem => ({
label, icon: "close", disabled: !tabsToClose(tabs, path, scope).length,
run: run(() => onCloseTabs(path, scope)),
});
const wrapActions = (items: ActionMenuItem[]): ActionMenuItem[] => items.map(item => {
if ("separator" in item) return item;
return { ...item, run: item.run ? run(item.run) : undefined,
children: item.children ? wrapActions(item.children) : undefined };
});
menu = actionMenu(x, y, [
closing("Close", "one"),
closing("Close others", "others"),
closing("Close tabs after", "after"),
closing("Close all", "all"),
{ separator: true },
{ label: tabs[index].pinned ? "Unpin" : "Pin", icon: "pin", run: run(() => onPin(path)) },
...(path === activePath ? wrapActions(fileActions(tabs[index])) : []),
{ separator: true },
{ label: "Move tab", icon: "swap", children: [
{ label: "Move tab left", icon: "move-left", disabled: index <= 0,
run: run(() => { void move(path, index - 1); }) },
{ label: "Move tab right", icon: "move-right", disabled: index === tabs.length - 1,
run: run(() => { void move(path, index + 1); }) },
] },
], () => closeMenu(true), "Tab actions");
menu.classList.add("tab-menu");
}
function onKey(event: KeyboardEvent, path: string) {
if (event.key === "Enter" || event.key === " ") suppressClick = false;
const index = tabs.findIndex(tab => tab.relPath === path);
if (event.key === "ContextMenu" || (event.shiftKey && event.key === "F10")) {
event.preventDefault();
event.stopPropagation();
const button = event.currentTarget as HTMLButtonElement, bounds = button.getBoundingClientRect();
showMenu(button, path, bounds.left, bounds.bottom);
} else if (["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key) && !event.ctrlKey && !event.metaKey) {
event.preventDefault();
event.stopPropagation();
const next = event.key === "Home" ? 0 : event.key === "End" ? tabs.length - 1
: Math.max(0, Math.min(tabs.length - 1, index + (event.key === "ArrowLeft" ? -1 : 1)));
if (event.altKey && event.shiftKey) void move(path, next);
else if (!event.altKey && !event.shiftKey) {
onSelect(tabs[next].relPath);
tabButton(tabs[next].relPath)?.focus();
}
}
}
function startDrag(event: PointerEvent, path: string) {
if (event.button !== 0 || !event.isPrimary) return;
cancelDrag();
closeMenu();
suppressClick = false;
const startX = event.clientX, startY = event.clientY, pointerId = event.pointerId;
let x = startX, y = startY, started = false, destination: number | null = null, frame = 0;
const update = () => {
dropBefore = dropAfter = null;
destination = null;
const bounds = bar.getBoundingClientRect();
if (y < bounds.top || y > bounds.bottom) return;
const others = [...bar.querySelectorAll<HTMLElement>(".tab")].filter(row => row.dataset.path !== path);
const before = others.findIndex(row => { const rect = row.getBoundingClientRect(); return x < rect.left + rect.width / 2; });
destination = before < 0 ? others.length : before;
if (destination === tabs.findIndex(tab => tab.relPath === path)) return;
if (before >= 0) dropBefore = others[before].dataset.path!;
else dropAfter = others.at(-1)?.dataset.path ?? null;
};
const scroll = () => {
if (!started) return;
const bounds = bar.getBoundingClientRect();
if (y >= bounds.top && y <= bounds.bottom) {
const speed = x < bounds.left + 28 ? -10 : x > bounds.right - 28 ? 10 : 0;
if (speed) { bar.scrollLeft += speed; update(); }
}
frame = requestAnimationFrame(scroll);
};
const clean = () => {
cancelAnimationFrame(frame);
dragged = dropBefore = dropAfter = null;
window.removeEventListener("pointermove", onMovePointer);
window.removeEventListener("pointerup", onUp);
window.removeEventListener("pointercancel", clean);
window.removeEventListener("blur", clean);
window.removeEventListener("keydown", onEscape, true);
cancelDrag = () => {};
};
const onMovePointer = (e: PointerEvent) => {
if (e.pointerId !== pointerId) return;
x = e.clientX; y = e.clientY;
if (!started && Math.hypot(x - startX, y - startY) < 5) return;
e.preventDefault();
if (!started) { started = true; suppressClick = true; dragged = path; frame = requestAnimationFrame(scroll); }
update();
};
const onUp = (e: PointerEvent) => {
if (e.pointerId !== pointerId) return;
x = e.clientX; y = e.clientY;
if (started) update();
const target = destination;
clean();
if (started && target !== null) void move(path, target);
};
const onEscape = (e: KeyboardEvent) => {
if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); clean(); }
};
cancelDrag = clean;
window.addEventListener("pointermove", onMovePointer, { passive: false });
window.addEventListener("pointerup", onUp);
window.addEventListener("pointercancel", clean);
window.addEventListener("blur", clean);
window.addEventListener("keydown", onEscape, true);
}
onDestroy(() => { cancelDrag(); closeMenu(); });
function onMiddleDown(event: MouseEvent) {
if (event.button === 1) {
@@ -25,20 +195,33 @@
}
</script>
<div class="tabs" role="tablist" aria-label="Open files">
<svelte:window onpointerdown={(event) => { if (menu && !menu.contains(event.target as Node)) closeMenu(); }} onresize={() => closeMenu()} />
<div bind:this={bar} class="tabs" role="tablist" aria-label="Open files">
{#each tabs as tab (tab.relPath)}
<div class="tab" class:active={tab.relPath === activePath}>
<div class="tab" data-path={tab.relPath} class:active={tab.relPath === activePath} class:dragging={dragged === tab.relPath}
class:drop-before={dropBefore === tab.relPath} class:drop-after={dropAfter === tab.relPath}>
<button
type="button"
role="tab"
data-path={tab.relPath}
tabindex={tab.relPath === activePath ? 0 : -1}
aria-selected={tab.relPath === activePath}
title={tab.kind === "empty" ? tab.name : tab.relPath}
onclick={() => onSelect(tab.relPath)}
aria-describedby={tab.pinned ? `tab-pin-${tab.id}` : undefined}
aria-keyshortcuts="Alt+Shift+ArrowLeft Alt+Shift+ArrowRight"
title={`${tab.kind === "empty" ? tab.name : tab.relPath}\nDrag to reorder. Right-click for tab actions.`}
onclick={() => { if (suppressClick) { suppressClick = false; return; } onSelect(tab.relPath); }}
onpointerdown={(event) => startDrag(event, tab.relPath)}
ondragstart={(event) => event.preventDefault()}
onkeydown={(event) => onKey(event, tab.relPath)}
oncontextmenu={(event) => { event.preventDefault(); event.stopPropagation(); showMenu(event.currentTarget, tab.relPath, event.clientX, event.clientY); }}
onmousedown={onMiddleDown}
onmouseup={(event) => onMiddleUp(event, tab.relPath)}
>
{#if tab.pinned}<svg class="pin" viewBox="0 0 24 24" aria-hidden="true">{@html actionIcons.pin}</svg>{/if}
{tab.name}{tab.contents !== tab.saved ? " •" : ""}
</button>
{#if tab.pinned}<span class="sr-only" id={`tab-pin-${tab.id}`}>Pinned tab</span>{/if}
<button
class="close"
type="button"
@@ -53,6 +236,7 @@
</div>
{/each}
</div>
<span class="sr-only" role="status">{announcement}</span>
<style>
.tabs {
@@ -64,21 +248,43 @@
}
.tab {
position: relative;
display: flex;
align-items: center;
max-width: 220px;
flex-shrink: 0;
border-radius: var(--rs-item-selection-radius) var(--rs-item-selection-radius) 0 0;
border-radius: var(--rs-item-selection-radius);
border-right: 1px solid var(--rs-border);
background: var(--rs-bg);
}
.tab.active {
background: var(--rs-bg-active);
box-shadow: inset 0 2px var(--rs-accent);
background: var(--rs-item-selection-bg);
box-shadow: inset 0 0 0 var(--rs-item-selection-width) var(--rs-item-selection-border);
}
.tab:hover { background: var(--rs-bg-active); }
.tab.dragging { opacity: 0.5; }
.tabs:has(.dragging) .tab > button:first-child { cursor: grabbing; }
.tab.drop-before::before, .tab.drop-after::after {
content: "";
position: absolute;
top: 3px;
bottom: 3px;
width: 3px;
border-radius: var(--rs-control-radius);
background: var(--rs-accent);
pointer-events: none;
}
.tab.drop-before::before { left: 0; }
.tab.drop-after::after { right: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.tab > button:first-child {
border-radius: var(--rs-control-radius);
touch-action: none;
user-select: none;
cursor: grab;
flex: 1;
min-width: 0;
border: 0;
@@ -96,6 +302,11 @@
color: var(--rs-text-strong);
}
.pin { width: 14px; height: 14px; margin-right: 6px; vertical-align: -2px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
:global(.tab-menu) { width: 244px; border-radius: var(--rs-control-radius); }
:global(.tab-menu button) { border-radius: var(--rs-control-radius); min-height: 30px; }
:global(.tab-menu .table-cell-submenu) { border-radius: var(--rs-control-radius); }
.close {
border: 0;
border-radius: var(--rs-control-radius);