M1.5: main window layout and project tree against mocked engine events

Left Project tool window (hide/show stripe, pointer and keyboard resize),
editor area placeholder and status bar. Project tree with lazy
project.children listings, WAI-ARIA keyboard navigation, Lucide icons,
dimmed ignored/excluded entries and refresh on TreeChanged. Empty state
runs project.openDialog. Pinia store fed only by engine events and
command results; events come through a local onEngineEvent adapter until
the desktop bridge (M1.4) lands. Lucide via @lucide/vue (the renamed
lucide-vue-next, ISC).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-14 19:24:23 +02:00
parent faadb05eb3
commit 003b7970e6
16 changed files with 1541 additions and 32 deletions

340
ui/src/stores/project.ts Normal file
View file

@ -0,0 +1,340 @@
/**
* State of the open project and its tree, for the project tool window.
*
* Fed only by engine events (`ProjectOpened`, `ProjectClosed`,
* `TreeChanged`) and by results of engine commands (`project.children`).
* Directory listings load lazily when a folder is first expanded. Selection
* and expansion are view state kept here so the tree survives remounts.
* Command failures are stored and shown, never thrown.
*/
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { run, type EngineError } from "../ipc";
import {
onEngineEvent,
type EngineEvent,
type ProjectRoot,
type TreeEntry,
} from "../components/project/engineEvents";
/** Engine command ids used by the project tool window. */
export const PROJECT_COMMANDS = {
/** Lists one directory: params `{ dir }`, result `TreeEntry[]`. */
children: "project.children",
/** Shows the folder picker and opens the chosen folder. */
openDialog: "project.openDialog",
/** Opens a file in an editor buffer: params `{ path }`. */
openFile: "file.open",
} as const;
/** Loading state of one directory listing. */
export interface DirState {
/** Entries in engine order, or null while not loaded yet. */
entries: TreeEntry[] | null;
/** Whether a `project.children` call is in flight. */
loading: boolean;
/** Error of the last `project.children` call, null if it succeeded. */
error: EngineError | null;
}
/** A visible line of the flattened tree. */
export type TreeRow =
| {
/** An entry. */
kind: "entry";
/** The entry. */
entry: TreeEntry;
/** Nesting depth, 0 for children of the root. */
depth: number;
/** Whether the entry is an expanded directory. */
expanded: boolean;
}
| {
/** Placeholder while a directory loads. */
kind: "loading";
/** Directory being loaded. */
dir: string;
/** Nesting depth of the placeholder. */
depth: number;
}
| {
/** A failed listing. */
kind: "error";
/** Directory whose listing failed. */
dir: string;
/** Nesting depth of the message. */
depth: number;
/** The error to show. */
error: EngineError;
};
/**
* Returns the parent of a project-relative path.
*
* @param path - a path such as `src/App.php`.
* @returns the parent (`src`), or `""` for top-level entries.
*/
export function parentOf(path: string): string {
const slash = path.lastIndexOf("/");
return slash < 0 ? "" : path.slice(0, slash);
}
/**
* Whether `path` is `dir` itself or lies inside it.
*
* @param path - the path to test.
* @param dir - a directory path; `""` is the root and contains everything.
*/
function isWithin(path: string, dir: string): boolean {
return dir === "" || path === dir || path.startsWith(`${dir}/`);
}
/** Store of the open project, its lazily loaded tree and the tree view state. */
export const useProjectStore = defineStore("project", () => {
/** The open project, null when no folder is open. */
const root = ref<ProjectRoot | null>(null);
/** Listings by directory path (`""` is the root). */
const dirs = ref<Record<string, DirState>>({});
/** Expanded directories. */
const expanded = ref<Set<string>>(new Set());
/** Path of the selected entry, null if none. */
const selected = ref<string | null>(null);
/** Error of the last open-folder or open-file command, null if none. */
const lastError = ref<EngineError | null>(null);
/** Latest request number per directory, to drop stale listings. */
const requests = new Map<string, number>();
/** Counter for {@link requests}. */
let nextRequest = 0;
/** Unsubscribe function of the event subscription, null if not started. */
let unsubscribe: (() => void) | null = null;
/** Clears everything that belongs to an open project. */
function reset(): void {
dirs.value = {};
expanded.value = new Set();
selected.value = null;
requests.clear();
}
/**
* Entries cached for a directory.
*
* @param dir - the directory path.
* @returns its last loaded entries, or null if never loaded.
*/
function cachedEntries(dir: string): TreeEntry[] | null {
return dirs.value[dir]?.entries ?? null;
}
/**
* Loads (or reloads) one directory listing through `project.children`.
* A failure is stored on the directory; earlier entries are kept so a
* failed refresh does not empty the tree.
*
* @param dir - project-relative directory, `""` for the root.
*/
async function loadChildren(dir: string): Promise<void> {
const request = ++nextRequest;
requests.set(dir, request);
dirs.value[dir] = { entries: cachedEntries(dir), loading: true, error: null };
const result = await run<TreeEntry[]>(PROJECT_COMMANDS.children, { dir });
if (requests.get(dir) !== request) {
return;
}
if (result.ok) {
const entries = result.value;
dirs.value[dir] = { entries, loading: false, error: null };
prune(dir, entries);
} else {
dirs.value[dir] = { entries: cachedEntries(dir), loading: false, error: result.error };
}
}
/**
* Drops cached listings, expansion and selection below `dir` for entries
* that disappeared from its new listing.
*
* @param dir - the reloaded directory.
* @param entries - its new entries.
*/
function prune(dir: string, entries: TreeEntry[]): void {
const alive = new Set(entries.map((entry) => entry.path));
const gone = (path: string): boolean => {
if (path === dir || !isWithin(path, dir)) {
return false;
}
const rest = dir === "" ? path : path.slice(dir.length + 1);
const first = rest.split("/")[0] ?? rest;
return !alive.has(dir === "" ? first : `${dir}/${first}`);
};
for (const path of Object.keys(dirs.value)) {
if (gone(path)) {
Reflect.deleteProperty(dirs.value, path);
requests.delete(path);
}
}
const keep = [...expanded.value].filter((path) => !gone(path));
if (keep.length !== expanded.value.size) {
expanded.value = new Set(keep);
}
if (selected.value !== null && gone(selected.value)) {
selected.value = null;
}
}
/**
* Expands a directory, loading its listing on first expansion.
*
* @param dir - the directory path.
*/
async function expand(dir: string): Promise<void> {
expanded.value.add(dir);
const state = dirs.value[dir];
if (!state || (state.entries === null && !state.loading)) {
await loadChildren(dir);
}
}
/**
* Collapses a directory; its listing stays cached.
*
* @param dir - the directory path.
*/
function collapse(dir: string): void {
expanded.value.delete(dir);
}
/**
* Expands a collapsed directory or collapses an expanded one.
*
* @param dir - the directory path.
*/
async function toggle(dir: string): Promise<void> {
if (expanded.value.has(dir)) {
collapse(dir);
} else {
await expand(dir);
}
}
/**
* Selects an entry.
*
* @param path - the entry path, or null to clear the selection.
*/
function select(path: string | null): void {
selected.value = path;
}
/**
* Runs the open-folder command. The engine reports the opened project
* with a `ProjectOpened` event; cancelling the picker is not an error.
*/
async function openFolder(): Promise<void> {
const result = await run(PROJECT_COMMANDS.openDialog);
lastError.value = result.ok ? null : result.error;
}
/**
* Asks the engine to open a file in an editor buffer. The editor learns
* about the buffer from engine events.
*
* @param path - project-relative path of the file.
*/
async function openFile(path: string): Promise<void> {
const result = await run(PROJECT_COMMANDS.openFile, { path });
lastError.value = result.ok ? null : result.error;
}
/**
* Applies one engine event to the store.
*
* @param event - the event; kinds the project view does not use are ignored.
*/
function handleEvent(event: EngineEvent): void {
switch (event.type) {
case "ProjectOpened":
reset();
root.value = event.root;
lastError.value = null;
void loadChildren("");
break;
case "ProjectClosed":
reset();
root.value = null;
break;
case "TreeChanged":
if (root.value !== null && dirs.value[event.dir]?.entries) {
void loadChildren(event.dir);
}
break;
default:
break;
}
}
/** Subscribes to engine events; calling it again has no effect. */
function start(): void {
unsubscribe ??= onEngineEvent(handleEvent);
}
/** Ends the event subscription. */
function stop(): void {
unsubscribe?.();
unsubscribe = null;
}
/**
* The tree flattened into visible rows: entries of the root and of every
* expanded directory, with a loading or error row where a listing is
* pending or failed.
*/
const rows = computed<TreeRow[]>(() => {
const out: TreeRow[] = [];
const visit = (dir: string, depth: number): void => {
const state = dirs.value[dir];
if (!state) {
return;
}
if (state.error) {
out.push({ kind: "error", dir, depth, error: state.error });
}
if (state.entries === null) {
if (state.loading) {
out.push({ kind: "loading", dir, depth });
}
return;
}
for (const entry of state.entries) {
const open = entry.kind === "dir" && expanded.value.has(entry.path);
out.push({ kind: "entry", entry, depth, expanded: open });
if (open) {
visit(entry.path, depth + 1);
}
}
};
if (root.value !== null) {
visit("", 0);
}
return out;
});
return {
root,
dirs,
expanded,
selected,
lastError,
rows,
loadChildren,
expand,
collapse,
toggle,
select,
openFolder,
openFile,
handleEvent,
start,
stop,
};
});