import Cocoa
-struct ColorName {
+enum AppearanceMode {
- let name: String
+ case normal
- init(name: String) {
+ case highContrast
+
+ case dark
+
+ case highContrastDark
+}
+
+extension AppearanceMode {
+
+ static var current: AppearanceMode {
- self.name = name
+ if #available(macOS 10.14, *) {
+
+ return currentMode1014()
+ }
+
+ if NSWorkspace.shared.accessibilityDisplayShouldIncreaseContrast {
+
+ return .highContrast
+ }
+
+ return .normal
}
- init(_ name: String) {
+ @available(macOS 10.14, *)
+ private static func currentMode1014() -> AppearanceMode {
- self.name = name
+ switch NSAppearance.current.name {
+
+ case .aqua: return .normal
+
+ /// not available in macOS 10.13 SDK
+// case .accessibilityHighContrastAqua: return .highContrast
+//
+// case .darkAqua: return .dark
+//
+// case .accessibilityHighContrastDarkAqua: return .highContrastDark
+
+ default: return .normal
+ }
}
}
-extension ColorName: Equatable {
+struct ColorName: Equatable {
- static func == (lhs: ColorName, rhs: ColorName) -> Bool {
+ private let name: String
+
+ private init(_ name: String) {
- return lhs.name == rhs.name
+ self.name = name
}
}
static var current: ColorSet {
- if NSWorkspace.shared.accessibilityDisplayShouldIncreaseContrast {
+ switch AppearanceMode.current {
+
+ case .normal: return BaseColorSet.shared
+
+ case .highContrast: return HighContrastColorSet.shared
- return HighContrastColorSet.shared
+ case .dark: return DarkModeColorSet.shared
+
+ case .highContrastDark: return HighContrastDarkModeColorSet.shared
}
-
- return DefaultColorSet.shared
}
}
-private struct DefaultColorSet: ColorSet {
+private struct BaseColorSet: ColorSet {
- static let shared = DefaultColorSet()
+ static let shared = BaseColorSet()
subscript(named: ColorName) -> NSColor {
}
-private struct HighContrastColorSet: ColorSet {
+private protocol SpecialColorSet: ColorSet {
+ func color(named: ColorName) -> NSColor?
+}
+
+private extension SpecialColorSet {
+
+ subscript(named: ColorName) -> NSColor {
+
+ return color(named: named) ?? BaseColorSet.shared[named]
+ }
+}
+
+
+private struct HighContrastColorSet: SpecialColorSet {
static let shared = HighContrastColorSet()
- subscript(named: ColorName) -> NSColor {
+ func color(named: ColorName) -> NSColor? {
switch named {
case .damageViewBoarderBadly: return .red
- default: return DefaultColorSet.shared[named]
+ default: return nil
}
}
}
-private struct DarkModeColorSet: ColorSet {
+private struct DarkModeColorSet: SpecialColorSet {
- private static let shared = DarkModeColorSet()
+ static let shared = DarkModeColorSet()
- subscript(named: ColorName) -> NSColor {
+ func color(named: ColorName) -> NSColor? {
switch named {
- default: return DefaultColorSet.shared[named]
+ default: return nil
}
}
}
+
+private struct HighContrastDarkModeColorSet: SpecialColorSet {
+
+ static let shared = HighContrastDarkModeColorSet()
+
+ func color(named: ColorName) -> NSColor? {
+
+ return nil
+ }
+}