Official
tabs
The tabbed detail pane beside the chat: files to edit and diffs (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install convergence/tabs@0.2.4
Permissions in 0.2.4
Files
views.ts8.1 KB
// The detail pane's strip and content trees, with effectful actions only in handlers.
// HTM whitespace is significant; keep these templates out of Prettier's HTML formatter.
import { ui, html as htm } from "convergence";
import * as kit from "convergence";
import type { EffectViewContext } from "convergence/effect";
import type { Tabs } from "./controller.ts";
import { activeTab, editorId, editorText, extension, isDirty, LANGUAGES, tabName } from "./tabs.ts";
import type { Tab } from "./tabs.ts";
// The shared surface looks from the runtime; an older runtime without them
// gets the same `elevation`, which its kernel already draws.
const RAISED = kit.RAISED ?? { elevation: "raised" };
const EDGED = kit.EDGED ?? { elevation: "edge" };
const FLAT = kit.FLAT ?? { elevation: "none" };
// Every tab strip in the app caps a tab at this width (DESIGN.md, "Tabs").
const TAB_MAX_WIDTH = 200;
// Layout for the templates below: a flex column, a row that centres its
// children across, a child that takes the free space (and may shrink below
// its content), the whole of the parent, the hover wash, the pointer of
// something clickable, and a child of a sideways strip that keeps its width.
const column = { display: "flex", flexDirection: "column" };
const row = { display: "flex", flexDirection: "row", alignItems: "center" };
const fill = { flexGrow: 1, flexShrink: 1, flexBasis: "0%", minWidth: 0, minHeight: 0 };
const full = { width: "100%", height: "100%" };
const HOVER = { background: "hover" };
const clickable = { group: "hover", cursor: "pointer" };
const keep = { flexGrow: 0, flexShrink: 0, flexBasis: "auto" };
// The close button and the unsaved mark lie over each other; the pointer
// on the tab swaps them.
const layer = {
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
...column,
justifyContent: "center",
alignItems: "center",
};
const hideOnHover = { groupHover: { group: "hover", visibility: "hidden" } };
const revealOnHover = { visibility: "hidden", groupHover: { group: "hover", visibility: "visible" } };
// The open tab is raised like the panes (the kernel's `elevation`). Every
// tab keeps the edge's width, so opening one never moves its title.
// A tab's icon sits as far from its left edge as from its top: the close
// button (1.5rem) sets the tab's height, the icon is 0.875rem, the tab's
// padding 0.25rem.
const TAB_LEAD = 15 * (0.25 + (1.5 - 0.875) / 2);
export function renderDetail(ctx: EffectViewContext<object>, actions: Tabs["Service"]) {
const { state } = actions;
// One line of tabs that scrolls sideways when they outgrow the pane.
function renderStrip() {
const reveal = Math.max(
0,
state.tabs.findIndex((t) => t.id === state.active),
);
return htm`
<div ...${row} id="detail-tabs" reveal=${reveal} gap="small" padding="small" paddingX="small" paddingY="small" overflowX="scroll">
${state.tabs.map((t) => htm`<div ...${keep}>${renderTab(t)}</div>`)}
</div>`;
}
function renderTab(t: Tab) {
const active = t.id === state.active;
const icon =
t.kind === "diff"
? "git-compare"
: t.kind === "terminal"
? "square-terminal"
: t.kind === "view"
? (t.icon ?? "app-window")
: "file";
const close = htm`<button id=${`close-${t.id}`} icon="x" variant="ghost" small tooltip="Close tab" onClick=${() => ctx.run(actions.requestClose(t.id))} />`;
// A long title (a subagent's task) is cut at the tab's width.
return htm`
<div ...${row} ...${clickable} gap="small" padding="small" paddingLeft=${TAB_LEAD} borderRadius="sm" maxWidth=${TAB_MAX_WIDTH}
background=${active ? "accent" : undefined} hover=${active ? undefined : HOVER}
...${active ? RAISED : FLAT}
onClick=${() => ctx.run(actions.select(t))}>
<icon name=${icon} file=${t.kind === "file" ? tabName(t) : undefined} small tone=${active ? "foreground" : "muted"} />
<text style="body" size="xs" bold=${active} tone=${active ? "foreground" : "muted"} truncate>${tabName(t)}</text>
${
isDirty(state.contents[t.id])
? htm`
<div ...${column} ...${keep} position="relative">
<div ...${layer} ...${hideOnHover}><div width=${6} height=${6} borderRadius="full" background="foreground/60" /></div>
<div ...${revealOnHover}>${close}</div>
</div>`
: close
}
</div>`;
}
// Asks before a tab with unsaved changes closes.
function renderClosePrompt() {
const tab = state.tabs.find((t) => t.id === state.closing);
if (!tab) return null;
const cancel = () => ctx.run(actions.answerClose("cancel"));
return htm`
<overlay id="tabs-close-prompt" open width=${380} keys=${{ escape: cancel }} onDismiss=${cancel}>
<div ...${column} gap="medium" padding="medium">
<text style="body">Do you want to save the changes you made to ${tabName(tab)}?</text>
<div ...${row} gap="small">
<spacer />
<button id="tabs-close-cancel" label="Cancel" variant="ghost" small onClick=${cancel} />
<button id="tabs-close-discard" label="Don't save" variant="ghost" small onClick=${() => ctx.run(actions.answerClose("discard"))} />
<button id="tabs-close-save" label="Save" small onClick=${() => ctx.run(actions.answerClose("save"))} />
</div>
</div>
</overlay>`;
}
function renderContent(tab: Tab) {
if (tab.kind === "terminal") {
return htm`<div ...${column} ...${fill} ...${full} gap="small" padding="small" paddingX="small"><terminal terminal=${tab.path} /></div>`;
}
if (tab.kind === "view") return htm`<view view=${tab.view} />`;
const content = state.contents[tab.id];
if (!content) return htm`<empty>Loading…</empty>`;
if (content.error) {
return htm`<div ...${column} gap="small" padding="medium"><text style="body" size="xs" tone="danger">${content.error}</text></div>`;
}
if (tab.kind === "diff") {
return htm`
<div ...${column} ...${fill} gap="small" padding="medium" overflowY="scroll">
${content.diff ? ui.diff(content.diff, tab.path) : htm`<text style="muted">No changes.</text>`}
</div>`;
}
const ext = extension(tab.path);
if (ext === "md" && tab.preview) {
return htm`<div ...${column} ...${fill} gap="small" padding="large" overflowY="scroll"><markdown id=${`md-${tab.id}`}>${editorText(content)}</markdown></div>`;
}
return htm`<editor id=${editorId(tab)} text=${editorText(content)} language=${LANGUAGES[ext] ?? "text"}
readOnly=${!!content.truncated}
onChange=${({ value }: { value: string }) => ctx.run(actions.edit(tab.id, value))}
onSave=${({ value }: { value: string }) => ctx.run(actions.save(tab, value))} />`;
}
const tab = activeTab(state);
if (!tab) return null;
const ext = extension(tab.path);
const markdown = tab.kind === "file" && ext === "md";
return htm`
<div ...${column} ...${fill} ...${full} padding="small">
${renderStrip()}
${
tab.kind === "terminal" || tab.kind === "view"
? null
: htm`
<div ...${row} gap="small" padding="small" paddingX="medium" paddingY="small">
<text style="mono" tone="muted" truncate>${tab.path}</text>
<spacer />
${
markdown
? htm`
<div ...${row} gap="small" padding="small">
<button id=${`preview-${tab.id}`} icon="eye" label="Preview" variant="ghost" small active=${tab.preview}
onClick=${() => {
tab.preview = true;
ctx.update();
}} />
<button id=${`editor-${tab.id}`} icon="code" label="Editor" variant="ghost" small active=${!tab.preview}
onClick=${() => {
tab.preview = false;
ctx.update();
}} />
</div>`
: null
}
</div>`
}
<div ...${column} ...${fill} gap="small" padding="small">${renderContent(tab)}</div>
${renderClosePrompt()}
</div>`;
}
No comments yet.