Skip to content
Permalink
Browse files

feat: add mac support to systemPrefs.getColor()

  • Loading branch information...
codebytere committed Jan 3, 2019
1 parent 439ba51 commit 552c7be00f3cc716ea9a2a56427e58028ab036f5
@@ -56,11 +56,12 @@ void SystemPreferences::BuildPrototype(
prototype->SetClassName(mate::StringToV8(isolate, "SystemPreferences"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
#if defined(OS_WIN) || defined(OS_MACOSX)
.SetMethod("getColor", &SystemPreferences::GetColor)
.SetMethod("getAccentColor", &SystemPreferences::GetAccentColor)
#endif

#if defined(OS_WIN)
.SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled)
.SetMethod("getColor", &SystemPreferences::GetColor)
#elif defined(OS_MACOSX)
.SetMethod("postNotification", &SystemPreferences::PostNotification)
.SetMethod("subscribeNotification",
@@ -91,6 +92,7 @@ void SystemPreferences::BuildPrototype(
&SystemPreferences::GetAppLevelAppearance)
.SetMethod("setAppLevelAppearance",
&SystemPreferences::SetAppLevelAppearance)
.SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
.SetMethod("isTrustedAccessibilityClient",
&SystemPreferences::IsTrustedAccessibilityClient)
.SetMethod("getMediaAccessStatus",
@@ -51,13 +51,12 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>

#if defined(OS_WIN) || defined(OS_MACOSX)
std::string GetAccentColor();
std::string GetColor(const std::string& color, mate::Arguments* args);
#endif

#if defined(OS_WIN)
bool IsAeroGlassEnabled();

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

void InitializeWindow();

// gfx::SysColorChangeListener:
@@ -95,6 +94,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
@@ -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,
@@ -395,11 +402,136 @@ 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);
}

std::string SystemPreferences::GetColor(const std::string& color,
mate::Arguments* args) {
NSColor* sysColor = nil;
if (color == "alternate-selected-control-text") {
sysColor = [NSColor alternateSelectedControlTextColor];
} else if (color == "control-background") {
sysColor = [NSColor controlBackgroundColor];
} else if (color == "control") {
sysColor = [NSColor controlColor];
} else if (color == "control-text") {
sysColor = [NSColor controlTextColor];
} else if (color == "disabled-control") {
sysColor = [NSColor disabledControlTextColor];
} else if (color == "find-highlight") {
if (@available(macOS 10.14, *))
sysColor = [NSColor findHighlightColor];
} else if (color == "grid") {
sysColor = [NSColor gridColor];
} else if (color == "header-text") {
sysColor = [NSColor headerTextColor];
} else if (color == "highlight") {
sysColor = [NSColor highlightColor];
} else if (color == "keyboard-focus-indicator") {
sysColor = [NSColor keyboardFocusIndicatorColor];
} else if (color == "label") {
if (@available(macOS 10.10, *))
sysColor = [NSColor labelColor];
} else if (color == "link") {
if (@available(macOS 10.10, *))
sysColor = [NSColor linkColor];
} else if (color == "placeholder-text") {
if (@available(macOS 10.10, *))
sysColor = [NSColor placeholderTextColor];
} else if (color == "quaternary-label") {
if (@available(macOS 10.10, *))
sysColor = [NSColor quaternaryLabelColor];
} else if (color == "scrubber-textured-background") {
if (@available(macOS 10.12.2, *))
sysColor = [NSColor scrubberTexturedBackgroundColor];
} else if (color == "secondary-label") {
if (@available(macOS 10.10, *))
sysColor = [NSColor secondaryLabelColor];
} else if (color == "selected-content-background") {
if (@available(macOS 10.14, *))
sysColor = [NSColor selectedContentBackgroundColor];
} else if (color == "selected-control") {
sysColor = [NSColor selectedControlColor];
} else if (color == "selected-control-text") {
sysColor = [NSColor selectedControlTextColor];
} else if (color == "selected-menu-item-text") {
sysColor = [NSColor selectedMenuItemTextColor];
} else if (color == "selected-text-background") {
sysColor = [NSColor selectedTextBackgroundColor];
} else if (color == "selected-text") {
sysColor = [NSColor selectedTextColor];
} else if (color == "separator") {
if (@available(macOS 10.14, *))
sysColor = [NSColor separatorColor];
} else if (color == "shadow") {
sysColor = [NSColor shadowColor];
} else if (color == "tertiary-label") {
if (@available(macOS 10.10, *))
sysColor = [NSColor tertiaryLabelColor];
} else if (color == "text-background") {
sysColor = [NSColor textBackgroundColor];
} else if (color == "text") {
sysColor = [NSColor textColor];
} else if (color == "under-page-background") {
sysColor = [NSColor underPageBackgroundColor];
} else if (color == "unemphasized-selected-content-background") {
if (@available(macOS 10.14, *))
sysColor = [NSColor unemphasizedSelectedContentBackgroundColor];
} else if (color == "unemphasized-selected-text-background") {
if (@available(macOS 10.14, *))
sysColor = [NSColor unemphasizedSelectedTextBackgroundColor];
} else if (color == "unemphasized-selected-text") {
if (@available(macOS 10.14, *))
sysColor = [NSColor unemphasizedSelectedTextColor];
} else if (color == "window-background") {
sysColor = [NSColor windowBackgroundColor];
} else if (color == "window-frame-text") {
sysColor = [NSColor windowFrameTextColor];
} else {
args->ThrowError("Unknown color: " + color);
return "";
}

return ToRGBHex(sysColor);
}

std::string SystemPreferences::GetMediaAccessStatus(
const std::string& media_type,
mate::Arguments* args) {
@@ -40,6 +40,46 @@ 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));

// misc dynamic colors declarations
@property(class, strong, readonly)
NSColor* linkColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* placeholderTextColor API_AVAILABLE(macosx(10.10));
@property(class, strong, readonly)
NSColor* findHighlightColor API_AVAILABLE(macosx(10.13));
@property(class, strong, readonly)
NSColor* separatorColor API_AVAILABLE(macosx(10.14));
@property(class, strong, readonly)
NSColor* selectedContentBackgroundColor API_AVAILABLE(macosx(10.14));
@property(class, strong, readonly)
NSColor* unemphasizedSelectedContentBackgroundColor API_AVAILABLE(
macosx(10.14));
@property(class, strong, readonly)
NSColor* unemphasizedSelectedTextBackgroundColor API_AVAILABLE(macosx(10.14)
);
@property(class, strong, readonly)
NSColor* unemphasizedSelectedTextColor API_AVAILABLE(macosx(10.14));
@end

extern "C" {

0 comments on commit 552c7be

Please sign in to comment.
You can’t perform that action at this time.