mac: Implement app.setBadgeCount
authorCheng Zhao <zcbenz@gmail.com>
Fri, 1 Jul 2016 08:39:01 +0000 (17:39 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 1 Jul 2016 08:39:26 +0000 (17:39 +0900)
atom/browser/api/atom_api_app.cc
atom/browser/browser.cc
atom/browser/browser.h
atom/browser/browser_linux.cc
atom/browser/browser_mac.mm
atom/browser/browser_win.cc
spec/api-app-spec.js

index ce6a7d33c6a40f0b33605a860ff7ab948ba84258..a39f9809ebf0ec29b57e1c38d9150c06a9ba5b30 100644 (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
index 8c00c12873762ab16dff23fa2a227fba3cb5d7d6..3267653a7457683859c24f0ceae72096b2f7a6be 100644 (file)
@@ -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,
index c9155d94b4e3a6fef6a3281ad238dabad8367752..5f496b89eeeda010a48d2927f197f6fb81d49772 100644 (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.
+  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_;
index 8e41ec8c1997b3e1365c233108d1b3a7fb8f722a..ff48e5bed040b39ed395a38f7b1a222596f717e4 100644 (file)
@@ -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
index a50de2a07f602b838530c3f42b5199139396b49f..ebd225c5996f5d0324dcd5935a147e56c464f212 100644 (file)
@@ -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) {
index 345e2bcf67ce0d96c6093ac6a27aa3ab91e67dd8..e49e3b4cee04c6a5506205ca94fbd7899a68c29e 100644 (file)
@@ -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(
index 214a084f2ec7c01b3dc318af56f808d8cc688c25..4d27755d278bb798fda2f6ed23615b60cf483ada 100644 (file)
@@ -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)
     })
   })
 })