Skip to content
Merged
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
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
38 changes: 20 additions & 18 deletions src/backend/common/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
getDefaultHotkeySettings,
HotkeySettingType,
} from "@/domain/hotkeyAction";
import { isMac } from "@/helpers/platform";

const lockKey = "save";

Expand Down Expand Up @@ -300,6 +299,7 @@ export type Metadata = {
*/
export abstract class BaseConfigManager {
protected config: ConfigType | undefined;
protected isMac: boolean;

private lock = new AsyncLock();

Expand All @@ -309,7 +309,9 @@ export abstract class BaseConfigManager {

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 +328,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 +359,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 +394,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: this.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: this.isMac }).find(
(defaultHotkey) => defaultHotkey.action === hotkey.action,
),
);
Expand All @@ -434,6 +436,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
Loading