1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 09:46:22 +00:00

refactor main-app process

clean unnecessary codes
This commit is contained in:
Dick Choi
2016-08-22 11:46:22 +09:00
parent 108e83a402
commit cff3fdae6e
5 changed files with 120 additions and 125 deletions

View File

@@ -1,10 +1,85 @@
const { app } = require('electron')
const ChildProcess = require('child_process')
const path = require('path')
var error = null
function isFinderCalled () { function isFinderCalled () {
var argv = process.argv.slice(1) var argv = process.argv.slice(1)
return argv.some((arg) => arg.match(/--finder/)) return argv.some((arg) => arg.match(/--finder/))
} }
function execMainApp () {
const appRootPath = path.join(process.execPath, '../..')
const updateDotExePath = path.join(appRootPath, 'Update.exe')
const exeName = path.basename(process.execPath)
function spawnUpdate (args, cb) {
var stdout = ''
var updateProcess = null
try {
updateProcess = ChildProcess.spawn(updateDotExePath, args)
} catch (e) {
process.nextTick(function () {
cb(e)
})
}
updateProcess.stdout.on('data', function (data) {
stdout += data
})
updateProcess.on('error', function (_error) {
error = _error
})
updateProcess.on('close', function (code, signal) {
if (code !== 0) {
error = new Error('Command failed: #{signal ? code}')
error.code = code
error.stdout = stdout
}
cb(error, stdout)
})
}
var handleStartupEvent = function () {
if (process.platform !== 'win32') {
return false
}
var squirrelCommand = process.argv[1]
switch (squirrelCommand) {
case '--squirrel-install':
spawnUpdate(['--createShortcut', exeName], function (err) {
if (err) console.error(err)
app.quit()
})
return true
case '--squirrel-updated':
app.quit()
return true
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName], function (err) {
if (err) console.error(err)
app.quit()
})
return true
case '--squirrel-obsolete':
app.quit()
return true
}
}
if (handleStartupEvent()) {
return
}
require('./lib/main-app')
}
if (isFinderCalled()) { if (isFinderCalled()) {
require('./lib/finder-app') require('./lib/finder-app')
} else { } else {
require('./lib/main-app') execMainApp()
} }

View File

