Skip to content

Commit

Permalink
テーマ周りをリファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Jul 6, 2024
1 parent 409ca08 commit 1712692
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 74 deletions.
23 changes: 3 additions & 20 deletions src/backend/browser/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,32 +284,15 @@ export const api: Sandbox = {
// TODO: Impl
return;
},
async theme(newData?: string) {
if (newData != undefined) {
await this.setSetting("currentTheme", newData);
return;
}
async getAvailableThemes() {
// NOTE: Electron版では起動時にテーマ情報が必要なので、
// この実装とは違って起動時に読み込んだキャッシュを返すだけになっている。
return Promise.all(
// FIXME: themeファイルのいい感じのパスの設定
["/themes/default.json", "/themes/dark.json"].map((url) =>
fetch(url).then((res) => res.json()),
fetch(url).then((res) => res.json() as Promise<ThemeConf>),
),
)
.then((v) => ({
currentTheme: "Default",
availableThemes: v,
}))
.then((v) =>
this.getSetting("currentTheme").then(
(currentTheme) =>
({
...v,
currentTheme,
}) as { currentTheme: string; availableThemes: ThemeConf[] },
),
);
);
},
vuexReady() {
// NOTE: 何もしなくて良さそう
Expand Down
11 changes: 2 additions & 9 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,15 +922,8 @@ ipcMainHandle("HOTKEY_SETTINGS", (_, { newData }) => {
return configManager.get("hotkeySettings");
});

ipcMainHandle("THEME", (_, { newData }) => {
if (newData != undefined) {
configManager.set("currentTheme", newData);
return;
}
return {
currentTheme: configManager.get("currentTheme"),
availableThemes: themes,
};
ipcMainHandle("GET_AVAILABLE_THEMES", () => {
return themes;
});

ipcMainHandle("ON_VUEX_READY", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ const api: Sandbox = {
ipcRenderer.invoke("SET_NATIVE_THEME", source);
},

theme: (newData) => {
return ipcRenderer.invoke("THEME", { newData });
getAvailableThemes: () => {
return ipcRenderer.invoke("GET_AVAILABLE_THEMES");
},

vuexReady: () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/Dialog/SettingDialog/SettingDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,14 @@ const isDefaultConfirmedTips = computed(() => {
// 外観
const currentThemeNameComputed = computed({
get: () => store.state.themeSetting.currentTheme,
get: () => store.state.currentTheme,
set: (currentTheme: string) => {
store.dispatch("SET_THEME_SETTING", { currentTheme: currentTheme });
store.dispatch("SET_CURRENT_THEME_SETTING", { currentTheme });
},
});
const availableThemeNameComputed = computed(() => {
return [...store.state.themeSetting.availableThemes]
return [...store.state.availableThemes]
.sort((a, b) => a.order - b.order)
.map((theme) => {
return { label: theme.displayName, value: theme.name };
Expand Down
41 changes: 14 additions & 27 deletions src/store/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SavingSetting,
ExperimentalSettingType,
ThemeColorType,
ThemeConf,
ToolbarSettingType,
EngineId,
ConfirmedTips,
Expand All @@ -32,10 +31,8 @@ export const settingStoreState: SettingStoreState = {
engineIds: [],
engineInfos: {},
engineManifests: {},
themeSetting: {
currentTheme: "Default",
availableThemes: [],
},
currentTheme: "Default",
availableThemes: [],
editorFont: "default",
showTextLineNumber: false,
showAddAudioItemButton: true,
Expand Down Expand Up @@ -78,16 +75,12 @@ export const settingStore = createPartialStore<SettingStoreTypes>({
});
});

const theme = await window.backend.theme();
if (theme) {
commit("SET_THEME_SETTING", {
currentTheme: theme.currentTheme,
themes: theme.availableThemes,
});
dispatch("SET_THEME_SETTING", {
currentTheme: theme.currentTheme,
});
}
commit("SET_AVAILABLE_THEMES", {
themes: await window.backend.getAvailableThemes(),
});
dispatch("SET_CURRENT_THEME_SETTING", {
currentTheme: await window.backend.getSetting("currentTheme"),
});

dispatch("SET_ACCEPT_RETRIEVE_TELEMETRY", {
acceptRetrieveTelemetry: await window.backend.getSetting(
Expand Down Expand Up @@ -221,19 +214,13 @@ export const settingStore = createPartialStore<SettingStoreTypes>({
},
},

SET_THEME_SETTING: {
mutation(
state,
{ currentTheme, themes }: { currentTheme: string; themes?: ThemeConf[] },
) {
if (themes) {
state.themeSetting.availableThemes = themes;
}
state.themeSetting.currentTheme = currentTheme;
SET_CURRENT_THEME_SETTING: {
mutation(state, { currentTheme }: { currentTheme: string }) {
state.currentTheme = currentTheme;
},
action({ state, commit }, { currentTheme }: { currentTheme: string }) {
window.backend.theme(currentTheme);
const theme = state.themeSetting.availableThemes.find((value) => {
window.backend.setSetting("currentTheme", currentTheme);
const theme = state.availableThemes.find((value) => {
return value.name == currentTheme;
});

Expand Down Expand Up @@ -275,7 +262,7 @@ export const settingStore = createPartialStore<SettingStoreTypes>({

window.backend.setNativeTheme(theme.isDark ? "dark" : "light");

commit("SET_THEME_SETTING", {
commit("SET_CURRENT_THEME_SETTING", {
currentTheme: currentTheme,
});
},
Expand Down
12 changes: 8 additions & 4 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
MoraDataType,
SavingSetting,
ThemeConf,
ThemeSetting,
ExperimentalSettingType,
ToolbarSettingType,
UpdateInfo,
Expand Down Expand Up @@ -1527,7 +1526,8 @@ export type SettingStoreState = {
engineIds: EngineId[];
engineInfos: Record<EngineId, EngineInfo>;
engineManifests: Record<EngineId, EngineManifest>;
themeSetting: ThemeSetting;
currentTheme: string;
availableThemes: ThemeConf[];
acceptTerms: AcceptTermsStatus;
acceptRetrieveTelemetry: AcceptRetrieveTelemetryStatus;
experimentalSetting: ExperimentalSettingType;
Expand Down Expand Up @@ -1568,8 +1568,8 @@ export type SettingStoreTypes = {
action(payload: KeyValuePayload<RootMiscSettingType>): void;
};

SET_THEME_SETTING: {
mutation: { currentTheme: string; themes?: ThemeConf[] };
SET_CURRENT_THEME_SETTING: {
mutation: { currentTheme: string };
action(payload: { currentTheme: string }): void;
};

Expand Down Expand Up @@ -1783,6 +1783,10 @@ export type UiStoreTypes = {
action(payload: { activePointScrollMode: ActivePointScrollMode }): void;
};

SET_AVAILABLE_THEMES: {
mutation: { themes: ThemeConf[] };
};

DETECT_UNMAXIMIZED: {
mutation: undefined;
action(): void;
Expand Down
6 changes: 6 additions & 0 deletions src/store/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ export const uiStore = createPartialStore<UiStoreTypes>({
},
},

SET_AVAILABLE_THEMES: {
mutation(state, { themes }) {
state.availableThemes = themes;
},
},

DETECT_UNMAXIMIZED: {
mutation(state) {
state.isMaximized = false;
Expand Down
6 changes: 3 additions & 3 deletions src/type/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ export type IpcIHData = {
return: ToolbarSettingType;
};

THEME: {
args: [obj: { newData?: string }];
return: ThemeSetting | void;
GET_AVAILABLE_THEMES: {
args: [];
return: ThemeSetting;
};

ON_VUEX_READY: {
Expand Down
7 changes: 1 addition & 6 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export interface Sandbox {
getDefaultHotkeySettings(): Promise<HotkeySettingType[]>;
getDefaultToolbarSetting(): Promise<ToolbarSettingType>;
setNativeTheme(source: NativeThemeType): void;
theme(newData?: string): Promise<ThemeSetting | void>;
getAvailableThemes(): Promise<ThemeConf[]>;
vuexReady(): void;
getSetting<Key extends keyof ConfigType>(key: Key): Promise<ConfigType[Key]>;
setSetting<Key extends keyof ConfigType>(
Expand Down Expand Up @@ -552,11 +552,6 @@ export type ThemeConf = {
};
};

export type ThemeSetting = {
currentTheme: string;
availableThemes: ThemeConf[];
};

export const experimentalSettingSchema = z.object({
enablePreset: z.boolean().default(false),
shouldApplyDefaultPresetOnVoiceChanged: z.boolean().default(false),
Expand Down

0 comments on commit 1712692

Please sign in to comment.