Skip to content

Commit

Permalink
feat: use default-app behavior in packaged apps (default menu / windo…
Browse files Browse the repository at this point in the history
…w-all-closed handling)
  • Loading branch information
miniak committed Jan 8, 2019
1 parent 668049a commit 0e6bde1
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 316 deletions.
17 changes: 3 additions & 14 deletions default_app/main.js
Expand Up @@ -5,8 +5,6 @@ const Module = require('module')
const path = require('path')
const url = require('url')

const { setDefaultApplicationMenu } = require('./menu')

// Parse command line options.
const argv = process.argv.slice(1)

Expand Down Expand Up @@ -57,18 +55,6 @@ if (nextArgIsRequire) {
process.exit(1)
}

// Quit when all windows are closed and no other one is listening to this.
app.on('window-all-closed', () => {
if (app.listeners('window-all-closed').length === 1 && !option.interactive) {
app.quit()
}
})

// Create default menu.
app.once('ready', () => {
setDefaultApplicationMenu()
})

// Set up preload modules
if (option.modules.length > 0) {
Module._preloadModules(option.modules)
Expand Down Expand Up @@ -140,6 +126,9 @@ function startRepl () {
process.exit(1)
}

// prevent quitting
app.on('window-all-closed', () => {})

const repl = require('repl')
repl.start('> ').on('exit', () => {
process.exit(0)
Expand Down
168 changes: 0 additions & 168 deletions default_app/menu.js

This file was deleted.

3 changes: 3 additions & 0 deletions docs/api/menu-item.md
Expand Up @@ -82,11 +82,14 @@ The `role` property can have following values:
* `resetZoom` - Reset the focused page's zoom level to the original size.
* `zoomIn` - Zoom in the focused page by 10%.
* `zoomOut` - Zoom out the focused page by 10%.
* `fileMenu` - Whole default "File" menu (Close / Quit)
* `editMenu` - Whole default "Edit" menu (Undo, Copy, etc.).
* `viewMenu` - Whole default "View" menu (Reload, Toggle Developer Tools, etc.)
* `windowMenu` - Whole default "Window" menu (Minimize, Close, etc.).

The following additional roles are available on _macOS_:

* `appMenu` - Whole default "App" menu (About, Services, etc.)
* `about` - Map to the `orderFrontStandardAboutPanel` action.
* `hide` - Map to the `hide` action.
* `hideOthers` - Map to the `hideOtherApplications` action.
Expand Down
98 changes: 55 additions & 43 deletions docs/api/menu.md
Expand Up @@ -30,6 +30,9 @@ effect on macOS.

**Note:** This API has to be called after the `ready` event of `app` module.

**Note:** Default menu is created automatically if the app does not set one.
You can also set `process.noDefaultMenu` to `true` to disable this behavior.

#### `Menu.getApplicationMenu()`

Returns `Menu | null` - The application menu, if set, or `null`, if not set.
Expand Down Expand Up @@ -158,6 +161,29 @@ simple template API:
const { app, Menu } = require('electron')

const template = [
// { role: 'appMenu' }
process.platform === 'darwin' ? {
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
} : null,
// { role: 'fileMenu' }
{
label: 'File',
submenu: [
isMac ? { role: 'close' } : { role: 'quit' }
]
},
// { role: 'editMenu' }
{
label: 'Edit',
submenu: [
Expand All @@ -167,11 +193,26 @@ const template = [
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
...(isMac ? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startspeaking' },
{ role: 'stopspeaking' }
]
}
] : [
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
])
]
},
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
Expand All @@ -186,11 +227,20 @@ const template = [
{ role: 'togglefullscreen' }
]
},
// { role: 'windowMenu' }
{
role: 'window',
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'close' }
{ role: 'zoom' },
...(isMac ? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }
])
]
},
{
Expand All @@ -204,44 +254,6 @@ const template = [
}
]

if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
})

// Edit menu
template[1].submenu.push(
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startspeaking' },
{ role: 'stopspeaking' }
]
}
)

// Window menu
template[3].submenu = [
{ role: 'close' },
{ role: 'minimize' },
{ role: 'zoom' },
{ type: 'separator' },
{ role: 'front' }
]
}

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
```
Expand Down
11 changes: 8 additions & 3 deletions docs/api/process.md
Expand Up @@ -69,6 +69,11 @@ A `Boolean`. For Mac App Store build, this property is `true`, for other builds
A `Boolean` that controls ASAR support inside your application. Setting this to `true`
will disable the support for `asar` archives in Node's built-in modules.

### `process.noDefaultMenu`

A `Boolean` that controls whether the default menu is created when no menu is created
by the app.

### `process.noDeprecation`

A `Boolean` that controls whether or not deprecation warnings are printed to `stderr`.
Expand All @@ -78,7 +83,7 @@ instead of the `--no-deprecation` command line flag.
### `process.enablePromiseAPIs`

A `Boolean` that controls whether or not deprecation warnings are printed to `stderr` when
formerly callback-based APIs converted to Promises are invoked using callbacks. Setting this to `true`
formerly callback-based APIs converted to Promises are invoked using callbacks. Setting this to `true`
will enable deprecation warnings.

### `process.resourcesPath`
Expand Down Expand Up @@ -168,7 +173,7 @@ Returns an object with V8 heap statistics. Note that all statistics are reported

Returns `Object`:

* `residentSet` Integer _Linux_ and _Windows_ - The amount of memory
* `residentSet` Integer _Linux_ and _Windows_ - The amount of memory
currently pinned to actual physical RAM in Kilobytes.
* `private` Integer - The amount of memory not shared by other processes, such as
JS heap or HTML content in Kilobytes.
Expand All @@ -179,7 +184,7 @@ Returns an object giving memory usage statistics about the current process. Note
that all statistics are reported in Kilobytes.
This api should be called after app ready.

Chromium does not provide `residentSet` value for macOS. This is because macOS
Chromium does not provide `residentSet` value for macOS. This is because macOS
performs in-memory compression of pages that haven't been recently used. As a
result the resident set size value is not what one would expect. `private` memory
is more representative of the actual pre-compression memory usage of the process
Expand Down

0 comments on commit 0e6bde1

Please sign in to comment.