From 2abf9eeaa2030a8c9faad7c2756047b90a0b56f9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 12:33:45 +0000 Subject: [PATCH] =?UTF-8?q?[130]=20Eine=20Fenster-Br=C3=BCcke=20statt=20vi?= =?UTF-8?q?er=20Kopien=20(useWindowBridgeHost)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67 --- TODO.md | 3 ++ src/native/contextImportWindow.ts | 45 ++--------------------- src/native/drawingLevelsWindow.ts | 47 ++---------------------- src/native/layerSettingsWindow.ts | 53 ++------------------------- src/native/settingsWindow.ts | 43 ++-------------------- src/native/windowBridge.ts | 60 +++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 179 deletions(-) create mode 100644 src/native/windowBridge.ts diff --git a/TODO.md b/TODO.md index 6adc3a0a..b2712ad1 100644 --- a/TODO.md +++ b/TODO.md @@ -1156,6 +1156,9 @@ Prüfung des ganzen Programms (Rust-Kern, TS-Hülle, Build/Sicherheit), nur Code relative Importe (Ausnahmelisten leer). · S - ◐ **[130] Kleinkram:** eine generische Fenster-Brücke statt 5 Kopien; Auswahl als ein `Selection`-Objekt statt 12 Felder; `primitives.tsx` auf Overlay zurückschneiden; Cargo-Workspace. · M + 4.11.: generische Fenster-Brücke `native/windowBridge.ts` (`useWindowBridgeHost`) für Einstellungen, Geschosse, + Ebenen-Einstellungen und Kontext-Import (−110 Zeilen); Ressourcen-Fenster bleibt eigen (Teil-Änderungen, [121]). + Nicht in der Tauri-App geprüft. 3.11. Lizenzteil erledigt: NOTICE ergänzt (OFL-Schriften, wgpu, Tauri, React, pdf.js, jsPDF, lcms, truck …), `src-tauri/deny.toml` + CI-Job `licenses` (check.yml, `cargo deny check licenses` je Crate). diff --git a/src/native/contextImportWindow.ts b/src/native/contextImportWindow.ts index 37260f09..3edeb5e4 100644 --- a/src/native/contextImportWindow.ts +++ b/src/native/contextImportWindow.ts @@ -15,10 +15,9 @@ // // Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md). -import { useEffect, useRef } from "react"; -import { isTauriRuntime } from "./projectFile"; import type { GeoAnchor } from "../ui/ContextImportDialog"; import { BORDERLESS_WINDOW_CHROME } from "./windowChrome"; +import { useWindowBridgeHost, type WindowHandlers } from "./windowBridge"; const CONTEXT_IMPORT_WINDOW_LABEL = "context-import"; @@ -55,45 +54,5 @@ export function useContextImportWindowHost( geoAnchor: GeoAnchor | undefined, handlers: ContextImportCommandHandlers, ): void { - const handlersRef = useRef(handlers); - handlersRef.current = handlers; - const geoAnchorRef = useRef(geoAnchor); - geoAnchorRef.current = geoAnchor; - - useEffect(() => { - if (!isTauriRuntime()) return; - let disposed = false; - const unlisten: Array<() => void> = []; - void (async () => { - const { listen, emitTo } = await import("@tauri-apps/api/event"); - if (disposed) return; - unlisten.push( - await listen<{ name: string; args: unknown[] }>("context-import-command", (e) => { - const fn = (handlersRef.current as unknown as Record)[e.payload.name]; - if (typeof fn === "function") (fn as (...a: unknown[]) => void)(...e.payload.args); - }), - ); - unlisten.push( - await listen("context-import-window-ready", () => { - void emitTo(CONTEXT_IMPORT_WINDOW_LABEL, "context-import-sync-state", { - geoAnchor: geoAnchorRef.current, - }).catch(() => {}); - }), - ); - })(); - return () => { - disposed = true; - unlisten.forEach((u) => u()); - }; - }, []); - - useEffect(() => { - if (!isTauriRuntime()) return; - void (async () => { - const { emitTo } = await import("@tauri-apps/api/event"); - await emitTo(CONTEXT_IMPORT_WINDOW_LABEL, "context-import-sync-state", { - geoAnchor, - }).catch(() => {}); - })(); - }, [geoAnchor]); + useWindowBridgeHost(CONTEXT_IMPORT_WINDOW_LABEL, "context-import", { geoAnchor }, handlers as unknown as WindowHandlers, [geoAnchor]); } diff --git a/src/native/drawingLevelsWindow.ts b/src/native/drawingLevelsWindow.ts index 02307845..2abccdef 100644 --- a/src/native/drawingLevelsWindow.ts +++ b/src/native/drawingLevelsWindow.ts @@ -17,11 +17,10 @@ // // Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md). -import { useEffect, useRef } from "react"; -import { isTauriRuntime } from "./projectFile"; import type { DrawingLevel } from "../model/types"; import type { DisplayMode } from "../panels/types"; import { BORDERLESS_WINDOW_CHROME } from "./windowChrome"; +import { useWindowBridgeHost, type WindowHandlers } from "./windowBridge"; const DRAWING_LEVELS_WINDOW_LABEL = "drawing-levels"; @@ -65,47 +64,5 @@ export function useDrawingLevelsWindowHost( state: DrawingLevelsSyncState, handlers: DrawingLevelsCommandHandlers, ): void { - const handlersRef = useRef(handlers); - handlersRef.current = handlers; - const stateRef = useRef(state); - stateRef.current = state; - - useEffect(() => { - if (!isTauriRuntime()) return; - let disposed = false; - const unlisten: Array<() => void> = []; - void (async () => { - const { listen, emitTo } = await import("@tauri-apps/api/event"); - if (disposed) return; - unlisten.push( - await listen<{ name: string; args: unknown[] }>("drawing-levels-command", (e) => { - const fn = (handlersRef.current as unknown as Record)[e.payload.name]; - if (typeof fn === "function") (fn as (...a: unknown[]) => void)(...e.payload.args); - }), - ); - unlisten.push( - await listen("drawing-levels-window-ready", () => { - void emitTo( - DRAWING_LEVELS_WINDOW_LABEL, - "drawing-levels-sync-state", - stateRef.current, - ).catch(() => {}); - }), - ); - })(); - return () => { - disposed = true; - unlisten.forEach((u) => u()); - }; - }, []); - - useEffect(() => { - if (!isTauriRuntime()) return; - void (async () => { - const { emitTo } = await import("@tauri-apps/api/event"); - await emitTo(DRAWING_LEVELS_WINDOW_LABEL, "drawing-levels-sync-state", state).catch( - () => {}, - ); - })(); - }, [state]); + useWindowBridgeHost(DRAWING_LEVELS_WINDOW_LABEL, "drawing-levels", state, handlers as unknown as WindowHandlers); } diff --git a/src/native/layerSettingsWindow.ts b/src/native/layerSettingsWindow.ts index e80a26c6..02be23f4 100644 --- a/src/native/layerSettingsWindow.ts +++ b/src/native/layerSettingsWindow.ts @@ -21,11 +21,10 @@ // // Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md). -import { useEffect, useRef } from "react"; -import { isTauriRuntime } from "./projectFile"; import { findCategory } from "../model/layers"; import type { HatchStyle, LayerCategory, Project } from "../model/types"; import { BORDERLESS_WINDOW_CHROME } from "./windowChrome"; +import { useWindowBridgeHost } from "./windowBridge"; const LAYER_SETTINGS_WINDOW_LABEL = "layer-settings"; @@ -83,52 +82,6 @@ export function useLayerSettingsWindowHost( code: string | null, onPatchCategory: (code: string, patch: Partial) => void, ): void { - const stateRef = useRef(code ? stateFor(project, code) : null); - stateRef.current = code ? stateFor(project, code) : null; - const onPatchRef = useRef(onPatchCategory); - onPatchRef.current = onPatchCategory; - - useEffect(() => { - if (!isTauriRuntime()) return; - let disposed = false; - const unlisten: Array<() => void> = []; - void (async () => { - const { listen, emitTo } = await import("@tauri-apps/api/event"); - if (disposed) return; - unlisten.push( - await listen<{ name: string; args: unknown[] }>("layer-settings-command", (e) => { - if (e.payload.name === "onPatchCategory") { - const [c, patch] = e.payload.args as [string, Partial]; - onPatchRef.current(c, patch); - } - }), - ); - unlisten.push( - await listen("layer-settings-window-ready", () => { - void emitTo( - LAYER_SETTINGS_WINDOW_LABEL, - "layer-settings-sync-state", - stateRef.current, - ).catch(() => {}); - }), - ); - })(); - return () => { - disposed = true; - unlisten.forEach((u) => u()); - }; - }, []); - - useEffect(() => { - if (!isTauriRuntime()) return; - void (async () => { - const { emitTo } = await import("@tauri-apps/api/event"); - await emitTo( - LAYER_SETTINGS_WINDOW_LABEL, - "layer-settings-sync-state", - stateRef.current, - ).catch(() => {}); - })(); - // eslint-disable-next-line react-hooks/exhaustive-deps -- stateRef.current ist bereits die aktuelle Ableitung - }, [project, code]); + const state: LayerSettingsSyncState | null = code ? stateFor(project, code) : null; + useWindowBridgeHost(LAYER_SETTINGS_WINDOW_LABEL, "layer-settings", state, { onPatchCategory: (c: string, patch: Partial) => onPatchCategory(c, patch) }, [project, code]); } diff --git a/src/native/settingsWindow.ts b/src/native/settingsWindow.ts index 6cd4fece..f3b206f9 100644 --- a/src/native/settingsWindow.ts +++ b/src/native/settingsWindow.ts @@ -13,11 +13,10 @@ // Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md). import type { AiAccessView } from "../ui/AiAccessSection"; -import { useEffect, useRef } from "react"; -import { isTauriRuntime } from "./projectFile"; import type { LayoutState } from "../panels/types"; import type { ThemeMode } from "../theme/themeMode"; import { BORDERLESS_WINDOW_CHROME } from "./windowChrome"; +import { useWindowBridgeHost, type WindowHandlers } from "./windowBridge"; const SETTINGS_WINDOW_LABEL = "settings"; @@ -74,43 +73,5 @@ export function useSettingsWindowHost( state: SettingsSyncState, handlers: SettingsCommandHandlers, ): void { - const handlersRef = useRef(handlers); - handlersRef.current = handlers; - const stateRef = useRef(state); - stateRef.current = state; - - useEffect(() => { - if (!isTauriRuntime()) return; - let disposed = false; - const unlisten: Array<() => void> = []; - void (async () => { - const { listen, emitTo } = await import("@tauri-apps/api/event"); - if (disposed) return; - unlisten.push( - await listen<{ name: string; args: unknown[] }>("settings-command", (e) => { - const fn = (handlersRef.current as unknown as Record)[e.payload.name]; - if (typeof fn === "function") (fn as (...a: unknown[]) => void)(...e.payload.args); - }), - ); - unlisten.push( - await listen("settings-window-ready", () => { - void emitTo(SETTINGS_WINDOW_LABEL, "settings-sync-state", stateRef.current).catch( - () => {}, - ); - }), - ); - })(); - return () => { - disposed = true; - unlisten.forEach((u) => u()); - }; - }, []); - - useEffect(() => { - if (!isTauriRuntime()) return; - void (async () => { - const { emitTo } = await import("@tauri-apps/api/event"); - await emitTo(SETTINGS_WINDOW_LABEL, "settings-sync-state", state).catch(() => {}); - })(); - }, [state]); + useWindowBridgeHost(SETTINGS_WINDOW_LABEL, "settings", state, handlers as unknown as WindowHandlers); } diff --git a/src/native/windowBridge.ts b/src/native/windowBridge.ts new file mode 100644 index 00000000..f67d8f43 --- /dev/null +++ b/src/native/windowBridge.ts @@ -0,0 +1,60 @@ +// Gemeinsame Hauptfenster-Seite der Sync-Brücke zu den macOS-Nebenfenstern ([130]; vorher je Fenster kopiert). +// +// Hauptfenster → Nebenfenster: „-sync-state" mit dem Zustand bei jeder Änderung und auf +// „-window-ready" (das Nebenfenster ist geladen und fragt nach). +// Nebenfenster → Hauptfenster: „-command" mit { name, args } — `name` ist ein Schlüssel von `handlers`. +// +// No-op ausserhalb von Tauri. Das Ressourcen-Fenster (resourceWindow.ts) schickt Teil-Änderungen und bleibt eigen. + +import { useEffect, useRef } from "react"; +import { isTauriRuntime } from "./projectFile"; + +/** Befehle eines Nebenfensters: Name → Funktion (Argumente kommen unverändert aus dem Fenster). */ +export type WindowHandlers = { [name: string]: (...args: never[]) => void }; + +/** + * Hält das Nebenfenster `label` (falls offen) synchron zu `state` und wendet dessen Befehle auf `handlers` an. + * `state` wird bei jeder neuen Identität gesendet; wer ihn bei jedem Render neu ableitet, übergibt `deps`. + */ +export function useWindowBridgeHost(label: string, prefix: string, state: S, handlers: WindowHandlers, deps?: readonly unknown[]): void { + const handlersRef = useRef(handlers); + handlersRef.current = handlers; + const stateRef = useRef(state); + stateRef.current = state; + + useEffect(() => { + if (!isTauriRuntime()) return; + let disposed = false; + const unlisten: Array<() => void> = []; + void (async () => { + const { listen, emitTo } = await import("@tauri-apps/api/event"); + if (disposed) return; + const offCommand = await listen<{ name: string; args: unknown[] }>(`${prefix}-command`, (e) => { + const fn = handlersRef.current[e.payload.name] as ((...a: unknown[]) => void) | undefined; + if (typeof fn === "function") fn(...(e.payload.args ?? [])); + }); + const offReady = await listen(`${prefix}-window-ready`, () => { + void emitTo(label, `${prefix}-sync-state`, stateRef.current).catch(() => {}); + }); + if (disposed) { + offCommand(); + offReady(); + return; + } + unlisten.push(offCommand, offReady); + })(); + return () => { + disposed = true; + unlisten.forEach((u) => u()); + }; + }, [label, prefix]); + + useEffect(() => { + if (!isTauriRuntime()) return; + void (async () => { + const { emitTo } = await import("@tauri-apps/api/event"); + await emitTo(label, `${prefix}-sync-state`, stateRef.current).catch(() => {}); + })(); + // eslint-disable-next-line react-hooks/exhaustive-deps -- `deps` ersetzt `state`, wenn der Zustand je Render neu entsteht + }, deps ?? [state]); +}