Emit 'open-file' event when OS X is trying to open file with the app.

This commit is contained in:
Cheng Zhao 2013-05-30 16:03:10 +08:00
parent 01af2fd0c5
commit 7dd48e24d3
10 changed files with 49 additions and 3 deletions

View file

@ -4,6 +4,7 @@
#include "browser/api/atom_api_app.h"
#include "base/values.h"
#include "base/command_line.h"
#include "browser/browser.h"
#include "vendor/node/src/node.h"
@ -29,6 +30,12 @@ void App::OnWindowAllClosed() {
Emit("window-all-closed");
}
void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
base::ListValue args;
args.AppendString(file_path);
*prevent_default = Emit("open-file", &args);
}
// static
v8::Handle<v8::Value> App::New(const v8::Arguments &args) {
v8::HandleScope scope;

View file

@ -26,6 +26,8 @@ class App : public EventEmitter,
// BrowserObserver implementations:
virtual void OnWillQuit(bool* prevent_default) OVERRIDE;
virtual void OnWindowAllClosed() OVERRIDE;
virtual void OnOpenFile(bool* prevent_default,
const std::string& file_path) OVERRIDE;
private:
static v8::Handle<v8::Value> New(const v8::Arguments &args);

View file

@ -16,6 +16,11 @@
andEventID:kAEQuitApplication];
}
- (BOOL)application:(NSApplication*)sender
openFile:(NSString*)filename {
return [[AtomApplication sharedApplication] openFile:filename];
}
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
[[AtomApplication sharedApplication] closeAllWindows:self];

View file

@ -10,13 +10,15 @@
BOOL handlingSendEvent_;
}
+ (AtomApplication*)sharedApplication;
// CrAppProtocol:
- (BOOL)isHandlingSendEvent;
// CrAppControlProtocol:
- (void)setHandlingSendEvent:(BOOL)handlingSendEvent;
+ (AtomApplication*)sharedApplication;
- (BOOL)openFile:(NSString*)file;
- (IBAction)closeAllWindows:(id)sender;

View file

@ -5,10 +5,15 @@
#import "browser/atom_application_mac.h"
#include "base/auto_reset.h"
#include "base/strings/sys_string_conversions.h"
#include "browser/browser.h"
@implementation AtomApplication
+ (AtomApplication*)sharedApplication {
return (AtomApplication*)[super sharedApplication];
}
- (BOOL)isHandlingSendEvent {
return handlingSendEvent_;
}
@ -22,8 +27,9 @@
handlingSendEvent_ = handlingSendEvent;
}
+ (AtomApplication*)sharedApplication {
return (AtomApplication*)[super sharedApplication];
- (BOOL)openFile:(NSString*)filename {
std::string filename_str(base::SysNSStringToUTF8(filename));
return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO;
}
- (IBAction)closeAllWindows:(id)sender {

View file

@ -31,6 +31,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
virtual void PostEarlyInitialization() OVERRIDE;
virtual void PreMainMessageLoopStart() OVERRIDE;
virtual void PreMainMessageLoopRun() OVERRIDE;
virtual void PostDestroyThreads() OVERRIDE;
private:
scoped_ptr<AtomBrowserBindings> atom_bindings_;

View file

@ -25,4 +25,8 @@ void AtomBrowserMainParts::PreMainMessageLoopStart() {
[mainNib release];
}
void AtomBrowserMainParts::PostDestroyThreads() {
[[[AtomApplication sharedApplication] delegate] release];
}
} // namespace atom

View file

@ -32,6 +32,15 @@ void Browser::Quit() {
window_list->CloseAllWindows();
}
bool Browser::OpenFile(const std::string& file_path) {
bool prevent_default = false;
FOR_EACH_OBSERVER(BrowserObserver,
observers_,
OnOpenFile(&prevent_default, file_path));
return prevent_default;
}
void Browser::NotifyAndTerminate() {
bool prevent_default = false;
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default));

View file

@ -27,6 +27,9 @@ class Browser : public WindowListObserver {
// Quit the application immediately without cleanup work.
void Terminate();
// Tell the application to open a file.
bool OpenFile(const std::string& file_path);
void AddObserver(BrowserObserver* obs) {
observers_.AddObserver(obs);
}

View file

@ -5,6 +5,8 @@
#ifndef ATOM_BROSER_BROWSER_OBSERVER_H_
#define ATOM_BROSER_BROWSER_OBSERVER_H_
#include <string>
namespace atom {
class BrowserObserver {
@ -16,6 +18,11 @@ class BrowserObserver {
// method will not be called, instead it will call OnWillQuit.
virtual void OnWindowAllClosed() {}
// The browser has opened a file by double clicking in Finder or dragging the
// file to the Dock icon. (OS X only)
virtual void OnOpenFile(bool* prevent_default,
const std::string& file_path) {}
protected:
virtual ~BrowserObserver() {}
};