Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load a 2nd webview, and swap it out with the visible one when it's done rendering. #1216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions MacDown.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,10 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-MacDown/Pods-MacDown-frameworks.sh",
"${PODS_ROOT}/Sparkle/Sparkle.framework",
"${PODS_ROOT}/Sparkle/Sparkle.framework.dSYM",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Sparkle.framework",
"${DWARF_DSYM_FOLDER_PATH}/Sparkle.framework.dSYM",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down
70 changes: 61 additions & 9 deletions MacDown/Code/Document/MPDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ - (int)rendererFlags
@interface MPDocument ()
<NSSplitViewDelegate, NSTextViewDelegate,
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
WebEditingDelegate, WebFrameLoadDelegate, WebPolicyDelegate, WebResourceLoadDelegate,
WebEditingDelegate, WebFrameLoadDelegate, WebPolicyDelegate, WebResourceLoadDelegate, WebUIDelegate,
#endif
MPAutosaving, MPRendererDataSource, MPRendererDelegate>

Expand All @@ -189,6 +189,7 @@ typedef NS_ENUM(NSUInteger, MPWordCountType) {
@property (unsafe_unretained) IBOutlet MPEditorView *editor;
@property (weak) IBOutlet NSLayoutConstraint *editorPaddingBottom;
@property (weak) IBOutlet WebView *preview;
@property (weak) IBOutlet WebView *oldPreview;
@property (weak) IBOutlet NSPopUpButton *wordCountWidget;
@property (strong) IBOutlet MPToolbarController *toolbarController;
@property (copy, nonatomic) NSString *autosaveName;
Expand Down Expand Up @@ -225,7 +226,7 @@ -(void) updateHeaderLocations;

@end

static void (^MPGetPreviewLoadingCompletionHandler(MPDocument *doc))()
static void (^MPGetPreviewLoadingCompletionHandler(WebView *webview, MPDocument *doc))()
{
__weak MPDocument *weakObj = doc;
return ^{
Expand All @@ -248,6 +249,15 @@ static void (^MPGetPreviewLoadingCompletionHandler(MPDocument *doc))()
bounds.origin.y = weakObj.lastPreviewScrollTop;
contentView.bounds = bounds;
}

if (doc.oldPreview && webView == doc.preview) {
WebView *viewToRemove = doc.oldPreview;
doc.oldPreview = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[viewToRemove removeFromSuperview];
});
}
};
}

Expand Down Expand Up @@ -862,7 +872,7 @@ - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
if (self.preferences.htmlMathJax)
{
MPMathJaxListener *listener = [[MPMathJaxListener alloc] init];
[listener addCallback:MPGetPreviewLoadingCompletionHandler(self)
[listener addCallback:MPGetPreviewLoadingCompletionHandler(sender, self)
forKey:@"End"];
[sender.windowScriptObject setValue:listener forKey:@"MathJaxListener"];
}
Expand All @@ -874,7 +884,7 @@ - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
// JavaScript handler injected in -webView:didCommitLoadForFrame:.
if (!self.preferences.htmlMathJax)
{
id callback = MPGetPreviewLoadingCompletionHandler(self);
id callback = MPGetPreviewLoadingCompletionHandler(sender, self);
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[queue addOperationWithBlock:callback];
}
Expand All @@ -884,7 +894,7 @@ - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
// Update word count
if (self.preferences.editorShowWordCount)
[self updateWordCount];

self.alreadyRenderingInWeb = NO;

if (self.renderToWebPending)
Expand Down Expand Up @@ -1111,6 +1121,40 @@ - (void)renderer:(MPRenderer *)renderer didProduceHTMLOutput:(NSString *)html
}
#endif

if (!self.oldPreview) {
NSView *superView = self.preview.superview;
WebView *previousPreview = self.preview;
previousPreview = self.preview;
previousPreview.UIDelegate = nil;
previousPreview.frameLoadDelegate = nil;
previousPreview.policyDelegate = nil;
previousPreview.editingDelegate = nil;
previousPreview.resourceLoadDelegate = nil;

WebView *loadingView = [[WebView alloc] initWithFrame:self.oldPreview.frame];
loadingView.UIDelegate = self;
loadingView.frameLoadDelegate = self;
loadingView.policyDelegate = self;
loadingView.editingDelegate = self;
loadingView.resourceLoadDelegate = self;
loadingView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[superView addSubview:loadingView positioned:NSWindowBelow relativeTo:self.oldPreview];
[loadingView setFrame:[superView bounds]];

self.preview = loadingView;
[self.oldPreview removeFromSuperview];
self.oldPreview = previousPreview;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([previousPreview superview]) {
[previousPreview removeFromSuperview];
if (self.oldPreview == previousPreview) {
self.oldPreview = nil;
}
}
});
}

// Reload the page if there's not valid tree to work with.
[self.preview.mainFrame loadHTMLString:html baseURL:baseUrl];
self.currentBaseUrl = baseUrl;
Expand Down Expand Up @@ -1775,11 +1819,19 @@ -(void) updateHeaderLocations
}

- (void)syncScrollers
{
[self syncScrollersFor:self.preview];
if (self.oldPreview) {
[self syncScrollersFor:self.oldPreview];
}
}

- (void)syncScrollersFor:(WebView*)webView
{
CGFloat editorContentHeight = ceilf(NSHeight(self.editor.enclosingScrollView.documentView.bounds));
CGFloat editorVisibleHeight = ceilf(NSHeight(self.editor.enclosingScrollView.contentView.bounds));
CGFloat previewContentHeight = ceilf(NSHeight(self.preview.enclosingScrollView.documentView.bounds));
CGFloat previewVisibleHeight = ceilf(NSHeight(self.preview.enclosingScrollView.contentView.bounds));
CGFloat previewContentHeight = ceilf(NSHeight(webView.enclosingScrollView.documentView.bounds));
CGFloat previewVisibleHeight = ceilf(NSHeight(webView.enclosingScrollView.contentView.bounds));
NSInteger relativeHeaderIndex = -1; // -1 is start of document, before any other header
CGFloat currY = NSMinY(self.editor.enclosingScrollView.contentView.bounds);
CGFloat minY = 0;
Expand Down Expand Up @@ -1847,9 +1899,9 @@ - (void)syncScrollers

// Now we scroll percentScrolledBetweenHeaders percent between those two positions in the webview
CGFloat previewY = topHeaderY + (bottomHeaderY - topHeaderY) * percentScrolledBetweenHeaders;
NSRect contentBounds = self.preview.enclosingScrollView.contentView.bounds;
NSRect contentBounds = webView.enclosingScrollView.contentView.bounds;
contentBounds.origin.y = previewY;
self.preview.enclosingScrollView.contentView.bounds = contentBounds;
webView.enclosingScrollView.contentView.bounds = contentBounds;
}

- (void)setSplitViewDividerLocation:(CGFloat)ratio
Expand Down
Loading