Skip to content

Commit 5d0000c

Browse files
lencxSomsak Meesangpetch
authored and
Somsak Meesangpetch
committed
fix: customize global shortcuts (lencx#108)
1 parent e9cd995 commit 5d0000c

File tree

3 files changed

+64
-83
lines changed

3 files changed

+64
-83
lines changed

UPDATE_LOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
## v0.7.4
44

55
fix:
6-
- trying to resolve linux errors: `error while loading shared libraries`
76
- customize global shortcuts (`Menu -> Preferences -> Control Center -> General -> Global Shortcut`)
87

98
## v0.7.3

src-tauri/src/app/setup.rs

+14-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{app::window, conf::ChatConfJson, utils};
22
use log::info;
33
use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, GlobalShortcutManager, Manager};
4-
use wry::application::accelerator::Accelerator;
54

65
pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>> {
76
info!("stepup");
@@ -15,31 +14,21 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
1514
});
1615

1716
if let Some(v) = chat_conf.global_shortcut {
18-
info!("global_shortcut: `{}`", v);
19-
match v.parse::<Accelerator>() {
20-
Ok(_) => {
21-
info!("global_shortcut_register");
22-
let handle = app.app_handle();
23-
let mut shortcut = app.global_shortcut_manager();
24-
shortcut
25-
.register(&v, move || {
26-
if let Some(w) = handle.get_window("core") {
27-
if w.is_visible().unwrap() {
28-
w.hide().unwrap();
29-
} else {
30-
w.show().unwrap();
31-
w.set_focus().unwrap();
32-
}
33-
}
34-
})
35-
.unwrap_or_else(|err| {
36-
info!("global_shortcut_register_error: {}", err);
37-
});
17+
info!("global_shortcut");
18+
let handle = app.app_handle();
19+
let mut shortcut = app.global_shortcut_manager();
20+
shortcut.register(&v, move|| {
21+
if let Some(w) = handle.get_window("core") {
22+
if w.is_visible().unwrap() {
23+
w.hide().unwrap();
24+
} else {
25+
w.show().unwrap();
26+
w.set_focus().unwrap();
27+
}
3828
}
39-
Err(err) => {
40-
info!("global_shortcut_parse_error: {}", err);
41-
}
42-
}
29+
}).unwrap_or_else(|err| {
30+
info!("global_shortcut_register_error: {}", err);
31+
});
4332
} else {
4433
info!("global_shortcut_unregister");
4534
};

src/view/General.tsx

+50-57
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { useEffect, useState } from 'react';
22
import { Form, Radio, Switch, Input, Button, Space, message, Tooltip } from 'antd';
33
import { QuestionCircleOutlined } from '@ant-design/icons';
4-
import { invoke, shell, path } from '@tauri-apps/api';
4+
import { invoke } from '@tauri-apps/api';
55
import { platform } from '@tauri-apps/api/os';
66
import { ask } from '@tauri-apps/api/dialog';
77
import { relaunch } from '@tauri-apps/api/process';
88
import { clone, omit, isEqual } from 'lodash';
99

10-
import useInit from '@/hooks/useInit';
11-
import { DISABLE_AUTO_COMPLETE, chatRoot } from '@/utils';
10+
import { DISABLE_AUTO_COMPLETE } from '@/utils';
1211

1312
const OriginLabel = ({ url }: { url: string }) => {
1413
return (
1514
<span>
16-
Switch Origin <Tooltip title={`Default: ${url}`}><QuestionCircleOutlined style={{ color: '#1677ff' }} /></Tooltip>
15+
Switch Origin <Tooltip title={`Default: ${url}`}><QuestionCircleOutlined /></Tooltip>
1716
</span>
1817
)
1918
}
@@ -30,25 +29,26 @@ const GlobalShortcut = () => {
3029
<a href="https://tauri.app/v1/api/js/globalshortcut" target="_blank">https://tauri.app/v1/api/js/globalshortcut</a>
3130
</div>
3231
)}>
33-
<QuestionCircleOutlined style={{ color: '#1677ff' }} />
32+
<QuestionCircleOutlined />
3433
</Tooltip>
3534
</div>
3635
)
3736
}
3837

