Browse Source

mac: Implement app.setBadgeCount

Cheng Zhao 8 years ago
parent
commit
cb00d3cfb3

+ 7 - 12
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<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

+ 4 - 0
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,

+ 6 - 6
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_;

+ 6 - 12
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

+ 6 - 0
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) {

+ 4 - 0
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(

+ 4 - 30
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)
     })
   })
 })