Files
macdown/MacDown/Code/Preferences/MPHtmlPreferencesViewController.m
2018-01-13 00:20:57 +08:00

156 lines
4.1 KiB
Objective-C

//
// MPHtmlPreferencesViewController.m
// MacDown
//
// Created by Tzu-ping Chung on 8/06/2014.
// Copyright (c) 2014 Tzu-ping Chung . All rights reserved.
//
#import "MPHtmlPreferencesViewController.h"
#import "MPUtilities.h"
#import "MPPreferences.h"
NS_INLINE NSString *MPPrismDefaultThemeName()
{
return NSLocalizedString(@"(Default)", @"Prism theme title");
}
@interface MPHtmlPreferencesViewController ()
@property (weak) IBOutlet NSPopUpButton *stylesheetSelect;
@property (weak) IBOutlet NSSegmentedControl *stylesheetFunctions;
@property (weak) IBOutlet NSPopUpButton *highlightingThemeSelect;
@end
@implementation MPHtmlPreferencesViewController
#pragma mark - MASPreferencesViewController
- (NSString *)viewIdentifier
{
return @"HtmlPreferences";
}
- (NSImage *)toolbarItemImage
{
return [NSImage imageNamed:@"PreferencesRendering"];
}
- (NSString *)toolbarItemLabel
{
return NSLocalizedString(@"Rendering", @"Preference pane title.");
}
#pragma mark - Override
- (void)viewWillAppear
{
[self loadStylesheets];
[self loadHighlightingThemes];
}
#pragma mark - IBAction
- (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;
else
self.preferences.htmlStyleName = title;
}
- (IBAction)changeHighlightingTheme:(NSPopUpButton *)sender
{
NSString *title = sender.selectedItem.title;
if ([title isEqualToString:MPPrismDefaultThemeName()])
self.preferences.htmlHighlightingThemeName = @"";
else
self.preferences.htmlHighlightingThemeName = title;
}
- (IBAction)invokeStylesheetFunction:(NSSegmentedControl *)sender
{
switch (sender.selectedSegment)
{
case 0: // Reveal
{
NSString *dirPath = MPDataDirectory(kMPStylesDirectoryName);
NSURL *url = [NSURL fileURLWithPath:dirPath];
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[workspace activateFileViewerSelectingURLs:@[url]];
break;
}
case 1: // Reload
{
[self loadStylesheets];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:MPDidRequestPreviewRenderNotification
object:self];
break;
}
default:
break;
}
}
#pragma mark - Private
- (void)loadStylesheets
{
self.stylesheetSelect.enabled = NO;
[self.stylesheetSelect removeAllItems];
NSArray *itemTitles = MPListEntriesForDirectory(
kMPStylesDirectoryName,
MPFileNameHasExtensionProcessor(kMPStyleFileExtension)
);
[self.stylesheetSelect addItemWithTitle:@""];
[self.stylesheetSelect addItemsWithTitles:itemTitles];
NSString *title = self.preferences.htmlStyleName;
if (title.length)
[self.stylesheetSelect selectItemWithTitle:title];
self.stylesheetSelect.enabled = YES;
}
- (void)loadHighlightingThemes
{
self.highlightingThemeSelect.enabled = NO;
[self.highlightingThemeSelect removeAllItems];
NSBundle *bundle = [NSBundle mainBundle];
NSArray *urls = [bundle URLsForResourcesWithExtension:@"css"
subdirectory:@"Prism/themes"];
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:urls.count];
for (NSURL *url in urls)
{
NSString *name = url.lastPathComponent;
if (name.length <= 10)
continue;
name = [name substringWithRange:NSMakeRange(6, name.length - 10)];
[titles addObject:[name capitalizedString]];
}
[self.highlightingThemeSelect addItemWithTitle:MPPrismDefaultThemeName()];
[self.highlightingThemeSelect addItemsWithTitles:titles];
NSString *currentName = self.preferences.htmlHighlightingThemeName;
if (currentName.length)
[self.highlightingThemeSelect selectItemWithTitle:currentName];
if (self.preferences.htmlSyntaxHighlighting)
self.highlightingThemeSelect.enabled = YES;
}
@end