Files
RuneStone/src/lib/layout/SettingsDialog.svelte
T

204 lines
15 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import PluginManager from "$lib/plugins/PluginManager.svelte";
import BrowserSlot from "$lib/plugins/BrowserSlot.svelte";
import PluginSettings from "$lib/plugins/PluginSettings.svelte";
import { installedPlugins, pluginTools } from "$lib/plugins/external";
import { pluginHost } from "$lib/plugins/registry";
import { onMount, untrack } from "svelte";
import Icon from "$lib/ui/Icon.svelte";
import IconButton, { type IconName } from "$lib/ui/IconButton.svelte";
import { DEFAULT_SETTINGS, MIN_AUTOSAVE_SECONDS, MAX_AUTOSAVE_SECONDS, validAutosaveSeconds, type AppSettings } from "$lib/workspace/settings";
import { version, license } from "../../../package.json";
let { settings, help = false, standalone = false, windowError = "", onSave, onClose }: {
settings: AppSettings;
help?: boolean;
standalone?: boolean;
windowError?: string;
onSave: (value: AppSettings) => void;
onClose: () => void;
} = $props();
type Section = string;
const sections: { id: Section; label: string; icon: IconName; keywords: string }[] = [
{ id: "about", label: "Help and about", icon: "help", keywords: "help about version shortcuts keyboard guide" },
{ id: "general", label: "General", icon: "settings", keywords: "auto-save autosave save interval seconds files" },
{ id: "plugins", label: "Plug-ins", icon: "puzzle", keywords: "plugins extensions community" },
];
let section = $state<Section>(untrack(() => help ? "about" : "general"));
let query = $state("");
let seconds = $state<number | undefined>(untrack(() => settings.autosaveSeconds));
let message = $state("");
let error = $state("");
let dialog: HTMLDialogElement;
const browserEnabled = $derived.by(() => { void $pluginHost; return !!pluginHost.get("browser"); });
const pluginSections = $derived($pluginTools.filter(tool => tool.surface === "settings").map(tool => ({
id: `plugin:${tool.owner}:${tool.id}`, owner: tool.owner, view: tool.id,
label: tool.title, icon: "settings" as const,
keywords: `${$installedPlugins.plugins.find(row => row.manifest.id === tool.owner)?.manifest.name ?? tool.owner} settings configuration`,
})));
const matches = $derived([...sections, ...(browserEnabled ? [{ id: "browser" as const, label: "Browser", icon: "globe" as const, keywords: "browser profiles search cookies cache web privacy" }] : []), ...pluginSections].filter(item => `${item.label} ${item.keywords}`.toLowerCase().includes(query.trim().toLowerCase())));
const selected = $derived(matches.find(item => item.id === section) ?? matches[0]);
const selectedPlugin = $derived(pluginSections.find(item => item.id === selected?.id));
$effect(() => { seconds = settings.autosaveSeconds; });
onMount(() => {
const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
dialog.showModal();
return () => { dialog.close(); previous?.focus(); };
});
function apply(value: AppSettings) {
error = "";
try { onSave(value); message = "Changes saved."; }
catch { error = "Could not save these settings. Your previous settings still apply. Try again."; message = ""; }
}
function changeInterval() {
if (!validAutosaveSeconds(seconds)) {
error = "Choose a whole number from 1 to 300 seconds.";
message = "";
return;
}
apply({ ...settings, autosaveSeconds: seconds });
}
function select(next: Section) { section = next; error = ""; message = ""; seconds = settings.autosaveSeconds; }
function trapFocus(event: KeyboardEvent) {
if (event.target instanceof Element && event.target.closest("dialog") !== dialog) return;
if (event.key !== "Tab") return;
const controls = [...dialog.querySelectorAll<HTMLElement>("button:not(:disabled), input:not(:disabled), select:not(:disabled), textarea:not(:disabled), a[href], summary, [tabindex]:not([tabindex='-1'])")]
.filter(element => element.getClientRects().length > 0);
const first = controls[0], last = controls.at(-1);
if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last?.focus(); }
else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first?.focus(); }
}
</script>
<dialog bind:this={dialog} class:help class:standalone aria-labelledby="settings-title" onkeydown={trapFocus} oncancel={event => { if (event.target === dialog) { event.preventDefault(); onClose(); } }}>
<header>
<h1 id="settings-title">{help ? "Help and about" : "Settings"}</h1>
<IconButton icon="close" label={help ? "Close help" : "Close settings"} onclick={onClose} />
</header>
<div class="body">
{#if !help}
<aside>
<label class="search"><Icon icon="search" /><input aria-label="Search settings" placeholder="Search settings…" bind:value={query} /></label>
<p class="nav-heading">Preferences</p>
<nav aria-label="Settings categories">
{#each matches as item}
<button class:active={selected?.id === item.id} aria-current={selected?.id === item.id ? "page" : undefined} onclick={() => select(item.id)}><Icon icon={item.icon} />{item.label}</button>
{/each}
</nav>
<p class="scope">For all workspaces on this device.</p>
</aside>
{/if}
<div class="content">
{#if windowError}<p class="error" role="alert">{windowError}</p>{/if}
{#if !selected}
<h2>No settings found</h2>
<p>Try “save”, “plug-ins”, or “help”.</p>
<button class="text-button" onclick={() => query = ""}>Clear search</button>
{:else if selected.id === "general"}
<h2>General</h2>
<p class="intro">Choose how RuneStone saves your work.</p>
<h3>Files</h3>
<div class="card">
<div class="setting-row">
<div><label for="autosave-enabled">Auto-save</label><p id="autosave-description">Save changed notes and scripts, including background tabs.</p></div>
<input id="autosave-enabled" class="switch" type="checkbox" role="switch" aria-describedby="autosave-description" checked={settings.autosaveEnabled} onchange={event => { apply({ ...settings, autosaveEnabled: event.currentTarget.checked }); event.currentTarget.checked = settings.autosaveEnabled; }} />
</div>
<div class="setting-row">
<div><label for="autosave-seconds">Auto-save interval</label><p id="interval-description">Time between saves, even while you type. Choose 1–300 seconds.</p></div>
<div class="interval"><input id="autosave-seconds" type="number" min={MIN_AUTOSAVE_SECONDS} max={MAX_AUTOSAVE_SECONDS} step="1" required disabled={!settings.autosaveEnabled} aria-describedby="interval-description" aria-invalid={!!error} bind:value={seconds} onchange={changeInterval} /><span>seconds</span></div>
</div>
</div>
<p class="note">{settings.autosaveEnabled ? "Only changed files are saved." : "Auto-save is off. Use Ctrl+S to save your changes."} Disk conflicts keep your edits for review.</p>
<div class="section-footer"><button class="text-button" onclick={() => { seconds = DEFAULT_SETTINGS.autosaveSeconds; apply({ ...DEFAULT_SETTINGS }); }}>Restore defaults</button><span>Default: every 5 seconds</span></div>
{#if error}<p class="error" role="alert">{error}</p>{/if}
<p class="saved" role="status">{message}</p>
{:else if selected.id === "plugins"}
<PluginManager />
{:else if selected.id === "browser"}
<BrowserSlot kind="settings" />
{:else if selectedPlugin}
{#key selectedPlugin.id}<PluginSettings owner={selectedPlugin.owner} id={selectedPlugin.view} />{/key}
{:else}
<div class="brand"><img src="/runestone.svg" alt="" width="88" height="88" /><h2>RuneStone</h2><p>Version {version}</p></div>
<div class="card help-card">
<details open><summary><Icon icon="book-open" /> Get started</summary><div class="help-copy"><p>Use the folder button to open a workspace or create one. Your notes and scripts stay as ordinary files on disk.</p><p>Use the + beside the tabs for a new tab. Press Ctrl+N to create a file, or Ctrl+P to find one.</p><p>Press Ctrl+E to switch between writing and source editing. Use the terminal button to work with a shell beside your files.</p></div></details>
<details><summary><Icon icon="save" /> Save your work</summary><div class="help-copy"><p>Auto-save saves changed files at the interval in Settings → General. Ctrl+S saves the active file immediately.</p><p>If another app changes a file, RuneStone keeps your edits and offers both versions for review.</p></div></details>
<details><summary><Icon icon="code" /> Keyboard shortcuts</summary><dl><div><dt>Open a workspace</dt><dd><kbd>Ctrl+O</kbd></dd></div><div><dt>Find a file</dt><dd><kbd>Ctrl+P</kbd></dd></div><div><dt>Save the active file</dt><dd><kbd>Ctrl+S</kbd></dd></div><div><dt>Search the workspace</dt><dd><kbd>Ctrl+Shift+F</kbd></dd></div><div><dt>Open settings</dt><dd><kbd>Ctrl+,</kbd></dd></div><div><dt>Help and about</dt><dd><kbd>F1</kbd></dd></div></dl></details>
</div>
<p class="about-note">RuneStone is under development. Maintained by kwyvern-gh.</p>
{/if}
</div>
</div>
</dialog>
<style>
dialog { width: min(920px, calc(100vw - 32px)); height: min(650px, calc(100dvh - 32px)); max-width: none; max-height: none; padding: 0; background: var(--rs-bg); color: var(--rs-text); border: 1px solid var(--rs-border); border-radius: 12px; box-shadow: 0 24px 80px #0008; overflow: hidden; }
dialog::backdrop { background: #0008; }
dialog.standalone { width: 100vw; height: 100dvh; margin: 0; border: 0; border-radius: 0; box-shadow: none; }
dialog.help { width: min(650px, calc(100vw - 32px)); }
header { height: 48px; padding: 0 14px 0 20px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--rs-border); background: var(--rs-bg-elevated); }
h1 { font-size: 14px; font-weight: 600; margin: 0; }
.body { display: flex; height: calc(100% - 48px); }
aside { width: 220px; flex-shrink: 0; padding: 18px 12px; background: var(--rs-bg-elevated); border-right: 1px solid var(--rs-border); display: flex; flex-direction: column; overflow: auto; }
.search { display: flex; gap: 7px; align-items: center; background: var(--rs-bg-active); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); padding: 8px; color: var(--rs-text-dim); }
.search:focus-within { outline: 2px solid var(--rs-accent); }
.search input { width: 100%; min-width: 0; border: 0; padding: 0; outline: none; background: transparent; color: var(--rs-text); }
.nav-heading { margin: 24px 10px 8px; font-size: 11px; color: var(--rs-text-dim); }
nav { display: flex; flex-direction: column; gap: 4px; }
nav button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px; border: 0; border-radius: var(--rs-control-radius); background: transparent; text-align: left; }
nav button:hover { background: var(--rs-bg-active); }
nav button.active { background: var(--rs-item-selection-bg); box-shadow: inset 0 0 0 var(--rs-item-selection-width) var(--rs-item-selection-border); border-radius: var(--rs-item-selection-radius); }
.scope { margin: auto 10px 0; padding-top: 24px; color: var(--rs-text-dim); font-size: 12px; }
.content { padding: 30px 32px; flex: 1; min-width: 0; overflow-y: auto; }
h2 { margin: 0; font-size: 23px; font-weight: 600; color: var(--rs-text-strong); }
h3 { margin: 28px 0 12px; font-size: 14px; font-weight: 600; }
p { color: var(--rs-text-dim); line-height: 1.6; }
.intro { margin: 7px 0 24px; }
.card { padding: 0 20px; background: var(--rs-bg-elevated); border: 1px solid var(--rs-border); border-radius: 10px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 24px; padding: 22px 0; }
.setting-row + .setting-row { border-top: 1px solid var(--rs-border); }
.setting-row label { font-weight: 500; }
.setting-row p { margin: 5px 0 0; font-size: 12px; }
.switch { appearance: none; width: 38px; height: 22px; flex: 0 0 38px; margin: 0; border-radius: 20px; background: var(--rs-bg-active); border: 1px solid var(--rs-text-dim); cursor: pointer; }
.switch::before { content: ""; display: block; width: 16px; height: 16px; margin: 2px; background: var(--rs-text); border-radius: 50%; transition: transform .12s; }
.switch:checked { background: var(--rs-accent); border-color: var(--rs-accent); }
.switch:checked::before { transform: translateX(16px); background: var(--rs-bg); }
.interval { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; font-size: 11px; color: var(--rs-text-dim); }
.interval input { width: 84px; padding: 9px; border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); background: var(--rs-bg); color: var(--rs-text); font-size: 14px; }
.interval input:disabled { opacity: .45; }
.note, .section-footer, .about-note { font-size: 12px; }
.note { margin: 16px 0 24px; }
.section-footer { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; color: var(--rs-text-dim); }
.text-button { border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); background: var(--rs-bg-elevated); padding: 8px 12px; color: var(--rs-text); }
.text-button:hover { background: var(--rs-bg-active); }
.saved { font-size: 12px; color: var(--rs-ok); min-height: 20px; }
.error { color: var(--rs-error); }
.brand { text-align: center; margin-bottom: 26px; }
.brand h2 { font-size: 28px; margin-top: 4px; }
.brand p { margin: 6px 0; font-size: 12px; }
.help-card details + details { border-top: 1px solid var(--rs-border); }
summary { display: flex; align-items: center; gap: 12px; padding: 18px 0; cursor: pointer; list-style: none; border-radius: var(--rs-control-radius); }
summary::after { content: "+"; margin-left: auto; color: var(--rs-text-dim); }
details[open] summary::after { content: "−"; }
.help-copy, dl { margin: 0 0 18px 28px; font-size: 12px; }
.help-copy p:first-child { margin-top: 0; }
dl div { display: flex; justify-content: space-between; gap: 12px; margin: 10px 0; }
dd { margin: 0; }
kbd { font-family: var(--rs-font-mono); color: var(--rs-text-dim); }
.about-note { text-align: center; margin: 20px 0 0; }
@media (max-width: 660px) {
.body { flex-direction: column; }
aside { width: 100%; padding: 12px; border-right: 0; border-bottom: 1px solid var(--rs-border); flex-shrink: 0; }
nav { flex-direction: row; margin-top: 10px; }
nav button { width: auto; flex: 1; padding: 8px; gap: 6px; }
.nav-heading, .scope { display: none; }
.content { padding: 24px 18px; }
.setting-row { gap: 14px; }
}
</style>