2019-02-14 22:29:20 +00:00
|
|
|
import * as path from 'path'
|
|
|
|
|
2019-06-02 20:03:03 +00:00
|
|
|
import { deprecate, Menu } from 'electron'
|
2019-02-14 22:29:20 +00:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const bindings = process.electronBinding('app')
|
|
|
|
const commandLine = process.electronBinding('command_line')
|
2019-02-14 22:29:20 +00:00
|
|
|
const { app, App } = bindings
|
|
|
|
|
|
|
|
// Only one app object permitted.
|
|
|
|
export default app
|
|
|
|
|
|
|
|
let dockMenu: Electron.Menu | null = null
|
|
|
|
|
|
|
|
// App is an EventEmitter.
|
|
|
|
Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
|
|
|
|
EventEmitter.call(app as any)
|
|
|
|
|
|
|
|
Object.assign(app, {
|
|
|
|
commandLine: {
|
|
|
|
hasSwitch: (theSwitch: string) => commandLine.hasSwitch(String(theSwitch)),
|
|
|
|
getSwitchValue: (theSwitch: string) => commandLine.getSwitchValue(String(theSwitch)),
|
|
|
|
appendSwitch: (theSwitch: string, value?: string) => commandLine.appendSwitch(String(theSwitch), typeof value === 'undefined' ? value : String(value)),
|
|
|
|
appendArgument: (arg: string) => commandLine.appendArgument(String(arg))
|
2019-04-24 04:07:40 +00:00
|
|
|
} as Electron.CommandLine
|
2019-02-14 22:29:20 +00:00
|
|
|
})
|
|
|
|
|
2019-04-02 20:36:57 +00:00
|
|
|
// we define this here because it'd be overly complicated to
|
|
|
|
// do in native land
|
|
|
|
Object.defineProperty(app, 'applicationMenu', {
|
|
|
|
get () {
|
|
|
|
return Menu.getApplicationMenu()
|
|
|
|
},
|
|
|
|
set (menu: Electron.Menu | null) {
|
|
|
|
return Menu.setApplicationMenu(menu)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
app.isPackaged = (() => {
|
|
|
|
const execFile = path.basename(process.execPath).toLowerCase()
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
return execFile !== 'electron.exe'
|
|
|
|
}
|
|
|
|
return execFile !== 'electron'
|
|
|
|
})()
|
|
|
|
|
2019-04-16 18:22:51 +00:00
|
|
|
app._setDefaultAppPaths = (packagePath) => {
|
|
|
|
// Set the user path according to application's name.
|
2019-04-30 20:55:33 +00:00
|
|
|
app.setPath('userData', path.join(app.getPath('appData'), app.name!))
|
|
|
|
app.setPath('userCache', path.join(app.getPath('cache'), app.name!))
|
2019-04-16 18:22:51 +00:00
|
|
|
app.setAppPath(packagePath)
|
|
|
|
|
|
|
|
// Add support for --user-data-dir=
|
2019-06-13 23:03:02 +00:00
|
|
|
if (app.commandLine.hasSwitch('user-data-dir')) {
|
|
|
|
const userDataDir = app.commandLine.getSwitchValue('user-data-dir')
|
2019-04-16 18:22:51 +00:00
|
|
|
if (path.isAbsolute(userDataDir)) app.setPath('userData', userDataDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2019-05-06 15:29:01 +00:00
|
|
|
const setDockMenu = app.dock!.setMenu
|
|
|
|
app.dock!.setMenu = (menu) => {
|
2019-02-14 22:29:20 +00:00
|
|
|
dockMenu = menu
|
|
|
|
setDockMenu(menu)
|
|
|
|
}
|
2019-05-06 15:29:01 +00:00
|
|
|
app.dock!.getMenu = () => dockMenu
|
2019-02-14 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Routes the events to webContents.
|
|
|
|
const events = ['login', 'certificate-error', 'select-client-certificate']
|
|
|
|
for (const name of events) {
|
|
|
|
app.on(name as 'login', (event, webContents, ...args: any[]) => {
|
|
|
|
webContents.emit(name, event, ...args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:49:04 +00:00
|
|
|
// Property Deprecations
|
2019-06-05 05:44:16 +00:00
|
|
|
deprecate.fnToProperty(App.prototype, 'accessibilitySupportEnabled', '_isAccessibilitySupportEnabled', '_setAccessibilitySupportEnabled')
|
|
|
|
deprecate.fnToProperty(App.prototype, 'badgeCount', '_getBadgeCount', '_setBadgeCount')
|
|
|
|
deprecate.fnToProperty(App.prototype, 'name', '_getName', '_setName')
|
2019-04-05 02:49:04 +00:00
|
|
|
|
2019-02-14 22:29:20 +00:00
|
|
|
// Wrappers for native classes.
|
2019-03-18 19:37:06 +00:00
|
|
|
const { DownloadItem } = process.electronBinding('download_item')
|
2019-02-14 22:29:20 +00:00
|
|
|
Object.setPrototypeOf(DownloadItem.prototype, EventEmitter.prototype)
|