Add backend multiwindow support
This commit is contained in:
parent
3134c8d8a5
commit
f16004dc42
|
|
@ -3,7 +3,8 @@
|
|||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": [
|
||||
"main"
|
||||
"main",
|
||||
"window*"
|
||||
],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
|
|
|
|||
|
|
@ -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::<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)]
|
||||
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!())
|
||||
|
|
|
|||
Loading…
Reference in New Issue