A browser tab shows the download size, Download the engine, Install from file, and Open in default browser until the engine can start. It shows progress, Cancel, the cause of a failure, and Retry, and then opens the page. Settings -> Browser shows the engine state and Remove engine. Web links open in the system browser until the engine is installed. Open link in app browser always opens a browser tab.
134 lines
7.4 KiB
TypeScript
134 lines
7.4 KiB
TypeScript
import { tr } from "../i18n/index.ts";
|
|
import { keys } from '../ui/shortcuts.ts';
|
|
import { mount, unmount } from 'svelte';
|
|
import { get } from 'svelte/store';
|
|
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
|
|
import type { PluginApi, PluginViewOpenOptions } from '$lib/plugins/sdk';
|
|
import BrowserTab from './BrowserTab.svelte';
|
|
import Settings from './Settings.svelte';
|
|
import Bookmarks from './Bookmarks.svelte';
|
|
import { connect, request, state } from './client';
|
|
import { engine, engineReady, watchEngine } from './engine';
|
|
import type { TabStatus } from './tabSession';
|
|
|
|
type BrowserControl = { find(): Promise<void>; detach(): void; action(command: string, args?: Record<string, unknown>): Promise<void> };
|
|
type SavedView = { view: string; profileId?: string; url?: string; runtimeId: string; status?: TabStatus; control?: BrowserControl };
|
|
|
|
export async function activate(api: PluginApi) {
|
|
const offEngine = await watchEngine(api.signal); api.onDispose(offEngine);
|
|
const dispose = await connect(api.signal); api.onDispose(dispose);
|
|
const owners = new Map<string, SavedView>();
|
|
const offMove = await getCurrentWebviewWindow().listen<{ tabId: string }>('browser-tab-moved', ({ payload }) => {
|
|
const saved = owners.get(payload.tabId);
|
|
if (!saved) return;
|
|
saved.control?.detach();
|
|
api.closeView?.(saved.view);
|
|
});
|
|
api.onDispose(offMove);
|
|
const off = state.subscribe(value => {
|
|
for (const tab of value?.tabs || []) {
|
|
const saved = owners.get(tab.id);
|
|
if (saved) {
|
|
saved.profileId = tab.profileId; if (tab.url) saved.url = tab.url;
|
|
api.updateViewTitle?.(saved.view, `${tab.title || 'Browser'} · ${value!.profiles.find(p => p.id === tab.profileId)?.name || 'User'}`.slice(0, 200));
|
|
api.updateViewIcon?.(saved.view, tab.favicon ?? null);
|
|
}
|
|
}
|
|
});
|
|
api.onDispose(off);
|
|
const mounts = new Set<() => Promise<void>>();
|
|
function retain(instance: ReturnType<typeof mount>) {
|
|
let result: Promise<void> | undefined;
|
|
const cleanup = () => result ??= unmount(instance).then(() => { mounts.delete(cleanup); });
|
|
mounts.add(cleanup); return cleanup;
|
|
}
|
|
api.onDispose(async () => { await Promise.all([...mounts].map(cleanup => cleanup())); });
|
|
async function open(profileId?: string, url?: string, options?: PluginViewOpenOptions, tabId?: string, pinned = false) {
|
|
const id = `tab-${crypto.randomUUID()}`;
|
|
const saved: SavedView = { view: id, profileId, url, runtimeId: '' };
|
|
const run = (command: string, args?: Record<string, unknown>) => { void saved.control?.action(command, args); };
|
|
const split = (direction: 'right' | 'down') => {
|
|
const current = get(state)?.tabs.find(tab => tab.id === saved.runtimeId);
|
|
void open(current?.profileId || saved.profileId, current?.url || saved.url || 'about:blank', { split: direction, relativeTo: id });
|
|
};
|
|
api.registerView({ id, title: 'Browser', placement: 'editor', icon: 'globe', pinned, launcher: false,
|
|
tabDragData: ({ pinned }) => {
|
|
const current = get(state)?.tabs.find(tab => tab.id === saved.runtimeId);
|
|
if (saved.status?.phase !== 'ready' || !current?.transferToken) return undefined;
|
|
return JSON.stringify({ tabId: current.id, transferToken: current.transferToken, pinned });
|
|
},
|
|
find: () => { api.openView(id); void saved.control?.find(); },
|
|
tabActions: ({ pinned }) => {
|
|
const current = get(state)?.tabs.find(tab => tab.id === saved.runtimeId);
|
|
const ready = saved.status?.phase === 'ready';
|
|
const web = ready && /^https?:\/\//.test(current?.url || '');
|
|
return [
|
|
{ separator: true },
|
|
{ label: tr("Move to new window"), icon: 'external', disabled: !ready, run: () => run('moveToWindow', { pinned }) },
|
|
{ label: tr("Split right"), icon: 'split-right', disabled: !ready, run: () => split('right') },
|
|
{ label: tr("Split down"), icon: 'split-down', disabled: !ready, run: () => split('down') },
|
|
{ separator: true },
|
|
{ label: tr("Find…"), icon: 'search', shortcut: keys('Mod+F'), disabled: !ready, run: () => { api.openView(id); void saved.control?.find(); } },
|
|
{ label: tr("Open in default browser"), icon: 'external', disabled: !web, run: () => run('openExternal') },
|
|
{ label: tr("Bookmark"), icon: 'bookmark', disabled: !web, run: () => run('bookmark') },
|
|
];
|
|
}, mount: container => {
|
|
let runtimeId = '';
|
|
const instance = mount(BrowserTab, { target: container, props: {
|
|
initial: { tabId, profileId: saved.profileId, url: saved.url },
|
|
onSessionChange: value => {
|
|
saved.status = value;
|
|
if (value.tabId !== runtimeId || value.phase === 'closed') {
|
|
owners.delete(runtimeId);
|
|
runtimeId = value.phase === 'closed' ? '' : value.tabId;
|
|
if (runtimeId) owners.set(runtimeId, saved);
|
|
}
|
|
saved.runtimeId = runtimeId;
|
|
api.updateViewIcon?.(saved.view, get(state)?.tabs.find(tab => tab.id === runtimeId)?.favicon ?? null);
|
|
if (value.profileId) saved.profileId = value.profileId;
|
|
if (value.url) saved.url = value.url;
|
|
},
|
|
newTab: (profile: string, url?: string) => open(profile, url),
|
|
} });
|
|
tabId = undefined;
|
|
saved.control = instance;
|
|
const cleanup = retain(instance);
|
|
return async () => { await cleanup(); if (saved.control === instance) saved.control = undefined; };
|
|
} });
|
|
api.openView(id, options);
|
|
}
|
|
api.registerCommand({ id: 'new-tab', title: 'New browser tab', category: 'open', icon: 'globe', run: () => open() });
|
|
api.contribute('browser', {
|
|
// A new tab shows the engine setup when the engine is missing.
|
|
newTab: url => open(undefined, url),
|
|
mountSettings: container => retain(mount(Settings, { target: container })),
|
|
mountBookmarks: container => retain(mount(Bookmarks, { target: container, props: { open } })),
|
|
// A web link never starts a download. Without an engine, the link opens in the system browser.
|
|
open: async url => {
|
|
if (!engineReady(get(engine))) return false;
|
|
const value = await request('state');
|
|
if (value.settings.linkDestination === 'external') return false;
|
|
await open(undefined, url); return true;
|
|
},
|
|
});
|
|
api.registerTabDrop?.(async data => {
|
|
const payload = JSON.parse(data);
|
|
if (!payload || typeof payload.tabId !== 'string' || typeof payload.transferToken !== 'string') throw new Error(tr("Invalid browser tab transfer."));
|
|
const existing = owners.get(payload.tabId);
|
|
if (existing) { api.openView(existing.view); return; }
|
|
const snapshot = await request('adoptTab', { tabId: payload.tabId, transferToken: payload.transferToken });
|
|
const current = snapshot.tabs.find(tab => tab.id === payload.tabId);
|
|
if (!current) throw new Error(tr("The browser tab is unavailable."));
|
|
await open(current.profileId, current.url, undefined, current.id, payload.pinned === true);
|
|
});
|
|
const incoming = new URLSearchParams(location.search).get('browserTab');
|
|
if (incoming) {
|
|
const snapshot = await request('adoptTab', { tabId: incoming });
|
|
const current = snapshot.tabs.find(tab => tab.id === incoming);
|
|
await open(current?.profileId, current?.url, undefined, incoming, new URLSearchParams(location.search).get('browserPinned') === 'true');
|
|
const url = new URL(location.href);
|
|
url.searchParams.delete('browserTab'); url.searchParams.delete('browserPinned');
|
|
history.replaceState(null, '', url.pathname + url.search);
|
|
}
|
|
}
|