From b22ce934775537c18a953f40efb7a9526df285fe Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Sun, 26 Jun 2016 01:55:24 +0200 Subject: [PATCH 1/7] Implement libunity launcher badge counter to be set via electron, fixes #4193 --- atom/browser/api/atom_api_app.cc | 10 ++++++++++ atom/browser/browser.h | 11 ++++++++++- atom/browser/browser_linux.cc | 14 ++++++++++++++ lib/browser/api/app.js | 7 +++++++ 4 files changed, 41 insertions(+), 1 deletion(-) 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..601f40044b2c 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,8 @@ class Browser : public WindowListObserver { std::string version_override_; std::string name_override_; + int current_badge_count_ = 0; + #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..668da661802f 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,17 @@ std::string Browser::GetExecutableFileProductName() const { return brightray::GetApplicationName(); } +bool Browser::UnityLauncherAvailable() { + return unity::IsRunning(); +} + +void Browser::UnityLauncherSetBadgeCount(int count) { + current_badge_count_ = count; + unity::SetDownloadCount(count); +} + +int Browser::UnityLauncherGetBadgeCount() { + return current_badge_count_; +} + } // namespace atom diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 34210f42090f..1fd972c57cae 100644 --- a/lib/browser/api/app.js +++ b/lib/browser/api/app.js @@ -45,6 +45,13 @@ if (process.platform === 'darwin') { } } +if (process.platform === 'linux' && bindings.unityLauncherAvailable()) { + app.unityLauncher = { + setBadgeCount: bindings.unityLauncherSetBadgeCount, + getBadgeCount: bindings.unityLauncherGetBadgeCount + }; +} + app.allowNTLMCredentialsForAllDomains = function (allow) { if (!process.noDeprecations) { deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains') From e3ba6818af8a649e868c1d604aef00f6cd0b4bd9 Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Sun, 26 Jun 2016 02:00:41 +0200 Subject: [PATCH 2/7] Fix linter errors --- atom/browser/browser.h | 4 ++-- lib/browser/api/app.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 601f40044b2c..739a9f5eb03c 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -149,14 +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 // defined(OS_WIN) +#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) +#endif // defined(OS_LINUX) // Tell the application to open a file. bool OpenFile(const std::string& file_path); diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 1fd972c57cae..602a7a0df51b 100644 --- a/lib/browser/api/app.js +++ b/lib/browser/api/app.js @@ -49,7 +49,7 @@ if (process.platform === 'linux' && bindings.unityLauncherAvailable()) { app.unityLauncher = { setBadgeCount: bindings.unityLauncherSetBadgeCount, getBadgeCount: bindings.unityLauncherGetBadgeCount - }; + } } app.allowNTLMCredentialsForAllDomains = function (allow) { From 336a55cb23502b89dc3cdcdca914bb2fb935e170 Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Sun, 26 Jun 2016 02:10:27 +0200 Subject: [PATCH 3/7] :memo: docs for unity launcher badge api --- docs/api/app.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/api/app.md b/docs/api/app.md index c014395eb10c..777f31551fd6 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -647,6 +647,17 @@ Sets the application's [dock menu][dock-menu]. Sets the `image` associated with this dock icon. +### `app.unityLauncher.setBadgeCount(count)` _Ubuntu Unity_ +* `count` Integer + +Sets the number to be displayed next to the app icon in the unity launcher. +Setting count to `0` will hide the badge. + +### `app.unityLauncher.getBadgeCount(count)` _Ubuntu Unity_ + +Returns the current value displayed in the counter badge next to the launcher icon. + + [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 From e2d389fbd45510b424ea178abd22add8747b0d4c Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Sun, 26 Jun 2016 11:48:58 +0200 Subject: [PATCH 4/7] Rename the unityLauncher api to launcher and make it available on linux in general. + Document it only works on ubuntu --- atom/browser/browser_linux.cc | 6 ++++-- docs/api/app.md | 14 ++++++++++++-- lib/browser/api/app.js | 7 ++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index 668da661802f..8e41ec8c1997 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -60,8 +60,10 @@ bool Browser::UnityLauncherAvailable() { } void Browser::UnityLauncherSetBadgeCount(int count) { - current_badge_count_ = count; - unity::SetDownloadCount(count); + if (UnityLauncherAvailable()) { + current_badge_count_ = count; + unity::SetDownloadCount(count); + } } int Browser::UnityLauncherGetBadgeCount() { diff --git a/docs/api/app.md b/docs/api/app.md index 777f31551fd6..da14a504451d 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -647,16 +647,26 @@ Sets the application's [dock menu][dock-menu]. Sets the `image` associated with this dock icon. -### `app.unityLauncher.setBadgeCount(count)` _Ubuntu Unity_ +### `app.launcher.setBadgeCount(count)` _Linux_ * `count` Integer Sets the number to be displayed next to the app icon in the unity launcher. Setting count to `0` will hide the badge. -### `app.unityLauncher.getBadgeCount(count)` _Ubuntu Unity_ +**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(count)` _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.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 diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 602a7a0df51b..466d2dd791ca 100644 --- a/lib/browser/api/app.js +++ b/lib/browser/api/app.js @@ -45,10 +45,11 @@ if (process.platform === 'darwin') { } } -if (process.platform === 'linux' && bindings.unityLauncherAvailable()) { - app.unityLauncher = { +if (process.platform === 'linux') { + app.launcher = { setBadgeCount: bindings.unityLauncherSetBadgeCount, - getBadgeCount: bindings.unityLauncherGetBadgeCount + getBadgeCount: bindings.unityLauncherGetBadgeCount, + isCounterBadgeAvailable: bindings.unityLauncherAvailable } } From 8ac205685d2cf2323bb5b72fd4ad37c2dd95b5ef Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Wed, 29 Jun 2016 19:23:00 +0200 Subject: [PATCH 5/7] Add ifdef for property, which should only be availabe on Linux --- atom/browser/browser.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 739a9f5eb03c..c9155d94b4e3 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -223,7 +223,9 @@ 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_; From 768ff7af5afb18a53479c6b0be32f01c0316a1c0 Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Wed, 29 Jun 2016 19:23:56 +0200 Subject: [PATCH 6/7] Add additional method isUnityRunning, fix multiple typos in docs --- docs/api/app.md | 14 +++++++++----- lib/browser/api/app.js | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index da14a504451d..260e64533b15 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -650,19 +650,23 @@ 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 count to `0` will hide the badge. +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:** 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. +**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(count)` _Linux_ +### `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. diff --git a/lib/browser/api/app.js b/lib/browser/api/app.js index 466d2dd791ca..0ea788b94a71 100644 --- a/lib/browser/api/app.js +++ b/lib/browser/api/app.js @@ -49,7 +49,8 @@ if (process.platform === 'linux') { app.launcher = { setBadgeCount: bindings.unityLauncherSetBadgeCount, getBadgeCount: bindings.unityLauncherGetBadgeCount, - isCounterBadgeAvailable: bindings.unityLauncherAvailable + isCounterBadgeAvailable: bindings.unityLauncherAvailable, + isUnityRunning: bindings.unityLauncherAvailable } } From 9aa8807f83ac3eb16d82552ed0ebfe509838164c Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Wed, 29 Jun 2016 19:47:20 +0200 Subject: [PATCH 7/7] add specs for app.launcher api fix linter errors --- spec/api-app-spec.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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) + } + }) + }) })