Reflect language settings without relaunch

This commit is contained in:
AkiraFukushima 2021-03-26 22:27:44 +09:00
parent acf7c74ea3
commit da4a8bc37c
3 changed files with 50 additions and 17 deletions

View File

@ -217,6 +217,33 @@ const getMenuPreferences = async (): Promise<MenuPreferences> => {
return conf.menu
}
/**
* Set application menu
* @return Whether the menu bar is auto hide.
*/
const updateApplicationMenu = async (accountsChange: Array<MenuItemConstructorOptions>): Promise<boolean> => {
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<MenuItemConstructorOptions>) => {
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<MenuItemConstructorOptions> = 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
})

View File

@ -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)
})
}
},

View File

@ -44,12 +44,12 @@ const mutations: MutationTree<LanguageState> = {
}
const actions: ActionTree<LanguageState, RootState> = {
loadLanguage: async ({ commit }) => {
loadLanguage: async ({ commit }): Promise<string> => {
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<string> => {
const value: string = await win.ipcRenderer.invoke('change-language', key)
commit(MUTATION_TYPES.CHANGE_LANGUAGE, value)
return value