shell: the shell frame, createShell(site) (moved from wayBetter for X)

packages/shell/src/frame (waybetter/shell/frame): the whole shell of an app. An app describes its
site (ShellSite: names and pages, account rules, search, address chips and completions, its panel
tab, the new-tab page, palette items, deck column kinds and flags and an optional compose dock, the
media panel's composer rule, a theme store, overlays, the native features it serves) and passes
its Tauri API and i18n instance; createShell returns the root component.

- Shell.tsx: dockview host (tabs in the titlebar row, splits, drag-link drops, shortcuts, context
  menu, deck back-and-forth, session restore, workspace on start, overlay raise); state.ts (pane
  placeholders, tabs_sync one-in-flight via syncGate, shell_raise layer, profiles, layout).
- WebPanel (address bar), NewTabPage, CommandPalette + paletteModel (frame groups; site items),
  DeckView + AddColumnDialog + deckStore (site column kinds), PaneTheme + themeStore
  (createThemeStore over the app's ThemeAdapter) + chromeTheme (ChromeTheme input) + themeScopes,
  Titlebar + TitlebarExtras + icons + brand (wB mark data URI), AccountSwitcher + accountModel,
  workspaces + workspaceModel, backupModel, syncHost, devHost + devModel, pluginCommandStore,
  MediaPanel + mediaModel, searchInput, hooks, bridge (installPreviewMocks), i18n, boot.ts
  (bootScript: the first-frame boot.js), CSS themed with --wb-* tokens.
- paneModel: paneVisibility, raiseSupported, cssRgb; deckModel: isSourcePage.
- Includes the live-QA titlebar fix (dock ResizeObserver + rAF titlebar marks, dock sized by its
  insets).
- tests/frame.test.ts: every frame key has English and is used, palette order with a site,
  address fallback, layout gate, boot script, chrome variables, switcher rules, workspaces,
  deck settings with site flags.
- npm package 0.4.0: exports ./shell/frame, ./shell/frame/*, ./shell/frame/*.css (copied by the
  build); dockview and acorn optional peers; root devDependencies dockview, acorn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
CRTkafa 2026-09-29 12:47:10 +07:00
parent 37e3d8e5fc
commit 60faed66be
49 changed files with 6442 additions and 6 deletions

View file

@ -0,0 +1,76 @@
// The shell frame's line to native code. The app passes its own Tauri API (`@tauri-apps/api`
// `invoke`, `listen`, `getCurrentWindow`) through `createShell`, so the framework has no Tauri
// dependency and the app keeps its version. `call` normalizes errors; `installPreviewMocks` stands
// in for native code in a plain browser (the dev preview harnesses: every command answered from
// memory, events delivered with `emit`).
// No React; pure enough for node tests (nothing runs until a command is called).
import type { NativeWindow } from '../windowControls.ts';
export type Invoke = <T = unknown>(command: string, args?: Record<string, unknown>) => Promise<T>;
export type Listen = <T = unknown>(event: string, handler: (event: { payload: T }) => void) => Promise<() => void>;
/** The Tauri `Window` calls the frame makes (window controls, and the window's own background). */
export type ShellWindow = NativeWindow & { setBackgroundColor(color: [number, number, number, number]): Promise<void> };
export type NativeApi = { invoke: Invoke; listen: Listen; currentWindow: () => ShellWindow };
let api: NativeApi | null = null;
export function setNative(native: NativeApi) {
api = native;
}
function need(): NativeApi {
if (!api) throw new Error('wayBetter shell: createShell(site) first (no native API)');
return api;
}
/** A native command (rejects with an Error whose message is native code's `CODE: detail`). */
export const invoke: Invoke = async <T>(command: string, args?: Record<string, unknown>) => {
try { return await need().invoke<T>(command, args); }
catch (error) { throw error instanceof Error ? error : new Error(String(error)); }
};
/** A native event; resolves to its unlisten function (rejects outside Tauri without mocks). */
export const listen: Listen = (event, handler) => {
try { return need().listen(event, handler); }
catch (error) { return Promise.reject(error); }
};
export const currentWindow = () => need().currentWindow();
/** Tauri's IPC is present (the real app, or a preview with mocks installed). */
export const isNative = () => typeof globalThis === 'object' && '__TAURI_INTERNALS__' in globalThis;
export type MockHandler = (args: Record<string, unknown>) => unknown;
/**
* Development only: answers native commands from `handlers` (`window.__TAURI_INTERNALS__`, which
* `@tauri-apps/api` reads) so the real shell renders in a plain browser. Event listeners are
* mocked too; `emit(event, payload)` delivers an event to them. Unknown commands reject (and warn).
*/
export function installPreviewMocks(handlers: Record<string, MockHandler>, options: { label?: string; name?: string } = {}) {
const label = options.label ?? 'shell';
let callbackId = 0;
const callbacks = new Map<number, (message: unknown) => void>();
const listeners: Record<string, number[]> = {};
// Tauri's event plugin; `handlers` is read on every call, so commands can be added later.
const events: Record<string, MockHandler> = {
'plugin:event|listen': ({ event, handler }) => { (listeners[String(event)] ??= []).push(Number(handler)); return Number(handler); },
'plugin:event|unlisten': () => null,
};
const emit = (event: string, payload: unknown) =>
(listeners[event] ?? []).forEach(id => callbacks.get(id)?.({ event, id, payload }));
const target = window as unknown as Record<string, unknown>;
target.__TAURI_INTERNALS__ = {
invoke: async (cmd: string, args: Record<string, unknown> = {}) => {
const handler = handlers[cmd] ?? events[cmd];
if (!handler) { console.warn(`[${options.name ?? 'preview'}] ${cmd} is not mocked`); throw new Error(`Preview: ${cmd} is not mocked`); }
return structuredClone(await handler(args));
},
transformCallback: (callback: (message: unknown) => void) => { callbacks.set(++callbackId, callback); return callbackId; },
unregisterCallback: () => {},
metadata: { currentWindow: { label }, currentWebview: { label } },
};
target.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} };
return { handlers, emit };
}