mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract global shortcut registration service
This commit is contained in:
81
src/core/services/shortcut-service.ts
Normal file
81
src/core/services/shortcut-service.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { BrowserWindow, globalShortcut } from "electron";
|
||||||
|
|
||||||
|
export interface GlobalShortcutConfig {
|
||||||
|
toggleVisibleOverlayGlobal: string | null | undefined;
|
||||||
|
toggleInvisibleOverlayGlobal: string | null | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerGlobalShortcutsService(options: {
|
||||||
|
shortcuts: GlobalShortcutConfig;
|
||||||
|
onToggleVisibleOverlay: () => void;
|
||||||
|
onToggleInvisibleOverlay: () => void;
|
||||||
|
onOpenYomitanSettings: () => void;
|
||||||
|
isDev: boolean;
|
||||||
|
getMainWindow: () => BrowserWindow | null;
|
||||||
|
}): void {
|
||||||
|
const visibleShortcut = options.shortcuts.toggleVisibleOverlayGlobal;
|
||||||
|
const invisibleShortcut = options.shortcuts.toggleInvisibleOverlayGlobal;
|
||||||
|
const normalizedVisible = visibleShortcut?.replace(/\s+/g, "").toLowerCase();
|
||||||
|
const normalizedInvisible = invisibleShortcut
|
||||||
|
?.replace(/\s+/g, "")
|
||||||
|
.toLowerCase();
|
||||||
|
|
||||||
|
if (visibleShortcut) {
|
||||||
|
const toggleVisibleRegistered = globalShortcut.register(
|
||||||
|
visibleShortcut,
|
||||||
|
() => {
|
||||||
|
options.onToggleVisibleOverlay();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (!toggleVisibleRegistered) {
|
||||||
|
console.warn(
|
||||||
|
`Failed to register global shortcut toggleVisibleOverlayGlobal: ${visibleShortcut}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
invisibleShortcut &&
|
||||||
|
normalizedInvisible &&
|
||||||
|
normalizedInvisible !== normalizedVisible
|
||||||
|
) {
|
||||||
|
const toggleInvisibleRegistered = globalShortcut.register(
|
||||||
|
invisibleShortcut,
|
||||||
|
() => {
|
||||||
|
options.onToggleInvisibleOverlay();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (!toggleInvisibleRegistered) {
|
||||||
|
console.warn(
|
||||||
|
`Failed to register global shortcut toggleInvisibleOverlayGlobal: ${invisibleShortcut}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
invisibleShortcut &&
|
||||||
|
normalizedInvisible &&
|
||||||
|
normalizedInvisible === normalizedVisible
|
||||||
|
) {
|
||||||
|
console.warn(
|
||||||
|
"Skipped registering toggleInvisibleOverlayGlobal because it collides with toggleVisibleOverlayGlobal",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const settingsRegistered = globalShortcut.register("Alt+Shift+Y", () => {
|
||||||
|
options.onOpenYomitanSettings();
|
||||||
|
});
|
||||||
|
if (!settingsRegistered) {
|
||||||
|
console.warn("Failed to register global shortcut: Alt+Shift+Y");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.isDev) {
|
||||||
|
const devtoolsRegistered = globalShortcut.register("F12", () => {
|
||||||
|
const mainWindow = options.getMainWindow();
|
||||||
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||||
|
mainWindow.webContents.toggleDevTools();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!devtoolsRegistered) {
|
||||||
|
console.warn("Failed to register global shortcut: F12");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/main.ts
72
src/main.ts
@@ -129,6 +129,7 @@ import {
|
|||||||
hasMpvWebsocketPlugin,
|
hasMpvWebsocketPlugin,
|
||||||
SubtitleWebSocketService,
|
SubtitleWebSocketService,
|
||||||
} from "./core/services/subtitle-ws-service";
|
} from "./core/services/subtitle-ws-service";
|
||||||
|
import { registerGlobalShortcutsService } from "./core/services/shortcut-service";
|
||||||
import {
|
import {
|
||||||
ConfigService,
|
ConfigService,
|
||||||
DEFAULT_CONFIG,
|
DEFAULT_CONFIG,
|
||||||
@@ -2330,71 +2331,14 @@ function openYomitanSettings(): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function registerGlobalShortcuts(): void {
|
function registerGlobalShortcuts(): void {
|
||||||
const shortcuts = getConfiguredShortcuts();
|
registerGlobalShortcutsService({
|
||||||
const visibleShortcut = shortcuts.toggleVisibleOverlayGlobal;
|
shortcuts: getConfiguredShortcuts(),
|
||||||
const invisibleShortcut = shortcuts.toggleInvisibleOverlayGlobal;
|
onToggleVisibleOverlay: () => toggleVisibleOverlay(),
|
||||||
const normalizedVisible = visibleShortcut?.replace(/\s+/g, "").toLowerCase();
|
onToggleInvisibleOverlay: () => toggleInvisibleOverlay(),
|
||||||
const normalizedInvisible = invisibleShortcut
|
onOpenYomitanSettings: () => openYomitanSettings(),
|
||||||
?.replace(/\s+/g, "")
|
isDev,
|
||||||
.toLowerCase();
|
getMainWindow: () => mainWindow,
|
||||||
|
|
||||||
if (visibleShortcut) {
|
|
||||||
const toggleVisibleRegistered = globalShortcut.register(
|
|
||||||
visibleShortcut,
|
|
||||||
() => {
|
|
||||||
toggleVisibleOverlay();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (!toggleVisibleRegistered) {
|
|
||||||
console.warn(
|
|
||||||
`Failed to register global shortcut toggleVisibleOverlayGlobal: ${visibleShortcut}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
invisibleShortcut &&
|
|
||||||
normalizedInvisible &&
|
|
||||||
normalizedInvisible !== normalizedVisible
|
|
||||||
) {
|
|
||||||
const toggleInvisibleRegistered = globalShortcut.register(
|
|
||||||
invisibleShortcut,
|
|
||||||
() => {
|
|
||||||
toggleInvisibleOverlay();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (!toggleInvisibleRegistered) {
|
|
||||||
console.warn(
|
|
||||||
`Failed to register global shortcut toggleInvisibleOverlayGlobal: ${invisibleShortcut}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
invisibleShortcut &&
|
|
||||||
normalizedInvisible &&
|
|
||||||
normalizedInvisible === normalizedVisible
|
|
||||||
) {
|
|
||||||
console.warn(
|
|
||||||
"Skipped registering toggleInvisibleOverlayGlobal because it collides with toggleVisibleOverlayGlobal",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const settingsRegistered = globalShortcut.register("Alt+Shift+Y", () => {
|
|
||||||
openYomitanSettings();
|
|
||||||
});
|
});
|
||||||
if (!settingsRegistered) {
|
|
||||||
console.warn("Failed to register global shortcut: Alt+Shift+Y");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDev) {
|
|
||||||
const devtoolsRegistered = globalShortcut.register("F12", () => {
|
|
||||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
|
||||||
mainWindow.webContents.toggleDevTools();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!devtoolsRegistered) {
|
|
||||||
console.warn("Failed to register global shortcut: F12");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getConfiguredShortcuts() {
|
function getConfiguredShortcuts() {
|
||||||
|
|||||||
Reference in New Issue
Block a user