Files
RuneStone/src/lib/browser/EngineSetup.svelte
T
2026-11-16 04:21:21 -07:00

79 lines
5.9 KiB
Svelte

<script lang="ts">
import { t } from "$lib/i18n";
import { confirm } from "@tauri-apps/plugin-dialog";
import { openUrl } from "@tauri-apps/plugin-opener";
import Icon from "$lib/ui/Icon.svelte";
import { engine, installEngine, installEngineFile, cancelEngine, removeEngine, megabytes } from "./engine";
// `tab` fills a browser tab while the engine is missing. `settings` is the Engine section of Settings → Browser.
let { variant, url = "" }: { variant: "tab" | "settings"; url?: string } = $props();
let failure = $state("");
const status = $derived($engine);
const phase = $derived(status?.state ?? "missing");
const busy = $derived(phase === "downloading" || phase === "installing");
const error = $derived(failure || status?.error || "");
const web = $derived(variant === "tab" && /^https?:\/\//i.test(url));
async function run(action: () => Promise<unknown>) {
failure = "";
try { await action(); } catch (reason) { failure = String(reason); }
}
async function remove() {
const accepted = await confirm($t("Remove the browser engine? Open browser tabs close. Your profiles, bookmarks, and history stay."), { title: $t("Remove browser engine"), kind: "warning", okLabel: $t("Remove"), cancelLabel: $t("Cancel") });
if (accepted) await run(removeEngine);
}
</script>
<section class="engine {variant}" aria-labelledby={`engine-${variant}`} data-engine-state={phase}>
{#if variant === "tab"}<span class="mark"><Icon icon="globe" /></span>{/if}
<h3 id={`engine-${variant}`}>{$t("Browser engine")}</h3>
{#if phase === "installed"}
<p>{$t("Version {version} is installed.", { version: status?.version ?? "" })}</p>
{:else if phase === "development"}
<p>{$t("This development build uses the engine in src-tauri/browser-engine.")}</p>
{:else if phase === "unavailable"}
<p>{$t("No browser engine is available for this platform yet.")}</p>
{:else if phase === "downloading"}
<p role="status">{$t("Downloading the browser engine: {received} of {total}.", { received: megabytes(status?.received ?? 0), total: megabytes(status?.bytes ?? 0) })}</p>
<progress max={status?.bytes || 1} value={status?.received ?? 0} aria-label={$t("Download progress")}></progress>
{:else if phase === "installing"}
<p role="status">{$t("Installing the browser engine…")}</p>
<progress aria-label={$t("Installation progress")}></progress>
{:else if phase === "update"}
<p>{$t("This RuneStone version needs browser engine {version}. The download is {size}.", { version: status?.version ?? "", size: megabytes(status?.bytes ?? 0) })}</p>
{:else}
<p>{$t("The built-in browser needs its engine. The engine is a separate download of {size}.", { size: megabytes(status?.bytes ?? 0) })}</p>
{/if}
{#if error && !busy}<p class="error" role="alert"><Icon icon="alert" /><span>{$t("The browser engine could not be installed: {reason}", { reason: error })}</span></p>{/if}
<div class="actions">
{#if busy}
<button type="button" disabled={phase === "installing"} onclick={() => void run(cancelEngine)}><Icon icon="close" />{$t("Cancel")}</button>
{:else if phase === "missing" || phase === "update"}
<button type="button" class="primary" onclick={() => void run(installEngine)}><Icon icon={error ? "refresh" : "download"} />{error ? $t("Retry") : phase === "update" ? $t("Update the engine") : $t("Download the engine")}</button>
<button type="button" onclick={() => void run(installEngineFile)}><Icon icon="folder-open" />{$t("Install from file…")}</button>
{:else if phase === "installed" && variant === "settings"}
<button type="button" class="danger" onclick={() => void remove()}><Icon icon="trash" />{$t("Remove engine")}</button>
{/if}
{#if web}<button type="button" onclick={() => void run(() => openUrl(url))}><Icon icon="globe" />{$t("Open in default browser")}</button>{/if}
</div>
</section>
<style>
.engine { color: var(--rs-text); font-family: var(--rs-font-ui); }
.engine.tab { box-sizing: border-box; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; height: 100%; min-height: 200px; padding: 24px 16px; text-align: center; background: var(--rs-bg); }
.engine.settings { padding: var(--rs-settings-row-padding); background: var(--rs-bg-elevated); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); }
.mark { display: grid; place-items: center; width: 48px; height: 48px; color: var(--rs-accent); }
.mark :global(svg) { width: 40px; height: 40px; }
h3 { margin: 0 0 4px; font-size: 14px; font-weight: 500; color: var(--rs-text-strong); }
.tab h3 { font-size: var(--rs-settings-heading-size); }
p { margin: 4px 0; max-width: 60ch; color: var(--rs-text); font-size: var(--rs-settings-description-size); line-height: 1.6; }
.error { display: flex; align-items: flex-start; gap: 6px; color: var(--rs-error); text-align: left; }
.error :global(svg) { flex: none; width: 16px; height: 16px; margin-top: 2px; }
.error span { overflow-wrap: anywhere; }
progress { width: min(320px, 100%); height: 8px; accent-color: var(--rs-accent); }
.actions { display: flex; flex-wrap: wrap; justify-content: inherit; gap: 8px; margin-top: 8px; }
.tab .actions { justify-content: center; }
button { display: inline-flex; align-items: center; gap: 7px; min-height: var(--rs-settings-control-height); padding: 4px 10px; border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); background: var(--rs-bg-elevated); color: var(--rs-text); font: inherit; cursor: pointer; }
button:hover:not(:disabled) { background: var(--rs-bg-active); }
button:focus-visible { outline: 2px solid var(--rs-focus); outline-offset: 1px; }
button:disabled { color: var(--rs-text-dim); cursor: default; }
button.primary, button.primary:hover { border-color: var(--rs-accent); background: var(--rs-accent); color: var(--rs-on-accent); }
button.danger { border-color: var(--rs-error); color: var(--rs-error); }
</style>