Merge pull request #6375 from electron/login-item

Add login item API
This commit is contained in:
Cheng Zhao 2016-07-08 10:39:20 +09:00 коммит произвёл GitHub
Родитель 204734f80b cde594775d
Коммит 5713e0599b
5 изменённых файлов: 131 добавлений и 0 удалений

Просмотреть файл

@ -71,6 +71,33 @@ struct Converter<Browser::UserTask> {
};
#endif
#if defined(OS_MACOSX)
template<>
struct Converter<Browser::LoginItemSettings> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> 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<v8::Value> 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))

Просмотреть файл

@ -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)

Просмотреть файл

@ -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();
}

Просмотреть файл

@ -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.

Просмотреть файл

@ -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
})
})
})
})