Skip to content

Commit 7fbeed3

Browse files
committed
View-based appearance detection.
1 parent b5908bd commit 7fbeed3

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

ColorSet/Interface/Base.lproj/ColorSet.PaletteViewController.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15705" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<objects>

ColorSetKit/NSColor+ColorSetKit.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ import Cocoa
6666
*/
6767
@objc( colorFromColorSet: )
6868
class func fromColorSet( name: String ) -> NSColor?
69+
{
70+
return NSColor.fromColorSet( name: name, forView: nil )
71+
}
72+
73+
@objc( colorFromColorSet:forView: )
74+
class func fromColorSet( name: String, forView view: NSView? ) -> NSColor?
6975
{
7076
if name.hasPrefix( "NS" )
7177
{
@@ -116,7 +122,7 @@ import Cocoa
116122

117123
if var l = lightness, abs( hsl.lightness - l ) > 0.001
118124
{
119-
if isDarkModeOn()
125+
if isDarkModeOn( forView: view )
120126
{
121127
var found = false
122128

@@ -147,7 +153,7 @@ import Cocoa
147153
return pair.color?.byChangingLightness( l )
148154
}
149155

150-
if isDarkModeOn(), let variant = pair.variant
156+
if isDarkModeOn( forView: view ), let variant = pair.variant
151157
{
152158
return variant
153159
}
@@ -542,11 +548,28 @@ import Cocoa
542548
return ( h, s, l )
543549
}
544550

545-
@nonobjc private class func isDarkModeOn() -> Bool
551+
@nonobjc private class func isDarkModeOn( forView view: NSView? ) -> Bool
546552
{
547553
if #available( macOS 10.14, * )
548554
{
549-
if NSApp != nil && NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua
555+
let name = view?.effectiveAppearance.name ?? NSApp.effectiveAppearance.name
556+
557+
if name == .darkAqua
558+
{
559+
return true
560+
}
561+
562+
if name == .vibrantDark
563+
{
564+
return true
565+
}
566+
567+
if name == .accessibilityHighContrastDarkAqua
568+
{
569+
return true
570+
}
571+
572+
if name == .accessibilityHighContrastVibrantDark
550573
{
551574
return true
552575
}

0 commit comments

Comments
 (0)