Files
RuneStone/src/lib/layout/SearchPane.svelte
T
Knotty Wyvern 0fdc991b2e Give way in narrow windows and use one icon for each action
Phase 5-6.

- Side panes give way when the window becomes narrow: the outline
  below 1100 pixels, the side pane below 800 pixels. They come back
  when the window grows. A pane that the user opens in a narrow window
  stays open, and the saved layout keeps the wide-window choice.
- One function has one icon. The SDK icon set gets duplicate, template,
  and tab. The reveal action has one label for each platform (Show in
  Explorer, Show in Finder, Show in file manager).
- The file menu follows one order: open, clipboard, organize, show and
  copy path, history, then Delete and Delete permanently in the error
  color after a separator. The tree copies paths through a Copy path
  submenu, as the tab menu does.
- The tree uses the shared chevron. Search hits come in groups by file,
  with counts and marked matches. Bookmarks show the name, the folder,
  and a remove button.
- All boolean settings use one switch design (src/lib/ui/switch.css).
  Browser settings apply at once, and the Save button is gone. The
  Settings window opens with the focus on its search.
- Targets of at least 24 pixels: the tab close button, outline rows,
  switches, and the settings search.
- Catalog rows in French, Japanese, and Simplified Chinese.
- Docs: UI-052, ui-design.md, and the user guide.

Tests: the new layout/panesAndSettings. Tests that closed or opened
panes by hand around narrow windows now rely on the new behavior, or
wait for the panes to give way before they reopen one. Tests that read
the old menu labels or the browser Save button use the new ones.
2026-10-28 21:31:36 -07:00

210 lines
5.8 KiB
Svelte

<script lang="ts">
import { t } from "$lib/i18n";
import IconButton from "$lib/ui/IconButton.svelte";
import { workspaceSearch, type SearchHit } from "$lib/workspace/api";
import type { TreeEntry } from "$lib/workspace/types";
type Props = {
onOpen: (entry: TreeEntry, line: number) => void;
searchRequest?: { text: string; id: number } | null;
};
let { onOpen, searchRequest }: Props = $props();
let query = $state("");
let hits = $state<SearchHit[]>([]);
let searched = $state("");
let error = $state<string | null>(null);
// Hits in groups by file, in the order of the first hit of each file.
const groups = $derived.by(() => {
const files = new Map<string, SearchHit[]>();
for (const hit of hits) files.set(hit.rel_path, [...files.get(hit.rel_path) ?? [], hit]);
return [...files].map(([path, items]) => ({ path, name: path.split("/").pop() ?? path, folder: path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : "", items }));
});
/** The parts of a hit line, with the parts that match the query marked. */
function parts(text: string, term: string) {
const lower = text.toLowerCase(), needle = term.toLowerCase();
if (!needle) return [{ text, match: false }];
const result: { text: string; match: boolean }[] = [];
let from = 0;
for (let index = lower.indexOf(needle); index >= 0; index = lower.indexOf(needle, index + needle.length)) {
if (index > from) result.push({ text: text.slice(from, index), match: false });
result.push({ text: text.slice(index, index + needle.length), match: true });
from = index + needle.length;
}
if (from < text.length) result.push({ text: text.slice(from), match: false });
return result;
}
let generation = 0;
$effect(() => { if (searchRequest) { query = searchRequest.text; void run(searchRequest.text); } });
async function run(value = query) {
const version = ++generation;
const q = value.trim();
if (!q) {
hits = [];
return;
}
try {
const results = await workspaceSearch(q);
if (version !== generation) return;
hits = results;
searched = q;
error = null;
} catch (err) {
if (version !== generation) return;
error = err instanceof Error ? err.message : String(err);
}
}
</script>
<div class="pane">
<div class="pane-label">{$t("Search")}</div>
<form
class="bar"
onsubmit={(event) => {
event.preventDefault();
void run();
}}
>
<input bind:value={query} placeholder={$t("Search files and notes")} />
<IconButton icon="search" label={$t("Search workspace")} type="submit" />
</form>
{#if error}
<p class="error">{error}</p>
{/if}
{#if searched}
<p class="summary" role="status">{hits.length === 1 ? $t("1 result") : $t("{count} results in {files} files", { count: hits.length, files: groups.length })}</p>
{/if}
<ul class="results">
{#each groups as group (group.path)}
<li class="file">
<div class="file-name" title={group.path}><span class="name">{group.name}</span>{#if group.folder}<span class="folder">{group.folder}</span>{/if}<span class="count">{group.items.length}</span></div>
<ul>
{#each group.items as hit (hit.line + hit.text)}
<li>
<button type="button" onclick={() => onOpen({ name: group.name, rel_path: hit.rel_path, kind: "file" }, hit.line)}>
<span class="line">{hit.line}</span>
<span class="text">{#each parts(hit.text, searched) as part}{#if part.match}<mark>{part.text}</mark>{:else}{part.text}{/if}{/each}</span>
</button>
</li>
{/each}
</ul>
</li>
{/each}
</ul>
</div>
<style>
.pane {
height: 100%;
display: flex;
flex-direction: column;
min-height: 0;
}
.pane-label {
padding: 6px 10px;
font-size: 11px;
letter-spacing: 0.06em;
text-transform: uppercase;
color: var(--rs-text-dim);
border-bottom: 1px solid var(--rs-border);
text-align: center;
}
.bar {
display: flex;
gap: 6px;
padding: 8px;
}
input {
flex: 1;
min-width: 0;
border-radius: 5px;
border: 1px solid var(--rs-border);
background: var(--rs-bg);
padding: 4px 8px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
.results {
padding: 0 0 12px;
overflow: auto;
}
.summary {
margin: 0 10px 6px;
color: var(--rs-text-dim);
font-size: 12px;
}
.file + .file { margin-top: 6px; }
.file-name {
display: flex;
align-items: baseline;
gap: 6px;
min-width: 0;
padding: 4px 10px;
}
.name { color: var(--rs-text-strong); font-weight: 600; white-space: nowrap; }
.folder { min-width: 0; overflow: hidden; color: var(--rs-text-dim); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.count { margin-left: auto; padding: 0 6px; border-radius: 9px; background: var(--rs-bg-active); color: var(--rs-text); font-size: 11px; }
button {
display: flex;
align-items: baseline;
gap: 8px;
width: 100%;
min-height: 24px;
border-radius: var(--rs-control-radius);
border: 0;
background: transparent;
text-align: left;
padding: 3px 10px 3px 18px;
}
button:hover {
background: var(--rs-bg-active);
}
.line {
flex: 0 0 auto;
min-width: 2ch;
color: var(--rs-text-dim);
font-size: 11px;
text-align: right;
}
.text {
min-width: 0;
color: var(--rs-text);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* A match uses the search match fill of the theme, so the text keeps its contrast. */
mark {
border-radius: 2px;
background: var(--rs-item-selection-bg);
box-shadow: inset 0 -2px 0 var(--rs-accent);
color: var(--rs-text-strong);
}
.error {
margin: 8px 10px;
color: var(--rs-error);
}
</style>