From 64eb13672d69dbb1d30a11b13e260f8707e3b87c Mon Sep 17 00:00:00 2001 From: Michel Fortin Date: Sun, 26 Aug 2018 11:51:10 -0400 Subject: [PATCH] Showing release notes with dark appearance according to the system theme. This is done by applying a user stylesheet, making the background of the WebView transparent, and using a NSBox to display the dynamic dark background color. The appearance of the release notes will unfortunately not adapt if the theme is changed while the release notes window is open. --- Resources/DarkAqua.css | 10 +++++++ Sparkle.xcodeproj/project.pbxproj | 8 ++++-- Sparkle/SUUpdateAlert.m | 46 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 Resources/DarkAqua.css diff --git a/Resources/DarkAqua.css b/Resources/DarkAqua.css new file mode 100644 index 00000000..9be7f7a9 --- /dev/null +++ b/Resources/DarkAqua.css @@ -0,0 +1,10 @@ +html { + color: white; + background: transparent; +} +:link { + color: #419CFF; +} +:link:active { + color: #FF1919; +} diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 56e5d053..11f340d7 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -43,6 +43,7 @@ 14950073195FCE4E00BC5B5B /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; 14958C6E19AEBC950061B14F /* signed-test-file.txt in Resources */ = {isa = PBXBuildFile; fileRef = 14958C6B19AEBC530061B14F /* signed-test-file.txt */; }; 14958C6F19AEBC980061B14F /* test-pubkey.pem in Resources */ = {isa = PBXBuildFile; fileRef = 14958C6C19AEBC610061B14F /* test-pubkey.pem */; }; + 1EAA4C8B2132C7BF00604473 /* DarkAqua.css in Resources */ = {isa = PBXBuildFile; fileRef = 1EAA4C8A2132C7BF00604473 /* DarkAqua.css */; }; 3772FEA913DE0B6B00F79537 /* SUVersionDisplayProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 3772FEA813DE0B6B00F79537 /* SUVersionDisplayProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 55C14BEF136EF21700649790 /* SUStatus.xib in Resources */ = {isa = PBXBuildFile; fileRef = 55C14BD8136EF00C00649790 /* SUStatus.xib */; }; 55C14C04136EF26100649790 /* SUUpdateAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = 55C14BF0136EF26100649790 /* SUUpdateAlert.xib */; }; @@ -810,6 +811,7 @@ 1A985A511C5C32BB0001163A /* uk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = uk; path = uk.lproj/InfoPlist.strings; sourceTree = ""; }; 1A985A521C5C32BC0001163A /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/InfoPlist.strings; sourceTree = ""; }; 1A985A531C5C32BD0001163A /* el */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = el; path = el.lproj/InfoPlist.strings; sourceTree = ""; }; + 1EAA4C8A2132C7BF00604473 /* DarkAqua.css */ = {isa = PBXFileReference; lastKnownFileType = text.css; path = DarkAqua.css; sourceTree = ""; }; 3772FEA813DE0B6B00F79537 /* SUVersionDisplayProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUVersionDisplayProtocol.h; sourceTree = ""; }; 4607BEA21948443800EF8DA4 /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/Sparkle.strings; sourceTree = ""; }; 4607BEA41948443800EF8DA4 /* nb */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = nb; path = nb.lproj/SUUpdateAlert.xib; sourceTree = ""; }; @@ -1489,6 +1491,7 @@ 14732BBC1960EFB500593899 /* README.markdown */, 14732BB91960EEEE00593899 /* SampleAppcast.xml */, 615AE3CF0D64DC40001CA7BD /* SUModelTranslation.plist */, + 1EAA4C8A2132C7BF00604473 /* DarkAqua.css */, ); path = Resources; sourceTree = ""; @@ -2524,13 +2527,13 @@ }; 726E07AC1CAF08D6001A286B = { CreatedOnToolsVersion = 7.3; - }; + }; 726E07EE1CAF37BD001A286B = { CreatedOnToolsVersion = 7.3; SystemCapabilities = { com.apple.Sandbox = { enabled = 1; - }; + }; }; }; 726E4A151C86C88F00C57C6A = { @@ -2732,6 +2735,7 @@ buildActionMask = 2147483647; files = ( 61AAE8280A321A7F00D8810D /* Sparkle.strings in Resources */, + 1EAA4C8B2132C7BF00604473 /* DarkAqua.css in Resources */, 615AE3D00D64DC40001CA7BD /* SUModelTranslation.plist in Resources */, 55C14BEF136EF21700649790 /* SUStatus.xib in Resources */, 55C14C04136EF26100649790 /* SUUpdateAlert.xib in Resources */, diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index d3320478..057379fa 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -58,6 +58,8 @@ static NSString *const SUUpdateAlertTouchBarIndentifier = @"" SPARKLE_BUNDLE_IDE @property (weak) IBOutlet NSButton *skipButton; @property (weak) IBOutlet NSButton *laterButton; +@property (strong) NSBox *darkBackgroundView; + @end @implementation SUUpdateAlert @@ -83,6 +85,8 @@ static NSString *const SUUpdateAlertTouchBarIndentifier = @"" SPARKLE_BUNDLE_IDE @synthesize skipButton; @synthesize laterButton; +@synthesize darkBackgroundView = _darkBackgroundView; + - (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost versionDisplayer:(id )aVersionDisplayer { self = [super initWithWindowNibName:@"SUUpdateAlert"]; @@ -214,6 +218,7 @@ static NSString *const SUUpdateAlertTouchBarIndentifier = @"" SPARKLE_BUNDLE_IDE // "-apple-system-font" is a reference to the system UI font. "-apple-system" is the new recommended token, but for backward compatibility we can't use it. prefs.standardFontFamily = @"-apple-system-font"; prefs.defaultFontSize = (int)[NSFont systemFontSize]; + [self adaptReleaseNotesAppearance]; // Stick a nice big spinner in the middle of the web view until the page is loaded. NSRect frame = [[self.releaseNotesView superview] frame]; @@ -231,6 +236,47 @@ static NSString *const SUUpdateAlertTouchBarIndentifier = @"" SPARKLE_BUNDLE_IDE } } +- (void)adaptReleaseNotesAppearance +{ +#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400 + if (@available(macOS 10.14, *)) + { + WebPreferences *prefs = [self.releaseNotesView preferences]; + if ([self.releaseNotesView effectiveAppearance].name == NSAppearanceNameDarkAqua || [self.releaseNotesView effectiveAppearance].name == NSAppearanceNameAccessibilityHighContrastDarkAqua) + { + // Set user stylesheet adapted to light on dark + prefs.userStyleSheetEnabled = YES; + prefs.userStyleSheetLocation = [[NSBundle bundleForClass:[self class]] URLForResource:@"DarkAqua" withExtension:@"css"]; + + // Remove web view background... + self.releaseNotesView.drawsBackground = NO; + // ... and use NSBox to get the dynamically colored background + if (self.darkBackgroundView == nil) + { + self.darkBackgroundView = [[NSBox alloc] initWithFrame:self.releaseNotesView.frame]; + self.darkBackgroundView.boxType = NSBoxCustom; + self.darkBackgroundView.fillColor = [NSColor textBackgroundColor]; + self.darkBackgroundView.borderColor = [NSColor clearColor]; + // Using auto-resizing mask instead of contraints works well enough + self.darkBackgroundView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; + [self.releaseNotesView.superview addSubview:self.darkBackgroundView positioned:NSWindowBelow relativeTo:self.releaseNotesView]; + + // The release note user stylesheet will not adjust to the user changing the theme until adaptReleaseNoteAppearance is called again. + // So lock the appearance of the background to keep the text readable if the system theme changes. + self.darkBackgroundView.appearance = self.darkBackgroundView.effectiveAppearance; + } + } + else + { + // Restore standard dark on light appearance + [self.darkBackgroundView removeFromSuperview]; + prefs.userStyleSheetEnabled = NO; + self.releaseNotesView.drawsBackground = YES; + } + } +#endif +} + - (void)showUpdateReleaseNotesWithDownloadData:(SPUDownloadData *)downloadData { if (!self.webViewFinishedLoading) {