Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shortcut QuickChat #4257

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Add Shortcut QuickChat #4257

wants to merge 5 commits into from

Conversation

Reekin
Copy link

@Reekin Reekin commented Mar 10, 2024

新增快捷键:呼出窗口并激活输入光标
默认Alt+B支持在settings中修改

Add a shortcut to show the window and focus input control.
Alt + B by default and can be changed in Settings.

快捷键:呼出窗口并激活输入光标
Copy link

vercel bot commented Mar 10, 2024

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

A member of the Team first needs to authorize it.

之前的提交有问题,窗口最小化无法呼出
Copy link
Contributor

Your build has completed!

Preview deployment

@H0llyW00dzZ
Copy link
Contributor

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.

Split a the code according to the guidance.
@Reekin
Copy link
Author

Reekin commented Mar 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants