Merge pull request #6309 from electron/app-launcher-rename

Implement app.setBadgeCount on Mac
This commit is contained in:
Cheng Zhao 2016-07-02 01:51:47 +00:00 committed by GitHub
commit ee0eb9ae08
8 changed files with 70 additions and 84 deletions

View file

@ -520,6 +520,8 @@ void App::BuildPrototype(
base::Bind(&Browser::SetAsDefaultProtocolClient, browser))
.SetMethod("removeAsDefaultProtocolClient",
base::Bind(&Browser::RemoveAsDefaultProtocolClient, browser))
.SetMethod("setBadgeCount", base::Bind(&Browser::SetBadgeCount, browser))
.SetMethod("getBadgeCount", base::Bind(&Browser::GetBadgeCount, browser))
#if defined(OS_MACOSX)
.SetMethod("hide", base::Bind(&Browser::Hide, browser))
.SetMethod("show", base::Bind(&Browser::Show, browser))
@ -529,8 +531,11 @@ void App::BuildPrototype(
base::Bind(&Browser::GetCurrentActivityType, browser))
#endif
#if defined(OS_WIN)
.SetMethod("setUserTasks",
base::Bind(&Browser::SetUserTasks, browser))
.SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser))
#endif
#if defined(OS_LINUX)
.SetMethod("isUnityRunning",
base::Bind(&Browser::IsUnityRunning, browser))
#endif
.SetMethod("setPath", &App::SetPath)
.SetMethod("getPath", &App::GetPath)
@ -614,16 +619,6 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
dict.SetMethod("dockSetMenu", &DockSetMenu);
dict.SetMethod("dockSetIcon", base::Bind(&Browser::DockSetIcon, browser));
#endif
#if defined(OS_LINUX)
auto browser = base::Unretained(Browser::Get());
dict.SetMethod("unityLauncherAvailable",
base::Bind(&Browser::UnityLauncherAvailable, browser));
dict.SetMethod("unityLauncherSetBadgeCount",
base::Bind(&Browser::UnityLauncherSetBadgeCount, browser));
dict.SetMethod("unityLauncherGetBadgeCount",
base::Bind(&Browser::UnityLauncherGetBadgeCount, browser));
#endif
}
} // namespace

View file

