From 8cfbd75ae90ad85b4d4e28034226efbbd84ffd02 Mon Sep 17 00:00:00 2001 From: Cyandev Date: Thu, 31 Mar 2022 02:38:20 +0800 Subject: [PATCH] fix PlatformColor resolves to incorrect color in some appearances --- .../NSAppearance+Extension.swift | 27 +++++++++++++++++++ .../PlatformColor+Extension.swift | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Sources/CyanExtensions/NSAppearance+Extension.swift diff --git a/Sources/CyanExtensions/NSAppearance+Extension.swift b/Sources/CyanExtensions/NSAppearance+Extension.swift new file mode 100644 index 0000000..cd7ab08 --- /dev/null +++ b/Sources/CyanExtensions/NSAppearance+Extension.swift @@ -0,0 +1,27 @@ +// +// Created by Cyandev on 2022/3/31. +// Copyright (c) 2021 Cyandev. All rights reserved. +// + +#if canImport(AppKit) +import AppKit + +public enum UserInterfaceStyle { + case light + case dark +} + +public extension NSAppearance { + + var userInterfaceStyle: UserInterfaceStyle { + if name == Name.darkAqua || + name == Name.vibrantDark || + name == Name.accessibilityHighContrastDarkAqua || + name == Name.accessibilityHighContrastVibrantDark { + return .dark + } + return .light + } + +} +#endif diff --git a/Sources/CyanExtensions/PlatformColor+Extension.swift b/Sources/CyanExtensions/PlatformColor+Extension.swift index 7bf1ea5..54d1c29 100644 --- a/Sources/CyanExtensions/PlatformColor+Extension.swift +++ b/Sources/CyanExtensions/PlatformColor+Extension.swift @@ -44,7 +44,7 @@ public extension PlatformColor { convenience init(lightColor: PlatformColor, darkColor: PlatformColor) { self.init(name: nil, dynamicProvider: { appearance in - if appearance.name == .darkAqua { + if appearance.userInterfaceStyle == .dark { return darkColor } else { return lightColor