Skip to content
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
4 changes: 4 additions & 0 deletions MacDown 3000.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
0319BD8F4A64D9E406448C13 /* MPSyntaxHighlightingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C64395EB126274BB2E01E01 /* MPSyntaxHighlightingTests.m */; };
03224E1B02E36783D5307F4A /* MPDocumentIOTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 35FB5AC5D9A67BFB58A9430F /* MPDocumentIOTests.m */; };
1457815B1A710E4C40F07FCA /* MPPaneToggleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E44CF1BD72E49A99DA6A4C1 /* MPPaneToggleTests.m */; };
A1B2C3D4E5F67890ABCDEF12 /* MPZoomTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C3D4E5F67890ABCDEF1234 /* MPZoomTests.m */; };
197TESTS0B00000000197RSTB /* MPRendererStateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 197TESTS0F00000000197RSTF /* MPRendererStateTests.m */; };
1F002A23195B3DAE008B8D93 /* MPRenderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F002A22195B3DAE008B8D93 /* MPRenderer.m */; };
1F0D9D65194AC7CF008E1856 /* NSString+Lookup.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F0D9D5F194AC7CF008E1856 /* NSString+Lookup.m */; };
Expand Down Expand Up @@ -522,6 +523,7 @@
4DBAA63927A60EB2150642B3 /* Pods-macdown-cmd.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-macdown-cmd.debug.xcconfig"; path = "Pods/Target Support Files/Pods-macdown-cmd/Pods-macdown-cmd.debug.xcconfig"; sourceTree = "<group>"; };
5162FB5F85C6B4B09403BE65 /* MPHTMLResourceURLs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPHTMLResourceURLs.h; sourceTree = "<group>"; };
5E44CF1BD72E49A99DA6A4C1 /* MPPaneToggleTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; path = MPPaneToggleTests.m; sourceTree = "<group>"; };
B2C3D4E5F67890ABCDEF1234 /* MPZoomTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; path = MPZoomTests.m; sourceTree = "<group>"; };
5F23020AD4494E700E72C264 /* MPRendererTestHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPRendererTestHelpers.h; sourceTree = "<group>"; };
615588251EB35DEE6204B94E /* MPResourceWatcherSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPResourceWatcherSet.h; sourceTree = "<group>"; };
6811F39B13C649FC86C4CE5B /* MPMathJaxRenderingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPMathJaxRenderingTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -991,6 +993,7 @@
QLTST00002PREF00000M /* MPQuickLookPreferencesTests.m */,
QLTST00003PVCT00000M /* MPPreviewViewControllerTests.m */,
5E44CF1BD72E49A99DA6A4C1 /* MPPaneToggleTests.m */,
B2C3D4E5F67890ABCDEF1234 /* MPZoomTests.m */,
);
path = MacDownTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1468,6 +1471,7 @@
QLBLD00005PREFTESTSRC /* MPQuickLookPreferencesTests.m in Sources */,
QLBLD00006PVCTESTSRC0 /* MPPreviewViewControllerTests.m in Sources */,
1457815B1A710E4C40F07FCA /* MPPaneToggleTests.m in Sources */,
A1B2C3D4E5F67890ABCDEF12 /* MPZoomTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 4 additions & 0 deletions MacDown/Code/Document/MPDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
*/
+ (NSString *)toggleCheckboxAtIndex:(NSUInteger)index inMarkdown:(NSString *)markdown;

- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;
- (IBAction)resetZoom:(id)sender;

@end
143 changes: 107 additions & 36 deletions MacDown/Code/Document/MPDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

static NSString * const kMPDefaultAutosaveName = @"Untitled";

static const CGFloat kMPMinZoom = 0.5;
static const CGFloat kMPMaxZoom = 3.0;


NS_INLINE NSString *MPEditorPreferenceKeyWithValueKey(NSString *key)
{
Expand Down Expand Up @@ -254,6 +257,8 @@ typedef NS_ENUM(NSUInteger, MPScrollOwner) {
// Store file content in initializer until nib is loaded.
@property (copy) NSString *loadedString;

@property CGFloat zoomMultiplier;

- (void)scaleWebview;
- (void)syncScrollers;
- (void)syncScrollersReverse;
Expand Down Expand Up @@ -440,6 +445,7 @@ - (instancetype)init
_scrollOwner = MPScrollOwnerNeither;
self.previousSplitRatio = -1.0;
self.lastNonCollapsedRatio = -1.0;
self.zoomMultiplier = 1.0;

return self;
}
Expand Down Expand Up @@ -853,7 +859,21 @@ - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
{
BOOL result = [super validateUserInterfaceItem:item];
SEL action = item.action;
if (action == @selector(toggleToolbar:))

// Zoom menu validation
if (action == @selector(zoomIn:))
{
return self.zoomMultiplier < kMPMaxZoom;
}
else if (action == @selector(zoomOut:))
{
return self.zoomMultiplier > kMPMinZoom;
}
else if (action == @selector(resetZoom:))
{
return fabs(self.zoomMultiplier - 1.0) > 0.001;
}
else if (action == @selector(toggleToolbar:))
{
NSMenuItem *it = ((NSMenuItem *)item);
it.title = self.toolbarVisible ?
Expand Down Expand Up @@ -2152,32 +2172,7 @@ - (void)setupEditor:(NSString *)changedKey
|| [changedKey isEqualToString:@"editorStyleName"]
|| [changedKey isEqualToString:@"editorLineSpacing"])
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = self.preferences.editorLineSpacing;

// Configure tab stops to match 4-space tab width (fixes #195)
NSFont *font = [self.preferences.editorBaseFont copy];
if (font)
{
NSDictionary *attrs = @{NSFontAttributeName: font};
CGFloat spaceWidth = [@" " sizeWithAttributes:attrs].width;
CGFloat tabInterval = spaceWidth * 4;

NSMutableArray *tabStops = [NSMutableArray array];
for (NSInteger i = 1; i <= 100; i++)
{
NSTextTab *tab = [[NSTextTab alloc]
initWithTextAlignment:NSTextAlignmentLeft
location:tabInterval * i
options:@{}];
[tabStops addObject:tab];
}
style.tabStops = tabStops;
}

self.editor.defaultParagraphStyle = [style copy];
if (font)
self.editor.font = font;
[self applyEditorFontAndParagraphStyle];
self.editor.textColor = nil;
self.editor.backgroundColor = [NSColor clearColor];
self.highlighter.styles = nil;
Expand Down Expand Up @@ -2315,18 +2310,24 @@ - (void)redrawDivider
}
}

