From d342c9a6dff2077d50bb34ad33ac6cb70441a81c Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Sun, 19 Jul 2015 12:12:28 +0800 Subject: [PATCH] Implement 'drop-files' tray event on OS X. --- atom/browser/api/atom_api_tray.cc | 4 ++++ atom/browser/api/atom_api_tray.h | 6 ++++-- atom/browser/ui/tray_icon.cc | 4 ++++ atom/browser/ui/tray_icon.h | 2 ++ atom/browser/ui/tray_icon_cocoa.mm | 20 ++++++++++++++++++++ atom/browser/ui/tray_icon_observer.h | 8 ++++++-- docs/api/tray.md | 9 +++++++++ 7 files changed, 49 insertions(+), 4 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index bf3a521d76e7..a209537c940f 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/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& files) { + Emit("drop-files", files); +} + bool Tray::IsDestroyed() const { return !tray_icon_; } diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 5b40937d717a..10ea3836819e 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -6,6 +6,7 @@ #define ATOM_BROWSER_API_ATOM_API_TRAY_H_ #include +#include #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& files) override; // mate::Wrappable: bool IsDestroyed() const override; diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index ab1ebfa67790..456cbe47a5a3 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/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& files) { + FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnDropFiles(files)); +} + } // namespace atom diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index a9e9b414ec94..d6885699d01d 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -6,6 +6,7 @@ #define ATOM_BROWSER_UI_TRAY_ICON_H_ #include +#include #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& files); protected: TrayIcon(); diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 4dcb6d2782c1..fddbd8c16eb0 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/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 )sender { + return NSDragOperationCopy; +} + +- (BOOL)performDragOperation:(id )sender { + NSPasteboard* pboard = [sender draggingPasteboard]; + + if ([[pboard types] containsObject:NSFilenamesPboardType]) { + std::vector 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); diff --git a/atom/browser/ui/tray_icon_observer.h b/atom/browser/ui/tray_icon_observer.h index 061115efdc64..6c9839a38a1f 100644 --- a/atom/browser/ui/tray_icon_observer.h +++ b/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 +#include + 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& files) {} protected: virtual ~TrayIconObserver() {} diff --git a/docs/api/tray.md b/docs/api/tray.md index fe0e2542b3a3..30225dd6f3ed 100644 --- a/docs/api/tray.md +++ b/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.