Browse Source

Implement 'drop-files' tray event on OS X.

Haojian Wu 9 years ago
parent
commit
d342c9a6df

+ 4 - 0
atom/browser/api/atom_api_tray.cc

@@ -64,6 +64,10 @@ void Tray::OnRightClicked(const gfx::Rect& bounds) {
   Emit("right-clicked", bounds);
 }
 
+void Tray::OnDropFiles(const std::vector<std::string>& files) {
+  Emit("drop-files", files);
+}
+
 bool Tray::IsDestroyed() const {
   return !tray_icon_;
 }

+ 4 - 2
atom/browser/api/atom_api_tray.h

@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_API_ATOM_API_TRAY_H_
 
 #include <string>
+#include <vector>
 
 #include "atom/browser/api/event_emitter.h"
 #include "atom/browser/ui/tray_icon_observer.h"
@@ -41,12 +42,13 @@ class Tray : public mate::EventEmitter,
   virtual ~Tray();
 
   // TrayIconObserver:
-  void OnClicked(const gfx::Rect&) override;
+  void OnClicked(const gfx::Rect& bounds) override;
   void OnDoubleClicked() override;
   void OnBalloonShow() override;
   void OnBalloonClicked() override;
   void OnBalloonClosed() override;
-  void OnRightClicked(const gfx::Rect&) override;
+  void OnRightClicked(const gfx::Rect& bounds) override;
+  void OnDropFiles(const std::vector<std::string>& files) override;
 
   // mate::Wrappable:
   bool IsDestroyed() const override;

+ 4 - 0
atom/browser/ui/tray_icon.cc

@@ -53,4 +53,8 @@ void TrayIcon::NotifyRightClicked(const gfx::Rect& bounds) {
   FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnRightClicked(bounds));
 }
 
+void TrayIcon::NotfiyDropFiles(const std::vector<std::string>& files) {
+  FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnDropFiles(files));
+}
+
 }  // namespace atom

+ 2 - 0
atom/browser/ui/tray_icon.h

@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_UI_TRAY_ICON_H_
 
 #include <string>
+#include <vector>
 
 #include "atom/browser/ui/tray_icon_observer.h"
 #include "base/observer_list.h"
@@ -59,6 +60,7 @@ class TrayIcon {
   void NotifyBalloonClicked();
   void NotifyBalloonClosed();
   void NotifyRightClicked(const gfx::Rect& bounds = gfx::Rect());
+  void NotfiyDropFiles(const std::vector<std::string>& files);
 
  protected:
   TrayIcon();

+ 20 - 0
atom/browser/ui/tray_icon_cocoa.mm

@@ -41,6 +41,8 @@ const CGFloat kMargin = 3;
                             kStatusItemLength,
                             [[statusItem_ statusBar] thickness]);
   if ((self = [super initWithFrame:frame])) {
+    [self registerForDraggedTypes:
+        [NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
     [statusItem_ setView:self];
   }
   return self;
@@ -174,6 +176,24 @@ const CGFloat kMargin = 3;
   trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]);
 }
 
+- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
+  return NSDragOperationCopy;
+}
+
+- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
+  NSPasteboard* pboard = [sender draggingPasteboard];
+
+  if ([[pboard types] containsObject:NSFilenamesPboardType]) {
+    std::vector<std::string> dropFiles;
+    NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
+    for (NSString* file in files)
+      dropFiles.push_back(base::SysNSStringToUTF8(file));
+    trayIcon_->NotfiyDropFiles(dropFiles);
+    return YES;
+  }
+  return NO;
+}
+
 - (BOOL)shouldHighlight {
   BOOL is_menu_open = [menuController_ isMenuOpen];
   return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open);

+ 6 - 2
atom/browser/ui/tray_icon_observer.h

@@ -5,6 +5,9 @@
 #ifndef ATOM_BROWSER_UI_TRAY_ICON_OBSERVER_H_
 #define ATOM_BROWSER_UI_TRAY_ICON_OBSERVER_H_
 
+#include <string>
+#include <vector>
+
 namespace gfx {
 class Rect;
 }
@@ -13,12 +16,13 @@ namespace atom {
 
 class TrayIconObserver {
  public:
-  virtual void OnClicked(const gfx::Rect&) {}
+  virtual void OnClicked(const gfx::Rect& bounds) {}
   virtual void OnDoubleClicked() {}
   virtual void OnBalloonShow() {}
   virtual void OnBalloonClicked() {}
   virtual void OnBalloonClosed() {}
-  virtual void OnRightClicked(const gfx::Rect&) {}
+  virtual void OnRightClicked(const gfx::Rect& bounds) {}
+  virtual void OnDropFiles(const std::vector<std::string>& files) {}
 
  protected:
   virtual ~TrayIconObserver() {}

+ 9 - 0
docs/api/tray.md

@@ -95,6 +95,15 @@ closes it.
 
 __Note:__ This is only implemented on Windows.
 
+### Event: 'drop-files'
+
+* `event`
+* `files` Array - the file path of dropped files.
+
+Emitted when dragged files are dropped in the tray icon.
+
+__Note:__ This is only implemented on OS X.
+
 ### Tray.destroy()
 
 Destroys the tray icon immediately.