@ -118,6 +118,10 @@ void Browser::SetName(const std::string& name) {
name_override_ = name;
}
int Browser::GetBadgeCount() {
return badge_count_;
}
bool Browser::OpenFile(const std::string& file_path) {
bool prevent_default = false;
FOR_EACH_OBSERVER(BrowserObserver,

View file

@ -87,6 +87,10 @@ class Browser : public WindowListObserver {
// Query the current state of default handler for a protocol.
bool IsDefaultProtocolClient(const std::string& protocol);
// Set/Get the badge count.
bool SetBadgeCount(int count);
int GetBadgeCount();
#if defined(OS_MACOSX)
// Hide the application.
void Hide();
@ -152,10 +156,8 @@ class Browser : public WindowListObserver {
#endif // defined(OS_WIN)
#if defined(OS_LINUX)
// Set/Get unity dock's badge counter.
bool UnityLauncherAvailable();
void UnityLauncherSetBadgeCount(int count);
int UnityLauncherGetBadgeCount();
// Whether Unity launcher is running.
bool IsUnityRunning();
#endif // defined(OS_LINUX)
// Tell the application to open a file.
@ -223,9 +225,7 @@ class Browser : public WindowListObserver {
std::string version_override_;
std::string name_override_;
#if defined(OS_LINUX)
int current_badge_count_ = 0;
#endif
int badge_count_ = 0;
#if defined(OS_WIN)
base::string16 app_user_model_id_;

View file

@ -47,6 +47,16 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
return false;
}
bool Browser::SetBadgeCount(int count) {
if (IsUnityRunning()) {
unity::SetDownloadCount(count);
badge_count_ = count;
return true;
} else {
return false;
}
}
std::string Browser::GetExecutableFileVersion() const {
return brightray::GetApplicationVersion();
}
@ -55,19 +65,8 @@ std::string Browser::GetExecutableFileProductName() const {
return brightray::GetApplicationName();
}
bool Browser::UnityLauncherAvailable() {
bool Browser::IsUnityRunning() {
return unity::IsRunning();
}
void Browser::UnityLauncherSetBadgeCount(int count) {
if (UnityLauncherAvailable()) {
current_badge_count_ = count;
unity::SetDownloadCount(count);
}
}
int Browser::UnityLauncherGetBadgeCount() {
return current_badge_count_;
}
} // namespace atom

View file

@ -11,6 +11,7 @@
#include "atom/browser/window_list.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "brightray/common/application_info.h"
#include "net/base/mac/url_conversions.h"
@ -114,6 +115,12 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
void Browser::SetAppUserModelID(const base::string16& name) {
}
bool Browser::SetBadgeCount(int count) {
DockSetBadgeText(count != 0 ? base::IntToString(count) : "");
badge_count_ = count;
return true;
}
void Browser::SetUserActivity(const std::string& type,
const base::DictionaryValue& user_info,
mate::Arguments* args) {

View file

@ -269,6 +269,10 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
}
}
bool Browser::SetBadgeCount(int count) {
return false;
}
PCWSTR Browser::GetAppUserModelID() {
if (app_user_model_id_.empty()) {
SetAppUserModelID(base::ReplaceStringPlaceholders(

View file

@ -577,6 +577,26 @@ Disables hardware acceleration for current app.
This method can only be called before app is ready.
### `app.setBadgeCount(count)` _Linux_ _macOS_
* `count` Integer
Sets the counter badge for current app. Setting the count to `0` will hide the
badge. Returns `true` when the call succeeded, otherwise returns `false`.
On macOS it shows on the dock icon. On Linux it only works for Unity launcher,
**Note:** Unity launcher requires the exsistence of a `.desktop` file to work,
for more information please read [Desktop Environment Integration][unity-requiremnt].
### `app.getBadgeCount()` _Linux_ _macOS_
Returns the current value displayed in the counter badge.
### `app.isUnityRunning()` _Linux_
Returns whether current desktop environment is Unity launcher.
### `app.commandLine.appendSwitch(switch[, value])`
Append a switch (with optional `value`) to Chromium's command line.
@ -647,31 +667,6 @@ Sets the application's [dock menu][dock-menu].
Sets the `image` associated with this dock icon.
### `app.launcher.setBadgeCount(count)` _Linux_
* `count` Integer
Sets the number to be displayed next to the app icon in the Unity launcher.
Setting the count to `0` will hide the badge.
**Note:** This feature is currently only supported on Ubuntu Unity. Calling this function has no effect when the application is running in a different environment.
**Note:** You need to specify the .desktop file name to the `desktopName` field in package.json. By default, it will assume `app.getName().desktop` in packaged apps.
### `app.launcher.getBadgeCount()` _Linux_
Returns the current value displayed in the counter badge next to the launcher icon.
**Note:** As `setBadgeCount` only supports Ubuntu Unity, the value will be 0 when the application is running in a different environment.
### `app.launcher.isUnityRunning()` _Linux_
This method checks if the current desktop environment is Unity and returns `true` in this case.
### `app.launcher.isCounterBadgeAvailable()` _Linux_
This method checks if the current desktop environment supports an app icon counter badge and returns `true` in this case.
[dock-menu]:https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/customizing_docktile/concepts/dockconcepts.html#//apple_ref/doc/uid/TP30000986-CH2-TPXREF103
[tasks]:http://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#tasks
[app-user-model-id]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd378459(v=vs.85).aspx
@ -679,3 +674,4 @@ This method checks if the current desktop environment supports an app icon count
[LSCopyDefaultHandlerForURLScheme]: https://developer.apple.com/library/mac/documentation/Carbon/Reference/LaunchServicesReference/#//apple_ref/c/func/LSCopyDefaultHandlerForURLScheme
[handoff]: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/Handoff/HandoffFundamentals/HandoffFundamentals.html
[activity-type]: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSUserActivity_Class/index.html#//apple_ref/occ/instp/NSUserActivity/activityType
[unity-requiremnt]: ../tutorial/desktop-environment-integration.md#unity-launcher-shortcuts-linux

View file

@ -285,36 +285,17 @@ describe('app module', function () {
})
})
describe('app.launcher API', function () {
it('should be available on linux', function () {
if (process.platform !== 'linux') {
assert.equal(app.launcher, undefined)
} else {
assert.notEqual(app.launcher, undefined)
}
describe('app.setBadgeCount API', function () {
const shouldFail = process.platform === 'win32' ||
(process.platform === 'linux' && !app.isUnityRunning())
it('returns false when failed', function () {
assert.equal(app.setBadgeCount(42), !shouldFail)
})
it('should be possible to set a badge count on supported environments', function () {
if (process.platform === 'linux' &&
app.launcher.isCounterBadgeAvailable()) {
app.launcher.setBadgeCount(42)
assert.equal(app.launcher.getBadgeCount(), 42)
}
})
it('should be possible to set a badge count on unity', function () {
if (process.platform === 'linux' &&
app.launcher.isUnityRunning()) {
assert.equal(app.launcher.isCounterBadgeAvailable(), true)
}
})
it('should not be possible to set a badge counter on unsupported environments', function () {
if (process.platform === 'linux' &&
!app.launcher.isCounterBadgeAvailable()) {
app.launcher.setBadgeCount(42)
assert.equal(app.launcher.getBadgeCount(), 0)
}
it('should set a badge count', function () {
app.setBadgeCount(42)
assert.equal(app.getBadgeCount(), shouldFail ? 0 : 42)
})
})
})