Skip to content

Commit

Permalink
♻️ 重构窗口创建逻辑,采用 rust invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Oct 24, 2023
1 parent 35dc972 commit 1914261
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 18 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// 放一个常数,用来判断应用是否初始化
Expand Down Expand Up @@ -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()
Expand All @@ -78,6 +94,7 @@ fn main() {
register_deep_link,
init_app,
execute_js,
create_window,
client::create_mhy_client,
])
.setup(|_app| {
Expand Down
54 changes: 34 additions & 20 deletions src/utils/TGWindow.ts
Original file line number Diff line number Diff line change
@@ -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 窗口标题
Expand All @@ -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);
});
}
}

/**
Expand Down

0 comments on commit 1914261

Please sign in to comment.