Browse Source

Add support for dropped text in osx

Wilson Page 8 years ago
parent
commit
90f8a7e828

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

@@ -74,6 +74,10 @@ void Tray::OnDropFiles(const std::vector<std::string>& files) {
   Emit("drop-files", files);
 }
 
+void Tray::OnDropText(const std::string& text) {
+  Emit("drop-text", text);
+}
+
 void Tray::OnDragEntered() {
   Emit("drag-enter");
 }

+ 1 - 0
atom/browser/api/atom_api_tray.h

@@ -53,6 +53,7 @@ class Tray : public mate::TrackableObject<Tray>,
   void OnBalloonClosed() override;
   void OnDrop() override;
   void OnDropFiles(const std::vector<std::string>& files) override;
+  void OnDropText(const std::string& text) override;
   void OnDragEntered() override;
   void OnDragExited() override;
   void OnDragEnded() override;

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

@@ -68,6 +68,10 @@ void TrayIcon::NotifyDropFiles(const std::vector<std::string>& files) {
   FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnDropFiles(files));
 }
 
+void TrayIcon::NotifyDropText(const std::string& text) {
+  FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnDropText(text));
+}
+
 void TrayIcon::NotifyDragEntered() {
   FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnDragEntered());
 }

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

@@ -75,6 +75,7 @@ class TrayIcon {
                           int modifiers = 0);
   void NotifyDrop();
   void NotifyDropFiles(const std::vector<std::string>& files);
+  void NotifyDropText(const std::string& text);
   void NotifyDragEntered();
   void NotifyDragExited();
   void NotifyDragEnded();

+ 9 - 1
atom/browser/ui/tray_icon_cocoa.mm

@@ -44,7 +44,10 @@ const CGFloat kVerticalTitleMargin = 2;
   inMouseEventSequence_ = NO;
 
   if ((self = [super initWithFrame: CGRectZero])) {
-    [self registerForDraggedTypes: @[NSFilenamesPboardType]];
+    [self registerForDraggedTypes: @[
+      NSFilenamesPboardType,
+      NSStringPboardType,
+    ]];
 
     // Create the status item.
     NSStatusItem * item = [[NSStatusBar systemStatusBar]
@@ -306,7 +309,12 @@ const CGFloat kVerticalTitleMargin = 2;
       dropFiles.push_back(base::SysNSStringToUTF8(file));
     trayIcon_->NotifyDropFiles(dropFiles);
     return YES;
+  } else if ([[pboard types] containsObject:NSStringPboardType]) {
+    NSString* dropText = [pboard stringForType:NSStringPboardType];
+    trayIcon_->NotifyDropText(base::SysNSStringToUTF8(dropText));
+    return YES;
   }
+
   return NO;
 }
 

+ 1 - 0
atom/browser/ui/tray_icon_observer.h

@@ -24,6 +24,7 @@ class TrayIconObserver {
   virtual void OnRightClicked(const gfx::Rect& bounds, int modifiers) {}
   virtual void OnDrop() {}
   virtual void OnDropFiles(const std::vector<std::string>& files) {}
+  virtual void OnDropText(const std::string& text) {}
   virtual void OnDragEntered() {}
   virtual void OnDragExited() {}
   virtual void OnDragEnded() {}

+ 7 - 0
docs/api/tray.md

@@ -122,6 +122,13 @@ Emitted when any dragged items are dropped on the tray icon.
 
 Emitted when dragged files are dropped in the tray icon.
 
+#### Event: 'drop-text' _macOS_
+
+* `event` Event
+* `text` String - the dropped text string
+
+Emitted when dragged text is dropped in the tray icon.
+
 #### Event: 'drag-enter' _macOS_
 
 Emitted when a drag operation enters the tray icon.