Skip to content
Closed
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/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
79 changes: 70 additions & 9 deletions MacDown/Code/Document/MPDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 @@ -247,6 +250,9 @@ typedef NS_ENUM(NSUInteger, MPWordCountType) {
// Store file content in initializer until nib is loaded.
@property (copy) NSString *loadedString;

// Transient per-document zoom level (not saved to preferences)
@property CGFloat zoomMultiplier;

- (void)scaleWebview;
- (void)syncScrollers;
- (void)syncScrollersReverse;
Expand Down Expand Up @@ -394,6 +400,7 @@ - (instancetype)init
self.shouldHandleBoundsChange = YES;
self.shouldHandlePreviewBoundsChange = YES;
self.previousSplitRatio = -1.0;
self.zoomMultiplier = 1.0;

return self;
}
Expand Down Expand Up @@ -774,7 +781,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 @@ -2100,16 +2121,19 @@ - (void)redrawDivider

- (void)scaleWebview
{
if (!self.preferences.previewZoomRelativeToBaseFontSize)
return;
CGFloat scale = self.zoomMultiplier;

CGFloat fontSize = self.preferences.editorBaseFontSize;
if (fontSize <= 0.0)
return;
if (self.preferences.previewZoomRelativeToBaseFontSize)
{
CGFloat fontSize = self.preferences.editorBaseFontSize;
if (fontSize > 0.0)
{
static const CGFloat defaultSize = 14.0;
scale = (fontSize / defaultSize)
* self.zoomMultiplier;
}
}

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 @@ -2124,6 +2148,43 @@ - (void)scaleWebview
#endif
}

- (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
{
NSFont *baseFont = self.preferences.editorBaseFont;
CGFloat zoomedSize = baseFont.pointSize * self.zoomMultiplier;

// Apply zoom to editor (transient, not saved to preferences)
[self.editor setFont:[NSFont fontWithName:baseFont.fontName
size:zoomedSize]];

// Apply zoom to preview
[self scaleWebview];
}

/**
* Updates cached positions of reference points (headers, standalone images) in both
* editor and preview for scroll synchronization.
Expand Down
16 changes: 16 additions & 0 deletions MacDown/Localization/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@
</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">
<connections>
<action selector="zoomOut:" target="-1" id="zOt-02-JKL"/>
</connections>
</menuItem>
<menuItem title="Actual Size" keyEquivalent="0" 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