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
48 changes: 47 additions & 1 deletion MacDown/Code/Application/MPMainController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@


static NSString * const kMPTreatLastSeenStampKey = @"treatLastSeenStamp";
static NSString * const kMPViewMenuTitle = @"View";
static NSString * const kMPFollowSystemAppearanceMenuItemTitle =
@"Follow System Appearance";


NS_INLINE void MPOpenBundledFile(NSString *resource, NSString *extension)
Expand Down Expand Up @@ -97,7 +100,7 @@ NS_INLINE void treat()
}


@interface MPMainController ()
@interface MPMainController () <NSMenuItemValidation>
@property (readonly) NSWindowController *preferencesWindowController;
@end

Expand All @@ -112,6 +115,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification
setEventHandler:self
andSelector:@selector(openUrlSchemeAppleEvent:withReplyEvent:)
forEventClass:kInternetEventClass andEventID:kAEGetURL];
[self installAppearanceThemeMenuItem];
}

// Open a file from a browser with url of the form :
Expand Down Expand Up @@ -216,6 +220,26 @@ - (IBAction)openGitHub:(id)sender
[[NSWorkspace sharedWorkspace] openURL:url];
}

- (IBAction)toggleAppearanceThemesFollowSystem:(id)sender
{
MPPreferences *preferences = self.preferences;
preferences.appearanceThemesFollowSystem =
!preferences.appearanceThemesFollowSystem;
}


#pragma mark - NSMenuItemValidation

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if (menuItem.action == @selector(toggleAppearanceThemesFollowSystem:))
{
menuItem.state = self.preferences.appearanceThemesFollowSystem ?
NSControlStateValueOn : NSControlStateValueOff;
}
return YES;
}


#pragma mark - Override

Expand Down Expand Up @@ -307,6 +331,28 @@ - (void)copyFiles
}
}

- (void)installAppearanceThemeMenuItem
{
NSMenuItem *viewItem = [NSApp.mainMenu itemWithTitle:kMPViewMenuTitle];
NSMenu *viewMenu = viewItem.submenu;
if (!viewMenu)
return;

SEL action = @selector(toggleAppearanceThemesFollowSystem:);
for (NSMenuItem *item in viewMenu.itemArray)
{
if (item.action == action)
return;
}

[viewMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *item =
[[NSMenuItem alloc] initWithTitle:kMPFollowSystemAppearanceMenuItemTitle
action:action keyEquivalent:@""];
item.target = self;
[viewMenu addItem:item];
}

- (void)openPendingFiles
{
NSDocumentController *c = [NSDocumentController sharedDocumentController];
Expand Down
32 changes: 27 additions & 5 deletions MacDown/Code/Document/MPDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#import <JavaScriptCore/JavaScriptCore.h>

static NSString * const kMPDefaultAutosaveName = @"Untitled";
static NSString * const kMPAppleInterfaceThemeChangedNotification =
@"AppleInterfaceThemeChangedNotification";


NS_INLINE NSString *MPEditorPreferenceKeyWithValueKey(NSString *key)
Expand Down Expand Up @@ -76,6 +78,8 @@
@"editorHorizontalInset", @"editorVerticalInset",
@"editorWidthLimited", @"editorMaximumWidth", @"editorLineSpacing",
@"editorOnRight", @"editorStyleName", @"editorShowWordCount",
@"appearanceThemesFollowSystem", @"editorLightStyleName",
@"editorDarkStyleName",
@"editorScrollsPastEnd",
@"htmlMathJax", @"htmlMathJaxInlineDollar", nil
];
Expand Down Expand Up @@ -535,6 +539,9 @@ - (void)windowControllerDidLoadNib:(NSWindowController *)controller
[center addObserver:self selector:@selector(previewBoundsDidChange:)
name:NSViewBoundsDidChangeNotification
object:self.preview.enclosingScrollView.contentView];
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self selector:@selector(applicationAppearanceDidChange:)
name:kMPAppleInterfaceThemeChangedNotification object:nil];

self.needsToUnregister = YES;

