[Mac] Add API for dock, fixes #46.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 6 Aug 2013 08:19:56 +0000 (16:19 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 6 Aug 2013 08:19:56 +0000 (16:19 +0800)
browser/api/atom_api_app.cc
browser/api/atom_api_app.h
browser/api/lib/app.coffee
browser/browser.h
browser/browser_mac.mm

index 221bce1..62ccc22 100644 (file)
@@ -139,6 +139,38 @@ v8::Handle<v8::Value> App::AppendArgument(const v8::Arguments &args) {
   return v8::Undefined();
 }
 
+#if defined(OS_MACOSX)
+
+// static
+v8::Handle<v8::Value> App::DockBounce(const v8::Arguments& args) {
+  std::string type(*v8::String::Utf8Value(args[0]));
+  int request_id = -1;
+
+  if (type == "critical")
+    request_id = Browser::Get()->DockBounce(Browser::BOUNCE_CRITICAL);
+  else if (type == "informational")
+    request_id = Browser::Get()->DockBounce(Browser::BOUNCE_INFORMATIONAL);
+  else
+    return node::ThrowTypeError("Invalid bounce type");
+
+  return v8::Integer::New(request_id);
+}
+
+// static
+v8::Handle<v8::Value> App::DockCancelBounce(const v8::Arguments& args) {
+  Browser::Get()->DockCancelBounce(args[0]->IntegerValue());
+  return v8::Undefined();
+}
+
+// static
+v8::Handle<v8::Value> App::DockSetBadgeText(const v8::Arguments& args) {
+  std::string label(*v8::String::Utf8Value(args[0]));
+  Browser::Get()->DockSetBadgeText(label);
+  return v8::Undefined();
+}
+
+#endif  // defined(OS_MACOSX)
+
 // static
 void App::Initialize(v8::Handle<v8::Object> target) {
   v8::HandleScope scope;
@@ -157,6 +189,12 @@ void App::Initialize(v8::Handle<v8::Object> target) {
 
   NODE_SET_METHOD(target, "appendSwitch", AppendSwitch);
   NODE_SET_METHOD(target, "appendArgument", AppendArgument);
+
+#if defined(OS_MACOSX)
+  NODE_SET_METHOD(target, "dockBounce", DockBounce);
+  NODE_SET_METHOD(target, "dockCancelBounce", DockCancelBounce);
+  NODE_SET_METHOD(target, "dockSetBadgeText", DockSetBadgeText);
+#endif  // defined(OS_MACOSX)
 }
 
 }  // namespace api
index 339178f..674b229 100644 (file)
@@ -43,6 +43,12 @@ class App : public EventEmitter,
   static v8::Handle<v8::Value> AppendSwitch(const v8::Arguments &args);
   static v8::Handle<v8::Value> AppendArgument(const v8::Arguments &args);
 
+#if defined(OS_MACOSX)
+  static v8::Handle<v8::Value> DockBounce(const v8::Arguments& args);
+  static v8::Handle<v8::Value> DockCancelBounce(const v8::Arguments& args);
+  static v8::Handle<v8::Value> DockSetBadgeText(const v8::Arguments& args);
+#endif  // defined(OS_MACOSX)
+
   DISALLOW_COPY_AND_ASSIGN(App);
 };
 
index 7f44dd8..a1c418e 100644 (file)
@@ -13,5 +13,11 @@ app.commandLine =
   appendSwitch: bindings.appendSwitch,
   appendArgument: bindings.appendArgument
 
+if process.platform is 'darwin'
+  app.dock =
+    bounce: (type = 'informational') -> bindings.dockBounce type
+    cancelBounce: bindings.dockCancelBounce
+    setBadge: bindings.dockSetBadgeText
+
 # Only one App object pemitted.
 module.exports = app
index d0189fc..15ab9ce 100644 (file)
@@ -33,6 +33,19 @@ class Browser : public WindowListObserver {
   // Returns the version of the executable (or bundle).
   std::string GetVersion();
 
+#if defined(OS_MACOSX)
+  // Bounce the dock icon.
+  enum BounceType {
+    BOUNCE_CRITICAL = 0,
+    BOUNCE_INFORMATIONAL = 10,
+  };
+  int DockBounce(BounceType type);
+  void DockCancelBounce(int request_id);
+
+  // Set dock's badge text.
+  void DockSetBadgeText(const std::string& label);
+#endif  // defined(OS_MACOSX)
+
   // Tell the application to open a file.
   bool OpenFile(const std::string& file_path);
 
index 1a8253c..c907265 100644 (file)
@@ -29,4 +29,17 @@ void Browser::CancelQuit() {
   [[AtomApplication sharedApplication] replyToApplicationShouldTerminate:NO];
 }
 
+int Browser::DockBounce(BounceType type) {
+  return [[AtomApplication sharedApplication] requestUserAttention:type];
+}
+
+void Browser::DockCancelBounce(int rid) {
+  [[AtomApplication sharedApplication] cancelUserAttentionRequest:rid];
+}
+
+void Browser::DockSetBadgeText(const std::string& label) {
+  NSDockTile *tile = [[AtomApplication sharedApplication] dockTile];
+  [tile setBadgeLabel:base::SysUTF8ToNSString(label)];
+}
+
 }  // namespace atom