[Refactor] Refactor.

This commit is contained in:
Sakamoto Shiina
2025-01-12 22:56:00 +09:00
parent ff57454073
commit a76ea77d5d
2 changed files with 52 additions and 81 deletions

View File

@@ -27,15 +27,12 @@ export const GlobalHotKeyController = () => {
const shortcut_raw = currentHotkeys.data.toggle_active_vrct; const shortcut_raw = currentHotkeys.data.toggle_active_vrct;
console.log(shortcut_raw); console.log(shortcut_raw);
if (!shortcut_raw) { if (!shortcut_raw) {
console.warn("No hotkey defined."); console.warn("No hotkey defined.");
return; return;
} }
// 入力文字列をTauriで使える形式に変換
const shortcut = parseHotkey(shortcut_raw); const shortcut = parseHotkey(shortcut_raw);
// const shortcut = "F9";
try { try {
// 既存のショートカットをすべて解除 // 既存のショートカットをすべて解除
@@ -46,14 +43,14 @@ export const GlobalHotKeyController = () => {
if (!isAlreadyRegistered) { if (!isAlreadyRegistered) {
await register(shortcut, async () => { await register(shortcut, async () => {
console.log(`Shortcut "${shortcut}" triggered, setting focus.`); console.log(`Shortcut "${shortcut}" triggered, setting focus.`);
// const minimized = await appWindow.isMinimized(); const minimized = await appWindow.isMinimized();
// if (minimized === true) { if (minimized === true) {
// appWindow.unminimize(); appWindow.unminimize();
// await appWindow.setFocus(); await appWindow.setFocus();
// store.text_area_ref.current?.focus(); store.text_area_ref.current?.focus();
// } else { } else {
// appWindow.minimize(); appWindow.minimize();
// } }
}); });
console.log(`Registered global shortcut: ${shortcut}`); console.log(`Registered global shortcut: ${shortcut}`);
} }

View File

@@ -3,130 +3,104 @@ import { _Entry } from "../_atoms/_entry/_Entry";
import { useState, useRef } from "react"; import { useState, useRef } from "react";
export const HotkeysEntry = (props) => { export const HotkeysEntry = (props) => {
const [is_accepting_input, setIsAcceptingInput] = useState(false); // キー入力受付中かどうか const [isAcceptingInput, setIsAcceptingInput] = useState(false);
const lastKeyRef = useRef(null); // 直前のキーを保持 const [displayValue, setDisplayValue] = useState(props.value[props.hotkey_id]);
const [displayValue, setDisplayValue] = useState(props.value[props.hotkey_id]); // 表示用の値 const lastKeyRef = useRef(null);
const isModifierOnlyRef = useRef(false); // 修飾キー単体かどうかのフラグ const isModifierOnlyRef = useRef(false);
const entryRef = useRef(null); const entryRef = useRef(null);
const pressedKeys = useRef(new Set());
const keysRef = useRef([]);
const pressedKeys = useRef(new Set()); // 押されているキーを追跡 const updateHotkeys = (keys) => {
const keysRef = useRef([]); // 最新のkeysを保存
const setHotkeys = (keys) => {
entryRef.current.blur(); entryRef.current.blur();
props.setHotkeys({ [props.hotkey_id]: keys }); props.setHotkeys({ [props.hotkey_id]: keys });
}; };
const processKey = (key) => {
if (/^[a-zA-Z]$/.test(key)) return key.toUpperCase();
if (key === "Meta") return "Super";
return key;
};
const handleKeyInput = (event) => { const handleKeyInput = (event) => {
const keys = []; const keys = [];
const nonModifierKeys = []; // 修飾キー以外を追跡する配列 const nonModifierKeys = [];
// 修飾キーを判定して追加(重複防止) ["Ctrl", "Shift", "Alt", "Super"].forEach((modKey) => {
if (event.ctrlKey && !keys.includes("Ctrl")) keys.push("Ctrl"); if (event[`${modKey.toLowerCase()}Key`] && !keys.includes(modKey)) {
if (event.shiftKey && !keys.includes("Shift")) keys.push("Shift"); keys.push(modKey);
if (event.altKey && !keys.includes("Alt")) keys.push("Alt"); }
if (event.metaKey && !keys.includes("Super")) keys.push("Super"); });
let register_key = event.key === "Meta" ? "Super" : event.key; const key = processKey(event.key);
// アルファベットの場合は大文字に変換
if (/^[a-zA-Z]$/.test(register_key)) {
register_key = register_key.toUpperCase();
}
// 修飾キー以外を追加
if (!["Control", "Shift", "Alt", "Meta"].includes(event.key)) { if (!["Control", "Shift", "Alt", "Meta"].includes(event.key)) {
keys.push(register_key); keys.push(key);
nonModifierKeys.push(register_key); // 修飾キー以外のキーを追跡 nonModifierKeys.push(key);
} }
// キーが既に追跡されていない場合のみ追加 if (!pressedKeys.current.has(key)) {
if (!pressedKeys.current.has(register_key)) { pressedKeys.current.add(key);
pressedKeys.current.add(register_key);
} }
// 最新のキー構成を保存 keysRef.current = keys;
keysRef.current = [...keys];
// 表示用の値を更新
setDisplayValue(keys.join(" + ")); setDisplayValue(keys.join(" + "));
// 修飾キー単体かどうかを更新
isModifierOnlyRef.current = nonModifierKeys.length === 0; isModifierOnlyRef.current = nonModifierKeys.length === 0;
// 修飾キーのみの場合は登録処理をスキップ
if (isModifierOnlyRef.current) return;
}; };
const handleKeyDown = (event) => { const handleKeyDown = (event) => {
event.preventDefault(); // デフォルト動作を防ぐ event.preventDefault();
if (lastKeyRef.current === event.key) return;
// 直前のキーと同じならスキップ lastKeyRef.current = event.key;
const currentKey = event.key;
if (lastKeyRef.current === currentKey) {
return;
}
lastKeyRef.current = currentKey; // 今回のキーを記録
handleKeyInput(event); handleKeyInput(event);
}; };
const handleKeyUp = (event) => { const handleKeyUp = (event) => {
lastKeyRef.current = null; // キーが離されたらリセット lastKeyRef.current = null;
const key = processKey(event.key);
pressedKeys.current.delete(key);
// 修飾キーのみの場合でも表示は維持
if (isModifierOnlyRef.current) { if (isModifierOnlyRef.current) {
setDisplayValue(""); // 非修飾キーが含まれていた場合リセット setDisplayValue("");
} }
let register_key = event.key === "Meta" ? "Super" : event.key;
// アルファベットの場合は大文字に変換
if (/^[a-zA-Z]$/.test(register_key)) {
register_key = register_key.toUpperCase();
}
// 押されているキーから削除
pressedKeys.current.delete(register_key);
// 全てのキーが放された場合
if (pressedKeys.current.size === 0) { if (pressedKeys.current.size === 0) {
// 修飾キーのみの場合はスキップ
const hasNonModifierKeys = keysRef.current.some( const hasNonModifierKeys = keysRef.current.some(
(key) => !["Ctrl", "Shift", "Alt", "Super"].includes(key) (key) => !["Ctrl", "Shift", "Alt", "Super"].includes(key)
); );
if (!hasNonModifierKeys) { if (hasNonModifierKeys) {
return; updateHotkeys(keysRef.current);
} }
// 保存されたキー構成を使用して登録
setHotkeys(keysRef.current);
} }
}; };
const handleBlur = () => { const handleBlur = () => {
setIsAcceptingInput(false); setIsAcceptingInput(false);
pressedKeys.current.clear(); pressedKeys.current.clear();
}; };
const handleDelete = () => {
updateHotkeys(null);
setDisplayValue("");
};
return ( return (
<div className={styles.container}> <div className={styles.container}>
<_Entry <_Entry
ref={entryRef} ref={entryRef}
type="text" type="text"
placeholder="Press hotkeys keys" placeholder="Press hotkeys keys"
onFocus={() => [setIsAcceptingInput(true)]} onFocus={() => setIsAcceptingInput(true)}
onBlur={handleBlur} onBlur={handleBlur}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp} onKeyUp={handleKeyUp}
value={displayValue} // 表示用の値を設定 value={displayValue}
width="20rem" width="20rem"
is_activated={is_accepting_input} is_activated={isAcceptingInput}
readOnly readOnly
/> />
<button <button className={styles.delete_button} onClick={handleDelete}>
className={styles.delete_button}
onClick={() => [setHotkeys(null), setDisplayValue("")]}
>
Delete Delete
</button> </button>
</div> </div>