67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
|
||
import type { FloatingWidgetInstance } from '@frontend/types/floatingWidget';
|
||
|
||
const widgetWindows = new Map<string, WebviewWindow>();
|
||
|
||
function widgetUrl(instance: FloatingWidgetInstance): string {
|
||
const params = new URLSearchParams({
|
||
id: instance.id,
|
||
rows: String(instance.rows),
|
||
cols: String(instance.cols),
|
||
width: String(instance.width),
|
||
height: String(instance.height),
|
||
});
|
||
if (instance.rowFr?.length) params.set('rowFr', instance.rowFr.join(','));
|
||
if (instance.colFr?.length) params.set('colFr', instance.colFr.join(','));
|
||
params.set('slots', JSON.stringify(instance.slots));
|
||
return `widget.html?${params.toString()}`;
|
||
}
|
||
|
||
export async function openWidgetWindow(instance: FloatingWidgetInstance): Promise<void> {
|
||
const label = `widget-${instance.id}`;
|
||
const existing = widgetWindows.get(instance.id) ?? (await WebviewWindow.getByLabel(label));
|
||
if (existing) {
|
||
await existing.setFocus();
|
||
widgetWindows.set(instance.id, existing);
|
||
return;
|
||
}
|
||
|
||
const title = `GoldenChart — 위젯 ${instance.rows}×${instance.cols}`;
|
||
|
||
const win = new WebviewWindow(label, {
|
||
url: widgetUrl(instance),
|
||
title,
|
||
width: instance.width,
|
||
height: instance.height,
|
||
minWidth: 320,
|
||
minHeight: 240,
|
||
decorations: true,
|
||
resizable: true,
|
||
center: false,
|
||
x: instance.position.x,
|
||
y: instance.position.y,
|
||
backgroundColor: '#1a1b26',
|
||
});
|
||
|
||
widgetWindows.set(instance.id, win);
|
||
|
||
void win.once('tauri://destroyed', () => {
|
||
widgetWindows.delete(instance.id);
|
||
});
|
||
}
|
||
|
||
export async function closeWidgetWindow(id: string): Promise<void> {
|
||
const label = `widget-${id}`;
|
||
const win = widgetWindows.get(id) ?? (await WebviewWindow.getByLabel(label));
|
||
if (win) {
|
||
await win.close();
|
||
widgetWindows.delete(id);
|
||
}
|
||
}
|
||
|
||
export async function focusWidgetWindow(id: string): Promise<void> {
|
||
const label = `widget-${id}`;
|
||
const win = widgetWindows.get(id) ?? (await WebviewWindow.getByLabel(label));
|
||
if (win) await win.setFocus();
|
||
}
|