diff --git a/Atom/Classes/Atom.mm b/Atom/Classes/Atom.mm index 57e56c1aa..b3db59e88 100755 --- a/Atom/Classes/Atom.mm +++ b/Atom/Classes/Atom.mm @@ -54,7 +54,8 @@ } - (void)open:(NSString *)path { - + CefRefPtr atomContext = _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context(); + [[AtomController alloc] initWithPath:path atomContext:atomContext]; } - (IBAction)runSpecs:(id)sender { @@ -70,7 +71,11 @@ CefShutdown(); } -- (void)loadStart:(CefRefPtr) browser { +- (void)afterCreated:(CefRefPtr) browser { + browser->ShowDevTools(); +} + +- (void)loadStart:(CefRefPtr)browser { CefRefPtr frame = browser->GetMainFrame(); CefRefPtr context = frame->GetV8Context(); CefRefPtr global = context->GetGlobal(); diff --git a/Atom/Classes/AtomController.h b/Atom/Classes/AtomController.h index 7e6dd22be..d2f3baae8 100644 --- a/Atom/Classes/AtomController.h +++ b/Atom/Classes/AtomController.h @@ -6,12 +6,14 @@ class ClientHandler; @interface AtomController : NSWindowController { NSView *_webView; NSString *_bootstrapScript; + NSString *_pathToOpen; CefRefPtr _atomContext; CefRefPtr _clientHandler; } - (id)initWithBootstrapScript:(NSString *)bootstrapScript atomContext:(CefRefPtr) context; +- (id)initWithPath:(NSString *)path atomContext:(CefRefPtr)atomContext; - (id)initSpecsWithAtomContext:(CefRefPtr)atomContext; - (void)createBrowser; diff --git a/Atom/Classes/AtomController.mm b/Atom/Classes/AtomController.mm index c3c4791c3..b06f0765c 100644 --- a/Atom/Classes/AtomController.mm +++ b/Atom/Classes/AtomController.mm @@ -9,6 +9,8 @@ - (void)dealloc { [_bootstrapScript release]; + [_webView release]; + [_pathToOpen release]; [super dealloc]; } @@ -17,21 +19,28 @@ self = [super initWithWindowNibName:@"ClientWindow"]; _bootstrapScript = [bootstrapScript retain]; _atomContext = atomContext; - - [self createBrowser]; + [self.window makeKeyAndOrderFront:nil]; + [self createBrowser]; return self; } +- (id)initWithPath:(NSString *)path atomContext:(CefRefPtr)atomContext { + _pathToOpen = [path retain]; + return [self initWithBootstrapScript:@"window-bootstrap" atomContext:atomContext]; +} + - (id)initSpecsWithAtomContext:(CefRefPtr)atomContext { return [self initWithBootstrapScript:@"spec-bootstrap" atomContext:atomContext]; } -- (void)createBrowser { +- (void)windowDidLoad { [self.window setDelegate:self]; [self.window setReleasedWhenClosed:NO]; - +} + +- (void)createBrowser { _clientHandler = new ClientHandler(self); CefWindowInfo window_info; @@ -47,7 +56,7 @@ } - (void)afterCreated:(CefRefPtr) browser { - browser->ShowDevTools(); + browser->ShowDevTools(); } - (void)loadStart:(CefRefPtr) browser { @@ -60,8 +69,10 @@ CefRefPtr bootstrapScript = CefV8Value::CreateString([_bootstrapScript UTF8String]); global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE); - CefRefPtr pathToOpen = CefV8Value::CreateString("~/"); - global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE); + if (_pathToOpen) { + CefRefPtr pathToOpen = CefV8Value::CreateString([_pathToOpen UTF8String]); + global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE); + } global->SetValue("atom", _atomContext->GetGlobal()->GetValue("atom"), V8_PROPERTY_ATTRIBUTE_NONE); @@ -70,14 +81,27 @@ #pragma mark NSWindowDelegate -// Called when the window is about to close. Perform the self-destruction -// sequence by getting rid of the window. By returning YES, we allow the window -// to be removed from the screen. -- (BOOL)windowShouldClose:(id)window { - _clientHandler->GetBrowser()->CloseDevTools(); +- (BOOL)windowShouldClose:(id)window { + CefRefPtr context = _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context(); + CefRefPtr global = context->GetGlobal(); + + context->Enter(); + + CefRefPtr atom = context->GetGlobal()->GetValue("atom"); + + CefRefPtr retval; + CefRefPtr exception; + CefV8ValueList arguments; + arguments.push_back(global); + + atom->GetValue("windowClosed")->ExecuteFunction(atom, arguments, retval, exception, true); + + context->Exit(); + + _clientHandler->GetBrowser()->CloseDevTools(); _atomContext = NULL; - _clientHandler = NULL; + _clientHandler = NULL; // Clean ourselves up after clearing the stack of anything that might have the window on it. [self autorelease]; diff --git a/spec/atom/app-spec.coffee b/spec/atom/app-spec.coffee index 61d7d01c7..a678da825 100644 --- a/spec/atom/app-spec.coffee +++ b/spec/atom/app-spec.coffee @@ -2,26 +2,28 @@ App = require 'app' fs = require 'fs' describe "App", -> - app = null - - beforeEach -> - app = new App() - afterEach -> - window.close() for window in app.windows() - waitsFor -> - app.windows().length == 0 + window.close() for window in atom.windows + waitsFor "there to be no windows", -> + atom.windows.length == 0 describe "open", -> describe "when opening a filePath", -> it "displays it in a new window with the contents of the file loaded", -> - filePath = require.resolve 'fixtures/sample.txt' - expect(app.windows().length).toBe 0 + filePath = null - app.open filePath + runs -> + filePath = require.resolve 'fixtures/sample.txt' + expect(atom.windows.length).toBe 0 - expect(app.windows().length).toBe 1 - newWindow = app.windows()[0] - expect(newWindow.rootView.editor.buffer.url).toEqual filePath - expect(newWindow.rootView.editor.buffer.getText()).toEqual fs.read(filePath) + atom.open filePath + + waitsFor "window to open", (windowOpened) -> + atom.on "open", -> + expect(atom.windows.length).toBe 1 + newWindow = atom.windows[0] + expect(newWindow.rootView.editor.buffer.url).toEqual filePath + expect(newWindow.rootView.editor.buffer.getText()).toEqual fs.read(filePath) + + windowOpened() diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 5bfaf2dcf..304810397 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -4,6 +4,7 @@ _ = require 'underscore' Native = require 'native' GlobalKeymap = require 'global-keymap' Point = require 'point' + require 'window' window.showConsole() diff --git a/src/atom/app.coffee b/src/atom/app.coffee index b76346688..0eadca294 100644 --- a/src/atom/app.coffee +++ b/src/atom/app.coffee @@ -1,3 +1,4 @@ +EventEmitter = require 'event-emitter' Native = require 'native' GlobalKeymap = require 'global-keymap' $ = require 'jquery' @@ -7,11 +8,12 @@ module.exports = class App globalKeymap: null native: null + windows: null constructor: (@loadPath, nativeMethods)-> - @native = new Native(nativeMethods) @globalKeymap = new GlobalKeymap - $(document).on 'keydown', (e) => @globalKeymap.handleKeyEvent(e) + @native = new Native(nativeMethods) + @windows = [] bindKeys: (selector, bindings) -> @globalKeymap.bindKeys(selector, bindings) @@ -25,6 +27,12 @@ class App quit: -> @native.terminate null - windows: -> - # controller.jsWindow for controller in OSX.NSApp.controllers - [] + windowOpened: (window) -> + @windows.push window + @trigger "open", window + + windowClosed: (window) -> + index = @windows.indexOf(window) + @windows.splice(index, 1) if index >= 0 + +_.extend(App.prototype, EventEmitter) diff --git a/src/bootstrap.coffee b/src/window-bootstrap.coffee similarity index 100% rename from src/bootstrap.coffee rename to src/window-bootstrap.coffee