docs: New behaviors of makeSingleInstance

This commit is contained in:
Cheng Zhao 2015-10-22 19:26:05 +08:00
parent 6bfe06ec4e
commit 61f07307cb

View file

@ -299,41 +299,51 @@ allowing multiple instances of your app to run, this will ensure that only a
single instance of your app is running, and other instances signal this single instance of your app is running, and other instances signal this
instance and exit. instance and exit.
`callback` is called when a second instance has been executed, and provides `callback` will be called with `callback(argv, workingDirectory)` when a second
the command-line (including Chromium flags) and the working directory of the instance has been executed. `argv` is an Array of the second instance's command
secondary instance. Usually applications respond to this by making their line arguments, and `workingDirectory` is its current working directory. Usually
primary window focused and non-minimized. applications respond to this by making their primary window focused and
non-minimized.
`callback` should return `true` if the message was successfully handled, or The `callback` is guaranteed to be executed after the `ready` event of `app`
`false` if the secondary process should retry sending it or it failed. gets emitted.
This method returns `false` if your process is the primary instance of the
application and your app should continue loading. And returns `true` if your
process has sent its parameters to another instance, and you should immediately
quit.
On OS X the system enforces single instance automatically when users try to open
a second instance of your app in Finder, and the `open-file` and `open-url`
events will be emitted for that. However when users start your app in command
line the system's single instance machanism will be bypassed and you have to
use this method to ensure single instance.
An example of activating the window of primary instance when a second instance
starts:
```js ```js
var myWindow; var myWindow = null;
app.on('ready', function() {
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
// Someone tried to run a second instance, we should focus our window
if (myWindow) {
if (myWindow.isMinimized()) myWindow.restore();
myWindow.focus();
}
// We successfully handled the command line var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
return true; // Someone tried to run a second instance, we should focus our window
}); if (myWindow) {
if (myWindow.isMinimized()) myWindow.restore();
if (shouldQuit) { myWindow.focus();
app.quit();
return;
} }
return true;
});
// Create myWindow, load the rest of the app, etc... if (shouldQuit) {
app.quit();
return;
}
// Create myWindow, load the rest of the app, etc...
app.on('ready', function() {
}); });
``` ```
Returns a Boolean - if `false`, your process is the primary instance of the
application and your app should continue loading. If `true`, your process has
sent its parameters to another instance, and you should immediately quit.
### `app.commandLine.appendSwitch(switch[, value])` ### `app.commandLine.appendSwitch(switch[, value])`
Append a switch (with optional `value`) to Chromium's command line. Append a switch (with optional `value`) to Chromium's command line.