Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ function App() {

// 应用内快捷键:开始/结束任务(默认 F10/F11,可在设置中自定义,支持组合键)
// 使用自定义事件通知 Toolbar 组件,以复用现有启动/停止逻辑
if (e.repeat) return;
if (shouldIgnoreHotkey(e)) return;
const combo = getKeyCombo(e);
if (!combo) {
Expand Down Expand Up @@ -917,9 +918,14 @@ function App() {
// Ctrl -> CommandOrControl
const toTauriKey = (k: string) => k.replace(/^Ctrl\+/i, 'CommandOrControl+');

const GLOBAL_HOTKEY_THROTTLE_MS = 1000;
let lastStartTime = 0;
const registerKeys = async () => {
try {
await register(toTauriKey(startKey), () => {
const now = Date.now();
if (now - lastStartTime < GLOBAL_HOTKEY_THROTTLE_MS) return;
lastStartTime = now;
document.dispatchEvent(
new CustomEvent('mxu-start-tasks', {
detail: { source: 'global-hotkey', combo: startKey },
Expand Down
32 changes: 19 additions & 13 deletions src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ export function Toolbar({ showAddPanel, onToggleAddPanel }: ToolbarProps) {
const resIds = await maaService.loadResource(targetId, resourcePaths);

// 注册 res_id 与资源名的映射
const resDisplayName = resource.label || resource.name;
const resDisplayName = resolveI18nText(resource.label, translations) || resource.name;
resIds.forEach((resId) => {
registerResIdName(resId, resDisplayName);
});
Expand Down Expand Up @@ -1083,9 +1083,12 @@ export function Toolbar({ showAddPanel, onToggleAddPanel }: ToolbarProps) {
}
};

const hotkeyStartingRef = useRef(false);

// 监听来自 App 的全局快捷键事件:F10 开始任务,F11 结束任务
useEffect(() => {
const handleStartTasks = async (evt: Event) => {
if (hotkeyStartingRef.current) return;
const currentInstance = useAppStore.getState().getActiveInstance();
if (!currentInstance) return;

Expand All @@ -1109,18 +1112,21 @@ export function Toolbar({ showAddPanel, onToggleAddPanel }: ToolbarProps) {
return;
}

const before = currentInstance.isRunning;
await handleStartStop();
const after = useAppStore
.getState()
.instances.find((i) => i.id === currentInstance.id)?.isRunning;
const success = !before && !!after;
addLog(currentInstance.id, {
type: success ? 'success' : 'error',
message: success
? t('logs.messages.hotkeyStartSuccess')
: t('logs.messages.hotkeyStartFailed'),
});
// 直接使用从 store 获取的最新 instance,避免闭包捕获旧的 selectedTasks
hotkeyStartingRef.current = true;
try {
const success = await startTasksForInstance(currentInstance, {
onPhaseChange: setAutoConnectPhase,
});
addLog(currentInstance.id, {
type: success ? 'success' : 'error',
message: success
? t('logs.messages.hotkeyStartSuccess')
: t('logs.messages.hotkeyStartFailed'),
});
} finally {
hotkeyStartingRef.current = false;
}
};

const handleStopTasks = async (evt: Event) => {
Expand Down
Loading