From cb00d3cfb3dc488f6a2d161283620f734ae7507b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 1 Jul 2016 17:39:01 +0900 Subject: [PATCH] mac: Implement app.setBadgeCount --- atom/browser/api/atom_api_app.cc | 19 +++++++----------- atom/browser/browser.cc | 4 ++++ atom/browser/browser.h | 12 +++++------ atom/browser/browser_linux.cc | 18 ++++++----------- atom/browser/browser_mac.mm | 6 ++++++ atom/browser/browser_win.cc | 4 ++++ spec/api-app-spec.js | 34 ++++---------------------------- 7 files changed, 37 insertions(+), 60 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index ce6a7d33c6a4..a39f9809ebf0 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -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 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.cc b/atom/browser/browser.cc index 8c00c1287376..3267653a7457 100644 --- a/atom/browser/browser.cc +++ b/atom/browser/browser.cc @@ -118,6 +118,10 @@ void Browser::SetName(const std::string& name) { name_override_ = name; } +int Browser::GetBadgeCount() { + return current_badge_count_; +} + bool Browser::OpenFile(const std::string& file_path) { bool prevent_default = false; FOR_EACH_OBSERVER(BrowserObserver, diff --git a/atom/browser/browser.h b/atom/browser/browser.h index c9155d94b4e3..5f496b89eeed 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -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. + void 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 #if defined(OS_WIN) base::string16 app_user_model_id_; diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index 8e41ec8c1997..ff48e5bed040 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -47,6 +47,11 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol) { return false; } +void Browser::SetBadgeCount(int count) { + current_badge_count_ = count; + unity::SetDownloadCount(count); +} + std::string Browser::GetExecutableFileVersion() const { return brightray::GetApplicationVersion(); } @@ -55,19 +60,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 diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index a50de2a07f60..ebd225c5996f 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -12,6 +12,7 @@ #include "base/mac/bundle_locations.h" #include "base/mac/foundation_util.h" #include "base/strings/sys_string_conversions.h" +#include "base/strings/string_number_conversions.h" #include "brightray/common/application_info.h" #include "net/base/mac/url_conversions.h" #include "url/gurl.h" @@ -114,6 +115,11 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol) { void Browser::SetAppUserModelID(const base::string16& name) { } +void Browser::SetBadgeCount(int count) { + current_badge_count_ = count; + DockSetBadgeText(count == 0 ? base::IntToString(count) : ""); +} + void Browser::SetUserActivity(const std::string& type, const base::DictionaryValue& user_info, mate::Arguments* args) { diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 345e2bcf67ce..e49e3b4cee04 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -269,6 +269,10 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol) { } } +void Browser::SetBadgeCount(int count) { + current_badge_count_ = count; +} + PCWSTR Browser::GetAppUserModelID() { if (app_user_model_id_.empty()) { SetAppUserModelID(base::ReplaceStringPlaceholders( diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 214a084f2ec7..4d27755d278b 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -285,36 +285,10 @@ 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) - } + describe('app.getBadgeCount API', function () { + it('should set a badge count', function () { + app.setBadgeCount(42) + assert.equal(app.getBadgeCount(), 42) }) }) })