[Update] Prevent hotkeys function while opening config page and register the hotkeys when closed.

This commit is contained in:
Sakamoto Shiina
2025-01-17 03:59:32 +09:00
parent bac7bb15d3
commit 04f7c1f556
5 changed files with 84 additions and 103 deletions

View File

@@ -6,7 +6,6 @@ import {
import { import {
KeyEventController, KeyEventController,
GlobalHotKeyController,
StartPythonController, StartPythonController,
UiLanguageController, UiLanguageController,
ConfigPageCloseTriggerController, ConfigPageCloseTriggerController,
@@ -33,7 +32,6 @@ export const App = () => {
return ( return (
<div className={styles.container}> <div className={styles.container}>
<KeyEventController /> <KeyEventController />
<GlobalHotKeyController />
<StartPythonController /> <StartPythonController />
<UiLanguageController /> <UiLanguageController />
<ConfigPageCloseTriggerController /> <ConfigPageCloseTriggerController />

View File

@@ -9,6 +9,8 @@ import {
useMainFunction, useMainFunction,
} from "@logics_main"; } from "@logics_main";
import { useHotkeys } from "@logics_configs";
import { useStore_MainFunctionsStateMemory } from "@store"; import { useStore_MainFunctionsStateMemory } from "@store";
export const ConfigPageCloseTriggerController = () => { export const ConfigPageCloseTriggerController = () => {
@@ -27,6 +29,8 @@ export const ConfigPageCloseTriggerController = () => {
volumeCheckStop_Speaker, volumeCheckStop_Speaker,
} = useVolume(); } = useVolume();
const { registerShortcuts, unregisterAll } = useHotkeys();
const memorizeLatestMainFunctionsState = () => { const memorizeLatestMainFunctionsState = () => {
updateMainFunctionsStateMemory({ updateMainFunctionsStateMemory({
@@ -43,9 +47,11 @@ export const ConfigPageCloseTriggerController = () => {
useEffect(() => { useEffect(() => {
if (currentIsOpenedConfigPage.data === true) { // When config page is opened. if (currentIsOpenedConfigPage.data === true) { // When config page is opened.
memorizeLatestMainFunctionsState(); memorizeLatestMainFunctionsState();
unregisterAll();
if (currentTranscriptionSendStatus.data === true) setTranscriptionSend(false); if (currentTranscriptionSendStatus.data === true) setTranscriptionSend(false);
if (currentTranscriptionReceiveStatus.data === true) setTranscriptionReceive(false); if (currentTranscriptionReceiveStatus.data === true) setTranscriptionReceive(false);
} else if (currentIsOpenedConfigPage.data === false) { // When config page is closed. } else if (currentIsOpenedConfigPage.data === false) { // When config page is closed.
registerShortcuts();
if (currentMicThresholdCheckStatus.data === true) volumeCheckStop_Mic(); if (currentMicThresholdCheckStatus.data === true) volumeCheckStop_Mic();
if (currentSpeakerThresholdCheckStatus.data === true) volumeCheckStop_Speaker(); if (currentSpeakerThresholdCheckStatus.data === true) volumeCheckStop_Speaker();
restoreMainFunctionState(); restoreMainFunctionState();

View File

@@ -1,99 +0,0 @@
import { appWindow } from "@tauri-apps/api/window";
import { register, unregisterAll, isRegistered } from "@tauri-apps/api/globalShortcut";
import { useEffect } from "react";
import { store } from "@store";
import { useHotkeys } from "@logics_configs";
import { useMainFunction } from "@logics_main";
// 修飾キーのパースを行う関数
const parseHotkey = (hotkeyString) => {
const keyMap = {
Ctrl: "Control",
Alt: "Alt",
Shift: "Shift",
Meta: "Super",
};
// 入力文字列を分解して対応するキーを再結合
return hotkeyString
.map((key) => keyMap[key] || key) // 修飾キーをマップし、その他はそのまま
.join("+");
};
export const GlobalHotKeyController = () => {
const { currentHotkeys } = useHotkeys();
const {
toggleTranslation,
toggleTranscriptionSend,
toggleTranscriptionReceive,
} = useMainFunction();
useEffect(() => {
const registerShortcuts = async () => {
try {
// 既存のショートカットをすべて解除
await unregisterAll();
const hotkeyEntries = Object.entries(currentHotkeys.data);
for (const [actionKey, hotkeyRaw] of hotkeyEntries) {
if (!hotkeyRaw) continue;
const shortcut = parseHotkey(hotkeyRaw);
const isAlreadyRegistered = await isRegistered(shortcut);
if (!isAlreadyRegistered) {
await register(shortcut, async () => {
console.log(`Shortcut for "${actionKey}" triggered.`);
switch (actionKey) {
case "toggle_vrct_visibility": {
const minimized = await appWindow.isMinimized();
if (minimized) {
appWindow.unminimize();
await appWindow.setFocus();
store.text_area_ref.current?.focus();
} else {
appWindow.minimize();
}
break;
}
case "toggle_translation": {
toggleTranslation();
break;
}
case "toggle_transcription_send": {
toggleTranscriptionSend();
break;
}
case "toggle_transcription_receive": {
toggleTranscriptionReceive();
break;
}
default: {
console.warn(`No handler defined for action: ${actionKey}`);
break;
}
}
});
console.log(`Registered global shortcut: ${shortcut} for action: ${actionKey}`);
}
}
} catch (error) {
console.error("Failed to register global shortcuts:", error);
}
};
registerShortcuts();
// クリーンアップ関数でショートカットを解除
return () => {
unregisterAll().catch((error) => {
console.error("Failed to unregister shortcuts:", error);
});
};
}, [currentHotkeys.data]); // 監視対象を全体に変更
return null;
};

View File

@@ -1,5 +1,4 @@
export { KeyEventController } from "./KeyEventController"; export { KeyEventController } from "./KeyEventController";
export { GlobalHotKeyController } from "./GlobalHotKeyController";
export { StartPythonController } from "./StartPythonController"; export { StartPythonController } from "./StartPythonController";
export { UiLanguageController } from "./UiLanguageController"; export { UiLanguageController } from "./UiLanguageController";
export { ConfigPageCloseTriggerController } from "./ConfigPageCloseTriggerController"; export { ConfigPageCloseTriggerController } from "./ConfigPageCloseTriggerController";

View File

@@ -1,9 +1,19 @@
import { useStore_Hotkeys } from "@store"; import { appWindow } from "@tauri-apps/api/window";
import { store, useStore_Hotkeys } from "@store";
import { useStdoutToPython } from "@logics/useStdoutToPython"; import { useStdoutToPython } from "@logics/useStdoutToPython";
import { useMainFunction } from "@logics_main";
import { register, unregisterAll, isRegistered } from "@tauri-apps/api/globalShortcut";
export const useHotkeys = () => { export const useHotkeys = () => {
const { asyncStdoutToPython } = useStdoutToPython(); const { asyncStdoutToPython } = useStdoutToPython();
const { currentHotkeys, updateHotkeys, pendingHotkeys } = useStore_Hotkeys(); const { currentHotkeys, updateHotkeys, pendingHotkeys } = useStore_Hotkeys();
const {
toggleTranslation,
toggleTranscriptionSend,
toggleTranscriptionReceive,
} = useMainFunction();
const getHotkeys = () => { const getHotkeys = () => {
pendingHotkeys(); pendingHotkeys();
@@ -17,8 +27,58 @@ export const useHotkeys = () => {
...hotkeys, ...hotkeys,
}; };
asyncStdoutToPython("/set/data/hotkeys", send_obj); asyncStdoutToPython("/set/data/hotkeys", send_obj);
};
const registerShortcuts = async () => {
try {
await unregisterAll();
const hotkeyEntries = Object.entries(currentHotkeys.data);
for (const [actionKey, hotkeyRaw] of hotkeyEntries) {
if (!hotkeyRaw) continue;
const shortcut = parseHotkey(hotkeyRaw);
const isAlreadyRegistered = await isRegistered(shortcut);
if (!isAlreadyRegistered) {
await register(shortcut, async () => {
switch (actionKey) {
case "toggle_vrct_visibility": {
const minimized = await appWindow.isMinimized();
if (minimized) {
appWindow.unminimize();
await appWindow.setFocus();
store.text_area_ref.current?.focus();
} else {
appWindow.minimize();
}
break;
}
case "toggle_translation": {
toggleTranslation();
break;
}
case "toggle_transcription_send": {
toggleTranscriptionSend();
break;
}
case "toggle_transcription_receive": {
toggleTranscriptionReceive();
break;
}
default: {
console.warn(`No handler defined for action: ${actionKey}`);
break;
}
}
});
// console.log(`Registered global shortcut: ${shortcut} for action: ${actionKey}`);
}
}
} catch (error) {
console.error("Failed to register global shortcuts:", error);
}
}; };
return { return {
@@ -26,5 +86,22 @@ export const useHotkeys = () => {
getHotkeys, getHotkeys,
updateHotkeys, updateHotkeys,
setHotkeys, setHotkeys,
registerShortcuts,
unregisterAll,
}; };
}; };
// 修飾キーのパースを行う関数
const parseHotkey = (hotkeyString) => {
const keyMap = {
Ctrl: "Control",
Alt: "Alt",
Shift: "Shift",
Meta: "Super",
};
return hotkeyString
.map((key) => keyMap[key] || key)
.join("+");
};