3938
export default function General() {
4039
const [form] = Form.useForm();
41-
const [jsonPath, setJsonPath] = useState('');
4240
const [platformInfo, setPlatform] = useState<string>('');
4341
const [chatConf, setChatConf] = useState<any>(null);
4442

45-
useInit(async () => {
46-
setJsonPath(await path.join(await chatRoot(), 'chat.conf.json'));
47-
43+
const init = async () => {
4844
setPlatform(await platform());
4945
const chatData = await invoke('get_chat_conf');
5046
setChatConf(chatData);
51-
});
47+
}
48+
49+
useEffect(() => {
50+
init();
51+
}, [])
5252

5353
useEffect(() => {
5454
form.setFieldsValue(clone(chatConf));
@@ -73,54 +73,47 @@ export default function General() {
7373
};
7474

7575
return (
76-
<>
77-
<div className="chat-table-tip">
78-
<div className="chat-sync-path">
79-
<div>PATH: <a onClick={() => shell.open(jsonPath)} title={jsonPath}>{jsonPath}</a></div>
80-
</div>
81-
</div>
82-
<Form
83-
form={form}
84-
style={{ maxWidth: 500 }}
85-
onFinish={onFinish}
86-
labelCol={{ span: 8 }}
87-
wrapperCol={{ span: 15, offset: 1 }}
88-
>
89-
<Form.Item label="Theme" name="theme">
90-
<Radio.Group>
91-
<Radio value="Light">Light</Radio>
92-
<Radio value="Dark">Dark</Radio>
93-
</Radio.Group>
94-
</Form.Item>
95-
<Form.Item label="Stay On Top" name="stay_on_top" valuePropName="checked">
76+
<Form
77+
form={form}
78+
style={{ maxWidth: 500 }}
79+
onFinish={onFinish}
80+
labelCol={{ span: 8 }}
81+
wrapperCol={{ span: 15, offset: 1 }}
82+
>
83+
<Form.Item label="Theme" name="theme">
84+
<Radio.Group>
85+
<Radio value="Light">Light</Radio>
86+
<Radio value="Dark">Dark</Radio>
87+
</Radio.Group>
88+
</Form.Item>
89+
<Form.Item label="Stay On Top" name="stay_on_top" valuePropName="checked">
90+
<Switch />
91+
</Form.Item>
92+
<Form.Item label={<GlobalShortcut />} name="global_shortcut">
93+
<Input placeholder="CmdOrCtrl+Shift+O" {...DISABLE_AUTO_COMPLETE} />
94+
</Form.Item>
95+
{platformInfo === 'darwin' && (
96+
<Form.Item label="Titlebar" name="titlebar" valuePropName="checked">
9697
<Switch />
9798
</Form.Item>
98-
<Form.Item label={<GlobalShortcut />} name="global_shortcut">
99-
<Input placeholder="CmdOrCtrl+Shift+O" {...DISABLE_AUTO_COMPLETE} />
100-
</Form.Item>
101-
{platformInfo === 'darwin' && (
102-
<Form.Item label="Titlebar" name="titlebar" valuePropName="checked">
103-
<Switch />
104-
</Form.Item>
105-
)}
106-
<Form.Item label={<OriginLabel url={chatConf?.default_origin} />} name="origin">
107-
<Input placeholder="https://chat.openai.com" {...DISABLE_AUTO_COMPLETE} />
108-
</Form.Item>
109-
<Form.Item label="User Agent (Window)" name="ua_window">
110-
<Input.TextArea autoSize={{ minRows: 4, maxRows: 4 }} {...DISABLE_AUTO_COMPLETE} placeholder="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" />
111-
</Form.Item>
112-
<Form.Item label="User Agent (SystemTray)" name="ua_tray">
113-
<Input.TextArea autoSize={{ minRows: 4, maxRows: 4 }} {...DISABLE_AUTO_COMPLETE} placeholder="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" />
114-
</Form.Item>
115-
<Form.Item>
116-
<Space size={20}>
117-
<Button onClick={onCancel}>Cancel</Button>
118-
<Button type="primary" htmlType="submit">
119-
Submit
120-
</Button>
121-
</Space>
122-
</Form.Item>
123-
</Form>
124-
</>
99+
)}
100+
<Form.Item label={<OriginLabel url={chatConf?.default_origin} />} name="origin">
101+
<Input placeholder="https://chat.openai.com" {...DISABLE_AUTO_COMPLETE} />
102+
</Form.Item>
103+
<Form.Item label="User Agent (Window)" name="ua_window">
104+
<Input.TextArea autoSize={{ minRows: 4, maxRows: 4 }} {...DISABLE_AUTO_COMPLETE} placeholder="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" />
105+
</Form.Item>
106+
<Form.Item label="User Agent (SystemTray)" name="ua_tray">
107+
<Input.TextArea autoSize={{ minRows: 4, maxRows: 4 }} {...DISABLE_AUTO_COMPLETE} placeholder="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" />
108+
</Form.Item>
109+
<Form.Item>
110+
<Space size={20}>
111+
<Button onClick={onCancel}>Cancel</Button>
112+
<Button type="primary" htmlType="submit">
113+
Submit
114+
</Button>
115+
</Space>
116+
</Form.Item>
117+
</Form>
125118
)
126119
}

0 commit comments

Comments
 (0)