Expand Down Expand Up @@ -654,6 +661,7 @@ - (void)close
}

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Expand Down Expand Up @@ -1323,7 +1331,7 @@ - (BOOL)rendererRendersTOC:(MPRenderer *)renderer

- (NSString *)rendererStyleName:(MPRenderer *)renderer
{
return self.preferences.htmlStyleName;
return self.preferences.effectiveHtmlStyleName;
}

- (BOOL)rendererDetectsFrontMatter:(MPRenderer *)renderer
Expand Down Expand Up @@ -1398,7 +1406,7 @@ - (void)renderer:(MPRenderer *)renderer didProduceHTMLOutput:(NSString *)html

// Check if CSS style or highlighting theme has changed.
// If either changed, we must do a full reload to update <head> with new CSS links.
NSString *newStyleName = self.preferences.htmlStyleName;
NSString *newStyleName = self.preferences.effectiveHtmlStyleName;
NSString *newHighlightingTheme = self.preferences.htmlHighlightingThemeName;
BOOL stylesChanged = !MPAreNilableStringsEqual(self.currentStyleName, newStyleName) ||
!MPAreNilableStringsEqual(self.currentHighlightingThemeName, newHighlightingTheme);
Expand Down Expand Up @@ -1689,6 +1697,15 @@ - (void)didRequestPreviewReload:(NSNotification *)notification
[self render:nil];
}

- (void)applicationAppearanceDidChange:(NSNotification *)notification
{
if (!self.preferences.appearanceThemesFollowSystem)
return;

[self setupEditor:nil];
[self.renderer renderIfPreferencesChanged];
}

- (void)previewBoundsDidChange:(NSNotification *)notification
{
// Issue #342: Only trigger reverse sync when the user is explicitly scrolling
Expand Down Expand Up @@ -1757,7 +1774,7 @@ - (IBAction)exportHtml:(id)sender

MPExportPanelAccessoryViewController *controller =
[[MPExportPanelAccessoryViewController alloc] init];
controller.stylesIncluded = (BOOL)self.preferences.htmlStyleName;
controller.stylesIncluded = (BOOL)self.preferences.effectiveHtmlStyleName;
controller.highlightingIncluded = self.preferences.htmlSyntaxHighlighting;
panel.accessoryView = controller.view;

Expand Down Expand Up @@ -2159,6 +2176,9 @@ - (void)setupEditor:(NSString *)changedKey

if (!changedKey || [changedKey isEqualToString:@"editorBaseFontInfo"]
|| [changedKey isEqualToString:@"editorStyleName"]
|| [changedKey isEqualToString:@"appearanceThemesFollowSystem"]
|| [changedKey isEqualToString:@"editorLightStyleName"]
|| [changedKey isEqualToString:@"editorDarkStyleName"]
|| [changedKey isEqualToString:@"editorLineSpacing"])
{
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
Expand Down Expand Up @@ -2192,15 +2212,17 @@ - (void)setupEditor:(NSString *)changedKey
self.highlighter.styles = nil;
[self.highlighter readClearTextStylesFromTextView];

NSString *themeName = [self.preferences.editorStyleName copy];
NSString *themeName = [self.preferences.effectiveEditorStyleName copy];
if (themeName.length)
{
NSString *path = MPThemePathForName(themeName);
NSString *themeString = MPReadFileOfPath(path);
[self.highlighter applyStylesFromStylesheet:themeString
withErrorHandler:
^(NSArray *errorMessages) {
self.preferences.editorStyleName = nil;
[self.preferences setEditorStyleName:nil
forDarkAppearance:
self.preferences.usesDarkSystemAppearance];
}];
}

Expand Down
15 changes: 11 additions & 4 deletions MacDown/Code/Preferences/MPEditorPreferencesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ - (void)loadThemes
[self.themeSelect addItemWithTitle:@""];
[self.themeSelect addItemsWithTitles:itemTitles];

NSString *title = [self.preferences.editorStyleName copy];
NSString *title = [self.preferences.effectiveEditorStyleName copy];
if (title.length)
[self.themeSelect selectItemWithTitle:title];

Expand Down Expand Up @@ -123,10 +123,17 @@ - (IBAction)changeTheme:(NSPopUpButton *)sender
NSString *title = sender.selectedItem.title;

// Special case: the first (empty) item. No stylesheets will be used.
if (!title.length)
self.preferences.editorStyleName = nil;
NSString *styleName = title.length ? title : nil;
if (self.preferences.appearanceThemesFollowSystem)
{
[self.preferences setEditorStyleName:styleName
forDarkAppearance:
self.preferences.usesDarkSystemAppearance];
}
else
self.preferences.editorStyleName = title;
{
self.preferences.editorStyleName = styleName;
}
}

- (IBAction)invokeStylesheetFunction:(NSSegmentedControl *)sender
Expand Down
15 changes: 11 additions & 4 deletions MacDown/Code/Preferences/MPHtmlPreferencesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,17 @@ - (IBAction)changeStylesheet:(NSPopUpButton *)sender
NSString *title = sender.selectedItem.title;

// Special case: the first (empty) item. No stylesheets will be used.
if (!title.length)
self.preferences.htmlStyleName = nil;
NSString *styleName = title.length ? title : nil;
if (self.preferences.appearanceThemesFollowSystem)
{
[self.preferences setHtmlStyleName:styleName
forDarkAppearance:
self.preferences.usesDarkSystemAppearance];
}
else
self.preferences.htmlStyleName = title;
{
self.preferences.htmlStyleName = styleName;
}
}

