Add Shortcut QuickChat
新增快捷键:呼出窗口并激活输入光标 默认Alt+B支持在settings中修改
Add a shortcut to show the window and focus input control. Alt + B by default and can be changed in Settings.
@Reekin is attempting to deploy a commit to the NextChat Team on Vercel.
A member of the Team first needs to authorize it.
In Rust, you can split your code into package levels instead of putting everything in main.rs. This approach makes maintenance easier.
Step 1: Create a module for the shortcut functionality
Create a new file named shortcuts.rs in the src directory of your project. In this file, you'll define the functionality related to handling shortcuts. Here's how you could structure this module:
// src/shortcuts.rs
use tauri::{AppHandle, GlobalShortcutManager};
pub fn update_shortcut(shortcut: String, handle: AppHandle) {
handle
.global_shortcut_manager()
.unregister_all()
.unwrap();
let window = handle.get_window("main").unwrap();
match handle
.global_shortcut_manager()
.register(&shortcut, move || {
println!("Shortcut triggered successfully");
window.unminimize().unwrap();
window.set_focus().unwrap();
window.emit("activate_input_field", {}).unwrap();
}) {
Ok(_) => println!("Shortcut registered successfully"),
Err(err) => eprintln!("Failed to register shortcut: {}", err),
}
}
Step 2: Refactor the main application to use the shortcuts module
In your main.rs file, you'll now modify it to use the shortcuts module for handling shortcuts. Here's how the refactored main.rs might look:
// src/main.rs
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::{CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu};
mod shortcuts; // Import the module you just created
fn main() {
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let tray_menu = SystemTrayMenu::new().add_item(quit);
let system_tray = SystemTray::new().with_menu(tray_menu);
tauri::Builder::default()
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![shortcuts::update_shortcut]) // Use the function from the shortcuts module
.system_tray(system_tray)
.on_system_tray_event(|app, event| match event {
SystemTrayEvent::LeftClick { .. } => {
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
},
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => {
std::process::exit(0);
},
_ => {}
},
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Explanation:
-
Modularization: By moving the shortcut handling logic to its own module (
shortcuts.rs), you've modularized your code. This makes themain.rsfile cleaner and focuses on the application setup and event handling. -
Importing Modules: The
mod shortcuts;line inmain.rstells Rust to include the code fromshortcuts.rsas a module. This way, you can callshortcuts::update_shortcutin yourinvoke_handler.
This approach of splitting functionality into modules makes your codebase easier to navigate and manage, especially as it grows in size and complexity.
@H0llyW00dzZ Thanks for your patient guidance, I have submitted the adjusted code.