Add backend multiwindow support

This commit is contained in:
Skyler Lehmkuhl 2025-01-14 07:38:01 -05:00
parent 3134c8d8a5
commit f16004dc42
2 changed files with 32 additions and 3 deletions

View File

@ -3,7 +3,8 @@
"identifier": "default", "identifier": "default",
"description": "Capability for the main window", "description": "Capability for the main window",
"windows": [ "windows": [
"main" "main",
"window*"
], ],
"permissions": [ "permissions": [
"core:default", "core:default",

View File

@ -1,10 +1,17 @@
use std::sync::{Arc, Mutex};
use tauri_plugin_log::{Target, TargetKind}; use tauri_plugin_log::{Target, TargetKind};
use log::{trace, info, debug, warn, error}; use log::{trace, info, debug, warn, error};
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use chrono::Local; 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/ // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command] #[tauri::command]
fn greet(name: &str) -> String { fn greet(name: &str) -> String {
@ -32,6 +39,23 @@ fn error(msg: String) {
error!("{}",msg); error!("{}",msg);
} }
#[tauri::command]
async fn create_window(app: tauri::AppHandle) {
let state = app.state::<Mutex<AppState>>();
// 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)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
let pkg_name = env!("CARGO_PKG_NAME").to_string(); let pkg_name = env!("CARGO_PKG_NAME").to_string();
@ -62,7 +86,8 @@ pub fn run() {
.plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_shell::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| { .setup(|app| {
#[cfg(debug_assertions)] // only include this code on debug builds #[cfg(debug_assertions)] // only include this code on debug builds
{ {
@ -70,6 +95,9 @@ pub fn run() {
window.open_devtools(); window.open_devtools();
window.close_devtools(); window.close_devtools();
} }
{
app.manage(Mutex::new(AppState::default()));
}
Ok(()) Ok(())
}) })
.run(tauri::generate_context!()) .run(tauri::generate_context!())