diff --git a/Atom/src/Atom.h b/Atom/src/Atom.h index 1f4f314c1..2e7a2297e 100755 --- a/Atom/src/Atom.h +++ b/Atom/src/Atom.h @@ -12,7 +12,7 @@ class ClientHandler; CefRefPtr _clientHandler; } -- (void)open:(NSString *)path; +- (AtomController *)open:(NSString *)path; - (IBAction)runSpecs:(id)sender; - (IBAction)runBenchmarks:(id)sender; diff --git a/Atom/src/Atom.mm b/Atom/src/Atom.mm index b3f2449ed..9afc909be 100755 --- a/Atom/src/Atom.mm +++ b/Atom/src/Atom.mm @@ -60,8 +60,8 @@ CefBrowser::CreateBrowser(window_info, _clientHandler.get(), [indexURLString UTF8String], settings); } -- (void)open:(NSString *)path { - [[AtomController alloc] initWithPath:path atomContext:[self atomContext]]; +- (AtomController *)open:(NSString *)path { + return [[AtomController alloc] initWithPath:path atomContext:[self atomContext]]; } - (IBAction)runSpecs:(id)sender { diff --git a/Atom/src/AtomController.h b/Atom/src/AtomController.h index c5b7ff43d..f2e8e6dd1 100644 --- a/Atom/src/AtomController.h +++ b/Atom/src/AtomController.h @@ -10,6 +10,8 @@ class ClientHandler; NSString *_bootstrapScript; NSString *_pathToOpen; + BOOL _loaded; + CefRefPtr _atomContext; CefRefPtr _clientHandler; } @@ -20,8 +22,10 @@ class ClientHandler; - (id)initBenchmarksWithAtomContext:(CefRefPtr)atomContext; - (void)createBrowser; +- (void)blockUntilBrowserLoaded; @property (nonatomic, retain) IBOutlet NSView *webView; +@property (nonatomic, readonly) CefRefPtr clientHandler; @end diff --git a/Atom/src/AtomController.mm b/Atom/src/AtomController.mm index 16d53d2ab..69e3f6428 100644 --- a/Atom/src/AtomController.mm +++ b/Atom/src/AtomController.mm @@ -7,6 +7,7 @@ @implementation AtomController @synthesize webView=_webView; +@synthesize clientHandler=_clientHandler; - (void)dealloc { [_bootstrapScript release]; @@ -20,6 +21,7 @@ self = [super initWithWindowNibName:@"ClientWindow"]; _bootstrapScript = [bootstrapScript retain]; _atomContext = atomContext; + _loaded = false; [self.window makeKeyAndOrderFront:nil]; [self createBrowser]; @@ -60,9 +62,16 @@ CefBrowser::CreateBrowser(window_info, _clientHandler.get(), [indexURLString UTF8String], settings); } -#pragma mark BrowserDelegate +- (void)blockUntilBrowserLoaded { + NSRunLoop *runLoop = [NSRunLoop mainRunLoop]; + while (!_loaded) { + [runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0]]; + } +} +#pragma mark BrowserDelegate - (void)loadStart { + _loaded = false; CefRefPtr context = _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context(); CefRefPtr global = context->GetGlobal(); @@ -82,6 +91,10 @@ context->Exit(); } +- (void)loadEnd { + _loaded = true; +} + - (bool)keyEventOfType:(cef_handler_keyevent_type_t)type code:(int)code modifiers:(int)modifiers diff --git a/Atom/src/native_handler.mm b/Atom/src/native_handler.mm index 5b48d36f9..f75aa99d5 100644 --- a/Atom/src/native_handler.mm +++ b/Atom/src/native_handler.mm @@ -1,6 +1,8 @@ #import "native_handler.h" #import "include/cef.h" #import "Atom.h" +#import "AtomController.h" +#import "client_handler.h" NSString *stringFromCefV8Value(const CefRefPtr& value) { std::string cc_value = value->GetStringValue().ToString(); @@ -51,6 +53,13 @@ bool NativeHandler::Execute(const CefString& name, NSString *path = stringFromCefV8Value(arguments[0]); NSString *content = stringFromCefV8Value(arguments[1]); + NSFileManager *fm = [NSFileManager defaultManager]; + + // Create parent directories if they don't exist + BOOL exists = [fm fileExistsAtPath:[path stringByDeletingLastPathComponent] isDirectory:nil]; + if (!exists) { + [fm createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; + } NSError *error = nil; BOOL success = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; @@ -62,7 +71,9 @@ bool NativeHandler::Execute(const CefString& name, std::string exception = "Cannot write to '"; exception += [path UTF8String]; exception += "'"; - } + } + + return true; } else if (name == "absolute") { NSString *path = stringFromCefV8Value(arguments[0]); @@ -224,12 +235,16 @@ bool NativeHandler::Execute(const CefString& name, } else if (name == "open") { NSString *path = stringFromCefV8Value(arguments[0]); - [NSApp open:path]; + AtomController *atomController = [(Atom *)NSApp open:path]; + [atomController blockUntilBrowserLoaded]; + + CefRefPtr clientHandler = [atomController clientHandler]; + retval = clientHandler->GetBrowser()->GetMainFrame()->GetV8Context()->GetGlobal(); return true; } else if (name == "newWindow") { - [NSApp open:nil]; + [(Atom *)NSApp open:nil]; return true; } diff --git a/spec/atom/global-keymap-spec.coffee b/spec/atom/keymap-spec.coffee similarity index 100% rename from spec/atom/global-keymap-spec.coffee rename to spec/atom/keymap-spec.coffee diff --git a/spec/atom/root-view-spec.coffee b/spec/atom/root-view-spec.coffee index 3cf907750..37f2c1cdb 100644 --- a/spec/atom/root-view-spec.coffee +++ b/spec/atom/root-view-spec.coffee @@ -319,7 +319,7 @@ describe "RootView", -> expect(editor1.buffer.url).not.toBe expectedUrl expect(editor2.buffer.url).toBe expectedUrl - describe "global keymap wiring", -> + describe "keymap wiring", -> commandHandler = null beforeEach -> commandHandler = jasmine.createSpy('commandHandler') diff --git a/src/atom/app.coffee b/src/atom/app.coffee index 72530397d..eb5f07c5b 100644 --- a/src/atom/app.coffee +++ b/src/atom/app.coffee @@ -1,4 +1,6 @@ Keymap = require 'keymap' +fs = require 'fs' + $ = require 'jquery' _ = require 'underscore' require 'underscore-extensions' @@ -8,11 +10,13 @@ class App keymap: null windows: null tabText: null + userConfigurationPath: null constructor: (@loadPath, nativeMethods)-> @windows = [] @setUpKeymap() @tabText = " " + @userConfigurationPath = fs.absolute "~/.atom/atom.coffee" setUpKeymap: -> @keymap = new Keymap() diff --git a/src/atom/keymap.coffee b/src/atom/keymap.coffee index bfdc484c3..d0734b72e 100644 --- a/src/atom/keymap.coffee +++ b/src/atom/keymap.coffee @@ -1,7 +1,9 @@ -$ = require 'jquery' +fs = require 'fs' BindingSet = require 'binding-set' Specificity = require 'specificity' +$ = require 'jquery' + module.exports = class Keymap bindingSetsBySelector: null @@ -11,19 +13,25 @@ class Keymap bindDefaultKeys: -> @bindKeys "*", - 'meta-n': 'newWindow' + 'meta-n': 'new-window' 'meta-o': 'open' + 'meta-,': 'open-user-configuration' @_newWindow = => $native.newWindow() @_open = => url = $native.openDialog() atom.open(url) if url + @_openUserConfiguration = => + openedWindow = atom.open(atom.userConfigurationPath) + defaultConfiguration = "# This is run when a window is loaded!\nconsole.log('see!')" + openedWindow.rootView.editor.buffer.setText defaultConfiguration unless fs.exists(atom.userConfigurationPath) - $(document).on 'newWindow', @_newWindow + $(document).on 'new-window', @_newWindow $(document).on 'open', @_open + $(document).on 'open-user-configuration', @_openUserConfiguration unbindDefaultKeys: -> - $(document).unbind 'newWindow', @_newWindow + $(document).unbind 'new-window', @_newWindow $(document).unbind 'open', @_open bindKeys: (selector, bindings) -> diff --git a/src/atom/window.coffee b/src/atom/window.coffee index ed8af4494..c041cad01 100644 --- a/src/atom/window.coffee +++ b/src/atom/window.coffee @@ -19,7 +19,7 @@ windowAdditions = startup: (url) -> @setUpKeymap() @attachRootView(url) - @loadUserConfig() + @loadUserConfiguration() $(window).on 'close', => @close() $(window).focus() atom.windowOpened this @@ -46,12 +46,12 @@ windowAdditions = @rootView = new RootView {url} $(@rootViewParentSelector).append @rootView - loadUserConfig: (path="~/.atom") -> - absolutePath = fs.absolute(path) + loadUserConfiguration: -> try - require absolutePath if fs.exists(absolutePath) + require atom.userConfigurationPath if fs.exists(atom.userConfigurationPath) catch error - console.error "Failed to load `#{absolutePath}`", error + console.error "Failed to load `#{atom.userConfigurationPath}`", error + @showConsole() requireStylesheet: (path) -> fullPath = require.resolve(path)