Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 68 additions & 12 deletions MacDown/Code/Application/MPMainController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@

static NSString * const kMPTreatLastSeenStampKey = @"treatLastSeenStamp";

typedef void (^MPOpenPendingFileCompletion)(NSDocument *document);


NS_INLINE NSWindow *MPWindowForDocument(NSDocument *document)
{
for (NSWindowController *controller in document.windowControllers)
{
if (controller.window)
return controller.window;
}
return document.windowForSheet;
}


NS_INLINE void MPOpenBundledFile(NSString *resource, NSString *extension)
{
Expand Down Expand Up @@ -309,24 +322,67 @@ - (void)copyFiles

- (void)openPendingFiles
{
NSArray *paths = [self.preferences.filesToOpen copy];
if (!paths.count)
return;

self.preferences.filesToOpen = nil;
[self.preferences synchronize];

NSDocumentController *c = [NSDocumentController sharedDocumentController];
[NSWindow setAllowsAutomaticWindowTabbing:YES];

for (NSString *path in self.preferences.filesToOpen)
{
NSURL *url = [NSURL fileURLWithPath:path];
if ([url checkResourceIsReachableAndReturnError:NULL])
{
[c openDocumentWithContentsOfURL:url display:YES
completionHandler:MPDocumentOpenCompletionEmpty];
}
else
[self openPendingFilePaths:paths index:0 tabbedToWindow:nil
documentController:c];
}

- (void)openPendingFilePaths:(NSArray *)paths index:(NSUInteger)index
tabbedToWindow:(NSWindow *)tabWindow
documentController:(NSDocumentController *)controller
{
if (index >= paths.count)
return;

[self openPendingFileAtPath:paths[index] documentController:controller
completion:^(NSDocument *document) {
NSWindow *window = MPWindowForDocument(document);
NSWindow *nextTabWindow = tabWindow ?: window;

if (tabWindow && window && window != tabWindow)
{
[c createNewEmptyDocumentForURL:url display:YES error:NULL];
tabWindow.tabbingMode = NSWindowTabbingModePreferred;
window.tabbingMode = NSWindowTabbingModePreferred;
[tabWindow addTabbedWindow:window ordered:NSWindowAbove];
[tabWindow makeKeyAndOrderFront:nil];
}

[self openPendingFilePaths:paths index:index + 1
tabbedToWindow:nextTabWindow
documentController:controller];
}];
}

- (void)openPendingFileAtPath:(NSString *)path
documentController:(NSDocumentController *)controller
completion:(MPOpenPendingFileCompletion)completion
{
NSURL *url = [NSURL fileURLWithPath:path];
if ([url checkResourceIsReachableAndReturnError:NULL])
{
[controller openDocumentWithContentsOfURL:url display:YES
completionHandler:^(
NSDocument *document, BOOL wasOpen, NSError *error) {
if (completion)
completion(document);
}];
return;
}

self.preferences.filesToOpen = nil;
[self.preferences synchronize];
NSDocument *document = [controller createNewEmptyDocumentForURL:url
display:YES
error:NULL];
if (completion)
completion(document);
}

- (void)openPendingPipedContent {
Expand Down
Loading