From f16004dc427954077022b4b7a5f4966b244b90c1 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Tue, 14 Jan 2025 07:38:01 -0500 Subject: [PATCH] Add backend multiwindow support --- src-tauri/capabilities/default.json | 3 ++- src-tauri/src/lib.rs | 32 +++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index e799015..3bbee78 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -3,7 +3,8 @@ "identifier": "default", "description": "Capability for the main window", "windows": [ - "main" + "main", + "window*" ], "permissions": [ "core:default", diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 97f0a7a..659fd0b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,10 +1,17 @@ +use std::sync::{Arc, Mutex}; + use tauri_plugin_log::{Target, TargetKind}; use log::{trace, info, debug, warn, error}; use tracing_subscriber::EnvFilter; use chrono::Local; -use tauri::Manager; +use tauri::{Manager, WebviewUrl, WebviewWindowBuilder}; +#[derive(Default)] +struct AppState { + counter: u32, +} + // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ #[tauri::command] fn greet(name: &str) -> String { @@ -32,6 +39,23 @@ fn error(msg: String) { error!("{}",msg); } +#[tauri::command] +async fn create_window(app: tauri::AppHandle) { + let state = app.state::>(); + + // Lock the mutex to get mutable access: + let mut state = state.lock().unwrap(); + + // Increment the counter and generate a unique window label + let window_label = format!("window{}", state.counter); + state.counter += 1; + + // Build the new window with the unique label + let _webview_window = WebviewWindowBuilder::new(&app, &window_label, WebviewUrl::App("index.html".into())) + .build() + .unwrap(); +} + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { let pkg_name = env!("CARGO_PKG_NAME").to_string(); @@ -62,7 +86,8 @@ pub fn run() { .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_shell::init()) - .invoke_handler(tauri::generate_handler![greet, trace, debug, info, warn, error]) + .invoke_handler(tauri::generate_handler![greet, trace, debug, info, warn, error, create_window]) + // .manage(window_counter) .setup(|app| { #[cfg(debug_assertions)] // only include this code on debug builds { @@ -70,6 +95,9 @@ pub fn run() { window.open_devtools(); window.close_devtools(); } + { + app.manage(Mutex::new(AppState::default())); + } Ok(()) }) .run(tauri::generate_context!())