Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow macOS apps to set activation policies #21983

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,17 @@ Updates the current activity if its type matches `type`, merging the entries fro

Changes the [Application User Model ID][app-user-model-id] to `id`.

### `app.setActivationPolicy(policy)` _macOS_

* `policy` String - Can be 'regular', 'accessory', or 'prohibited'.

Sets the activation policy for a given app.

Activation policy types:
* 'regular' - The application is an ordinary app that appears in the Dock and may have a user interface.
* 'accessory' - The application doesn’t appear in the Dock and doesn’t have a menu bar, but it may be activated programmatically or by clicking on one of its windows.
* 'prohibited' - The application doesn’t appear in the Dock and may not create windows or be activated.

### `app.importCertificate(options, callback)` _Linux_

* `options` Object
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ void App::BuildPrototype(v8::Isolate* isolate,
base::BindRepeating(&Browser::UpdateCurrentActivity, browser))
.SetMethod("moveToApplicationsFolder", &App::MoveToApplicationsFolder)
.SetMethod("isInApplicationsFolder", &App::IsInApplicationsFolder)
.SetMethod("setActivationPolicy", &App::SetActivationPolicy)
#endif
.SetMethod("setAboutPanelOptions",
base::BindRepeating(&Browser::SetAboutPanelOptions, browser))
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/api/electron_api_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class App : public ElectronBrowserClient::Delegate,
bool CanBrowserClientUseCustomSiteInstance();

#if defined(OS_MACOSX)
void SetActivationPolicy(gin_helper::ErrorThrower thrower,
const std::string& policy);
bool MoveToApplicationsFolder(gin_helper::ErrorThrower, gin::Arguments* args);
bool IsInApplicationsFolder();
v8::Local<v8::Value> GetDockAPI(v8::Isolate* isolate);
Expand Down
20 changes: 20 additions & 0 deletions shell/browser/api/electron_api_app_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include <string>

#include "base/path_service.h"
#include "shell/browser/api/electron_api_app.h"
#include "shell/browser/electron_paths.h"
Expand Down Expand Up @@ -32,6 +34,24 @@
}
}

void App::SetActivationPolicy(gin_helper::ErrorThrower thrower,
const std::string& policy) {
NSApplicationActivationPolicy activation_policy;
if (policy == "accessory") {
activation_policy = NSApplicationActivationPolicyAccessory;
} else if (policy == "prohibited") {
activation_policy = NSApplicationActivationPolicyProhibited;
} else if (policy == "regular") {
activation_policy = NSApplicationActivationPolicyRegular;
} else {
thrower.ThrowError("Invalid activation policy: must be one of 'regular', "
"'accessory', or 'prohibited'");
return;
}

[NSApp setActivationPolicy:activation_policy];
}

} // namespace api

} // namespace electron
8 changes: 8 additions & 0 deletions spec-main/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ describe('app module', () => {
})
})

ifdescribe(process.platform === 'darwin')('app.setActivationPolicy', () => {
it('throws an error on invalid application policies', () => {
expect(() => {
app.setActivationPolicy('terrible' as any)
}).to.throw(/Invalid activation policy: must be one of 'regular', 'accessory', or 'prohibited'/)
})
})

describe('app.requestSingleInstanceLock', () => {
it('prevents the second launch of app', function (done) {
this.timeout(120000)
Expand Down