// Tauri-v2-Einstieg: die Befehls-Bruecke und der App-Start. // M2: native wgpu-Viewports (2D und/oder 3D) im Tauri-Prozess, ueber eine // eigene winit-Event-Loop auf einem Hintergrund-Thread. NICHT auf macOS // (winit verlangt dort den Hauptthread) — dort s. `native_tauri_window` (2D). // `native3d` hat (noch) keinen macOS-Weg und ist dort ein No-op. #[cfg(all(any(feature = "native2d", feature = "native3d"), not(target_os = "macos")))] mod native; // Stufe 2 (macOS): native Metal-Flaeche als Kind-View im Hauptfenster, unter der // transparenten Webview (s. Modul-Kommentar). #[cfg(all(feature = "native2d", target_os = "macos"))] mod native_embed; // Lock-Datei gegen gleichzeitiges Oeffnen derselben Projektdatei aus zwei // App-Instanzen (OS-Advisory-Lock via `fs4`, siehe lock.rs). mod lock; // Absturzsicheres Speichern ([116]): Temp-Datei + fsync + Umbenennen, `~` als Sicherung. mod atomic_save; // Blender als eigener Prozess ([211], Rendering mit Blender/Cycles): suchen, starten, abbrechen. mod blender; use std::path::PathBuf; use tauri::State; /// Fordert den exklusiven Lock fuer eine Projektdatei an. `Ok(())` wenn frei /// (oder schon von uns gehalten), sonst `Err(Some(LockInfo))` mit Angaben zur /// haltenden Instanz (pid/hostname/timestamp), oder `Err(None)` falls die /// Sidecar-Datei gesperrt, aber ihr Info-Block nicht lesbar war. #[tauri::command] fn acquire_project_lock( path: String, state: State, ) -> Result<(), Option> { lock::acquire(&state, &PathBuf::from(path)) } /// Schreibt eine Projektdatei atomar ([116]); die Vorversion bleibt als `~` erhalten. #[tauri::command] fn save_atomic(path: String, content: String) -> Result<(), String> { atomic_save::save_atomic_impl(&PathBuf::from(path), content.as_bytes()) } /// Blender-Programmdatei ([211]): eigene Angabe aus den Einstellungen, sonst PATH und die üblichen Orte. `None` = nicht gefunden. #[tauri::command] fn blender_find(custom: Option) -> Option { blender::find(custom.as_deref(), std::env::var_os("PATH").as_deref(), blender::CANDIDATES).map(|p| p.to_string_lossy().into_owned()) } /// Neuer Arbeitsordner für ein Rendering (`/dossier-render/…`); die Webview schreibt das Paket hinein. #[tauri::command] fn blender_job_dir() -> Result { blender::job_dir().map(|p| p.to_string_lossy().into_owned()) } /// Startet Blender als Hintergrundprozess. Ausgabezeilen kommen als Ereignis `blender://line` `{ jobId, line }`, das /// Ende als `blender://exit` `{ jobId, code }`. Nur Programme, die nach Blender heissen. #[tauri::command] fn blender_start( app: tauri::AppHandle, job_id: String, exe: String, args: Vec, state: State, ) -> Result<(), String> { use tauri::Emitter; let (a1, a2) = (app.clone(), app); let (i1, i2) = (job_id.clone(), job_id.clone()); blender::start( &state, &job_id, &PathBuf::from(exe), &args, move |line| { let _ = a1.emit("blender://line", serde_json::json!({ "jobId": i1, "line": line })); }, move |code| { let _ = a2.emit("blender://exit", serde_json::json!({ "jobId": i2, "code": code })); }, ) } /// [228] Startet Blender mit Oberfläche (ohne `-b`), z. B. auf eine .blend-Datei, und kehrt sofort zurück. Nur Programme, /// die nach Blender heissen. #[tauri::command] fn blender_open(exe: String, args: Vec) -> Result<(), String> { blender::open(&PathBuf::from(exe), &args) } /// Bricht ein laufendes Rendering ab. #[tauri::command] fn blender_cancel(job_id: String, state: State) { blender::cancel(&state, &job_id); } /// Gibt einen von dieser Instanz gehaltenen Lock frei. No-op, wenn der Pfad /// nicht gelockt war. #[tauri::command] fn release_project_lock(path: String, state: State) { lock::release(&state, &PathBuf::from(path)); } // Live-Spiegelung: die Webview schiebt bei jeder Modellaenderung (debounced) // die aktuelle 2D-Szene bzw. die 3D-Waende hierher. Auf macOS (nur native2d) // geht das direkt an `native_tauri_window` (Tauri-Fenster als Surface); sonst // (Linux, oder native3d auf jeder Plattform) ueber `native.rs`s // EventLoopProxy in die winit-Loop. Die 2D-Szene kommt als roher Binaerpuffer // (`render2d::scene_bin`, Body des Requests), die 3D-Waende als JSON-Wert — // beide Commands existieren unter EINEM cfg, egal welche Feature-Kombination // gebaut wird. #[cfg(any(feature = "native2d", feature = "native3d"))] #[tauri::command] fn push_native_scene(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) { #[cfg(all(feature = "native2d", target_os = "macos"))] { // macOS: die 2D-Ansicht laeuft ueber `native_embed` (Chunk-Upload), nicht // ueber eine Szene. let _ = (&app, &request); } #[cfg(all(feature = "native2d", not(target_os = "macos")))] { let _ = &app; if let tauri::ipc::InvokeBody::Raw(bytes) = request.body() { native::push_scene(bytes); } else { eprintln!("push_native_scene: Rohdaten erwartet"); } } #[cfg(not(feature = "native2d"))] { let _ = (app, request); } } #[cfg(any(feature = "native2d", feature = "native3d"))] #[tauri::command] fn push_native_walls(walls: serde_json::Value) { #[cfg(all(feature = "native3d", not(target_os = "macos")))] native::push_walls(walls); #[cfg(not(all(feature = "native3d", not(target_os = "macos"))))] let _ = walls; } /// Ob dieser Build ein natives wgpu-Fenster hat, das die Webview mit Szenen /// füttern kann (Features native2d/native3d). Die Webview fragt einmal und /// pusht nur dann — sonst würde sie bei JEDER Planänderung die gesamte Szene /// (bei Kataster-Importen Millionen Primitive) serialisieren und an einen /// nicht vorhandenen Befehl schicken. /// Webview meldet Position/Groesse des Plan-Bereichs (CSS-Pixel); die native /// Flaeche folgt. Ausserhalb macOS/native2d ein No-op. #[tauri::command] fn native2d_embed_rect(app: tauri::AppHandle, x: f64, y: f64, w: f64, h: f64) { #[cfg(all(feature = "native2d", target_os = "macos"))] native_embed::set_rect(&app, x, y, w, h); #[cfg(not(all(feature = "native2d", target_os = "macos")))] let _ = (app, x, y, w, h); } /// Gibt es in diesem Build die eingebettete native 2D-Flaeche (macOS + native2d)? #[tauri::command] fn native2d_embed_available() -> bool { cfg!(all(feature = "native2d", target_os = "macos")) } #[tauri::command] fn native2d_embed_hide(app: tauri::AppHandle) { #[cfg(all(feature = "native2d", target_os = "macos"))] native_embed::hide(&app); #[cfg(not(all(feature = "native2d", target_os = "macos")))] let _ = app; } /// Binaerer Puffer-Upload (Header `x-buf-id`, Body = rohe Bytes). Synchroner /// Command → laeuft auf dem Hauptthread, Reihenfolge der Aufrufe bleibt erhalten. #[tauri::command] fn native2d_upload(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) -> Result<(), String> { #[cfg(all(feature = "native2d", target_os = "macos"))] { let id: u32 = request .headers() .get("x-buf-id") .and_then(|v| v.to_str().ok()) .and_then(|v| v.parse().ok()) .ok_or("x-buf-id fehlt")?; let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else { return Err("Rohdaten erwartet".into()); }; native_embed::upload(&app, id, bytes); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] let _ = (app, request); Ok(()) } /// Puffer-Patch: alten Puffer (`x-from`) unter neuer ID (`x-to`) weiterfuehren und /// nur die Bereiche aus `x-ranges` ("offset:laenge,offset:laenge") ueberschreiben. /// Body = die Bytes dieser Bereiche hintereinander. `false` → Aufrufer sendet voll. #[tauri::command] fn native2d_patch(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) -> Result { #[cfg(all(feature = "native2d", target_os = "macos"))] { let h = request.headers(); let get = |k: &str| h.get(k).and_then(|v| v.to_str().ok()).map(|v| v.to_string()); let from: u32 = get("x-from").and_then(|v| v.parse().ok()).ok_or("x-from fehlt")?; let to: u32 = get("x-to").and_then(|v| v.parse().ok()).ok_or("x-to fehlt")?; let ranges: Vec<(u64, u64)> = get("x-ranges") .ok_or("x-ranges fehlt")? .split(',') .filter_map(|r| { let (o, l) = r.split_once(':')?; Some((o.parse().ok()?, l.parse().ok()?)) }) .collect(); let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else { return Err("Rohdaten erwartet".into()); }; return Ok(native_embed::patch(&app, from, to, &ranges, bytes)); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] { let _ = (app, request); Ok(false) } } #[tauri::command] fn native2d_highlight(app: tauri::AppHandle, geom: Option, delete: Vec) -> Result<(), String> { #[cfg(all(feature = "native2d", target_os = "macos"))] { let geom = match geom { Some(v) => Some(serde_json::from_value(v).map_err(|e| e.to_string())?), None => None, }; native_embed::set_highlight(&app, geom, delete); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] let _ = (app, geom, delete); Ok(()) } #[tauri::command] fn native2d_layout(app: tauri::AppHandle, layout: serde_json::Value, delete: Vec) -> Result<(), String> { #[cfg(all(feature = "native2d", target_os = "macos"))] { let layout = serde_json::from_value(layout).map_err(|e| e.to_string())?; native_embed::set_layout(&app, layout, delete); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] let _ = (app, layout, delete); Ok(()) } /// Vektor-Texte (Delta): `upsert` = Eintraege, `remove` = IDs. Liefert `false`, wenn /// keine Schrift gefunden wurde (dann zeichnet die Webview die Texte selbst). #[tauri::command] fn native2d_texts(app: tauri::AppHandle, upsert: serde_json::Value, remove: Vec) -> Result { #[cfg(all(feature = "native2d", target_os = "macos"))] { let upsert = serde_json::from_value(upsert).map_err(|e| e.to_string())?; return Ok(native_embed::set_texts(&app, upsert, remove)); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] { let _ = (app, upsert, remove); Ok(false) } } /// Gesetzte Textzeilen fuer den Glyphen-Pass der Flaeche ([194]): Body = f64-Bytes von /// `plan_screen_texts`, Header `x-text-scale` = Massstab 1:N des Satzes. `false` → die Webview zeichnet /// die Texte selbst (kein natives 2D, Flaeche noch nicht da, Puffer kaputt). #[tauri::command] fn native2d_scene_texts(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) -> Result { #[cfg(all(feature = "native2d", target_os = "macos"))] { let n: f64 = request .headers() .get("x-text-scale") .and_then(|v| v.to_str().ok()) .and_then(|v| v.parse().ok()) .unwrap_or(100.0); let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else { return Err("Rohdaten erwartet".into()); }; return Ok(native_embed::set_scene_texts(&app, bytes, n)); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] { let _ = (app, request); Ok(false) } } /// [226] Schriftdatei fuer den Glyphen-Pass der Flaeche: Body = WOFF2/SFNT-Bytes (leer = Datei gibt es nicht), /// Header `x-font-key` = Familie << 8 | Schnitt. #[tauri::command] fn native2d_font(app: tauri::AppHandle, request: tauri::ipc::Request<'_>) -> Result { #[cfg(all(feature = "native2d", target_os = "macos"))] { let key: u32 = request .headers() .get("x-font-key") .and_then(|v| v.to_str().ok()) .and_then(|v| v.parse().ok()) .ok_or("x-font-key fehlt")?; let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else { return Err("Rohdaten erwartet".into()); }; return Ok(native_embed::add_font(&app, key, bytes)); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] { let _ = (app, request); Ok(false) } } /// [226] Schriftdateien (`Familie << 8 | Schnitt`), die die gesetzten Textzeilen noch brauchen. #[tauri::command] fn native2d_needed_fonts(app: tauri::AppHandle) -> Vec { #[cfg(all(feature = "native2d", target_os = "macos"))] { return native_embed::needed_fonts(&app); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] { let _ = app; Vec::new() } } #[tauri::command] fn native2d_frame(app: tauri::AppHandle, view: serde_json::Value, paper_scale: f64, clear: Option<[f64; 3]>, dash_scale: Option) -> Result<(), String> { #[cfg(all(feature = "native2d", target_os = "macos"))] { let view = serde_json::from_value(view).map_err(|e| e.to_string())?; native_embed::frame(&app, view, paper_scale, clear, dash_scale); } #[cfg(not(all(feature = "native2d", target_os = "macos")))] let _ = (app, view, paper_scale, clear, dash_scale); Ok(()) } /// /// macOS: die eingebettete 2D-Fläche (`native_embed`) bekommt ihre Daten über /// Chunk-Uploads, und es gibt dort kein natives 3D-Fenster — dort also `false`, /// sonst würde die Webview nach JEDER Plan-/Projektänderung den ganzen Plan bzw. /// das 3D-Modell als JSON aufbauen, nur damit Rust es verwirft (bei Kataster- /// Importen sekundenlanges Stocken). #[tauri::command] fn native_viewport_enabled() -> bool { cfg!(all(any(feature = "native2d", feature = "native3d"), not(target_os = "macos"))) } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { let builder = tauri::Builder::default() // Native „Speichern unter"-Dialog + Datei-Schreiben aus der Webview. // Immer aktiv (unabhaengig von native2d/native3d), damit Datei-Exporte // im Tauri-Fenster einen echten Save-Dialog zeigen statt still zu laden. .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_fs::init()) // Selbst-Update: prueft beim Start gegen `endpoints` aus tauri.conf.json // (Gitea-Release-Asset `latest.json`), Installation via JS-Seite // (@tauri-apps/plugin-updater). `tauri_plugin_process` liefert `relaunch()` // fuer den Neustart nach der Installation. .plugin(tauri_plugin_updater::Builder::new().build()) .plugin(tauri_plugin_process::init()) // HTTP-Requests aus der Webview (Versionsverlauf, versions.json vom // selben Gitea-Release) ohne Browser-CORS-Einschraenkung; Shell: oeffnet // den DMG-Download einer aelteren Version im System-Browser (Rollback, // s. src/update/checkForUpdate.ts). .plugin(tauri_plugin_http::init()) .plugin(tauri_plugin_shell::init()) // Haelt die pro Instanz offen gelockten Projektdatei-Handles (lock.rs). .manage(lock::LockState::default()) // Laufende Blender-Aufträge ([211], blender.rs). .manage(blender::Jobs::default()); #[cfg(all(feature = "native2d", target_os = "macos"))] let builder = builder.manage(native_embed::EmbedState::default()); let builder = builder.setup(|_app| { // Native GPU-Fenster auf einem eigenen Thread hochfahren, damit der // Tauri-Hauptthread frei bleibt. NICHT auf macOS (winit verlangt dort // den Hauptthread) — dort s. `open_native2d_window`/`native_tauri_window`. #[cfg(all(any(feature = "native2d", feature = "native3d"), not(target_os = "macos")))] native::spawn(); // macOS: das separate Diagnose-Fenster (`native_tauri_window::open`) wird // nicht mehr automatisch geoeffnet — die 2D-Ansicht ist jetzt in das // Hauptfenster eingebettet (`native_embed`). Ok(()) }); #[cfg(any(feature = "native2d", feature = "native3d"))] let builder = builder.invoke_handler(tauri::generate_handler![ native_viewport_enabled, native2d_embed_rect, native2d_embed_available, native2d_embed_hide, native2d_upload, native2d_patch, native2d_highlight, native2d_layout, native2d_frame, native2d_texts, native2d_scene_texts, native2d_font, native2d_needed_fonts, push_native_scene, push_native_walls, acquire_project_lock, release_project_lock, save_atomic, blender_find, blender_job_dir, blender_start, blender_cancel, blender_open ]); #[cfg(not(any(feature = "native2d", feature = "native3d")))] let builder = builder.invoke_handler(tauri::generate_handler![ native_viewport_enabled, native2d_embed_rect, native2d_embed_available, native2d_embed_hide, native2d_upload, native2d_patch, native2d_highlight, native2d_layout, native2d_frame, native2d_texts, native2d_scene_texts, native2d_font, native2d_needed_fonts, acquire_project_lock, release_project_lock, save_atomic, blender_find, blender_job_dir, blender_start, blender_cancel, blender_open ]); builder .run(tauri::generate_context!()) .expect("error while running tauri application"); }