From 1914261e808fc41d0a5b67155309306864c34cf8 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Tue, 24 Oct 2023 14:24:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E5=88=9B=E5=BB=BA=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E9=87=87=E7=94=A8=20rust=20invoke?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/tauri-apps/tauri/issues/5380 --- src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/src/main.rs | 19 ++++++++++++++- src/utils/TGWindow.ts | 54 +++++++++++++++++++++++++++---------------- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 03becfa4..9515d33e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -12,6 +12,7 @@ dependencies = [ "tauri-build", "tauri-plugin-deep-link", "tauri-plugin-sql", + "tauri-utils", "url", "webview2-com 0.27.0", "windows 0.51.1", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 23b7ca8a..b1002c95 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -19,6 +19,7 @@ serde_json = "1.0" webview2-com = "0.27.0" windows = "0.51.1" url = "2.4.1" +tauri-utils = "1.5.0" # sqlite 插件 [dependencies.tauri-plugin-sql] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 03b4dbb1..51993b89 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,7 +5,8 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use tauri::Manager; +use tauri::{Manager, WindowBuilder}; +use tauri_utils::config::WindowConfig; mod client; // 放一个常数,用来判断应用是否初始化 @@ -52,6 +53,21 @@ async fn execute_js(app_handle: tauri::AppHandle, label: String, js: String) { window.eval(&js).ok().unwrap(); } +// 创建窗口 +#[tauri::command] +async fn create_window(app_handle: tauri::AppHandle, label: String, mut option: WindowConfig) { + let window_old = app_handle.get_window(&label); + option.label = label.clone(); + if window_old.is_some() { + dbg!("window exists"); + window_old.unwrap().close().unwrap(); + return; + } + let window_new = + Some(WindowBuilder::from_config(&app_handle, option).build().expect("failed to create window")); + window_new.unwrap(); +} + fn main() { tauri_plugin_deep_link::prepare("teyvatguide"); tauri::Builder::default() @@ -78,6 +94,7 @@ fn main() { register_deep_link, init_app, execute_js, + create_window, client::create_mhy_client, ]) .setup(|_app| { diff --git a/src/utils/TGWindow.ts b/src/utils/TGWindow.ts index 78f75b51..ec752884 100644 --- a/src/utils/TGWindow.ts +++ b/src/utils/TGWindow.ts @@ -1,16 +1,18 @@ /** * @file utils TGWindow.ts * @description 窗口创建相关工具函数 - * @since Beta v0.3.3 + * @since Beta v0.3.4 */ -import { window as TauriWindow } from "@tauri-apps/api"; +import { invoke, window as TauriWindow } from "@tauri-apps/api"; +import type { WindowOptions } from "@tauri-apps/api/window"; import { useAppStore } from "../store/modules/app"; /** * @description 创建TG窗口 - * @since Alpha v0.1.2 + * @since Beta v0.3.4 + * @see https://github.com/tauri-apps/tauri/issues/5380 * @param {string} url 窗口地址 * @param {string} label 窗口标签 * @param {string} title 窗口标题 @@ -32,29 +34,41 @@ export function createTGWindow( // 计算窗口位置 const left = (window.screen.width - width) / 2; const top = (window.screen.height - height) / 2; - // https://github.com/tauri-apps/tauri/issues/5380 - void new TauriWindow.WebviewWindow(label, { + const option: WindowOptions = { height, width, - x: left, - y: top, resizable, url, title, visible, - }); - void new TauriWindow.WindowManager(label).close().then(() => { - void new TauriWindow.WebviewWindow(label, { - height, - width, - x: left, - y: top, - resizable, - url, - title, - visible, - }); - }); + x: left, + y: top, + }; + const isGet = TauriWindow.WebviewWindow.getByLabel(label); + if (isGet === null) { + invoke("create_window", { label, option }) + .then(() => { + createTGWindow(url, label, title, width, height, resizable, visible); + }) + .catch((err) => { + console.error(err); + }); + } else { + isGet + .close() + .then(() => { + invoke("create_window", { label, option }) + .then(() => { + console.log(`[createTGWindow][${label}] ${title} created.`); + }) + .catch((err) => { + console.error(err); + }); + }) + .catch((err) => { + console.error(err); + }); + } } /**