Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose systemPreferences.getSystemColor() #16248

Merged
merged 1 commit into from Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_system_preferences.cc
Expand Up @@ -91,6 +91,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 @@ -95,6 +95,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
44 changes: 43 additions & 1 deletion atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -105,14 +105,21 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

// Convert color to RGBA value like "#ABCDEF"
// Convert color to RGBA value like "aabbccdd"
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));
}

// Convert color to RGB hex value like "#ABCDEF"
std::string ToRGBHex(NSColor* color) {
return base::StringPrintf("#%02X%02X%02X", (int)(color.redComponent * 0xFF),
(int)(color.greenComponent * 0xFF),
(int)(color.blueComponent * 0xFF));
}

} // namespace

void SystemPreferences::PostNotification(const std::string& name,
Expand Down Expand Up @@ -395,6 +402,41 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return ToRGBA(sysColor);
}

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
20 changes: 20 additions & 0 deletions atom/browser/mac/atom_application.h
Expand Up @@ -40,6 +40,26 @@ typedef NS_ENUM(NSInteger, AVAuthorizationStatusMac) {
@interface NSColor (MojaveSDK)
@property(class, strong, readonly)
NSColor* controlAccentColor API_AVAILABLE(macosx(10.14));

// macOS system colors
@property(class, strong, readonly)
NSColor* systemBlueColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemBrownColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemGrayColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemGreenColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemOrangeColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemPinkColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemPurpleColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemRedColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* systemYellowColor API_AVAILABLE(macosx(10.10));
@end

extern "C" {
Expand Down
15 changes: 15 additions & 0 deletions docs/api/system-preferences.md
Expand Up @@ -291,6 +291,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