feat(command):增加前后端交互模块。

This commit is contained in:
徐涛 2023-03-08 09:17:36 +08:00
parent 91a5a2d1f4
commit be5675cd5c
2 changed files with 37 additions and 6 deletions

View File

@ -0,0 +1,25 @@
use tauri::{App, AppHandle, Runtime, Window};
/// 用于持有应用实例,可存放不同的应用实例。
pub enum AppHold<'a, R: Runtime> {
Instance(&'a App<R>),
Handle(&'a AppHandle<R>),
}
/// 根据提供的应用实例和窗体实例设定窗体的标题。
pub fn update_window_title_with_app<R: Runtime>(
app: AppHold<R>,
window: &Window<R>,
ext: Option<String>,
) -> 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(())
}

View File

@ -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");
}