mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-10 08:58:15 +08:00
♻️ 重构窗口创建逻辑,采用 rust invoke
https://github.com/tauri-apps/tauri/issues/5380
This commit is contained in:
1
src-tauri/Cargo.lock
generated
1
src-tauri/Cargo.lock
generated
@@ -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",
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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| {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user