From be5675cd5c8e8d1bde048ff9b138296d6dbbafc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Wed, 8 Mar 2023 09:17:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(command):=E5=A2=9E=E5=8A=A0=E5=89=8D?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E4=BA=A4=E4=BA=92=E6=A8=A1=E5=9D=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands/mod.rs | 25 +++++++++++++++++++++++++ src-tauri/src/main.rs | 18 ++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 src-tauri/src/commands/mod.rs diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs new file mode 100644 index 0000000..b70eeb8 --- /dev/null +++ b/src-tauri/src/commands/mod.rs @@ -0,0 +1,25 @@ +use tauri::{App, AppHandle, Runtime, Window}; + +/// 用于持有应用实例,可存放不同的应用实例。 +pub enum AppHold<'a, R: Runtime> { + Instance(&'a App), + Handle(&'a AppHandle), +} + +/// 根据提供的应用实例和窗体实例设定窗体的标题。 +pub fn update_window_title_with_app( + app: AppHold, + window: &Window, + ext: Option, +) -> anyhow::Result<()> { + let app_name = match app { + AppHold::Instance(app) => app.package_info().name.clone(), + AppHold::Handle(app) => app.package_info().name.clone(), + }; + if let Some(ext) = ext { + window.set_title(&format!("{} - {}", app_name, ext))?; + } else { + window.set_title(&app_name)?; + } + Ok(()) +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 523550d..72c8e61 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,15 +1,21 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command -#[tauri::command] -fn greet(name: &str) -> String { - format!("Hello, {}! You've been greeted from Rust!", name) -} +mod commands; + +use commands::AppHold; +use tauri::Manager; fn main() { tauri::Builder::default() - .invoke_handler(tauri::generate_handler![greet]) + .invoke_handler(tauri::generate_handler![]) + .setup(|app| { + let main_window = app.get_window("main").unwrap(); + #[cfg(debug_assertions)] + main_window.open_devtools(); + commands::update_window_title_with_app(AppHold::Instance(app), &main_window, None)?; + Ok(()) + }) .run(tauri::generate_context!()) .expect("error while running tauri application"); }