NextChat icon indicating copy to clipboard operation
NextChat copied to clipboard

Add Shortcut QuickChat

Open Reekin opened this issue 1 year ago • 4 comments

新增快捷键:呼出窗口并激活输入光标 默认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 avatar Mar 10 '24 08:03 Reekin

@Reekin is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Mar 10 '24 08:03 vercel[bot]

Your build has completed!

Preview deployment

github-actions[bot] avatar Mar 11 '24 11:03 github-actions[bot]

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 the main.rs file cleaner and focuses on the application setup and event handling.

  • Importing Modules: The mod shortcuts; line in main.rs tells Rust to include the code from shortcuts.rs as a module. This way, you can call shortcuts::update_shortcut in your invoke_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 avatar Mar 11 '24 13:03 H0llyW00dzZ

@H0llyW00dzZ Thanks for your patient guidance, I have submitted the adjusted code.

Reekin avatar Mar 12 '24 05:03 Reekin