Skip to content
Permalink
Browse files

Add support for Dark Mode in MySQL Help Viewer (10.14)

  • Loading branch information...
dmoagx committed Jul 15, 2018
1 parent b536850 commit cef386b604257ea3471ca908efcd558e9bbd4a5e
@@ -11,16 +11,14 @@
font-size:9pt;
}
h2 {}
ul {}
li {}
.internallink {
color: #6A81DD;
text-decoration: none;
}
.internallink:hover {
text-decoration: underline;
}
.description {
font-family: Monaco, monospace;
@@ -42,9 +40,23 @@
color: #ff415a;
font-family: monospace;
}
body.dark {
background-color: rgb(42, 38, 38);
color: white;
}
.dark .internallink {
color: rgb(65, 156, 255);
}
</style>
<script type="text/javascript">
window.onThemeChange = function(theme) {
document.body.className = (theme === 'dark') ? 'dark' : '';
};
</script>
</head>
<body>
<body class="{{bodyClass}}">
{{body}}
</body>
</html>
@@ -62,6 +62,16 @@

@end

// This is actually a @protocol
@interface NSWindow (Mavericks)

// actual return type is `NSAppearance *`
@property (readonly) id effectiveAppearance;

@end

#define NSAppearanceNameAqua @"NSAppearanceNameAqua"

#endif

#pragma mark - 10.10 Yosemite
@@ -157,3 +167,32 @@ typedef struct {
#endif

#endif

#pragma mark - 10.13 High Sierra

#ifndef __MAC_10_13
#define __MAC_10_13 101300
#endif

#if __MAC_OS_X_VERSION_MAX_ALLOWED < __MAC_10_13

#endif

#pragma mark - 10.14 Mojave

#ifndef __MAC_10_14
#define __MAC_10_14 101400
#endif

#if __MAC_OS_X_VERSION_MAX_ALLOWED < __MAC_10_14

// NSAppearance class is supported since 10.9, but this file has to go back to 10.8
@interface NSObject (NSAppearance_Mojave)

- (NSString *)bestMatchFromAppearancesWithNames:(NSArray *)appearances;

@end

#define NSAppearanceNameDarkAqua @"NSAppearanceNameDarkAqua"

#endif
@@ -35,6 +35,9 @@
#import "RegexKitLite.h"
#import "MGTemplateEngine.h"
#import "ICUTemplateMatcher.h"
#import "SPOSInfo.h"

static BOOL isOSAtLeast10_14 = NO;

@interface SPHelpViewerClient () <SPHelpViewerDataSource>

@@ -46,6 +49,11 @@ - (void)helpViewerClosed:(NSNotification *)notification;

@implementation SPHelpViewerClient

+ (void)initialize
{
isOSAtLeast10_14 = [SPOSInfo isOSVersionAtLeastMajor:10 minor:14 patch:0];
}

- (instancetype)init
{
if (self = [super init]) {
@@ -220,10 +228,23 @@ - (NSString *)HTMLHelpContentsForSearchString:(NSString *)searchString autoHelp:
[tableDetails release];

generate_help:
return [engine processTemplate:helpHTMLTemplate withVariables:@{
@"title": theTitle,
@"body": theHelp,
}];
{ // C syntax disallows a new variable directly following a label…
NSString *addBodyClass = @"";
// Add CSS class if running in dark UI mode (10.14+)
if (isOSAtLeast10_14) {
NSString *match = [[[controller window] effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
// aqua is already the default theme
if ([NSAppearanceNameDarkAqua isEqualToString:match]) {
addBodyClass = @"dark";
}
}

return [engine processTemplate:helpHTMLTemplate withVariables:@{
@"bodyClass": addBodyClass,
@"title": theTitle,
@"body": theHelp,
}];
}
}

+ (NSString *)linkToHelpTopic:(NSString *)aTopic
@@ -31,6 +31,7 @@

#import "SPHelpViewerController.h"

#import "SPOSInfo.h"
#import <WebKit/WebKit.h>

NSString * const SPHelpViewerSearchTOC = @"contents";
@@ -44,6 +45,7 @@ typedef NS_ENUM(NSInteger, HelpNavButton) {
};

static void *HelpViewerControllerKVOContext = &HelpViewerControllerKVOContext;
static BOOL isOSAtLeast10_14 = NO;

@interface SPHelpViewerController () <WebPolicyDelegate, WebUIDelegate, NSWindowDelegate>
- (IBAction)showHelpForSearchString:(id)sender;
@@ -58,6 +60,7 @@ - (IBAction)helpSelectHelpTargetWeb:(id)sender;
- (IBAction)showHelpForWebViewSelection:(id)sender;
- (IBAction)searchInDocForWebViewSelection:(id)sender;
- (void)helpTargetValidation;
- (void)themeChanged;
- (void)updateWindowTitle;
@end

@@ -67,6 +70,11 @@ @implementation SPHelpViewerController

@synthesize dataSource = dataSource;

+ (void)initialize
{
isOSAtLeast10_14 = [SPOSInfo isOSVersionAtLeastMajor:10 minor:14 patch:0];
}

- (instancetype)init
{
if ((self = [super initWithWindowNibName:@"HelpViewer"])) {
@@ -79,6 +87,9 @@ - (instancetype)init
- (void)dealloc
{
[helpWebView removeObserver:self forKeyPath:@"mainFrameTitle"]; //TODO: update to ...context: variant after 10.6
if(isOSAtLeast10_14) {
[[self window] removeObserver:self forKeyPath:@"effectiveAppearance" context:HelpViewerControllerKVOContext];
}
[super dealloc];
}

@@ -91,6 +102,9 @@ - (void)windowDidLoad
[self updateWindowTitle];

[helpWebView addObserver:self forKeyPath:@"mainFrameTitle" options:0 context:HelpViewerControllerKVOContext];
if(isOSAtLeast10_14) {
[[self window] addObserver:self forKeyPath:@"effectiveAppearance" options:0 context:HelpViewerControllerKVOContext];
}
}

- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context
@@ -99,12 +113,33 @@ - (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable i
if([@"mainFrameTitle" isEqualToString:keyPath] && object == helpWebView) {
[self updateWindowTitle];
}
else if([@"effectiveAppearance" isEqualToString:keyPath]) {
// Apple says to not do stuff here that could take some time or it may interrupt animations
[self performSelector:@selector(themeChanged) withObject:nil afterDelay:0.0];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void)themeChanged
{
if(!isOSAtLeast10_14) return;

NSString *match = [[[self window] effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
NSString *newTheme = @"unknown";
if([NSAppearanceNameAqua isEqualToString:match]) {
newTheme = @"light";
}
else if([NSAppearanceNameDarkAqua isEqualToString:match]) {
newTheme = @"dark";
}

NSString *eval = [NSString stringWithFormat:@"window.onThemeChange('%@')", newTheme];
[helpWebView stringByEvaluatingJavaScriptFromString:eval];
}

- (void)updateWindowTitle
{
NSString *title = NSLocalizedString(@"MySQL Help", @"mysql help");

0 comments on commit cef386b

Please sign in to comment.
You can’t perform that action at this time.