app.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import * as path from 'path'
  2. import * as electron from 'electron'
  3. import { EventEmitter } from 'events'
  4. const bindings = process.electronBinding('app')
  5. const commandLine = process.electronBinding('command_line')
  6. const { app, App } = bindings
  7. // Only one app object permitted.
  8. export default app
  9. const { deprecate, Menu } = electron
  10. let dockMenu: Electron.Menu | null = null
  11. // App is an EventEmitter.
  12. Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
  13. EventEmitter.call(app as any)
  14. Object.assign(app, {
  15. // TODO(codebytere): remove in 7.0
  16. setApplicationMenu (menu: Electron.Menu | null) {
  17. return Menu.setApplicationMenu(menu)
  18. },
  19. // TODO(codebytere): remove in 7.0
  20. getApplicationMenu () {
  21. return Menu.getApplicationMenu()
  22. },
  23. commandLine: {
  24. hasSwitch: (theSwitch: string) => commandLine.hasSwitch(String(theSwitch)),
  25. getSwitchValue: (theSwitch: string) => commandLine.getSwitchValue(String(theSwitch)),
  26. appendSwitch: (theSwitch: string, value?: string) => commandLine.appendSwitch(String(theSwitch), typeof value === 'undefined' ? value : String(value)),
  27. appendArgument: (arg: string) => commandLine.appendArgument(String(arg))
  28. } as Electron.CommandLine,
  29. enableMixedSandbox () {
  30. deprecate.log(`'enableMixedSandbox' is deprecated. Mixed-sandbox mode is now enabled by default. You can safely remove the call to enableMixedSandbox().`)
  31. }
  32. })
  33. // we define this here because it'd be overly complicated to
  34. // do in native land
  35. Object.defineProperty(app, 'applicationMenu', {
  36. get () {
  37. return Menu.getApplicationMenu()
  38. },
  39. set (menu: Electron.Menu | null) {
  40. return Menu.setApplicationMenu(menu)
  41. }
  42. })
  43. app.isPackaged = (() => {
  44. const execFile = path.basename(process.execPath).toLowerCase()
  45. if (process.platform === 'win32') {
  46. return execFile !== 'electron.exe'
  47. }
  48. return execFile !== 'electron'
  49. })()
  50. app._setDefaultAppPaths = (packagePath) => {
  51. // Set the user path according to application's name.
  52. app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
  53. app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
  54. app.setAppPath(packagePath)
  55. // Add support for --user-data-dir=
  56. const userDataDirFlag = '--user-data-dir='
  57. const userDataArg = process.argv.find(arg => arg.startsWith(userDataDirFlag))
  58. if (userDataArg) {
  59. const userDataDir = userDataArg.substr(userDataDirFlag.length)
  60. if (path.isAbsolute(userDataDir)) app.setPath('userData', userDataDir)
  61. }
  62. }
  63. if (process.platform === 'darwin') {
  64. const setDockMenu = app.dock.setMenu
  65. app.dock.setMenu = (menu) => {
  66. dockMenu = menu
  67. setDockMenu(menu)
  68. }
  69. app.dock.getMenu = () => dockMenu
  70. }
  71. // Routes the events to webContents.
  72. const events = ['login', 'certificate-error', 'select-client-certificate']
  73. for (const name of events) {
  74. app.on(name as 'login', (event, webContents, ...args: any[]) => {
  75. webContents.emit(name, event, ...args)
  76. })
  77. }
  78. // Function Deprecations
  79. app.getFileIcon = deprecate.promisify(app.getFileIcon)
  80. // Property Deprecations
  81. deprecate.fnToProperty(app, 'accessibilitySupportEnabled', '_isAccessibilitySupportEnabled', '_setAccessibilitySupportEnabled')
  82. // Wrappers for native classes.
  83. const { DownloadItem } = process.electronBinding('download_item')
  84. Object.setPrototypeOf(DownloadItem.prototype, EventEmitter.prototype)