handle storage on the objective-c side.

This commit is contained in:
Corey Johnson
2011-11-04 17:43:15 -07:00
parent 9671e09be0
commit 750ede4f2c
8 changed files with 119 additions and 72 deletions

View File

@@ -9,4 +9,7 @@
- (AtomController *)createController:(NSString *)path;
- (void)removeController:(AtomController *)controller;
- (id)storageGet:(NSString *)keyPath defaultValue:(id)defaultValue;
- (id)storageSet:(NSString *)keyPath value:(id)value;
@end

View File

@@ -13,13 +13,11 @@
@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];
NSMutableArray *openedPaths = [self storageGet:@"app.openedPaths" defaultValue:[NSMutableArray array]];
if (![openedPaths containsObject:path]) {
[openedPaths addObject:path];
[self storageSet:@"app.openedPaths" value:openedPaths];
}
}
@@ -32,9 +30,9 @@
- (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];
NSMutableArray *openedPaths = [self storageGet:@"app.openedPaths" defaultValue:[NSMutableArray array]];
[openedPaths removeObject:controller.path];
[self storageSet:@"app.openedPaths" value:openedPaths];
}
- (void)open:(NSString *)path {
@@ -65,23 +63,23 @@
}
// Overridden
- (void)sendEvent:(NSEvent *)event {
if ([event type] == NSKeyDown) {
BOOL handeled = NO;
AtomController *controller = [[self keyWindow] windowController];
// The keyWindow could be a Cocoa Dialog or something, ignore that.
if ([controller isKindOfClass:[AtomController class]]) {
JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil];
handeled = [controller.jscocoa toBool:value];
}
if (!handeled) [super sendEvent:event];
}
else {
[super sendEvent:event];
}
}
//- (void)sendEvent:(NSEvent *)event {
// if ([event type] == NSKeyDown) {
// BOOL handeled = NO;
// AtomController *controller = [[self keyWindow] windowController];
//
// // The keyWindow could be a Cocoa Dialog or something, ignore that.
// if ([controller isKindOfClass:[AtomController class]]) {
// JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil];
// handeled = [controller.jscocoa toBool:value];
// }
//
// if (!handeled) [super sendEvent:event];
// }
// else {
// [super sendEvent:event];
// }
//}
// AppDelegate
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {
@@ -106,16 +104,56 @@
[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) {
NSArray *openedPaths = [self storageGet:@"app.openedPaths" defaultValue:[NSMutableArray array]];
if (openedPaths.count == 0) {
[self createController:NULL];
}
else {
for (NSString *path in storage) {
for (NSString *path in openedPaths) {
[self createController:path];
}
}
}
// Helper Methods that should probably go elsewhere
- (id)storage {
id storage = [NSMutableDictionary dictionaryWithContentsOfFile:ATOM_STORAGE_PATH];
if (!storage) storage = [NSMutableDictionary dictionary];
return storage;
}
- (id)storageGet:(NSString *)keyPath defaultValue:(id)defaultValue {
id storage = [NSMutableDictionary dictionaryWithContentsOfFile:ATOM_STORAGE_PATH];
if (!storage) storage = [NSMutableDictionary dictionary];
id value = [storage valueForKeyPath:keyPath];
if (!value) value = defaultValue;
return value;
}
- (id)storageSet:(NSString *)keyPath value:(id)value {
id storage = [NSMutableDictionary dictionaryWithContentsOfFile:ATOM_STORAGE_PATH];
if (!storage) storage = [NSMutableDictionary dictionary];
NSArray *keys = [keyPath componentsSeparatedByString:@"."];
id parent = storage;
for (int i = 0; i < keys.count - 1; i++) {
NSString *key = [keys objectAtIndex:i];
id newParent = [parent valueForKey:key];
if (!newParent) {
newParent = [NSMutableDictionary dictionary];
[parent setValue:newParent forKey:key];
}
parent = newParent;
}
[storage setValue:value forKeyPath:keyPath];
[storage writeToFile:ATOM_STORAGE_PATH atomically:YES];
return value;
}
@end

View File

@@ -22,8 +22,8 @@
}
- (id)initWithPath:(NSString *)aPath {
aPath = aPath ? aPath : @"/tmp";
aPath = aPath ? aPath : @"/tmp";
self = [super initWithWindowNibName:@"AtomWindow"];
path = [[aPath stringByStandardizingPath] retain];
@@ -33,6 +33,8 @@
- (void)windowDidLoad {
[super windowDidLoad];
[[webView inspector] showConsole:self];
[self.window setDelegate:self];
[webView setUIDelegate:self];
@@ -49,6 +51,7 @@
[[webView mainFrame] loadRequest:request];
}
// Helper methods that should go elsewhere
- (NSString *)tempfile {
char *directory = "/tmp";
char *prefix = "temp-file";

View File

@@ -1,4 +1,4 @@
//
//
// JSCocoa.m
// JSCocoa
//

View File

@@ -56,7 +56,6 @@ class TreePane extends Pane
paths = fs.list root
list = $('<ul>')
console.log @tree.shownDirs()
for path in paths
filename = path.replace(root, "").substring 1
type = if fs.isDirectory path then 'dir' else 'file'

View File

@@ -5,6 +5,7 @@ ace = require 'ace/ace'
Event = require 'event'
KeyBinder = require 'key-binder'
Native = require 'native'
Storage = require 'storage'
{EditSession} = require 'ace/edit_session'
{UndoManager} = require 'ace/undomanager'
@@ -15,6 +16,9 @@ class Editor
buffers: {}
openPathsKey: "editor.openPaths.#{atomController.path}"
focusedPathKey: "editor.focusedPath.#{atomController.path}"
constructor: (path) ->
KeyBinder.register "editor", @
@@ -59,6 +63,13 @@ class Editor
catch e
null
restoreOpenBuffers: ->
openPaths = Storage.get @openPathsKey, []
focusedPath = Storage.get(@focusedPathKey)
@addBuffer path for path in openPaths
@openPathsfocusBuffer focusedPath if focusedPath
addBuffer: (path) ->
throw "#{@constructor.name}: Cannot create buffer from a directory `#{path}`" if fs.isDirectory path
@@ -75,6 +86,12 @@ class Editor
@buffers[path] = buffer
openPaths = Storage.get @openPathsKey, []
unless path in openPaths
openPaths.push path
Storage.set @openPathsKey(), openPaths
buffer.on 'change', -> buffer.$atom_dirty = true
Event.trigger "editor:bufferAdd", path
@@ -107,6 +124,8 @@ class Editor
delete @buffers[path]
openPaths = Storage.get @openPathsKey, []
Storage.set @openPathsKey, _.without openPaths, path
Event.trigger "editor:bufferRemove", path
if path is @activePath
@@ -124,6 +143,7 @@ class Editor
buffer = @buffers[path] or @addBuffer path
@ace.setSession buffer
Storage.set @focusedPathKey, path
Event.trigger "editor:bufferFocus", path
save: (path) ->

View File

@@ -1,16 +1,26 @@
module.exports =
class Storage
@get: (key, defaultValue) ->
try
value = JSON.parse(localStorage[key] ? null) ? defaultValue
catch error
error.message += "\nGetting #{key}"
console.error(error)
value
try
value = OSX.NSApp.storageGet_defaultValue(key, defaultValue)
@toJS value
catch error
error.message += "\nGetting #{key}"
console.error(error)
@set: (key, value) ->
if value == undefined
delete localStorage[key]
OSX.NSApp.storageSet_value key, value
@toJS: (value) ->
if not value
value
else if value.isKindOfClass OSX.NSDictionary.class
dict = {}
dict[k.valueOf()] = @toJS v for k, v of value
dict
else if value.isKindOfClass OSX.NSArray.class
array = []
array.push @toJS v for v in value
array
else
localStorage[key] = JSON.stringify(value.valueOf())
value.valueOf()

View File

@@ -29,36 +29,10 @@ windowAdditions =
@loadExtensions()
@loadKeyBindings()
@restoreEditorState()
@editor.restoreOpenBuffers()
storageKey: ->
"project:" + atomController.path
restoreEditorState: ->
storage = Storage.get @storageKey(), {}
@editor.addBuffer path for path in storage.openPaths ? []
@editor.focusBuffer storage.lastOpenedPath if storage.lastOpenedPath
# Remember what buffers were open and closed
Event.on "editor:bufferFocus", (e) =>
path = e.details
storage = Storage.get @storageKey(), {}
storage.lastOpenedPath = path.valueOf()
Storage.set @storageKey(), storage
Event.on "editor:bufferAdd", (e) =>
path = e.details
storage = Storage.get @storageKey(), {}
storage.openPaths ?= []
unless path.valueOf() in storage.openPaths
storage.openPaths.push path.valueOf()
Storage.set @storageKey(), storage
Event.on "editor:bufferRemove", (e) =>
path = e.details
storage = Storage.get @storageKey(), {}
storage.openPaths = (p for p in storage.openPaths when p != path)
Storage.set @storageKey(), storage
"window:" + atomController.path
loadExtensions: ->
extension.shutdown() for extension in @extensions