Multiple windows can be opened, and their buffer state is saved.

This commit is contained in:
Corey Johnson
2011-11-03 15:48:19 -07:00
parent e4d79c6625
commit 7e7bbe3f01
5 changed files with 59 additions and 30 deletions

View File

@@ -4,21 +4,37 @@
#import <WebKit/WebKit.h>
#define ATOM_USER_PATH @"~/.atomicity/"
#define ATOM_USER_PATH ([[NSString stringWithString:@"~/.atomicity/"] stringByStandardizingPath])
#define ATOM_STORAGE_PATH ([ATOM_USER_PATH stringByAppendingPathComponent:@".app-storage"])
#define WEB_STORAGE_PATH ([ATOM_USER_PATH stringByAppendingPathComponent:@".web-storage"])
@implementation AtomApp
@synthesize controllers;
- (AtomController *)createController:(NSString *)path {
// Don't like this storage code in here.
if (path) {
NSMutableArray *storage = [NSMutableArray arrayWithContentsOfFile:ATOM_STORAGE_PATH];
if (!storage) storage = [NSMutableArray array];
if (![storage containsObject:path]) {
[storage addObject:path];
[storage writeToFile:ATOM_STORAGE_PATH atomically:YES];
}
}
AtomController *controller = [[AtomController alloc] initWithPath:path];
[controllers addObject:controller];
[controller.window makeKeyAndOrderFront:self];
[[controller window] makeKeyWindow];
return controller;
}
- (void)removeController:(AtomController *)controller {
[controllers removeObject:controller];
NSMutableArray *storage = [NSMutableArray arrayWithContentsOfFile:ATOM_STORAGE_PATH];
[storage removeObject:controller.path];
[storage writeToFile:ATOM_STORAGE_PATH atomically:YES];
}
- (void)open:(NSString *)path {
@@ -31,7 +47,7 @@
}
for (AtomController *controller in controllers) {
JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"canOpen" withArguments:path , nil];
JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"canOpen" withArguments:path, nil];
if ([controller.jscocoa toBool:value]) {
[controller.jscocoa callJSFunctionNamed:@"open" withArguments:path, nil];
return;
@@ -73,15 +89,33 @@
// Hack to make localStorage work
WebPreferences* prefs = [WebPreferences standardPreferences];
[prefs _setLocalStorageDatabasePath:ATOM_USER_PATH @"storage"];
[prefs _setLocalStorageDatabasePath:WEB_STORAGE_PATH];
[prefs setLocalStorageEnabled:YES];
NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"WebKitDeveloperExtras", nil];
NSDictionary *defaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"WebKitDeveloperExtras",
[NSNumber numberWithBool:YES], @"WebKitScriptDebuggerEnabled",
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self createController:NULL];
NSError *error = nil;
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:ATOM_USER_PATH withIntermediateDirectories:YES attributes:nil error:&error];
if (!success || error) {
[NSException raise:@"Atom: Failed to open storage path at '%@'. %@" format:ATOM_USER_PATH, [error localizedDescription]];
}
// Don't like this storage code in here.
NSMutableArray *storage = [NSMutableArray arrayWithContentsOfFile:ATOM_STORAGE_PATH];
if (storage.count == 0) {
[self createController:NULL];
}
else {
for (NSString *path in storage) {
[self createController:path];
}
}
}
@end

View File

@@ -3,11 +3,11 @@
@class JSCocoa;
@class WebView;
@interface AtomController : NSWindowController {
@interface AtomController : NSWindowController <NSWindowDelegate> {
}
@property (retain) WebView *webView;
@property (retain) NSString *path;
@property (retain, readonly) NSString *path;
@property (retain) JSCocoa *jscocoa;
- (AtomController *)initWithPath:(NSString *)aPath;

View File

@@ -22,8 +22,10 @@
}
- (id)initWithPath:(NSString *)aPath {
aPath = aPath ? aPath : @"/tmp";
self = [super initWithWindowNibName:@"AtomWindow"];
[self setPath:[aPath stringByStandardizingPath]];
path = [[aPath stringByStandardizingPath] retain];
return self;
}
@@ -31,6 +33,8 @@
- (void)windowDidLoad {
[super windowDidLoad];
[self.window setDelegate:self];
[webView setUIDelegate:self];
[self setShouldCascadeWindows:YES];
@@ -45,11 +49,6 @@
[[webView mainFrame] loadRequest:request];
}
- (void)close {
[(AtomApp *)NSApp removeController:self];
[super close];
}
- (NSString *)tempfile {
char *directory = "/tmp";
char *prefix = "temp-file";
@@ -60,9 +59,15 @@
return tmpPathString;
}
// WebUIDelegate Protocol
// WebUIDelegate
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems {
return defaultMenuItems;
}
// WindowDelegate
- (BOOL)windowShouldClose:(id)sender {
[(AtomApp *)NSApp removeController:self];
return YES;
}
@end

View File

@@ -73,7 +73,6 @@ class Editor
mode = @modeForPath path
buffer.setMode new mode if mode
return
@buffers[path] = buffer
buffer.on 'change', -> buffer.$atom_dirty = true

View File

@@ -19,12 +19,9 @@ windowAdditions =
startup: () ->
KeyBinder.register "window", window
if atomController.path
@setRecentPath atomController.path
else
atomController.path = @recentPath()
@setTitle atomController.path
@editor = if atomController.path and fs.isFile atomController.path
@editor = if fs.isFile atomController.path
new Editor atomController.path
else
new Editor
@@ -40,7 +37,7 @@ windowAdditions =
restoreEditorState: ->
storage = Storage.get @storageKey(), {}
@editor.addBuffer path for path in storage.openPaths ? []
@editor.focusBuffer storage.lastOpenedPath
@editor.focusBuffer storage.lastOpenedPath if storage.lastOpenedPath
# Remember what buffers were open and closed
Event.on "editor:bufferFocus", (e) =>
@@ -88,12 +85,6 @@ windowAdditions =
KeyBinder.load "#{@appRoot}/static/key-bindings.coffee"
KeyBinder.load "~/.atomicity/key-bindings.coffee"
recentPath: ->
localStorage.lastOpenedPath
setRecentPath: (path) ->
localStorage.lastOpenedPath = path
showConsole: ->
atomController.webView.inspector.showConsole true
@@ -102,7 +93,7 @@ windowAdditions =
reload: ->
atomController.close
Native.newWindow atomController.path
OSX.NSApp.createController atomController.path
open: (path) ->
atomController.window.makeKeyAndOrderFront atomController
@@ -126,7 +117,7 @@ windowAdditions =
child = path.replace(/([^\/])$/, "$1/")
# If the child is contained by the parent, it can be opened by this window
return child.match "^" + parent
child.match "^" + parent
for key, value of windowAdditions
console.warn "DOMWindow already has a key named `#{key}`" if window[key]