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

Add login item API #6375

Merged
merged 13 commits into from
Jul 8, 2016
8 changes: 6 additions & 2 deletions atom/browser/api/atom_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,12 @@ void App::BuildPrototype(
.SetMethod("show", base::Bind(&Browser::Show, browser))
.SetMethod("setUserActivity",
base::Bind(&Browser::SetUserActivity, browser))
.SetMethod("getCurrentActivityType",
base::Bind(&Browser::GetCurrentActivityType, browser))
.SetMethod("getLoginItemStatus",
base::Bind(&Browser::GetLoginItemStatus, browser))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong lines removed.

.SetMethod("setAsLoginItem",
base::Bind(&Browser::SetAsLoginItem, browser))
.SetMethod("removeAsLoginItem",
base::Bind(&Browser::RemoveAsLoginItem, browser))
#endif
#if defined(OS_WIN)
.SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser))
Expand Down
9 changes: 9 additions & 0 deletions atom/browser/browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ class Browser : public WindowListObserver {

// Set docks' icon.
void DockSetIcon(const gfx::Image& image);

// Get login item status of app
v8::Local<v8::Value> GetLoginItemStatus(mate::Arguments* args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atom::Browser class should not interactive with V8 directly, APIs added here are also meant to be used by non-V8 parts of code. When type conversions and optional arguments are needed, we usually add a wrapper in api::App.


// Set app as a login item
void SetAsLoginItem(mate::Arguments* args);

// Remove app as a login item
void RemoveAsLoginItem();
#endif // defined(OS_MACOSX)

#if defined(OS_WIN)
Expand Down
24 changes: 24 additions & 0 deletions atom/browser/browser_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
#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"
#include "native_mate/dictionary.h"
#include "net/base/mac/url_conversions.h"
#include "url/gurl.h"

Expand Down Expand Up @@ -148,6 +150,28 @@
return prevent_default;
}

v8::Local<v8::Value> Browser::GetLoginItemStatus(mate::Arguments* args) {
bool hidden = false;
mate::Dictionary dict = mate::Dictionary::CreateEmpty(args->isolate());
dict.Set("openAtLogin", base::mac::CheckLoginItemStatus(&hidden));
dict.Set("openAsHidden", hidden);
dict.Set("restoreState", base::mac::WasLaunchedAsLoginItemRestoreState());
dict.Set("openedAtLogin", base::mac::WasLaunchedAsLoginOrResumeItem());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little confused by the different tenses here—I wonder if calling the keys wasOpenedAtLogin or didOpenAtLogin might make it more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if calling the keys wasOpenedAtLogin or didOpenAtLogin might make it more clear?

👍 I really like both suggestions, so how about these:

  • openedAtLogin -> wasOpenedAtLogin
  • openedAsHidden -> wasOpenedAsHidden

dict.Set("openedAsHidden", base::mac::WasLaunchedAsHiddenLoginItem());

return dict.GetHandle();
}

void Browser::SetAsLoginItem(mate::Arguments* args) {
bool hidden = false;
args->GetNext(&hidden);
base::mac::AddToLoginItems(hidden);
}

void Browser::RemoveAsLoginItem() {
base::mac::RemoveFromLoginItems();
}

std::string Browser::GetExecutableFileVersion() const {
return brightray::GetApplicationVersion();
}
Expand Down
29 changes: 29 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,35 @@ Returns the current value displayed in the counter badge.

Returns whether current desktop environment is Unity launcher.

### `app.getLoginItemStatus()` _macOS_

Return an Object with the login item status 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.
* `openedAtLogin` Boolean - `true` if the app was opened at login automatically.
* `openedAsHidden` 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.setAsLoginItem([openAsHidden])` _macOS_

Set the app as a login item. This will cause the app to be opened automatically
at login.

* `openAsHidden` Boolean - `true` to open the app as hidden. Defaults to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameters should be placed above the description.

`false`. The user can edit this setting from the System Preferences so
`app.getLoginItemStatus().openedAsHidden` should be checked when the app
is opened to know the current value.

### `app.removeAsLoginItem()` _macOS_

Removes the app as a login item. The app will no longer be opened automatically
at login.

### `app.commandLine.appendSwitch(switch[, value])`

Append a switch (with optional `value`) to Chromium's command line.
Expand Down
25 changes: 25 additions & 0 deletions spec/api-app-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,29 @@ describe('app module', function () {
assert.equal(app.getBadgeCount(), shouldFail ? 0 : 42)
})
})

describe('app.getLoginItemStatus API', function () {
if (process.platform !== 'darwin') return

beforeEach(function () {
assert.equal(app.getLoginItemStatus().openedAtLogin, false)
assert.equal(app.getLoginItemStatus().openedAsHidden, false)
assert.equal(app.getLoginItemStatus().restoreState, false)
})

afterEach(function () {
app.removeAsLoginItem()
assert.equal(app.getLoginItemStatus().openAtLogin, false)
})

it('returns the login item status of the app', function () {
app.setAsLoginItem(true)
assert.equal(app.getLoginItemStatus().openAtLogin, true)
assert.equal(app.getLoginItemStatus().openAsHidden, true)

app.setAsLoginItem(false)
assert.equal(app.getLoginItemStatus().openAtLogin, true)
assert.equal(app.getLoginItemStatus().openAsHidden, false)
})
})
})