-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
282 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ const settingMap = { | |
}, | ||
}, | ||
}, | ||
keyboard: {} | ||
} | ||
|
||
export enum SettingKeys { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { default as useGlobalCacheData } from './useCacheData' | ||
export { default as useGlobalSettingData } from './useSettingData' | ||
export { default as useGlobalTheme } from './useTheme' | ||
export { default as useGlobalKeyboard } from './useKeyboard' | ||
export { default as useGlobalOSInfo } from './useOSInfo' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { invoke } from '@tauri-apps/api' | ||
import { createGlobalStore } from 'hox' | ||
import { useEffect, useState } from 'react' | ||
import type { KeyBindingMap } from 'tinykeys' | ||
import { createKeybindingsHandler } from 'tinykeys' | ||
import { useCommandStore } from '@/stores' | ||
|
||
const getTinyKeyBinding = (keyMap: string[]) => { | ||
let keyBinding = '' | ||
keyMap.forEach((key, index) => { | ||
if (key === 'mod') { | ||
keyBinding += '$mod' | ||
} else { | ||
keyBinding += key | ||
} | ||
|
||
if (index < keyMap.length - 1) { | ||
keyBinding += '+' | ||
} | ||
}) | ||
return keyBinding | ||
} | ||
|
||
function useKeyboard() { | ||
const [keyboardInfos, setKeyboardInfos] = useState<KeyboardInfo[]>([]) | ||
const { execute } = useCommandStore() | ||
|
||
useEffect(() => { | ||
invoke<{ cmds: KeyboardInfo[] }>('get_keyboard_infos').then((res) => { | ||
setKeyboardInfos(res.cmds) | ||
}) | ||
}, []) | ||
|
||
useEffect(() => { | ||
const keybindingMap: KeyBindingMap = {} | ||
|
||
keyboardInfos.forEach((keyboardInfo) => { | ||
if (keyboardInfo.key_map.length > 0) { | ||
const keybind = getTinyKeyBinding(keyboardInfo.key_map) | ||
keybindingMap[keybind] = () => { | ||
execute(keyboardInfo.id) | ||
} | ||
} | ||
}) | ||
console.log('keybindingMap', keybindingMap) | ||
const handler = createKeybindingsHandler(keybindingMap) | ||
|
||
window.addEventListener('keydown', handler) | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handler) | ||
} | ||
}, [execute, keyboardInfos]) | ||
|
||
return { | ||
keyboardInfos, | ||
} | ||
} | ||
|
||
const [useGlobalKeyboard] = createGlobalStore(useKeyboard) | ||
|
||
export default useGlobalKeyboard | ||
|
||
interface KeyboardInfo { | ||
id: string | ||
desc: string | ||
key_map: string[] | ||
} |
41 changes: 41 additions & 0 deletions
41
apps/linebyline/src/router/Setting/KeyboardTable/KeyboardTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Table from '@mui/material/Table' | ||
import TableBody from '@mui/material/TableBody' | ||
import TableCell from '@mui/material/TableCell' | ||
import TableContainer from '@mui/material/TableContainer' | ||
import TableHead from '@mui/material/TableHead' | ||
import TableRow from '@mui/material/TableRow' | ||
import Paper from '@mui/material/Paper' | ||
import { useGlobalKeyboard, useGlobalTheme } from '@/hooks' | ||
|
||
export function KeyboardTable() { | ||
const { themeColors } = useGlobalTheme() | ||
const { keyboardInfos } = useGlobalKeyboard() | ||
|
||
return ( | ||
<TableContainer component={Paper}> | ||
<Table size='small' aria-label='caption table'> | ||
<TableHead | ||
sx={{ | ||
backgroundColor: themeColors.tipsBgColor, | ||
}} | ||
> | ||
<TableRow> | ||
<TableCell>Command</TableCell> | ||
<TableCell>Descibe</TableCell> | ||
<TableCell>Keybinding</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{keyboardInfos.map((row) => ( | ||
<TableRow key={row.id}> | ||
<TableCell>{row.id}</TableCell> | ||
<TableCell>{row.desc}</TableCell> | ||
<TableCell>{row.key_map.join('+')}</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
<caption>Currently, custom shortcut keys are not supported.</caption> | ||
</Table> | ||
</TableContainer> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './KeyboardTable' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as useEditorStore } from './useEditorStore' | ||
export { default as useCommandStore } from './useCommandStore' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { create } from 'zustand' | ||
|
||
const useCommandStore = create<CommandStore>((set, get) => { | ||
return { | ||
commands: {}, | ||
addCommand: ({ id, handler }) => { | ||
set((state) => { | ||
return { | ||
...state, | ||
commands: { | ||
...state.commands, | ||
[id]: { | ||
exec: handler, | ||
}, | ||
}, | ||
} | ||
}) | ||
}, | ||
execute: (id: string) => { | ||
const { commands } = get() | ||
const command = commands[id] | ||
if (command) { | ||
command.exec() | ||
} else { | ||
console.error(`command ${id} not found`) | ||
} | ||
}, | ||
} | ||
}) | ||
|
||
interface CommandStore { | ||
commands: Commands | ||
addCommand: (command: { id: string; handler: () => any }) => void | ||
execute: (id: string) => void | ||
} | ||
|
||
export default useCommandStore | ||
|
||
type Commands = Record<string, Command> | ||
|
||
interface Command { | ||
exec: () => void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use super::conf; | ||
use crate::fc::{create_file, exists}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct KeyboardInfo { | ||
id: String, | ||
desc: String, | ||
key_map: Vec<String>, | ||
} | ||
|
||
impl KeyboardInfo { | ||
pub fn new(id: String, desc: String, key_map: Vec<String>) -> Self { | ||
Self { id, desc, key_map } | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct KeyboardInfos { | ||
cmds: Vec<KeyboardInfo>, | ||
} | ||
|
||
impl KeyboardInfos { | ||
pub fn new() -> Self { | ||
let key_map: Vec<String> = Vec::new(); | ||
Self { | ||
cmds: vec![ | ||
KeyboardInfo::new( | ||
"app:dialog_about".to_string(), | ||
"获取应用信息".to_string(), | ||
Vec::new(), | ||
), | ||
KeyboardInfo::new( | ||
"app:window_setting".to_string(), | ||
"打开设置窗口".to_string(), | ||
Vec::new(), | ||
), | ||
KeyboardInfo::new( | ||
"app:open_folder".to_string(), | ||
"打开文件夹".to_string(), | ||
vec!["mod".to_string(), "Shift".to_string(), "o".to_string()], | ||
) | ||
], | ||
} | ||
} | ||
|
||
pub fn get_path() -> PathBuf { | ||
conf::app_root().join("keybord_map.json") | ||
} | ||
|
||
pub fn read() -> Self { | ||
match std::fs::read_to_string(Self::get_path()) { | ||
Ok(v) => { | ||
if let Ok(v2) = serde_json::from_str::<KeyboardInfos>(&v) { | ||
v2 | ||
} else { | ||
Self::default() | ||
} | ||
} | ||
Err(err) => Self::default(), | ||
} | ||
} | ||
|
||
pub fn write(self) -> Self { | ||
let path = &Self::get_path(); | ||
if !exists(path) { | ||
create_file(path).unwrap(); | ||
} | ||
if let Ok(v) = serde_json::to_string_pretty(&self) { | ||
std::fs::write(path, v).unwrap_or_else(|err| { | ||
Self::default().write(); | ||
}); | ||
} else { | ||
} | ||
self | ||
} | ||
} | ||
|
||
impl Default for KeyboardInfos { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
pub mod cmd { | ||
use super::KeyboardInfos; | ||
use tauri::{command, AppHandle}; | ||
|
||
#[command] | ||
pub fn get_keyboard_infos() -> KeyboardInfos { | ||
KeyboardInfos::read() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod conf; | ||
pub mod keyboard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters