# File tree and file management

The sidebar and the central Files page share one tree on [`@pierre/trees`](https://github.com/pierrecomputer/pierre/tree/main/packages/trees) 1.0.0-beta.6. Pierre owns rows, hierarchy, selection, focus, virtual scrolling, sticky ancestors and keyboard navigation; OpenProgram owns filesystem state. Row height 30 pixels, density 1.

1. **Path** Breadcrumb; copy button at the right end
2. **Tools** Search, new file, new folder, refresh, sort, info
3. **Search** Input, opened on demand
4. **Header** Name, Size, Date Modified, Kind
5. **Rows** Name: remaining width, middle truncation Size 100 px, Date Modified 200 px, Kind 140 px Below 720 px: Name and Size only

## Columns

| Aspect | Behaviour |
| --- | --- |
| Width | A `ResizeObserver` measures the viewport and sets the Name width and visible columns; it disconnects on unmount. Rows never outgrow the panel and nothing scrolls sideways, as in Codex's tree, so Pierre's selection, focus ring and name editor draw whole without overrides. |
| Header | Stays visible during vertical scrolling and shares its column boundaries with the rows. |
| Cells | Pierre has no sortable columns. Three text parts through `renderRowDecoration`, styled with `unsafeCSS`; parts that do not fit are clipped. No table framework, no per-file metadata request. |
| Date Modified | `mtime` in epoch seconds from the directory and search responses, shown in the current language and local timezone. Blank for missing timestamps and synthetic search ancestors. |
| Kind | Folder; uppercase extension plus file; File when there is no extension. |
| Sorting | Directory headers toggle the per-project server sort preference, keeping folders-first and the hidden and ignored filters. Search keeps relevance order and its headers do nothing. |
| Deep folders | Indentation is capped inside Name, leaving room for the icon and filename and keeping columns aligned. |
| Rename | Inline rename keeps Pierre's decoration suppression. |
| Out of scope | Column reordering, manual resizing, extra metadata fields, filesystem watchers. |

## Appearance

| Element | Rule |
| --- | --- |
| Folders | The arrow is remapped through `icons.remap` to a sprite with the sidebar's Solar folder; expanded state picks closed or open. Folders never rotate. |
| Rows | 6 pixels in from both panel edges, 6 pixels of padding inside. No extra vertical padding. Pierre's indent guides are always shown; no fixed indentation or custom connectors. |
| Scrollbar | The app's overlay thumb takes no width, so rows reach the panel's right edge. |
| Selection | Selected rows have no border. Keyboard focus stays visible on unselected rows. |
| Toolbar | 8-pixel side margins, shared with the path row. No divider between toolbar and list. Existing buttons and rounded menus. |

## Path row

| Aspect | Behaviour |
| --- | --- |
| Component | shadcn Breadcrumb, radix-nova style, the panel's 14-pixel text, one line. Nova's chevrons sit in the list's 6-pixel gap. |
| Colour | Ancestors muted, foreground on hover; current item foreground. |
| Overflow | Ancestors that do not fit fold into Nova's ellipsis, which opens a menu of them. Spare room shows part of one more ancestor. No tooltips repeating the name. |
| Click | Locates the path among loaded entries and expands its ancestors, with no re-listing or size recount. Queries only if the target is missing. |
| Copy button | Copies the absolute path; a checkmark shows for 1.5 seconds on success. |
| Context menu | Relative path, absolute path or name of the clicked location. |

## Listing and search

| Aspect | Behaviour |
| --- | --- |
| Listing | `project_file_tree`: lazy per directory, paginated, sorted on the server. Sort by name, type, modification time or size, with folders-first. Framework comparators keep backend order and never revert to alphabetical. |
| Pagination | The next page loads near the end of a directory's loaded entries, with no pagination button. |
| Search | Backend permission scope and pagination. Results use Pierre's hierarchy; highlight mode keeps directories. |
| Matches | The backend decides fuzzy matches, shown by match markers and the native current-item selection. Literal queries use framework text highlighting. |
| Reveal | Controllers expand and scroll; nothing depends on handwritten row DOM. |

## File and folder sizes

| Case | Shown |
| --- | --- |
| File | Byte count. |
| Visible folder | One `project_folder_size` request per folder (`measure`): the saved sample, or else one walk of at most a second that the server saves, finished or not. Asked again only after Refresh or a Files change, never chunk by chunk. A shared queue runs two at once. States: cached, scanning, complete, partial, canceled, error. Search-excluded folders such as `.cache` and `node_modules` still have metadata and sizes. |
| In the list | The number alone, without `≈` or `≥`. |
| Still counting | The existing number keeps updating and slowly varies in grey brightness; normal again when counting stops. No added text or icons. |
| No value yet | Blank, no dash. |
| Details | Estimates, lower bounds, timestamp and completeness. Recalculate and Continue are the only chunked counts. |

### Saved samples

| Aspect | Behaviour |
| --- | --- |
| Reuse | Finished samples survive view remounts and worker restarts and display without recursive I/O. At most 256, in the default profile state. Size sorting keeps the recent-complete-sample rule. |
| New scan | Only the first view of a folder without a sample, Refresh, a Files change and Recalculate. |
| Invalidation | A successful Files change removes affected ancestor and subtree samples from memory and disk and cancels affected active scans, including overlapping project roots. |
| Key | Validated absolute path. Device and inode must also match, so a rebound project or replaced directory cannot reuse another directory's bytes. |
| Incomplete scans | Stay incomplete and keep their skipped-entry record. A walk cut off by the time limit is saved as a lower bound, so the next view shows it without walking. Active scan tokens are never saved. |
| Stale samples | Nested edits can outdate a sample; Details shows its time and completeness. No watcher, recursive freshness check, content read or new profile. |
| Storage | The existing atomic JSON writer: private temporary file, atomic replace. A corrupt or unavailable cache falls back to scanning. |

**Why not another store**

| Option | Result |
| --- | --- |
| [Python sqlite3](https://docs.python.org/3/library/sqlite3.html) | Durable transactions and concurrent connections, not needed for a bounded cache owned by one worker. No schema or dependency added. |
| Pierre tree, React queue | Kept for virtualization and the two visible-folder jobs. Neither supplies durable server-side samples. |

## Creating, renaming and menus

| Action | Behaviour |
| --- | --- |
| New entry | Named in the tree: its folder gains a placeholder row with an empty name editor and the default name as a dim hint. |
| Commit | Enter or leaving the editor creates it under the typed name. Escape, or leaving with nothing typed, drops the row. Existing forms validate before submitting. |
| Context menu | Details, reveal in system, new file, new folder, copy path, cut, copy, paste, Move to, rename, delete with confirmation. |
| Several rows | Cut, copy, move and delete act on all of them and the menu names the count; rename needs one row. |
| Selection | Cmd/Ctrl-click toggles a row, Shift-click selects a range. Opening a tab or revealing a file elsewhere replaces the selection with that row. |

| Key | Action |
| --- | --- |
| `Cmd/Ctrl+C`, `Cmd/Ctrl+X` | Copy, cut |
| `Cmd/Ctrl+V` | Paste into the selected folder, or the selected file's folder |
| `Cmd+Backspace`, `Delete` | Delete, after confirmation |
| `F2` | Rename |
| `Cmd/Ctrl+Shift+M` | Move to |

## Moving

| Aspect | Behaviour |
| --- | --- |
| Drag | Pierre's drag and drop onto a folder or the root; a hovered folder opens after 600 ms. |
| Carried out by | The tree shows the move at once; the same server rename as Cut and Paste does it, with draft protection and open-tab retargeting. A refused move re-lists the tree. |
| Undo | Every move shows a toast with Undo, which moves the entries back. |
| Skipped | A move onto the same parent or into the moved folder itself. |

### Move to

A shadcn Command dialog that reads its input as a path. Goes through the same server rename and Undo toast; the chosen folder opens in the tree.

| Input | Listed |
| --- | --- |
| Nothing | Project root, the five folders moved to most recently in this project, top-level folders. |
| `folder/name` | Subfolders of the part before the last slash, from `project_file_tree`, filtered by the rest, names starting with it first. Folders list first, so reading stops at the first file. One listing per level, so any project size works. Tab steps into the highlighted folder. |
| `name` | Also `project_file_search` with `type: "dir"` for hits elsewhere; none past the search's snapshot limit. |
| A path naming no folder | Offers to create it, missing parents included, and move into it. |

The current folder and the moved folders are disabled.

## Files from the desktop

| Aspect | Behaviour |
| --- | --- |
| Destination | The folder under the pointer, the folder of the file under it, or the project root. The tree outlines itself and a chip names the destination. |
| Transfer | The browser reads the dropped files and streams each to `POST /api/files/upload` with a project-relative path. No local path is sent; nothing outside the project is opened. |
| Folders | Arrive as their files, with missing parents created. |
| Existing name | Never replaced: the upload refuses it and a toast counts what was left unchanged. |
| Limit | 1 GiB per file. |
| Composer | The drop stays in the tree and does not reach the attachment drop. |

## Refresh and errors

| Situation | Behaviour |
| --- | --- |
| Window regains focus | Re-lists open folders, at most every three seconds, without recounting sizes. |
| Any refresh | Keeps the selected path, expanded directories and loaded pages; old rows stay while it is pending. |
| Collapsed parent | Stays closed even if descendants were expanded; reopening restores them. |
| Directory or page fails | Automatic retries stop. The path and an explicit Retry show, never fictitious entries. |
| Project folder unavailable | The reason and the repair replace the listing: confirm the folder at the recorded place, or Locate. See [project location](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/workspace/project-location.html). |
| Contracts kept | Workspace scope, cancellation, stale-response isolation, draft protection, server-side idempotent changes, metadata and size-cache permissions. The framework never touches file contents or decides permissions. |

**Implementation status**

| Area | Checked |
| --- | --- |
| Columns | Real `FileTree` and `PierreFileTree` in Chromium: default Name and Size, four-column alignment, wide view, sticky ancestors, ten-level indentation at narrow widths, date and kind values, sort requests, preference preservation, search relevance, vertical viewport restoration. |
| Saved sizes | Through `project_folder_size`: a finished sample survives a fresh load with no recursive walk; corrupt storage leaves metadata queries usable; incomplete samples stay incomplete; replaced directories do not reuse samples. Through Files: reopening both views and Details starts no scan for a cached sample; Refresh and Recalculate still update bytes through the shared queue; changes invalidate saved ancestors and root scans. |
| App, dark theme | Expand and collapse, virtual scrolling, sizes, file tabs, native context menus, sort menus, path preservation after refresh, nested-parent collapse, automatic pagination. Component regression tests cover refresh after a parent collapse. |
| Not checked | Separate manual pass in light mode. |

File-type icons: [icon design](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/visual-system/file-type-icons.html). File contents, drafts and recovery: [file operations](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/operations/file-management.html). Pierre's Apache 2.0 and icon MIT licenses ship with the product.
