Dark mode implemented

This commit is contained in:
James Magahern
2020-07-29 18:17:22 -07:00
parent 32cdcf71f7
commit 220fc6f070
7 changed files with 163 additions and 3 deletions

View File

@@ -13,18 +13,23 @@
#import <WebKit/_WKRemoteObjectInterface.h>
#import <WebKit/_WKRemoteObjectRegistry.h>
#import <WebKit/_WKProcessPoolConfiguration.h>
#import <WebKit/_WKUserStyleSheet.h>
#import <WebKit/WKProcessPoolPrivate.h>
#import <WebKit/WKWebViewPrivate.h>
#import <WebKit/WKWebViewConfigurationPrivate.h>
#import <WebKit/WKUserContentControllerPrivate.h>
@interface SBRProcessBundleBridge () <SBRWebProcessDelegate>
@end
@implementation SBRProcessBundleBridge {
WKWebView *_webView;
WKWebView *_webView;
WKWebViewConfiguration *_webViewConfiguration;
id<SBRWebProcessProxy> _webProcessProxy;
_WKUserStyleSheet *_darkModeStyleSheet;
}
- (WKWebView *)webView
@@ -58,6 +63,7 @@
[[webView _remoteObjectRegistry] registerExportedObject:self interface:delegateInterface];
_webView = webView;
_webViewConfiguration = configuration;
}
return _webView;
@@ -90,4 +96,23 @@
[_webProcessProxy setAllScriptsAllowed:allowAllScripts];
}
- (void)setDarkModeEnabled:(BOOL)darkModeEnabled
{
_darkModeEnabled = darkModeEnabled;
WKUserContentController *userContentController = [_webViewConfiguration userContentController];
if (darkModeEnabled) {
if (!_darkModeStyleSheet) {
NSURL *styleSheetURL = [[NSBundle mainBundle] URLForResource:@"darkmode" withExtension:@"css"];
NSString *styleSheetSource = [NSString stringWithContentsOfURL:styleSheetURL encoding:NSUTF8StringEncoding error:nil];
_darkModeStyleSheet = [[_WKUserStyleSheet alloc] initWithSource:styleSheetSource forMainFrameOnly:NO];
}
[userContentController _addUserStyleSheet:_darkModeStyleSheet];
} else if (_darkModeStyleSheet) {
[userContentController _removeUserStyleSheet:_darkModeStyleSheet];
}
}
@end