Skip to content

Commit

Permalink
Note currently open notebook at application termination and reopen it…
Browse files Browse the repository at this point in the history
… at launch.
  • Loading branch information
liyanage committed Dec 7, 2013
1 parent 7093026 commit cd9a3e3
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions IPython Notebook/Notebook Controller/NotebookWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,46 @@ - (void)fatalErrorAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode

- (void)initializeWebView
{
NSURLRequest *request = [NSURLRequest requestWithURL:self.notebookURL];
NSURLRequest *request = [NSURLRequest requestWithURL:[self launchURL]];
[[self.webView mainFrame] loadRequest:request];
}


- (NSURL *)launchURL
{
NSString *launchNotebookIdentifier = [self launchNotebookIdentifier];
if (!launchNotebookIdentifier) {
return self.notebookURL;
}

NSURLComponents *components = [NSURLComponents componentsWithURL:self.notebookURL resolvingAgainstBaseURL:NO];
components.path = [NSString stringWithFormat:@"/%@", launchNotebookIdentifier];

return [components URL];
}


- (NSString *)launchNotebookIdentifier
{
NSString *lastOpenNotebookName = [[NSUserDefaults standardUserDefaults] stringForKey:@"CurrentNotebookName"];
if (!lastOpenNotebookName) {
return nil;
}

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"CurrentNotebookName"];

__block NSString *notebookIdentifier = nil;
[[self notebookInfo] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *notebookInfo, BOOL *stop) {
if ([notebookInfo[@"name"] isEqualToString:lastOpenNotebookName]) {
*stop = YES;
notebookIdentifier = key;
}
}];

return notebookIdentifier;
}


- (NSInteger)findAvailablePort
{

Expand Down Expand Up @@ -521,6 +556,10 @@ - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem

- (void)applicationWillTerminate:(NSNotification *)aNotification
{
NSString *name = [self currentNotebookName];
if (name) {
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"CurrentNotebookName"];
}
self.applicationState = ApplicationStateTerminating;
if ([self.task isRunning]) {
[self.task terminate];
Expand Down Expand Up @@ -846,7 +885,7 @@ - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
if (![self currentPageIsNotebook]) {
return;
}

// Disable some menu items that are undesirable in the context of an app or are redundant with the app's menus
[self evaluateWebScript:@"$('#new_notebook').parent().children('li').filter(function () {return $.inArray(this.id, ['copy_notebook', 'rename_notebook', 'save_checkpoint']) == -1}).hide();"];
}
Expand Down

0 comments on commit cd9a3e3

Please sign in to comment.