From 8537ff8ac63ae79aedf969777487b341e1cb500a Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 3 Jan 2019 13:24:55 -0800 Subject: [PATCH] feat: add mac support to systemPrefs.getAccentColor() (#16251) * feat: add mac support to systemPrefs.getAccentColor() * note 10.14 retriction --- atom/browser/api/atom_api_system_preferences.cc | 4 +++- atom/browser/api/atom_api_system_preferences.h | 5 ++++- .../api/atom_api_system_preferences_mac.mm | 17 +++++++++++++++++ atom/browser/mac/atom_application.h | 5 +++++ docs/api/system-preferences.md | 4 +++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index c02ada9144170..2e8674523338d 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -55,8 +55,10 @@ void SystemPreferences::BuildPrototype( v8::Local prototype) { prototype->SetClassName(mate::StringToV8(isolate, "SystemPreferences")); mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) -#if defined(OS_WIN) +#if defined(OS_WIN) || defined(OS_MACOSX) .SetMethod("getAccentColor", &SystemPreferences::GetAccentColor) +#endif +#if defined(OS_WIN) .SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled) .SetMethod("getColor", &SystemPreferences::GetColor) #elif defined(OS_MACOSX) diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index 4ec4e2e682be6..5881c34353158 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -49,10 +49,13 @@ class SystemPreferences : public mate::EventEmitter static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); +#if defined(OS_WIN) || defined(OS_MACOSX) + std::string GetAccentColor(); +#endif + #if defined(OS_WIN) bool IsAeroGlassEnabled(); - std::string GetAccentColor(); std::string GetColor(const std::string& color, mate::Arguments* args); void InitializeWindow(); diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 505944fa1a25e..2e33cea903d1c 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -13,6 +13,7 @@ #include "atom/browser/mac/dict_util.h" #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/value_converter.h" +#include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" #include "base/values.h" #include "native_mate/object_template_builder.h" @@ -104,6 +105,14 @@ AVMediaType ParseMediaType(const std::string& media_type) { } } +// Convert color to RGBA value like "#ABCDEF" +std::string ToRGBA(NSColor* color) { + return base::StringPrintf( + "%02X%02X%02X%02X", (int)(color.redComponent * 0xFF), + (int)(color.greenComponent * 0xFF), (int)(color.blueComponent * 0xFF), + (int)(color.alphaComponent * 0xFF)); +} + } // namespace void SystemPreferences::PostNotification(const std::string& name, @@ -378,6 +387,14 @@ AVMediaType ParseMediaType(const std::string& media_type) { } } +std::string SystemPreferences::GetAccentColor() { + NSColor* sysColor = nil; + if (@available(macOS 10.14, *)) + sysColor = [NSColor controlAccentColor]; + + return ToRGBA(sysColor); +} + bool SystemPreferences::IsTrustedAccessibilityClient(bool prompt) { NSDictionary* options = @{(id)kAXTrustedCheckOptionPrompt : @(prompt)}; return AXIsProcessTrustedWithOptions((CFDictionaryRef)options); diff --git a/atom/browser/mac/atom_application.h b/atom/browser/mac/atom_application.h index 2f07676e3059d..c433f6ee8860d 100644 --- a/atom/browser/mac/atom_application.h +++ b/atom/browser/mac/atom_application.h @@ -37,6 +37,11 @@ typedef NS_ENUM(NSInteger, AVAuthorizationStatusMac) { (AVMediaType)mediaType API_AVAILABLE(macosx(10.14)); @end +@interface NSColor (MojaveSDK) +@property(class, strong, readonly) + NSColor* controlAccentColor API_AVAILABLE(macosx(10.14)); +@end + extern "C" { #if !defined(MAC_OS_X_VERSION_10_14) || \ MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_14 diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 5b70f0e4fe243..64d8aa25975e7 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -227,7 +227,7 @@ if (browserOptions.transparent) { [dwm-composition]:https://msdn.microsoft.com/en-us/library/windows/desktop/aa969540.aspx -### `systemPreferences.getAccentColor()` _Windows_ +### `systemPreferences.getAccentColor()` _Windows_ _macOS_ Returns `String` - The users current system wide accent color preference in RGBA hexadecimal form. @@ -240,6 +240,8 @@ const blue = color.substr(4, 2) // "cc" const alpha = color.substr(6, 2) // "dd" ``` +This API is only available on macOS 10.14 Mojave or newer. + ### `systemPreferences.getColor(color)` _Windows_ * `color` String - One of the following values: