window-bootstrap is called when a file is opened.

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-02-28 17:24:58 -08:00
parent 6a2ac7b25e
commit c84320b536
7 changed files with 77 additions and 35 deletions

View File

@ -54,7 +54,8 @@
}
- (void)open:(NSString *)path {
CefRefPtr<CefV8Context> atomContext = _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context();
[[AtomController alloc] initWithPath:path atomContext:atomContext];
}
- (IBAction)runSpecs:(id)sender {
@ -70,7 +71,11 @@
CefShutdown();
}
- (void)loadStart:(CefRefPtr<CefBrowser>) browser {
- (void)afterCreated:(CefRefPtr<CefBrowser>) browser {
browser->ShowDevTools();
}
- (void)loadStart:(CefRefPtr<CefBrowser>)browser {
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> context = frame->GetV8Context();
CefRefPtr<CefV8Value> global = context->GetGlobal();

View File

@ -6,12 +6,14 @@ class ClientHandler;
@interface AtomController : NSWindowController <NSWindowDelegate> {
NSView *_webView;
NSString *_bootstrapScript;
NSString *_pathToOpen;
CefRefPtr<CefV8Context> _atomContext;
CefRefPtr<ClientHandler> _clientHandler;
}
- (id)initWithBootstrapScript:(NSString *)bootstrapScript atomContext:(CefRefPtr<CefV8Context>) context;
- (id)initWithPath:(NSString *)path atomContext:(CefRefPtr<CefV8Context>)atomContext;
- (id)initSpecsWithAtomContext:(CefRefPtr<CefV8Context>)atomContext;
- (void)createBrowser;

View File

@ -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<CefV8Context>)atomContext {
_pathToOpen = [path retain];
return [self initWithBootstrapScript:@"window-bootstrap" atomContext:atomContext];
}
- (id)initSpecsWithAtomContext:(CefRefPtr<CefV8Context>)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<CefBrowser>) browser {
browser->ShowDevTools();
browser->ShowDevTools();
}
- (void)loadStart:(CefRefPtr<CefBrowser>) browser {
@ -60,8 +69,10 @@
CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString([_bootstrapScript UTF8String]);
global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Value> pathToOpen = CefV8Value::CreateString("~/");
global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE);
if (_pathToOpen) {
CefRefPtr<CefV8Value> 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<CefV8Context> context = _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context();
CefRefPtr<CefV8Value> global = context->GetGlobal();
context->Enter();
CefRefPtr<CefV8Value> atom = context->GetGlobal()->GetValue("atom");
CefRefPtr<CefV8Value> retval;
CefRefPtr<CefV8Exception> 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];

View File

@ -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()

View File

@ -4,6 +4,7 @@ _ = require 'underscore'
Native = require 'native'
GlobalKeymap = require 'global-keymap'
Point = require 'point'
require 'window'
window.showConsole()

View File

@ -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)