2019-05-05 13:34:21 +02:00
|
|
|
const path = require('path')
|
2019-05-06 01:50:08 +02:00
|
|
|
const { app, BrowserWindow, systemPreferences } = require('electron')
|
2019-05-13 22:54:40 +02:00
|
|
|
const { touchBarWrapper } = require('react-touchbar-electron')
|
2019-05-16 22:25:19 +02:00
|
|
|
const buildMenu = require('./menu')
|
2019-05-05 13:34:21 +02:00
|
|
|
|
|
|
|
let mainWindow
|
|
|
|
|
|
|
|
// Keep a reference for dev mode
|
|
|
|
let isDev = false
|
|
|
|
if (
|
|
|
|
process.defaultApp ||
|
|
|
|
/[\\/]electron-prebuilt[\\/]/.test(process.execPath) ||
|
|
|
|
/[\\/]electron[\\/]/.test(process.execPath)
|
|
|
|
) {
|
|
|
|
isDev = true
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:39:59 +02:00
|
|
|
const width = 620
|
|
|
|
const height = 440
|
2019-05-05 13:34:21 +02:00
|
|
|
|
2019-05-06 01:50:08 +02:00
|
|
|
const isDarkMode = systemPreferences.isDarkMode()
|
|
|
|
|
2019-05-06 14:02:49 +02:00
|
|
|
const createWindow = async () => {
|
2019-05-05 13:34:21 +02:00
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
minWidth: width,
|
|
|
|
minHeight: height,
|
|
|
|
acceptFirstMouse: true,
|
|
|
|
titleBarStyle: 'hiddenInset',
|
|
|
|
fullscreenWindowTitle: true,
|
2019-05-06 01:50:08 +02:00
|
|
|
backgroundColor: isDarkMode ? '#141414' : '#fff',
|
2019-05-05 13:34:21 +02:00
|
|
|
frame: false,
|
|
|
|
show: false,
|
2019-05-17 00:19:00 +02:00
|
|
|
title: 'Ocean Balance',
|
2019-05-05 13:34:21 +02:00
|
|
|
webPreferences: {
|
2019-05-09 00:58:05 +02:00
|
|
|
nodeIntegration: true,
|
|
|
|
scrollBounce: true
|
2019-05-05 13:34:21 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.loadURL(
|
|
|
|
isDev
|
|
|
|
? 'http://localhost:8080'
|
2019-05-11 22:55:51 +02:00
|
|
|
: `file://${path.join(__dirname, '../build/index.html')}`
|
2019-05-05 13:34:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if (isDev) {
|
|
|
|
const {
|
|
|
|
default: installExtension,
|
|
|
|
REACT_DEVELOPER_TOOLS
|
|
|
|
} = require('electron-devtools-installer')
|
|
|
|
|
2019-05-06 14:02:49 +02:00
|
|
|
try {
|
|
|
|
const name = await installExtension(REACT_DEVELOPER_TOOLS)
|
|
|
|
console.log(`Added Extension: ${name}`) // eslint-disable-line no-console
|
|
|
|
} catch (error) {
|
|
|
|
console.log('An error occurred: ', error) // eslint-disable-line no-console
|
|
|
|
}
|
2019-05-05 13:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow.once('ready-to-show', () => {
|
|
|
|
mainWindow.show()
|
|
|
|
mainWindow.focus()
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
mainWindow = null
|
|
|
|
})
|
|
|
|
|
|
|
|
//
|
|
|
|
// Events
|
|
|
|
//
|
|
|
|
mainWindow.on('enter-full-screen', () => {
|
|
|
|
mainWindow.webContents.executeJavaScript(
|
|
|
|
'document.getElementsByTagName(\'html\')[0].classList.add(\'fullscreen\')'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.on('leave-full-screen', () => {
|
|
|
|
mainWindow.webContents.executeJavaScript(
|
|
|
|
'document.getElementsByTagName(\'html\')[0].classList.remove(\'fullscreen\')'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.on('blur', () => {
|
|
|
|
mainWindow.webContents.executeJavaScript(
|
|
|
|
'document.getElementsByTagName(\'html\')[0].classList.add(\'blur\')'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.on('focus', () => {
|
|
|
|
mainWindow.webContents.executeJavaScript(
|
|
|
|
'document.getElementsByTagName(\'html\')[0].classList.remove(\'blur\')'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make window bigger automatically when devtools are opened
|
|
|
|
mainWindow.webContents.on('devtools-opened', () => {
|
|
|
|
mainWindow.setSize(1024, 420, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.webContents.on('devtools-closed', () => {
|
|
|
|
mainWindow.setSize(width, height, true)
|
|
|
|
})
|
|
|
|
|
2019-05-08 01:02:02 +02:00
|
|
|
switchTheme()
|
|
|
|
|
2019-05-05 13:34:21 +02:00
|
|
|
// Load menubar menu items
|
2019-05-16 22:25:19 +02:00
|
|
|
buildMenu(mainWindow)
|
2019-05-13 22:54:40 +02:00
|
|
|
|
|
|
|
touchBarWrapper(mainWindow)
|
2019-05-05 13:34:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:25:19 +02:00
|
|
|
module.exports = { mainWindow }
|
|
|
|
|
2019-05-05 13:34:21 +02:00
|
|
|
app.on('ready', () => {
|
|
|
|
createWindow()
|
|
|
|
|
|
|
|
// Switch to user theme on start, and on reload
|
|
|
|
mainWindow.webContents.on('dom-ready', () => switchTheme())
|
|
|
|
})
|
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (mainWindow === null) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const switchTheme = () => {
|
2019-05-06 01:50:08 +02:00
|
|
|
isDarkMode
|
|
|
|
? mainWindow.webContents.executeJavaScript(
|
|
|
|
'document.getElementsByTagName(\'html\')[0].classList.add(\'dark\')'
|
|
|
|
)
|
|
|
|
: mainWindow.webContents.executeJavaScript(
|
|
|
|
'document.getElementsByTagName(\'html\')[0].classList.remove(\'dark\')'
|
|
|
|
)
|
2019-05-05 13:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for theme changes in System Preferences
|
|
|
|
systemPreferences.subscribeNotification(
|
|
|
|
'AppleInterfaceThemeChangedNotification',
|
|
|
|
() => switchTheme()
|
|
|
|
)
|