diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 265ed8f36b7d..ce6a7d33c6a4 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -614,6 +614,16 @@ void Initialize(v8::Local exports, v8::Local 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 diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 2c1678083034..c9155d94b4e3 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -149,7 +149,14 @@ class Browser : public WindowListObserver { // one from app's name. // The returned string managed by Browser, and should not be modified. PCWSTR GetAppUserModelID(); -#endif +#endif // defined(OS_WIN) + +#if defined(OS_LINUX) + // Set/Get unity dock's badge counter. + bool UnityLauncherAvailable(); + void UnityLauncherSetBadgeCount(int count); + int UnityLauncherGetBadgeCount(); +#endif // defined(OS_LINUX) // Tell the application to open a file. bool OpenFile(const std::string& file_path); @@ -216,6 +223,10 @@ class Browser : public WindowListObserver { std::string version_override_; std::string name_override_; +#if defined(OS_LINUX) + int current_badge_count_ = 0; +#endif + #if defined(OS_WIN) base::string16 app_user_model_id_; #endif diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index d994bb4109bb..8e41ec8c1997 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -10,6 +10,7 @@ #include "atom/browser/window_list.h" #include "atom/common/atom_version.h" #include "brightray/common/application_info.h" +#include "chrome/browser/ui/libgtk2ui/unity_service.h" namespace atom { @@ -54,4 +55,19 @@ std::string Browser::GetExecutableFileProductName() const { return brightray::GetApplicationName(); } +bool Browser::UnityLauncherAvailable() { + 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 diff --git a/docs/api/app.md b/docs/api/app.md index c014395eb10c..260e64533b15 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -647,6 +647,31 @@ 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 diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 34210f42090f..0ea788b94a71 100644 --- a/lib/browser/api/app.js +++ b/lib/browser/api/app.js @@ -45,6 +45,15 @@ if (process.platform === 'darwin') { } } +if (process.platform === 'linux') { + app.launcher = { + setBadgeCount: bindings.unityLauncherSetBadgeCount, + getBadgeCount: bindings.unityLauncherGetBadgeCount, + isCounterBadgeAvailable: bindings.unityLauncherAvailable, + isUnityRunning: bindings.unityLauncherAvailable + } +} + app.allowNTLMCredentialsForAllDomains = function (allow) { if (!process.noDeprecations) { deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains') diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 6cda0e11bc06..214a084f2ec7 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -284,4 +284,37 @@ 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) + } + }) + + 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) + } + }) + }) })