Files
yaak-mountain-loop/apps/yaak-proxy/lib/tauri.ts
T
Gregory SchierandClaude Opus 5.5 f7402f5f4f Open Yaak Proxy's Settings in its own window
Settings opens in a window labelled `settings`, created through the shared
window code, as Yaak's does. The page renders the settings alone or the app
by its window label. Opening it again focuses it and moves it to the tab
asked for; Escape closes it, and closing the main window closes it too.
Changes made there reach the main window at once: theme and sizes by an
event carrying the values, and engine settings by an event after each RPC
mutation that makes other windows read the proxy state and settings
queries again. The browser harness, with no second window, keeps the
dialog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 11:34:34 -07:00

61 lines
2.1 KiB
TypeScript

import { invoke } from "@tauri-apps/api/core";
import { emit as tauriEmit, listen as tauriListen } from "@tauri-apps/api/event";
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
import { type as tauriOsType } from "@tauri-apps/plugin-os";
import { unsubscribeWhenReady } from "./unsubscribe";
/** Call a Tauri command. */
export function command<T>(cmd: string, args?: Record<string, unknown>): Promise<T> {
return invoke(cmd, args) as Promise<T>;
}
/**
* Subscribe to a Tauri event. Returns an unsubscribe function, which also removes a listener
* still being registered once it is, and stops it delivering meanwhile: an effect cleaned up
* that fast, as strict mode does to every one, would otherwise leave its listener attached and
* hear each event twice.
*/
export function subscribe<T>(event: string, callback: (payload: T) => void): () => void {
const subscription = unsubscribeWhenReady(
tauriListen<T>(event, (e) => {
if (subscription.active()) callback(e.payload);
}),
);
return subscription.unsubscribe;
}
/** Calls `callback` each time the window regains focus. Returns an unsubscribe function. */
export function onWindowFocus(callback: () => void): () => void {
const subscription = unsubscribeWhenReady(
getCurrentWebviewWindow().onFocusChanged(
({ payload: focused }) => focused && subscription.active() && callback(),
),
);
return subscription.unsubscribe;
}
/** Show the current webview window. */
export function showWindow(): Promise<void> {
return getCurrentWebviewWindow().show();
}
/** Close the current webview window. */
export function closeWindow(): Promise<void> {
return getCurrentWebviewWindow().close();
}
/** The current window's label. */
export function currentWindowLabel(): string {
return getCurrentWebviewWindow().label;
}
/** Send an event to every window of the app, this one included. */
export function broadcast(event: string, payload: unknown): Promise<void> {
return tauriEmit(event, payload);
}
/** Get the current OS type (e.g. "macos", "linux", "windows"). */
export function getOsType() {
return tauriOsType();
}