Lutra/ui/src/components/layout/MainLayout.vue
danny a8daf269fb M3.6: integrated terminal with PTY sessions and xterm.js sub-tabs
- crates/terminal: portable-pty sessions (ConPTY on Windows) with reader,
  writer and waiter threads, UTF-8 chunking, a 1 MiB output buffer with
  backpressure, a non-blocking input queue, resize, kill and exit codes;
  default shell pwsh -> powershell -> cmd on Windows, $SHELL or /bin/sh.
- Engine: terminal.create/input/resize/close, terminalOutput (capped per
  terminal and per pump, round-robin) and terminalExited through the pump;
  terminals close with the project and on app.quit.
- UI: Terminal tab in the bottom panel (tabs next to Find), xterm.js with
  the fit addon, sub-tabs, copy/paste, a focus rule that leaves plain Ctrl
  keys to the shell, terminal.open (Mod+Shift+T), --lt-terminal-* ANSI
  tokens in both themes (text colours AA) and a 4.5:1 minimum contrast.
- Tests: real-shell crate tests (UTF-8, backpressure), engine and TCP
  control tests, Vitest with a mocked xterm; live check on port 7936;
  docs and parity rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 21:10:37 +02:00

315 lines
9.2 KiB
Vue

<script setup lang="ts">
/**
* Layout of the main window: the left tool-window stripe, the left tool
* window, the editor area in the middle, the bottom panel under both
* (M2.4) and the status bar along the bottom.
*
* The editor area is a slot so the editor (M1.6) can replace the
* placeholder without touching the layout.
*
* The left stripe (M2.7) is a vertical tab list with Project and
* Structure. The left tool window shows one of them; clicking the shown
* one's tab hides the window, clicking the other switches to it. Both stay
* mounted while the window is open, so switching keeps their state. The
* layout supplies the UI commands `view.toggleProjectTree` (`Mod+1`) and
* `view.toggleStructure` (`Mod+2`): each shows its tool window, or hides
* the left window when that tool window is already shown.
*
* The bottom panel ({@link BottomPanel}) holds the Find and Terminal tool
* windows, as tabs in its header while both are shown. Find appears with
* the UI command `find.open` (`Mod+Alt+F`), Terminal with `terminal.open`
* (`Mod+Shift+T`, M3.6); both are supplied here so they work before the
* panel exists, and each makes its tool window the active tab. The panel
* collapses to its header; closing it hides the active tool window (the
* other one, if shown, becomes active). The Find store keeps the results
* and the Terminal store the running terminals while they are hidden.
*
* A slim header bar along the top (M3.9) holds the app menu
* ({@link AppMenu}); its `header` slot can replace it.
*/
import { FolderTree, ListTree } from "@lucide/vue";
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import { useFindStore } from "../../stores/find";
import { useTerminalStore } from "../../stores/terminal";
import FindToolWindow from "../find/FindToolWindow.vue";
import TerminalToolWindow from "../terminal/TerminalToolWindow.vue";
import ProjectToolWindow from "../project/ProjectToolWindow.vue";
import StatusBar from "../StatusBar.vue";
import StructureToolWindow from "../structure/StructureToolWindow.vue";
import AppMenu from "../menu/AppMenu.vue";
import BottomPanel from "./BottomPanel.vue";
import ToolWindow from "./ToolWindow.vue";
/** Width of the left tool window in CSS pixels. */
const leftWidth = ref(260);
/** Whether the left tool window is shown. */
const leftOpen = ref(true);
/** Height of the bottom panel's content in CSS pixels. */
const bottomHeight = ref(240);
/** Tool windows of the left side. */
type LeftTool = "project" | "structure";
/** The left tool window shown while {@link leftOpen} is set. */
const leftTool = ref<LeftTool>("project");
/** Stripe tabs in display order: id, title and icon. */
const leftTools = computed(() => [
{ id: "project" as const, title: t("project.toolWindow.title"), icon: FolderTree },
{ id: "structure" as const, title: t("structure.toolWindow.title"), icon: ListTree },
]);
/** Title of the shown left tool window. */
const leftTitle = computed(() => leftTools.value.find((tool) => tool.id === leftTool.value)?.title ?? "");
/**
* Shows a left tool window, or hides the left window when it is already
* shown.
*
* @param tool - the tool window.
*/
function toggleLeft(tool: LeftTool): void {
if (leftOpen.value && leftTool.value === tool) {
leftOpen.value = false;
return;
}
leftTool.value = tool;
leftOpen.value = true;
}
const commands = useCommandsStore();
const find = useFindStore();
const terminal = useTerminalStore();
/** Tool windows of the bottom panel. */
type BottomTool = "find" | "terminal";
/** The bottom tool window last chosen. */
const bottomTool = ref<BottomTool>("find");
/** Bottom tool windows that are shown, in tab order. */
const bottomTools = computed(() => {
const tools: { id: BottomTool; title: string }[] = [];
if (find.visible) tools.push({ id: "find", title: t("find.toolWindow.title") });
if (terminal.visible) tools.push({ id: "terminal", title: t("terminal.toolWindow.title") });
return tools;
});
/** The active bottom tool window: the chosen one if shown, else the first. */
const bottomActive = computed<BottomTool | null>(() => {
const tools = bottomTools.value;
return tools.some((tool) => tool.id === bottomTool.value) ? bottomTool.value : (tools[0]?.id ?? null);
});
/** Expansion of the panel, kept per tool window by its store. */
const bottomOpen = computed({
get: () => (bottomActive.value === "terminal" ? terminal.expanded : find.expanded),
set: (open: boolean) => {
if (bottomActive.value === "terminal") {
terminal.expanded = open;
} else {
find.expanded = open;
}
},
});
/**
* Makes a bottom tool window the active tab.
*
* @param id - the tool window id from the panel.
*/
function chooseBottom(id: string): void {
if (id === "find" || id === "terminal") {
bottomTool.value = id;
}
}
/** Hides the active bottom tool window. */
function closeBottom(): void {
if (bottomActive.value === "terminal") {
terminal.hide();
} else {
find.hide();
}
}
/** Withdraws the command handlers; filled while mounted. */
let withdraw: (() => void)[] = [];
onMounted(() => {
find.start();
terminal.start();
withdraw = [
commands.provide("view.toggleProjectTree", {
run: () => {
toggleLeft("project");
},
}),
commands.provide("find.open", {
run: (params) => {
bottomTool.value = "find";
return find.open(params);
},
}),
commands.provide("terminal.open", {
run: (params) => {
bottomTool.value = "terminal";
return terminal.open(params);
},
enabled: () => commands.isEnabled("terminal.create"),
}),
commands.provide("view.toggleStructure", {
run: () => {
toggleLeft("structure");
},
}),
];
});
onBeforeUnmount(() => {
for (const fn of withdraw) {
fn();
}
withdraw = [];
find.stop();
terminal.stop();
});
</script>
<template>
<div class="main-layout">
<header
class="main-layout__header"
data-test="main-header"
>
<slot name="header">
<AppMenu />
</slot>
</header>
<div class="main-layout__row">
<nav
class="main-layout__stripe"
role="tablist"
aria-orientation="vertical"
:aria-label="t('common.toolWindow.stripe')"
data-test="left-stripe"
>
<button
v-for="tool in leftTools"
:key="tool.id"
type="button"
role="tab"
class="main-layout__stripe-button"
:class="{ 'main-layout__stripe-button--active': leftOpen && leftTool === tool.id }"
:aria-selected="leftOpen && leftTool === tool.id"
:title="tool.title"
:aria-label="tool.title"
:data-test="`stripe-${tool.id}`"
@click="toggleLeft(tool.id)"
>
<component
:is="tool.icon"
:size="18"
/>
</button>
</nav>
<ToolWindow
v-if="leftOpen"
v-model:width="leftWidth"
v-model:open="leftOpen"
:title="leftTitle"
>
<ProjectToolWindow v-show="leftTool === 'project'" />
<StructureToolWindow v-show="leftTool === 'structure'" />
</ToolWindow>
<main
class="main-layout__editor"
data-test="main"
>
<slot />
</main>
</div>
<BottomPanel
v-if="bottomActive !== null"
v-model:height="bottomHeight"
v-model:open="bottomOpen"
:title="bottomTools.find((tool) => tool.id === bottomActive)?.title ?? ''"
:tabs="bottomTools.length > 1 ? bottomTools : []"
:active="bottomActive"
@update:active="chooseBottom"
@close="closeBottom"
>
<FindToolWindow v-show="bottomActive === 'find'" />
<TerminalToolWindow
v-if="terminal.visible"
v-show="bottomActive === 'terminal'"
/>
</BottomPanel>
<StatusBar />
</div>
</template>
<style scoped>
.main-layout {
display: flex;
flex-direction: column;
height: 100%;
}
.main-layout__header {
display: flex;
align-items: center;
flex: none;
height: 28px;
padding: 0 var(--lt-space-s, 4px);
background: var(--lt-bg);
border-bottom: 1px solid var(--lt-border);
}
.main-layout__row {
display: flex;
flex: 1;
min-height: 0;
}
.main-layout__stripe {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--lt-space-xs, 2px);
flex: none;
padding: var(--lt-space-s, 4px) var(--lt-space-xs, 2px);
background: var(--lt-bg);
border-right: 1px solid var(--lt-border);
}
.main-layout__stripe-button {
display: inline-flex;
align-items: center;
justify-content: center;
padding: var(--lt-space-xs, 2px);
color: var(--lt-fg-muted);
background: transparent;
border: none;
border-radius: var(--lt-radius, 4px);
cursor: pointer;
}
.main-layout__stripe-button:hover {
color: var(--lt-fg);
background: color-mix(in srgb, var(--lt-fg) 8%, transparent);
}
.main-layout__stripe-button--active {
color: var(--lt-accent);
background: color-mix(in srgb, var(--lt-accent) 18%, transparent);
}
.main-layout__stripe-button:focus-visible {
outline: 2px solid var(--lt-accent);
}
.main-layout__editor {
flex: 1;
min-width: 0;
display: flex;
align-items: center;
justify-content: center;
background: var(--lt-editor-bg);
}
</style>