Skip to content

Commit

Permalink
Merge pull request #1514 from h3poteto/iss-1348
Browse files Browse the repository at this point in the history
closes #1348 Add a menu to hide menu bar
  • Loading branch information
h3poteto committed Jun 8, 2020
2 parents 3996934 + de6e173 commit 5605ddf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/config/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"toggle_full_screen": "Toggle Full Screen"
},
"window": {
"always_show_menu_bar": "Always Show Menu Bar",
"name": "Window",
"close": "Close Window",
"open": "Open Window",
Expand Down
49 changes: 44 additions & 5 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { Proxy } from '~/src/types/proxy'
import ProxyConfiguration from './proxy'
import confirm from './timelines'
import { EnabledTimelines } from '~/src/types/enabledTimelines'
import { Menu as MenuPreferences } from '~/src/types/preference'

/**
* Context menu
Expand Down Expand Up @@ -199,6 +200,12 @@ async function getLanguage() {
}
}

const getMenuPreferences = async (): Promise<MenuPreferences> => {
const preferences = new Preferences(preferencesDBPath)
const conf = await preferences.load()
return conf.menu
}

async function createWindow() {
/**
* List accounts
Expand Down Expand Up @@ -226,7 +233,13 @@ async function createWindow() {
/**
* Set application menu
*/
ApplicationMenu(accountsChange, i18next)
const menuPreferences = await getMenuPreferences()
const menu = ApplicationMenu(accountsChange, menuPreferences, i18next)
Menu.setApplicationMenu(menu)
let autoHideMenuBar = false
if (menuPreferences.autoHideMenu) {
autoHideMenuBar = true
}

/**
* Set dock menu for mac
Expand Down Expand Up @@ -262,6 +275,7 @@ async function createWindow() {
height: mainWindowState.height,
useContentSize: true,
icon: path.resolve(__dirname, '../../build/icons/256x256.png'),
autoHideMenuBar: autoHideMenuBar,
webPreferences: {
// It is required to use ipcRenderer in renderer process.
// But it is not secure, so if you want to disable this option, please use preload script.
Expand Down Expand Up @@ -1192,9 +1206,9 @@ app.on('ready', () => {
class EmptyTokenError {}

/**
* Set application menu
* Genrate application menu
*/
const ApplicationMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n: I18n) => {
const ApplicationMenu = (accountsChange: Array<MenuItemConstructorOptions>, menu: MenuPreferences, i18n: I18n): Menu => {
/**
* For mac menu
*/
Expand Down Expand Up @@ -1330,6 +1344,17 @@ const ApplicationMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n
{
label: i18n.t('main_menu.window.name'),
submenu: [
{
label: i18n.t('main_menu.window.always_show_menu_bar'),
type: 'checkbox',
checked: !menu.autoHideMenu,
click: item => {
changeMenuAutoHide(!item.checked)
}
},
{
type: 'separator'
},
{
label: i18n.t('main_menu.window.close'),
role: 'close'
Expand Down Expand Up @@ -1364,8 +1389,7 @@ const ApplicationMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n
}
]

const menu: Menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
return Menu.buildFromTemplate(template)
}

const TrayMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n: I18n): Menu => {
Expand All @@ -1392,6 +1416,21 @@ const TrayMenu = (accountsChange: Array<MenuItemConstructorOptions>, i18n: I18n)
return menu
}

const changeMenuAutoHide = async (autoHide: boolean) => {
if (mainWindow === null) {
return null
}
mainWindow.autoHideMenuBar = autoHide
mainWindow.setMenuBarVisibility(!autoHide)
const preferences = new Preferences(preferencesDBPath)
preferences.update({
menu: {
autoHideMenu: autoHide
}
})
return null
}

async function reopenWindow() {
if (mainWindow === null) {
await createWindow()
Expand Down
9 changes: 7 additions & 2 deletions src/main/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Timeline } from '~/src/types/timeline'
import { Notify } from '~/src/types/notify'
import { Appearance } from '~/src/types/appearance'
import { Language as LanguageSet } from '~/src/types/language'
import { General, State, Notification, BaseConfig, Other } from '~/src/types/preference'
import { General, State, Notification, BaseConfig, Other, Menu } from '~/src/types/preference'
import { Proxy, ProxySource } from '~/src/types/proxy'

const sound: Sound = {
Expand Down Expand Up @@ -79,13 +79,18 @@ const proxy: Proxy = {
}
}

const menu: Menu = {
autoHideMenu: false
}

const Base: BaseConfig = {
general: general,
state: state,
language: language,
notification: notification,
appearance: appearance,
proxy: proxy
proxy: proxy,
menu: menu
}

export default class Preferences {
Expand Down
5 changes: 5 additions & 0 deletions src/types/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ export type Notification = {
notify: Notify
}

export type Menu = {
autoHideMenu: boolean
}

export type BaseConfig = {
general: General
state: State
language: Language
notification: Notification
appearance: Appearance
proxy: Proxy
menu: Menu
}

0 comments on commit 5605ddf

Please sign in to comment.