1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-02-08 15:58:42 +01:00

Merge pull request #2305 from h3poteto/improvement/relaunch

Reflect language settings without relaunch
This commit is contained in:
AkiraFukushima 2021-03-26 23:08:20 +09:00 committed by GitHub
commit 16dd6f42d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 17 deletions

View File

@ -217,6 +217,33 @@ const getMenuPreferences = async (): Promise<MenuPreferences> => {
return conf.menu 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() { async function createWindow() {
/** /**
* List accounts * List accounts
@ -247,23 +274,14 @@ async function createWindow() {
nativeTheme.themeSource = 'system' nativeTheme.themeSource = 'system'
/** /**
* Set application menu * Set Application Menu
*/ */
const menuPreferences = await getMenuPreferences() const autoHideMenuBar = await updateApplicationMenu(accountsChange)
const menu = ApplicationMenu(accountsChange, menuPreferences, i18next)
Menu.setApplicationMenu(menu)
let autoHideMenuBar = false
if (menuPreferences.autoHideMenu) {
autoHideMenuBar = true
}
/** /**
* Set dock menu for mac * Set dock menu for mac
*/ */
if (process.platform === 'darwin') { await updateDockMenu(accountsChange)
const dockMenu = Menu.buildFromTemplate(accountsChange)
app.dock.setMenu(dockMenu)
}
/** /**
* Windows10 don't notify, so we have to set appId * 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) 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 return conf.language.language
}) })

View File

@ -65,8 +65,8 @@ export default {
return this.$store.state.Preferences.Language.language.language return this.$store.state.Preferences.Language.language.language
}, },
set(value) { set(value) {
this.$store.dispatch('Preferences/Language/changeLanguage', value).then(() => { this.$store.dispatch('Preferences/Language/changeLanguage', value).then(key => {
this.confirm() this.$i18n.i18next.changeLanguage(key)
}) })
} }
}, },

View File

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