diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index a39f9809ebf0..9d799f115007 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -71,6 +71,33 @@ struct Converter { }; #endif +#if defined(OS_MACOSX) +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + Browser::LoginItemSettings* out) { + mate::Dictionary dict; + if (!ConvertFromV8(isolate, val, &dict)) + return false; + + dict.Get("openAtLogin", &(out->open_at_login)); + dict.Get("openAsHidden", &(out->open_as_hidden)); + return true; + } + + static v8::Local ToV8(v8::Isolate* isolate, + Browser::LoginItemSettings val) { + mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + dict.Set("openAtLogin", val.open_at_login); + dict.Set("openAsHidden", val.open_as_hidden); + dict.Set("restoreState", val.restore_state); + dict.Set("wasOpenedAtLogin", val.opened_at_login); + dict.Set("wasOpenedAsHidden", val.opened_as_hidden); + return dict.GetHandle(); + } +}; +#endif + } // namespace mate @@ -529,6 +556,10 @@ void App::BuildPrototype( base::Bind(&Browser::SetUserActivity, browser)) .SetMethod("getCurrentActivityType", base::Bind(&Browser::GetCurrentActivityType, browser)) + .SetMethod("getLoginItemSettings", + base::Bind(&Browser::GetLoginItemSettings, browser)) + .SetMethod("setLoginItemSettings", + base::Bind(&Browser::SetLoginItemSettings, browser)) #endif #if defined(OS_WIN) .SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser)) diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 37d2e11e6799..be51d7ec006d 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -92,6 +92,14 @@ class Browser : public WindowListObserver { int GetBadgeCount(); #if defined(OS_MACOSX) + struct LoginItemSettings { + bool open_at_login = false; + bool open_as_hidden = false; + bool restore_state = false; + bool opened_at_login = false; + bool opened_as_hidden = false; + }; + // Hide the application. void Hide(); @@ -134,6 +142,12 @@ class Browser : public WindowListObserver { // Set docks' icon. void DockSetIcon(const gfx::Image& image); + + // Get login item settings of app + LoginItemSettings GetLoginItemSettings(); + + // Set login item settings of app + void SetLoginItemSettings(LoginItemSettings settings); #endif // defined(OS_MACOSX) #if defined(OS_WIN) diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index bb789365ffb2..4328dcba4820 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -11,6 +11,7 @@ #include "atom/browser/window_list.h" #include "base/mac/bundle_locations.h" #include "base/mac/foundation_util.h" +#include "base/mac/mac_util.h" #include "base/strings/string_number_conversions.h" #include "base/strings/sys_string_conversions.h" #include "brightray/common/application_info.h" @@ -148,6 +149,23 @@ bool Browser::ContinueUserActivity(const std::string& type, return prevent_default; } +Browser::LoginItemSettings Browser::GetLoginItemSettings() { + LoginItemSettings settings; + settings.open_at_login = base::mac::CheckLoginItemStatus( + &settings.open_as_hidden); + settings.restore_state = base::mac::WasLaunchedAsLoginItemRestoreState(); + settings.opened_at_login = base::mac::WasLaunchedAsLoginOrResumeItem(); + settings.opened_as_hidden = base::mac::WasLaunchedAsHiddenLoginItem(); + return settings; +} + +void Browser::SetLoginItemSettings(LoginItemSettings settings) { + if (settings.open_at_login) + base::mac::AddToLoginItems(settings.open_as_hidden); + else + base::mac::RemoveFromLoginItems(); +} + std::string Browser::GetExecutableFileVersion() const { return brightray::GetApplicationVersion(); } diff --git a/docs/api/app.md b/docs/api/app.md index cfda6b574be1..c7e0998b6cc3 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -598,6 +598,33 @@ Returns the current value displayed in the counter badge. Returns whether current desktop environment is Unity launcher. +### `app.getLoginItemSettings()` _macOS_ + +Return an Object with the login item settings of the app. + +* `openAtLogin` Boolean - `true` if the app is set to open at login. +* `openAsHidden` Boolean - `true` if the app is set to open as hidden at login. +* `wasOpenedAtLogin` Boolean - `true` if the app was opened at login + automatically. +* `wasOpenedAsHidden` Boolean - `true` if the app was opened as a hidden login + item. This indicates that the app should not open any windows at startup. +* `restoreState` Boolean - `true` if the app was opened as a login item that + should restore the state from the previous session. This indicates that the + app should restore the windows that were open the last time the app was + closed. + +### `app.setLoginItemSettings(settings)` _macOS_ + +* `settings` Object + * `openAtLogin` Boolean - `true` to open the app at login, `false` to remove + the app as a login item. Defaults to `false`. + * `openAsHidden` Boolean - `true` to open the app as hidden. Defaults to + `false`. The user can edit this setting from the System Preferences so + `app.getLoginItemStatus().wasOpenedAsHidden` should be checked when the app + is opened to know the current value. + +Set the app's login item settings. + ### `app.commandLine.appendSwitch(switch[, value])` Append a switch (with optional `value`) to Chromium's command line. diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 111d387d3404..8ceb44f19d33 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -298,4 +298,45 @@ describe('app module', function () { assert.equal(app.getBadgeCount(), shouldFail ? 0 : 42) }) }) + + describe('app.get/setLoginItemSettings API', function () { + if (process.platform !== 'darwin') return + + beforeEach(function () { + app.setLoginItemSettings({openAtLogin: false}) + }) + + afterEach(function () { + app.setLoginItemSettings({openAtLogin: false}) + }) + + it('returns the login item status of the app', function () { + app.setLoginItemSettings({openAtLogin: true}) + assert.deepEqual(app.getLoginItemSettings(), { + openAtLogin: true, + openAsHidden: false, + wasOpenedAtLogin: false, + wasOpenedAsHidden: false, + restoreState: false + }) + + app.setLoginItemSettings({openAtLogin: true, openAsHidden: true}) + assert.deepEqual(app.getLoginItemSettings(), { + openAtLogin: true, + openAsHidden: true, + wasOpenedAtLogin: false, + wasOpenedAsHidden: false, + restoreState: false + }) + + app.setLoginItemSettings({}) + assert.deepEqual(app.getLoginItemSettings(), { + openAtLogin: false, + openAsHidden: false, + wasOpenedAtLogin: false, + wasOpenedAsHidden: false, + restoreState: false + }) + }) + }) })