feat(command):增加前后端交互模块。
This commit is contained in:
		
							
								
								
									
										25
									
								
								src-tauri/src/commands/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src-tauri/src/commands/mod.rs
									
									
									
									
									
										Normal 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(()) | ||||||
|  | } | ||||||
| @@ -1,15 +1,21 @@ | |||||||
| // Prevents additional console window on Windows in release, DO NOT REMOVE!! | // Prevents additional console window on Windows in release, DO NOT REMOVE!! | ||||||
| #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] | #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] | ||||||
|  |  | ||||||
| // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command | mod commands; | ||||||
| #[tauri::command] |  | ||||||
| fn greet(name: &str) -> String { | use commands::AppHold; | ||||||
|     format!("Hello, {}! You've been greeted from Rust!", name) | use tauri::Manager; | ||||||
| } |  | ||||||
|  |  | ||||||
| fn main() { | fn main() { | ||||||
|     tauri::Builder::default() |     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!()) |         .run(tauri::generate_context!()) | ||||||
|         .expect("error while running tauri application"); |         .expect("error while running tauri application"); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user