- (IBAction)changeHighlightingTheme:(NSPopUpButton *)sender
Expand Down Expand Up @@ -153,7 +160,7 @@ - (void)loadStylesheets
[self.stylesheetSelect addItemWithTitle:@""];
[self.stylesheetSelect addItemsWithTitles:itemTitles];

NSString *title = self.preferences.htmlStyleName;
NSString *title = self.preferences.effectiveHtmlStyleName;
if (title.length)
[self.stylesheetSelect selectItemWithTitle:title];

Expand Down
14 changes: 14 additions & 0 deletions MacDown/Code/Preferences/MPPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ extern NSString * const MPDidDetectFreshInstallationNotification;
@property (assign) BOOL editorSyncScrolling;
@property (assign) BOOL editorSmartHome;
@property (assign) NSString *editorStyleName;
@property (assign) BOOL appearanceThemesFollowSystem;
@property (assign) NSString *editorLightStyleName;
@property (assign) NSString *editorDarkStyleName;
@property (assign) CGFloat editorHorizontalInset;
@property (assign) CGFloat editorVerticalInset;
@property (assign) CGFloat editorLineSpacing;
Expand All @@ -61,6 +64,8 @@ extern NSString * const MPDidDetectFreshInstallationNotification;

@property (assign) NSString *htmlTemplateName;
@property (assign) NSString *htmlStyleName;
@property (assign) NSString *htmlLightStyleName;
@property (assign) NSString *htmlDarkStyleName;
@property (assign) BOOL htmlDetectFrontMatter;
@property (assign) BOOL htmlTaskList;
@property (assign) BOOL htmlHardWrap;
Expand All @@ -80,8 +85,17 @@ extern NSString * const MPDidDetectFreshInstallationNotification;
@property (readonly) CGFloat editorBaseFontSize;
@property (nonatomic, assign) NSFont *editorBaseFont;
@property (readonly) NSString *editorUnorderedListMarker;
@property (readonly) NSString *effectiveEditorStyleName;
@property (readonly) NSString *effectiveHtmlStyleName;

- (instancetype)init;
- (BOOL)usesDarkSystemAppearance;
- (NSString *)editorStyleNameForDarkAppearance:(BOOL)darkAppearance;
- (NSString *)htmlStyleNameForDarkAppearance:(BOOL)darkAppearance;
- (void)setEditorStyleName:(NSString *)styleName
forDarkAppearance:(BOOL)darkAppearance;
- (void)setHtmlStyleName:(NSString *)styleName
forDarkAppearance:(BOOL)darkAppearance;

// Convinience methods.
@property (nonatomic, assign) NSArray *filesToOpen;
Expand Down
Loading
Loading