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

This commit is contained in:
Haojian Wu 2015-07-19 12:12:28 +08:00
parent 2cd6ad1a97
commit d342c9a6df
7 changed files with 49 additions and 4 deletions

View file

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

View file

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

View file

@ -53,4 +53,8 @@ void TrayIcon::NotifyRightClicked(const gfx::Rect& bounds) {
FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnRightClicked(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 } // namespace atom

View file

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

View file

@ -41,6 +41,8 @@ const CGFloat kMargin = 3;
kStatusItemLength, kStatusItemLength,
[[statusItem_ statusBar] thickness]); [[statusItem_ statusBar] thickness]);
if ((self = [super initWithFrame:frame])) { if ((self = [super initWithFrame:frame])) {
[self registerForDraggedTypes:
[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
[statusItem_ setView:self]; [statusItem_ setView:self];
} }
return self; return self;
@ -174,6 +176,24 @@ const CGFloat kMargin = 3;
trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]); 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)shouldHighlight {
BOOL is_menu_open = [menuController_ isMenuOpen]; BOOL is_menu_open = [menuController_ isMenuOpen];
return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open); return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open);

View file

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

View file

@ -95,6 +95,15 @@ closes it.
__Note:__ This is only implemented on Windows. __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() ### Tray.destroy()
Destroys the tray icon immediately. Destroys the tray icon immediately.