feat: expose systemPreferences.getSystemColor() (#16248)
This commit is contained in:
		
					parent
					
						
							
								439ba5116f
							
						
					
				
			
			
				commit
				
					
						03892153de
					
				
			
		
					 5 changed files with 81 additions and 1 deletions
				
			
		|  | @ -91,6 +91,7 @@ void SystemPreferences::BuildPrototype( | ||||||
|                  &SystemPreferences::GetAppLevelAppearance) |                  &SystemPreferences::GetAppLevelAppearance) | ||||||
|       .SetMethod("setAppLevelAppearance", |       .SetMethod("setAppLevelAppearance", | ||||||
|                  &SystemPreferences::SetAppLevelAppearance) |                  &SystemPreferences::SetAppLevelAppearance) | ||||||
|  |       .SetMethod("getSystemColor", &SystemPreferences::GetSystemColor) | ||||||
|       .SetMethod("isTrustedAccessibilityClient", |       .SetMethod("isTrustedAccessibilityClient", | ||||||
|                  &SystemPreferences::IsTrustedAccessibilityClient) |                  &SystemPreferences::IsTrustedAccessibilityClient) | ||||||
|       .SetMethod("getMediaAccessStatus", |       .SetMethod("getMediaAccessStatus", | ||||||
|  |  | ||||||
|  | @ -95,6 +95,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> | ||||||
|   void RemoveUserDefault(const std::string& name); |   void RemoveUserDefault(const std::string& name); | ||||||
|   bool IsSwipeTrackingFromScrollEventsEnabled(); |   bool IsSwipeTrackingFromScrollEventsEnabled(); | ||||||
| 
 | 
 | ||||||
|  |   std::string GetSystemColor(const std::string& color, mate::Arguments* args); | ||||||
|  | 
 | ||||||
|   bool IsTrustedAccessibilityClient(bool prompt); |   bool IsTrustedAccessibilityClient(bool prompt); | ||||||
| 
 | 
 | ||||||
|   // TODO(codebytere): Write tests for these methods once we
 |   // TODO(codebytere): Write tests for these methods once we
 | ||||||
|  |  | ||||||
|  | @ -105,7 +105,7 @@ std::string ConvertAuthorizationStatus(AVAuthorizationStatusMac status) { | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Convert color to RGBA value like "#ABCDEF" | // Convert color to RGBA value like "aabbccdd" | ||||||
| std::string ToRGBA(NSColor* color) { | std::string ToRGBA(NSColor* color) { | ||||||
|   return base::StringPrintf( |   return base::StringPrintf( | ||||||
|       "%02X%02X%02X%02X", (int)(color.redComponent * 0xFF), |       "%02X%02X%02X%02X", (int)(color.redComponent * 0xFF), | ||||||
|  | @ -113,6 +113,13 @@ std::string ToRGBA(NSColor* color) { | ||||||
|       (int)(color.alphaComponent * 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 | }  // namespace | ||||||
| 
 | 
 | ||||||
| void SystemPreferences::PostNotification(const std::string& name, | void SystemPreferences::PostNotification(const std::string& name, | ||||||
|  | @ -395,6 +402,41 @@ std::string SystemPreferences::GetAccentColor() { | ||||||
|   return ToRGBA(sysColor); |   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) { | bool SystemPreferences::IsTrustedAccessibilityClient(bool prompt) { | ||||||
|   NSDictionary* options = @{(id)kAXTrustedCheckOptionPrompt : @(prompt)}; |   NSDictionary* options = @{(id)kAXTrustedCheckOptionPrompt : @(prompt)}; | ||||||
|   return AXIsProcessTrustedWithOptions((CFDictionaryRef)options); |   return AXIsProcessTrustedWithOptions((CFDictionaryRef)options); | ||||||
|  |  | ||||||
|  | @ -40,6 +40,26 @@ typedef NS_ENUM(NSInteger, AVAuthorizationStatusMac) { | ||||||
| @interface NSColor (MojaveSDK) | @interface NSColor (MojaveSDK) | ||||||
| @property(class, strong, readonly) | @property(class, strong, readonly) | ||||||
|     NSColor* controlAccentColor API_AVAILABLE(macosx(10.14)); |     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 | @end | ||||||
| 
 | 
 | ||||||
| extern "C" { | extern "C" { | ||||||
|  |  | ||||||
|  | @ -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 | [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_ | ### `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. | Returns `Boolean` - `true` if an inverted color scheme (a high contrast color scheme with light text and dark backgrounds) is active, `false` otherwise. | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Shelley Vohr
				Shelley Vohr