From da4a8bc37cefa8a196ca6ebcd58a04c7b802a335 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Fri, 26 Mar 2021 22:27:44 +0900 Subject: [PATCH] Reflect language settings without relaunch --- src/main/index.ts | 57 +++++++++++++++---- .../components/Preferences/Language.vue | 4 +- src/renderer/store/Preferences/Language.ts | 6 +- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 2b748764..21eb26bf 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -217,6 +217,33 @@ const getMenuPreferences = async (): Promise => { return conf.menu } +/** + * Set application menu + * @return Whether the menu bar is auto hide. + */ +const updateApplicationMenu = async (accountsChange: Array): Promise => { + const menuPreferences = await getMenuPreferences() + const menu = ApplicationMenu(accountsChange, menuPreferences, i18next) + Menu.setApplicationMenu(menu) + let autoHideMenuBar = false + if (menuPreferences.autoHideMenu) { + autoHideMenuBar = true + } + return autoHideMenuBar +} + +/** + * Set dock menu for mac + */ +const updateDockMenu = async (accountsChange: Array) => { + if (process.platform !== 'darwin') { + return + } + + const dockMenu = Menu.buildFromTemplate(accountsChange) + app.dock.setMenu(dockMenu) +} + async function createWindow() { /** * List accounts @@ -247,23 +274,14 @@ async function createWindow() { nativeTheme.themeSource = 'system' /** - * Set application menu + * Set Application Menu */ - const menuPreferences = await getMenuPreferences() - const menu = ApplicationMenu(accountsChange, menuPreferences, i18next) - Menu.setApplicationMenu(menu) - let autoHideMenuBar = false - if (menuPreferences.autoHideMenu) { - autoHideMenuBar = true - } + const autoHideMenuBar = await updateApplicationMenu(accountsChange) /** * Set dock menu for mac */ - if (process.platform === 'darwin') { - const dockMenu = Menu.buildFromTemplate(accountsChange) - app.dock.setMenu(dockMenu) - } + await updateDockMenu(accountsChange) /** * Windows10 don't notify, so we have to set appId @@ -1018,6 +1036,21 @@ ipcMain.handle('change-language', async (_: IpcMainInvokeEvent, value: string) = } }) i18next.changeLanguage(conf.language.language) + + const accounts = await listAccounts() + const accountsChange: Array = accounts.map((a, index) => { + return { + label: a.domain, + accelerator: `CmdOrCtrl+${index + 1}`, + click: () => changeAccount(a, index) + } + }) + + await updateApplicationMenu(accountsChange) + await updateDockMenu(accountsChange) + if (process.platform !== 'darwin' && tray !== null) { + tray.setContextMenu(TrayMenu(accountsChange, i18next)) + } return conf.language.language }) diff --git a/src/renderer/components/Preferences/Language.vue b/src/renderer/components/Preferences/Language.vue index 36575b49..954b2a64 100644 --- a/src/renderer/components/Preferences/Language.vue +++ b/src/renderer/components/Preferences/Language.vue @@ -65,8 +65,8 @@ export default { return this.$store.state.Preferences.Language.language.language }, set(value) { - this.$store.dispatch('Preferences/Language/changeLanguage', value).then(() => { - this.confirm() + this.$store.dispatch('Preferences/Language/changeLanguage', value).then(key => { + this.$i18n.i18next.changeLanguage(key) }) } }, diff --git a/src/renderer/store/Preferences/Language.ts b/src/renderer/store/Preferences/Language.ts index 96ba15f9..46cbc621 100644 --- a/src/renderer/store/Preferences/Language.ts +++ b/src/renderer/store/Preferences/Language.ts @@ -44,12 +44,12 @@ const mutations: MutationTree = { } const actions: ActionTree = { - loadLanguage: async ({ commit }) => { + loadLanguage: async ({ commit }): Promise => { const conf: BaseConfig = await win.ipcRenderer.invoke('get-preferences') commit(MUTATION_TYPES.UPDATE_LANGUAGE, conf.language as LanguageSet) - return conf + return conf.language.language }, - changeLanguage: async ({ commit }, key: string) => { + changeLanguage: async ({ commit }, key: string): Promise => { const value: string = await win.ipcRenderer.invoke('change-language', key) commit(MUTATION_TYPES.CHANGE_LANGUAGE, value) return value