Skip to content

Commit

Permalink
feat: add mac support to systemPrefs.getAccentColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jan 3, 2019
1 parent 3f1d227 commit 5952d4b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion atom/browser/api/atom_api_system_preferences.cc
Expand Up @@ -55,8 +55,10 @@ void SystemPreferences::BuildPrototype(
v8::Local<v8::FunctionTemplate> 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)
Expand Down
5 changes: 4 additions & 1 deletion atom/browser/api/atom_api_system_preferences.h
Expand Up @@ -49,10 +49,13 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> 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();
Expand Down
19 changes: 19 additions & 0 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -104,6 +104,17 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

// Convert color to RGBA value like "#ABCDEF"
std::string ToRGBA(NSColor* color) {
NSString* rgbHex = [NSString
stringWithFormat:@"%02X%02X%02X%02X", (int)(color.redComponent * 0xFF),
(int)(color.greenComponent * 0xFF),
(int)(color.blueComponent * 0xFF),
(int)(color.alphaComponent * 0xFF)];

return std::string([rgbHex UTF8String]);
}

} // namespace

void SystemPreferences::PostNotification(const std::string& name,
Expand Down Expand Up @@ -378,6 +389,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);
Expand Down
2 changes: 1 addition & 1 deletion docs/api/system-preferences.md
Expand Up @@ -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.
Expand Down

0 comments on commit 5952d4b

Please sign in to comment.