Skip to content

Commit

Permalink
Add Shortcut QuickChat
Browse files Browse the repository at this point in the history
快捷键:呼出窗口并激活输入光标
  • Loading branch information
Reekin committed Mar 10, 2024
1 parent ff9f0e6 commit a695c4e
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import { ExportMessageModal } from "./exporter";
import { getClientConfig } from "../config/client";
import { useAllModels } from "../utils/hooks";
import { MultimodalContent } from "../client/api";
import { listen } from '@tauri-apps/api/event';

const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
loading: () => <LoadingIcon />,
Expand Down Expand Up @@ -711,6 +712,16 @@ function _Chat() {
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(measure, [userInput]);

useEffect(() => {
const unlisten = listen('activate_input_field', () => {
inputRef.current?.focus();
});

return () => {
unlisten.then((f) => f());;
};
}, []);

// chat commands shortcuts
const chatCommands = useChatCommand({
new: () => chatStore.newSession(),
Expand Down
2 changes: 2 additions & 0 deletions app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { AuthPage } from "./auth";
import { getClientConfig } from "../config/client";
import { ClientApi } from "../client/api";
import { useAccessStore } from "../store";
import { invoke } from '@tauri-apps/api/tauri';

export function Loading(props: { noLogo?: boolean }) {
return (
Expand Down Expand Up @@ -183,6 +184,7 @@ export function useLoadData() {
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
invoke('update_shortcut', { shortcut: config.shortcutQuickChat });
}

export function Home() {
Expand Down
23 changes: 23 additions & 0 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import { useSyncStore } from "../store/sync";
import { nanoid } from "nanoid";
import { useMaskStore } from "../store/mask";
import { ProviderType } from "../utils/cloud";
import { invoke } from '@tauri-apps/api/tauri';

function EditPromptModal(props: { id: string; onClose: () => void }) {
const promptStore = usePromptStore();
Expand Down Expand Up @@ -557,6 +558,9 @@ function SyncItems() {
</>
);
}
async function updateShortcut(newShortcut: string) {
await invoke('update_shortcut', { shortcut: newShortcut });
}

export function Settings() {
const navigate = useNavigate();
Expand Down Expand Up @@ -650,6 +654,8 @@ export function Settings() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

updateShortcut(config.shortcutQuickChat);

const clientConfig = useMemo(() => getClientConfig(), []);
const showAccessCode = enabledAccessControl && !clientConfig?.isApp;

Expand Down Expand Up @@ -825,6 +831,23 @@ export function Settings() {
}
></input>
</ListItem>

<ListItem
title={Locale.Settings.ShortcutQuickChat}
>
<input
type="text"
value={config.shortcutQuickChat}
onChange={(e) =>{
updateConfig(
(config) =>
(config.shortcutQuickChat = e.currentTarget.value),
);
updateShortcut(config.shortcutQuickChat);
}
}
></input>
</ListItem>
</List>

<SyncItems />
Expand Down
1 change: 1 addition & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const cn = {
Title: "预览气泡",
SubTitle: "在预览气泡中预览 Markdown 内容",
},
ShortcutQuickChat:"快捷键-快速对话",
AutoGenerateTitle: {
Title: "自动生成标题",
SubTitle: "根据对话内容生成合适的标题",
Expand Down
1 change: 1 addition & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const en: LocaleType = {
Title: "Send Preview Bubble",
SubTitle: "Preview markdown in bubble",
},
ShortcutQuickChat:"Shortcut-QuickChat",
AutoGenerateTitle: {
Title: "Auto Generate Title",
SubTitle: "Generate a suitable title based on the conversation content",
Expand Down
1 change: 1 addition & 0 deletions app/store/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const DEFAULT_CONFIG = {
theme: Theme.Auto as Theme,
tightBorder: !!getClientConfig()?.isApp,
sendPreviewBubble: true,
shortcutQuickChat: "Alt+B",
enableAutoGenerateTitle: true,
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,

Expand Down
1 change: 1 addition & 0 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export function isVisionModel(model: string) {
return (
// model.startsWith("gpt-4-vision") ||
// model.startsWith("gemini-pro-vision") ||
model.startsWith("claude-3-opus-20240229") ||
model.includes("vision")
);
}
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tauri-build = { version = "1.5.1", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.5.4", features = [
tauri = { version = "1.5.4", features = [ "global-shortcut-all",
"notification-all",
"fs-all",
"clipboard-all",
Expand Down
20 changes: 20 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::{GlobalShortcutManager, Manager};

#[tauri::command]
fn update_shortcut(shortcut: String, handle: tauri::AppHandle) {
handle
.global_shortcut_manager()
.unregister_all()
.unwrap();

let window = handle.get_window("main").unwrap();
handle
.global_shortcut_manager()
.register(&shortcut, move || {
window.show().unwrap();
window.set_focus().unwrap();
window.emit("activate_input_field", {}).unwrap();
})
.unwrap();
}

fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![update_shortcut])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
3 changes: 3 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"tauri": {
"allowlist": {
"all": false,
"globalShortcut": {
"all":true
},
"shell": {
"all": false,
"open": true
Expand Down

0 comments on commit a695c4e

Please sign in to comment.