2026-09-12 08:41:08 +08:00
|
|
|
import { contextBridge, ipcRenderer } from 'electron';
|
2026-09-12 15:19:13 +08:00
|
|
|
import type { IslandAppearance, IslandState } from '@classhelper/shared';
|
2026-09-12 08:41:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 灵动岛窗口的预加载脚本。
|
2026-09-12 15:19:13 +08:00
|
|
|
* 只暴露「订阅状态/外观」与「发送用户操作」两件事,渲染进程不接触任何 Node 能力。
|
2026-09-12 08:41:08 +08:00
|
|
|
*/
|
|
|
|
|
export interface IslandBridge {
|
|
|
|
|
onState(handler: (state: IslandState) => void): void;
|
|
|
|
|
sendAction(action: 'expand' | 'collapse' | 'dismiss' | 'mark-read' | 'open-app', id?: string): void;
|
|
|
|
|
getState(): Promise<IslandState>;
|
2026-09-12 15:19:13 +08:00
|
|
|
/** 订阅个性化外观(设置页改动后实时生效) */
|
|
|
|
|
onAppearance(handler: (appearance: IslandAppearance) => void): void;
|
|
|
|
|
getAppearance(): Promise<IslandAppearance>;
|
2026-09-12 08:41:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bridge: IslandBridge = {
|
|
|
|
|
onState: (handler) => {
|
|
|
|
|
ipcRenderer.on('island:state', (_event, state: IslandState) => handler(state));
|
|
|
|
|
},
|
|
|
|
|
sendAction: (action, id) => {
|
|
|
|
|
ipcRenderer.send('island:action', { action, id });
|
|
|
|
|
},
|
|
|
|
|
getState: () => ipcRenderer.invoke('island:get-state'),
|
2026-09-12 15:19:13 +08:00
|
|
|
onAppearance: (handler) => {
|
|
|
|
|
ipcRenderer.on('island:appearance', (_event, appearance: IslandAppearance) => handler(appearance));
|
|
|
|
|
},
|
|
|
|
|
getAppearance: () => ipcRenderer.invoke('island:get-appearance'),
|
2026-09-12 08:41:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
contextBridge.exposeInMainWorld('island', bridge);
|