1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-02-09 08:18:44 +01:00

Merge pull request #446 from h3poteto/iss-377

closes #377 Hide and show application in mac
This commit is contained in:
AkiraFukushima 2018-07-21 14:36:08 +09:00 committed by GitHub
commit 9f5db01ca8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -76,169 +76,225 @@ async function listAccounts () {
} }
} }
function createWindow () { async function changeAccount (account, index) {
// In MacOS, user can hide the window.
// In this time, mainWindow in not exist, so we have to create window.
if (mainWindow === null) {
await createWindow()
// We have to wait the web contents is loaded.
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('change-account', Object.assign(account, { index: index }))
})
} else {
mainWindow.webContents.send('change-account', Object.assign(account, { index: index }))
}
}
async function createWindow () {
/** /**
* List accounts * List accounts
*/ */
listAccounts() const accounts = await listAccounts()
.then((accounts) => { const accountsChange = accounts.map((a, index) => {
const accountsChange = accounts.map((a, index) => { return {
return { label: a.domain,
label: a.domain, accelerator: `CmdOrCtrl+${index + 1}`,
accelerator: `CmdOrCtrl+${index + 1}`, click: () => changeAccount(a, index)
}
})
/**
* For mac menu
*/
const macGeneralMenu = process.platform !== 'darwin' ? [] : [
{
type: 'separator'
},
{
label: 'Services',
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide Whalebird',
role: 'hide'
},
{
label: 'Hide Othres',
role: 'hideothers'
},
{
label: 'Show All',
role: 'unhide'
}
]
/**
* Set application menu
*/
const template = [
{
label: 'Whalebird',
submenu: [
{
label: 'About Whalebird',
role: 'about',
click: () => { click: () => {
mainWindow.webContents.send('change-account', Object.assign(a, { index: index })) openAboutWindow({
icon_path: path.resolve(__dirname, '../../build/icons/256x256.png'),
copyright: 'Copyright (c) 2018 AkiraFukushima',
package_json_dir: path.resolve(__dirname, '../../'),
open_devtools: process.env.NODE_ENV !== 'production'
})
} }
}
})
/**
* Set menu
*/
const template = [
{
label: 'Whalebird',
submenu: [
{
label: 'About Whalebird',
role: 'about',
click: () => {
openAboutWindow({
icon_path: path.resolve(__dirname, '../../build/icons/256x256.png'),
copyright: 'Copyright (c) 2018 AkiraFukushima',
package_json_dir: path.resolve(__dirname, '../../'),
open_devtools: process.env.NODE_ENV !== 'production'
})
}
},
{
type: 'separator'
},
{
label: 'Preferences...',
accelerator: 'CmdOrCtrl+,',
click: () => {
mainWindow.webContents.send('open-preferences')
}
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
role: 'quit'
}
]
}, },
{ {
label: 'Toot', type: 'separator'
submenu: [
{
label: 'New Toot',
accelerator: 'CmdOrCtrl+N',
click: () => {
mainWindow.webContents.send('CmdOrCtrl+N')
}
}
]
}, },
{ {
label: 'Edit', label: 'Preferences...',
submenu: [ accelerator: 'CmdOrCtrl+,',
{ click: () => {
label: 'Undo', mainWindow.webContents.send('open-preferences')
accelerator: 'CmdOrCtrl+Z', }
role: 'undo' },
}, ...macGeneralMenu,
{ {
label: 'Redo', type: 'separator'
accelerator: 'Shift+CmdOrCtrl+Z',
role: 'redo'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall'
}
]
}, },
{ {
label: 'Window', label: 'Quit',
submenu: [ accelerator: 'CmdOrCtrl+Q',
{ role: 'quit'
label: 'Close Window',
role: 'close'
},
{
label: 'Minimize',
role: 'minimize'
},
{
type: 'separator'
}
].concat(accountsChange)
.concat([
{
type: 'separator'
},
{
label: 'Jump to',
accelerator: 'CmdOrCtrl+K',
click: () => {
mainWindow.webContents.send('CmdOrCtrl+K')
}
}
])
} }
] ]
},
{
label: 'Toot',
submenu: [
{
label: 'New Toot',
accelerator: 'CmdOrCtrl+N',
click: () => {
mainWindow.webContents.send('CmdOrCtrl+N')
}
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
role: 'redo'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall'
}
]
},
{
label: 'View',
submenu: [
{
label: 'Toggle Full Screen',
role: 'togglefullscreen'
}
]
},
{
label: 'Window',
submenu: [
{
label: 'Close Window',
role: 'close'
},
{
label: 'Minimize',
role: 'minimize'
},
{
type: 'separator'
}
].concat(accountsChange)
.concat([
{
type: 'separator'
},
{
label: 'Jump to',
accelerator: 'CmdOrCtrl+K',
click: () => {
mainWindow.webContents.send('CmdOrCtrl+K')
}
}
])
}
]
const menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu) Menu.setApplicationMenu(menu)
/** /**
* Initial window options * Set dock menu for mac
*/ */
let mainWindowState = windowStateKeeper({ if (process.platform === 'darwin') {
defaultWidth: 1000, const dockMenu = Menu.buildFromTemplate(accountsChange)
height: 563 app.dock.setMenu(dockMenu)
}) }
mainWindow = new BrowserWindow({
titleBarStyle: 'hidden',
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
useContentSize: true,
icon: path.resolve(__dirname, '../../build/icons/256x256.png')
})
mainWindowState.manage(mainWindow)
mainWindow.loadURL(winURL) /**
* Initial window options
*/
let mainWindowState = windowStateKeeper({
defaultWidth: 1000,
height: 563
})
mainWindow = new BrowserWindow({
titleBarStyle: 'hidden',
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
useContentSize: true,
icon: path.resolve(__dirname, '../../build/icons/256x256.png')
})
mainWindowState.manage(mainWindow)
mainWindow.webContents.on('will-navigate', (event) => event.preventDefault()) mainWindow.loadURL(winURL)
mainWindow.on('closed', () => { mainWindow.webContents.on('will-navigate', (event) => event.preventDefault())
mainWindow = null
}) mainWindow.on('closed', () => {
}) mainWindow = null
})
} }
// Do not lower the rendering priority of Chromium when background // Do not lower the rendering priority of Chromium when background
@ -247,12 +303,11 @@ app.commandLine.appendSwitch('disable-renderer-backgrounding')
app.on('ready', createWindow) app.on('ready', createWindow)
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
app.quit() // this action is called when user click the close button.
// This is a single-window application. // In macOS, close button does not shutdown application. It is hide application window.
// So quit application when main window is closed. if (process.platform !== 'darwin') {
// if (process.platform !== 'darwin') { app.quit()
// app.quit() }
// }
}) })
app.on('activate', () => { app.on('activate', () => {