Skip to content

Commit

Permalink
ショートカットキー周りのisMacを{isMac: boolean}にする
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Dec 29, 2024
1 parent 4cab1c5 commit 7c52ee3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/backend/browser/browserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const defaultEngineId = EngineId(defaultEngine.uuid);
export async function getConfigManager() {
await configManagerLock.acquire("configManager", async () => {
if (!configManager) {
configManager = new BrowserConfigManager(isMac);
configManager = new BrowserConfigManager({ isMac });
await configManager.initialize();
}
});
Expand Down
37 changes: 20 additions & 17 deletions src/backend/common/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,17 @@ export abstract class BaseConfigManager {
protected config: ConfigType | undefined;

private lock = new AsyncLock();
protected isMac: boolean;

protected abstract exists(): Promise<boolean>;
protected abstract load(): Promise<Record<string, unknown> & Metadata>;
protected abstract save(config: ConfigType & Metadata): Promise<void>;

protected abstract getAppVersion(): string;

constructor(protected isMac: boolean) {}
constructor({ isMac }: { isMac: boolean }) {
this.isMac = isMac;
}

public reset() {
this.config = this.getDefaultConfig();
Expand All @@ -326,7 +329,7 @@ export abstract class BaseConfigManager {
}
}
this.config = this.migrateHotkeySettings(
getConfigSchema(this.isMac).parse(data),
getConfigSchema({ isMac: this.isMac }).parse(data),
);
this._save();
} else {
Expand Down Expand Up @@ -357,7 +360,7 @@ export abstract class BaseConfigManager {
private _save() {
void this.lock.acquire(lockKey, async () => {
await this.save({
...getConfigSchema(this.isMac).parse({
...getConfigSchema({ isMac: this.isMac }).parse({
...this.config,
}),
__internal__: {
Expand Down Expand Up @@ -392,22 +395,22 @@ export abstract class BaseConfigManager {
private migrateHotkeySettings(data: ConfigType): ConfigType {
const COMBINATION_IS_NONE = HotkeyCombination("####");
const loadedHotkeys = structuredClone(data.hotkeySettings);
const hotkeysWithoutNewCombination = getDefaultHotkeySettings(isMac).map(
(defaultHotkey) => {
const loadedHotkey = loadedHotkeys.find(
(loadedHotkey) => loadedHotkey.action === defaultHotkey.action,
);
const hotkeyWithoutCombination: HotkeySettingType = {
action: defaultHotkey.action,
combination: COMBINATION_IS_NONE,
};
return loadedHotkey ?? hotkeyWithoutCombination;
},
);
const hotkeysWithoutNewCombination = getDefaultHotkeySettings({
isMac,
}).map((defaultHotkey) => {
const loadedHotkey = loadedHotkeys.find(
(loadedHotkey) => loadedHotkey.action === defaultHotkey.action,
);
const hotkeyWithoutCombination: HotkeySettingType = {
action: defaultHotkey.action,
combination: COMBINATION_IS_NONE,
};
return loadedHotkey ?? hotkeyWithoutCombination;
});
const migratedHotkeys = hotkeysWithoutNewCombination.map((hotkey) => {
if (hotkey.combination === COMBINATION_IS_NONE) {
const newHotkey = ensureNotNullish(
getDefaultHotkeySettings(isMac).find(
getDefaultHotkeySettings({ isMac }).find(
(defaultHotkey) => defaultHotkey.action === hotkey.action,
),
);
Expand All @@ -434,6 +437,6 @@ export abstract class BaseConfigManager {
}

protected getDefaultConfig(): ConfigType {
return getConfigSchema(this.isMac).parse({});
return getConfigSchema({ isMac: this.isMac }).parse({});
}
}
2 changes: 1 addition & 1 deletion src/backend/electron/electronConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let configManager: ElectronConfigManager | undefined;

export function getConfigManager(): ElectronConfigManager {
if (!configManager) {
configManager = new ElectronConfigManager(isMac);
configManager = new ElectronConfigManager({ isMac });
}
return configManager;
}
4 changes: 2 additions & 2 deletions src/components/Dialog/HotkeySettingDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const setHotkeyDialogOpened = () => {
};
const isDefaultCombination = (action: string) => {
const defaultSetting = getDefaultHotkeySettings(isMac).find(
const defaultSetting = getDefaultHotkeySettings({ isMac }).find(
(value) => value.action === action,
);
const hotkeySetting = hotkeySettings.value.find(
Expand All @@ -245,7 +245,7 @@ const resetHotkey = async (action: string) => {
if (result !== "OK") return;
const setting = getDefaultHotkeySettings(isMac).find(
const setting = getDefaultHotkeySettings({ isMac }).find(
(value) => value.action == action,
);
if (setting == undefined) {
Expand Down
6 changes: 5 additions & 1 deletion src/domain/hotkeyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export const hotkeySettingSchema = z.object({
});
export type HotkeySettingType = z.infer<typeof hotkeySettingSchema>;

export function getDefaultHotkeySettings(isMac: boolean): HotkeySettingType[] {
export function getDefaultHotkeySettings({
isMac,
}: {
isMac: boolean;
}): HotkeySettingType[] {
return [
{
action: "音声書き出し",
Expand Down
4 changes: 2 additions & 2 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export const rootMiscSettingSchema = z.object({
});
export type RootMiscSettingType = z.infer<typeof rootMiscSettingSchema>;

export function getConfigSchema(isMac: boolean) {
export function getConfigSchema({ isMac }: { isMac: boolean }) {
return z
.object({
inheritAudioInfo: z.boolean().default(true),
Expand All @@ -427,7 +427,7 @@ export function getConfigSchema(isMac: boolean) {
.default({}),
hotkeySettings: hotkeySettingSchema
.array()
.default(getDefaultHotkeySettings(isMac)),
.default(getDefaultHotkeySettings({ isMac })),
toolbarSetting: toolbarSettingSchema
.array()
.default(defaultToolbarButtonSetting),
Expand Down
18 changes: 11 additions & 7 deletions tests/unit/backend/common/configManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseConfigManager } from "@/backend/common/ConfigManager";
import { getConfigSchema } from "@/type/preload";

const configBase = {
...getConfigSchema(false).parse({}),
...getConfigSchema({ isMac: false }).parse({}),
__internal__: {
migrations: {
version: "999.999.999",
Expand All @@ -13,6 +13,10 @@ const configBase = {
};

class TestConfigManager extends BaseConfigManager {
constructor() {
super({ isMac: false });
}

getAppVersion() {
return "999.999.999";
}
Expand Down Expand Up @@ -49,7 +53,7 @@ it("新規作成できる", async () => {
async () => undefined,
);

const configManager = new TestConfigManager(false);
const configManager = new TestConfigManager();
await configManager.initialize();
expect(configManager).toBeTruthy();
});
Expand All @@ -62,7 +66,7 @@ it("バージョンが保存される", async () => {
.spyOn(TestConfigManager.prototype, "save")
.mockImplementation(async () => undefined);

const configManager = new TestConfigManager(false);
const configManager = new TestConfigManager();
await configManager.initialize();
await configManager.ensureSaved();
expect(saveSpy).toHaveBeenCalled();
Expand All @@ -82,7 +86,7 @@ for (const [version, data] of pastConfigs) {
async () => data,
);

const configManager = new TestConfigManager(false);
const configManager = new TestConfigManager();
await configManager.initialize();
expect(configManager).toBeTruthy();

Expand Down Expand Up @@ -122,7 +126,7 @@ it("0.19.1からのマイグレーション時にハミング・ソングスタ
).map((key) => getStyleIdFromVoiceId(key));

// マイグレーション
const configManager = new TestConfigManager(false);
const configManager = new TestConfigManager();
await configManager.initialize();
const presets = configManager.get("presets");
const defaultPresetKeys = configManager.get("defaultPresetKeys");
Expand Down Expand Up @@ -164,7 +168,7 @@ it("getできる", async () => {
}),
);

const configManager = new TestConfigManager(false);
const configManager = new TestConfigManager();
await configManager.initialize();
expect(configManager.get("inheritAudioInfo")).toBe(false);
});
Expand All @@ -183,7 +187,7 @@ it("setできる", async () => {
}),
);

const configManager = new TestConfigManager(false);
const configManager = new TestConfigManager();
await configManager.initialize();
configManager.set("inheritAudioInfo", true);
expect(configManager.get("inheritAudioInfo")).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/domain/hotkeyAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "@/domain/hotkeyAction";

test("すべてのホットキーに初期値が設定されている", async () => {
const defaultHotkeySettings = getDefaultHotkeySettings(false);
const defaultHotkeySettings = getDefaultHotkeySettings({ isMac: false });
const allActionNames = new Set(hotkeyActionNameSchema.options);
const defaultHotkeyActionsNames = new Set(
defaultHotkeySettings.map((setting) => setting.action),
Expand Down

0 comments on commit 7c52ee3

Please sign in to comment.