- (void)scaleWebview
- (CGFloat)previewScale
{
if (!self.preferences.previewZoomRelativeToBaseFontSize)
return;
if (self.preferences.previewZoomRelativeToBaseFontSize)
{
CGFloat fontSize = self.preferences.editorBaseFontSize;
if (fontSize > 0.0)
{
static const CGFloat defaultSize = 14.0;
return (fontSize / defaultSize) * self.zoomMultiplier;
}
}
return self.zoomMultiplier;
}

CGFloat fontSize = self.preferences.editorBaseFontSize;
if (fontSize <= 0.0)
return;
- (void)scaleWebview
{
CGFloat scale = [self previewScale];

static const CGFloat defaultSize = 14.0;
CGFloat scale = fontSize / defaultSize;

#if 0
// Sadly, this doesn’t work correctly.
// It looks fine, but selections are offset relative to the mouse cursor.
Expand All @@ -2341,6 +2342,76 @@ - (void)scaleWebview
#endif
}

- (NSFont *)zoomedEditorFont
{
NSFont *baseFont = self.preferences.editorBaseFont;
if (!baseFont)
return nil;
CGFloat zoomedSize = baseFont.pointSize * self.zoomMultiplier;
return [NSFont fontWithName:baseFont.fontName size:zoomedSize];
}

- (void)applyEditorFontAndParagraphStyle
{
NSFont *font = [[self zoomedEditorFont] copy];

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = self.preferences.editorLineSpacing;

// Configure tab stops to match 4-space tab width (fixes #195)
if (font)
{
NSDictionary *attrs = @{NSFontAttributeName: font};
CGFloat spaceWidth = [@" " sizeWithAttributes:attrs].width;
CGFloat tabInterval = spaceWidth * 4;

NSMutableArray *tabStops = [NSMutableArray array];
for (NSInteger i = 1; i <= 100; i++)
{
NSTextTab *tab = [[NSTextTab alloc]
initWithTextAlignment:NSTextAlignmentLeft
location:tabInterval * i
options:@{}];
[tabStops addObject:tab];
}
style.tabStops = tabStops;
}

self.editor.defaultParagraphStyle = [style copy];
if (font)
self.editor.font = font;
}

- (IBAction)zoomIn:(id)sender
{
if (self.zoomMultiplier >= kMPMaxZoom)
return;

self.zoomMultiplier = MIN(self.zoomMultiplier + 0.1, kMPMaxZoom);
[self applyCurrentZoom];
}

- (IBAction)zoomOut:(id)sender
{
if (self.zoomMultiplier <= kMPMinZoom)
return;

self.zoomMultiplier = MAX(self.zoomMultiplier - 0.1, kMPMinZoom);
[self applyCurrentZoom];
}

- (IBAction)resetZoom:(id)sender
{
self.zoomMultiplier = 1.0;
[self applyCurrentZoom];
}

- (void)applyCurrentZoom
{
[self applyEditorFontAndParagraphStyle];
[self scaleWebview];
}

/**
* Updates cached positions of reference points (headers, standalone images) in both
* editor and preview for scroll synchronization.
Expand Down
17 changes: 17 additions & 0 deletions MacDown/Localization/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,23 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="QMZ-Ld-Nza"/>
<menuItem title="Zoom In" keyEquivalent="+" id="zIn-01-ABC">
<connections>
<action selector="zoomIn:" target="-1" id="zIn-02-DEF"/>
</connections>
</menuItem>
<menuItem title="Zoom Out" keyEquivalent="-" id="zOt-01-GHI">
<modifierMask key="keyEquivalentModifierMask" shift="YES" command="YES"/>
<connections>
<action selector="zoomOut:" target="-1" id="zOt-02-JKL"/>
</connections>
</menuItem>
<menuItem title="Actual Size" id="zRs-01-PQR">
<connections>
<action selector="resetZoom:" target="-1" id="zRs-02-STU"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="zSp-01-MNO"/>
<menuItem title="Enter Full Screen" keyEquivalent="f" id="1gz-YB-DZE">
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
<connections>
Expand Down
Loading
Loading