Skip to content

Commit

Permalink
feat: expose systemPreferences.getSystemColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jan 3, 2019
1 parent baaeb7c commit 208ea1f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_system_preferences.cc
Expand Up @@ -89,6 +89,7 @@ void SystemPreferences::BuildPrototype(
&SystemPreferences::GetAppLevelAppearance)
.SetMethod("setAppLevelAppearance",
&SystemPreferences::SetAppLevelAppearance)
.SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
.SetMethod("isTrustedAccessibilityClient",
&SystemPreferences::IsTrustedAccessibilityClient)
.SetMethod("getMediaAccessStatus",
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/api/atom_api_system_preferences.h
Expand Up @@ -92,6 +92,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
void RemoveUserDefault(const std::string& name);
bool IsSwipeTrackingFromScrollEventsEnabled();

std::string GetSystemColor(const std::string& color, mate::Arguments* args);

bool IsTrustedAccessibilityClient(bool prompt);

// TODO(codebytere): Write tests for these methods once we
Expand Down
45 changes: 45 additions & 0 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -104,6 +104,16 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

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

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

} // namespace

void SystemPreferences::PostNotification(const std::string& name,
Expand Down Expand Up @@ -378,6 +388,41 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

std::string SystemPreferences::GetSystemColor(const std::string& color,
mate::Arguments* args) {
if (@available(macOS 10.10, *)) {
NSColor* sysColor;
if (color == "blue") {
sysColor = [NSColor systemBlueColor];
} else if (color == "brown") {
sysColor = [NSColor systemBrownColor];
} else if (color == "gray") {
sysColor = [NSColor systemGrayColor];
} else if (color == "green") {
sysColor = [NSColor systemGreenColor];
} else if (color == "orange") {
sysColor = [NSColor systemOrangeColor];
} else if (color == "pink") {
sysColor = [NSColor systemPinkColor];
} else if (color == "purple") {
sysColor = [NSColor systemPurpleColor];
} else if (color == "red") {
sysColor = [NSColor systemRedColor];
} else if (color == "yellow") {
sysColor = [NSColor systemYellowColor];
} else {
args->ThrowError("Unknown system color: " + color);
return "";
}

return ToRGBHex(sysColor);
} else {
args->ThrowError(
"This api is not available on MacOS version 10.9 or lower.");
return "";
}
}

bool SystemPreferences::IsTrustedAccessibilityClient(bool prompt) {
NSDictionary* options = @{(id)kAXTrustedCheckOptionPrompt : @(prompt)};
return AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
Expand Down
15 changes: 15 additions & 0 deletions docs/api/system-preferences.md
Expand Up @@ -289,6 +289,21 @@ See the [Windows docs][windows-colors] for more details.

[windows-colors]:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724371(v=vs.85).aspx

### `systemPreferences.getSystemColor(color)` _macOS_

* `color` String - One of the following values:
* `blue`
* `brown`
* `gray`
* `green`
* `orange`
* `pink`
* `purple`
* `red`
* `yellow`

Returns one of several standard system colors that automatically adapt to vibrancy and changes in accessibility settings like 'Increase contrast' and 'Reduce transparency'. See [Apple Documentation](https://developer.apple.com/design/human-interface-guidelines/macos/visual-design/color#system-colors) for more details.

### `systemPreferences.isInvertedColorScheme()` _Windows_

Returns `Boolean` - `true` if an inverted color scheme (a high contrast color scheme with light text and dark backgrounds) is active, `false` otherwise.
Expand Down

0 comments on commit 208ea1f

Please sign in to comment.