@@ -1,4 +1,5 @@
const electron = require('electron') const electron = require('electron')
const { app } = electron
const BrowserWindow = electron.BrowserWindow const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu const Menu = electron.Menu
const MenuItem = electron.MenuItem const MenuItem = electron.MenuItem
@@ -44,7 +45,7 @@ finderWindow.on('close', function (e) {
}) })
var appIcon = new Tray(path.join(__dirname, '../resources/tray-icon.png')) var appIcon = new Tray(path.join(__dirname, '../resources/tray-icon.png'))
appIcon.setToolTip('Boost') appIcon.setToolTip('Boostnote')
var trayMenu = new Menu() var trayMenu = new Menu()
trayMenu.append(new MenuItem({ trayMenu.append(new MenuItem({
@@ -84,4 +85,9 @@ function hideFinder () {
} }
finderWindow.hide() finderWindow.hide()
} }
app.on('before-quit', function (e) {
finderWindow.removeAllListeners()
})
module.exports = finderWindow module.exports = finderWindow

View File

@@ -8,76 +8,11 @@ const ChildProcess = require('child_process')
const _ = require('lodash') const _ = require('lodash')
const GhReleases = require('electron-gh-releases') const GhReleases = require('electron-gh-releases')
// electron.crashReporter.start() // electron.crashReporter.start()
require('./ipc')
var mainWindow = null var mainWindow = null
var finderProcess = null
var finderWindow = null var finderWindow = null
const appRootPath = path.join(process.execPath, '../..')
const updateDotExePath = path.join(appRootPath, 'Update.exe')
const exeName = path.basename(process.execPath)
// For windows app
function spawnUpdate (args, cb) {
var stdout = ''
var updateProcess = null
try {
updateProcess = ChildProcess.spawn(updateDotExePath, args)
} catch (e) {
process.nextTick(function () {
cb(e)
})
}
updateProcess.stdout.on('data', function (data) {
stdout += data
})
error = null
updateProcess.on('error', function (_error) {
error = _error
})
updateProcess.on('close', function (code, signal) {
if (code !== 0) {
error = new Error("Command failed: #{signal ? code}")
error.code = code
error.stdout = stdout
}
cb(error, stdout)
})
}
var handleStartupEvent = function () {
if (process.platform !== 'win32') {
return false
}
var squirrelCommand = process.argv[1]
switch (squirrelCommand) {
case '--squirrel-install':
spawnUpdate(['--createShortcut', exeName], function (err) {
app.quit()
})
return true
case '--squirrel-updated':
app.quit()
return true
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName], function (err) {
app.quit()
})
return true
case '--squirrel-obsolete':
app.quit()
return true
}
}
if (handleStartupEvent()) {
return
}
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) { var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
if (mainWindow) { if (mainWindow) {
if (process.platform === 'win32') { if (process.platform === 'win32') {
@@ -90,7 +25,6 @@ var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory)
}) })
if (shouldQuit) { if (shouldQuit) {
if (mainWindow != null) mainWindow.removeAllListeners()
app.quit() app.quit()
return return
} }
@@ -117,15 +51,10 @@ function checkUpdate () {
if (err) { if (err) {
var isLatest = err.message === 'There is no newer version.' var isLatest = err.message === 'There is no newer version.'
if (!isLatest) console.error('Updater error! %s', err.message) if (!isLatest) console.error('Updater error! %s', err.message)
return
} }
if (!err) {
if (status) { if (status) {
// Download start
mainWindow.webContents.send('update-found', 'Update found!')
updater.download() updater.download()
} else {
// Latest version
}
} }
}) })
} }
@@ -137,69 +66,53 @@ updater.on('update-downloaded', (info) => {
} }
}) })
function spawnFinder() {
if (process.platform === 'darwin') {
var finderArgv = [path.join(__dirname, 'finder-app.js'), '--finder']
if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
finderProcess = ChildProcess
.execFile(process.execPath, finderArgv)
}
}
app.on('ready', function () {
var template = require('./main-menu')
var menu = Menu.buildFromTemplate(template)
if (process.platform === 'darwin' || process.platform === 'linux') {
Menu.setApplicationMenu(menu)
}
// Check update every 24 hours
setInterval(function () {
checkUpdate()
}, 1000 * 60 * 60 * 24)
ipc.on('check-update', function (event, msg) {
checkUpdate()
})
ipc.on('update-app-confirm', function (event, msg) { ipc.on('update-app-confirm', function (event, msg) {
if (isUpdateReady) { if (isUpdateReady) {
mainWindow.removeAllListeners()
updater.install() updater.install()
} }
}) })
ipc.on('quit-app-confirm', function () { function spawnFinder () {
mainWindow.removeAllListeners() var finderArgv = [path.join(__dirname, 'finder-app.js'), '--finder']
app.quit() if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
var finderProcess = ChildProcess
.execFile(process.execPath, finderArgv)
app.on('before-quit', function () {
finderProcess.kill()
}) })
checkUpdate()
mainWindow = require('./main-window')
if (process.platform === 'win32' || process.platform === 'linux') {
mainWindow.setMenu(menu)
} }
app.on('ready', function () {
mainWindow = require('./main-window')
var template = require('./main-menu')
var menu = Menu.buildFromTemplate(template)
switch (process.platform) { switch (process.platform) {
case 'darwin': case 'darwin':
spawnFinder() spawnFinder()
Menu.setApplicationMenu(menu)
break break
case 'win32': case 'win32':
finderWindow = require('./finder-window') finderWindow = require('./finder-window')
finderWindow.on('close', function (e) { mainWindow.setMenu(menu)
e.preventDefault()
finderWindow.hide()
})
break break
case 'linux': case 'linux':
// Finder is available on cinnamon only. // Finder is available on cinnamon only.
if (process.env.DESKTOP_SESSION === 'cinnamon') { if (process.env.DESKTOP_SESSION === 'cinnamon') {
finderWindow = require('./finder-window') finderWindow = require('./finder-window')
} }
Menu.setApplicationMenu(menu)
mainWindow.setMenu(menu)
} }
// Check update every hour
setInterval(function () {
checkUpdate()
}, 1000 * 60 * 60)
checkUpdate()
require('./hotkey') require('./hotkey')
}) })

View File

@@ -36,11 +36,7 @@ var boost = OSX
type: 'separator' type: 'separator'
}, },
{ {
label: 'Quit', role: 'quit'
accelerator: 'Command+Q',
click: function () {
mainWindow.webContents.send('quit-app', {})
}
} }
] ]
} }

View File

@@ -2,6 +2,7 @@ const electron = require('electron')
const app = electron.app const app = electron.app
const BrowserWindow = electron.BrowserWindow const BrowserWindow = electron.BrowserWindow
const path = require('path') const path = require('path')
const ipc = require('./ipc')
var mainWindow = new BrowserWindow({ var mainWindow = new BrowserWindow({
width: 1080, width: 1080,
@@ -46,4 +47,8 @@ app.on('activate', function () {
mainWindow.show() mainWindow.show()
}) })
app.on('before-quit', function (e) {
mainWindow.removeAllListeners()
})
module.exports = mainWindow module.exports = mainWindow