Skip to content

Commit

Permalink
refactor: ホットキー設定のデフォルト値やConfigManagerをisMac指定可能に (VOICEVOX#2426)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba authored Dec 28, 2024
1 parent eb5dd04 commit af606d7
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 248 deletions.
3 changes: 2 additions & 1 deletion src/backend/browser/browserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager";
import { ConfigType, EngineId, engineSettingSchema } from "@/type/preload";
import { ensureNotNullish } from "@/helpers/errorHelper";
import { UnreachableError } from "@/type/utility";
import { isMac } from "@/helpers/platform";

const dbName = `${import.meta.env.VITE_APP_NAME}-web`;
const settingStoreKey = "config";
Expand All @@ -20,7 +21,7 @@ const defaultEngineId = EngineId(defaultEngine.uuid);
export async function getConfigManager() {
await configManagerLock.acquire("configManager", async () => {
if (!configManager) {
configManager = new BrowserConfigManager();
configManager = new BrowserConfigManager(isMac);
await configManager.initialize();
}
});
Expand Down
19 changes: 12 additions & 7 deletions src/backend/common/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AsyncLock from "async-lock";
import {
AcceptTermsStatus,
ConfigType,
configSchema,
getConfigSchema,
DefaultStyleId,
ExperimentalSettingType,
VoiceId,
Expand All @@ -13,9 +13,10 @@ import { ensureNotNullish } from "@/helpers/errorHelper";
import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo";
import {
HotkeyCombination,
defaultHotkeySettings,
getDefaultHotkeySettings,
HotkeySettingType,
} from "@/domain/hotkeyAction";
import { isMac } from "@/helpers/platform";

const lockKey = "save";

Expand Down Expand Up @@ -308,6 +309,8 @@ export abstract class BaseConfigManager {

protected abstract getAppVersion(): string;

constructor(protected isMac: boolean) {}

public reset() {
this.config = this.getDefaultConfig();
this._save();
Expand All @@ -322,7 +325,9 @@ export abstract class BaseConfigManager {
migration(data);
}
}
this.config = this.migrateHotkeySettings(configSchema.parse(data));
this.config = this.migrateHotkeySettings(
getConfigSchema(this.isMac).parse(data),
);
this._save();
} else {
this.reset();
Expand Down Expand Up @@ -352,7 +357,7 @@ export abstract class BaseConfigManager {
private _save() {
void this.lock.acquire(lockKey, async () => {
await this.save({
...configSchema.parse({
...getConfigSchema(this.isMac).parse({
...this.config,
}),
__internal__: {
Expand Down Expand Up @@ -387,7 +392,7 @@ export abstract class BaseConfigManager {
private migrateHotkeySettings(data: ConfigType): ConfigType {
const COMBINATION_IS_NONE = HotkeyCombination("####");
const loadedHotkeys = structuredClone(data.hotkeySettings);
const hotkeysWithoutNewCombination = defaultHotkeySettings.map(
const hotkeysWithoutNewCombination = getDefaultHotkeySettings(isMac).map(
(defaultHotkey) => {
const loadedHotkey = loadedHotkeys.find(
(loadedHotkey) => loadedHotkey.action === defaultHotkey.action,
Expand All @@ -402,7 +407,7 @@ export abstract class BaseConfigManager {
const migratedHotkeys = hotkeysWithoutNewCombination.map((hotkey) => {
if (hotkey.combination === COMBINATION_IS_NONE) {
const newHotkey = ensureNotNullish(
defaultHotkeySettings.find(
getDefaultHotkeySettings(isMac).find(
(defaultHotkey) => defaultHotkey.action === hotkey.action,
),
);
Expand All @@ -429,6 +434,6 @@ export abstract class BaseConfigManager {
}

protected getDefaultConfig(): ConfigType {
return configSchema.parse({});
return getConfigSchema(this.isMac).parse({});
}
}
3 changes: 2 additions & 1 deletion src/backend/electron/electronConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { app } from "electron";
import { writeFileSafely } from "./fileHelper";
import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager";
import { ConfigType } from "@/type/preload";
import { isMac } from "@/helpers/platform";

export class ElectronConfigManager extends BaseConfigManager {
protected getAppVersion() {
Expand Down Expand Up @@ -35,7 +36,7 @@ let configManager: ElectronConfigManager | undefined;

export function getConfigManager(): ElectronConfigManager {
if (!configManager) {
configManager = new ElectronConfigManager();
configManager = new ElectronConfigManager(isMac);
}
return configManager;
}
9 changes: 6 additions & 3 deletions src/components/Dialog/HotkeySettingDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ import { useHotkeyManager, eventToCombination } from "@/plugins/hotkeyPlugin";
import {
HotkeyCombination,
HotkeyActionNameType,
defaultHotkeySettings,
getDefaultHotkeySettings,
} from "@/domain/hotkeyAction";
import { isMac } from "@/helpers/platform";
const props = defineProps<{
modelValue: boolean;
Expand Down Expand Up @@ -226,7 +227,7 @@ const setHotkeyDialogOpened = () => {
};
const isDefaultCombination = (action: string) => {
const defaultSetting = defaultHotkeySettings.find(
const defaultSetting = getDefaultHotkeySettings(isMac).find(
(value) => value.action === action,
);
const hotkeySetting = hotkeySettings.value.find(
Expand All @@ -244,7 +245,9 @@ const resetHotkey = async (action: string) => {
if (result !== "OK") return;
const setting = defaultHotkeySettings.find((value) => value.action == action);
const setting = getDefaultHotkeySettings(isMac).find(
(value) => value.action == action,
);
if (setting == undefined) {
return;
}
Expand Down
Loading

0 comments on commit af606d7

Please sign in to comment.