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

fix: crash on systemPreferences.getAccentColor() #18194

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
22 changes: 4 additions & 18 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Expand Up @@ -11,6 +11,7 @@

#include "atom/browser/mac/atom_application.h"
#include "atom/browser/mac/dict_util.h"
#include "atom/browser/ui/cocoa/NSColor+Hex.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "base/mac/sdk_forward_declarations.h"
Expand Down Expand Up @@ -106,21 +107,6 @@ AVMediaType ParseMediaType(const std::string& media_type) {
}
}

// 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 @@ -400,7 +386,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
if (@available(macOS 10.14, *))
sysColor = [NSColor controlAccentColor];

return ToRGBA(sysColor);
return base::SysNSStringToUTF8([sysColor RGBAValue]);
}

std::string SystemPreferences::GetSystemColor(const std::string& color,
Expand Down Expand Up @@ -429,7 +415,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return "";
}

return ToRGBHex(sysColor);
return base::SysNSStringToUTF8([sysColor hexadecimalValue]);
}

// static
Expand Down Expand Up @@ -519,7 +505,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
return "";
}

return ToRGBHex(sysColor);
return base::SysNSStringToUTF8([sysColor hexadecimalValue]);
}

std::string SystemPreferences::GetMediaAccessStatus(
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/ui/cocoa/NSColor+Hex.h
Expand Up @@ -10,6 +10,8 @@
#import <Cocoa/Cocoa.h>

@interface NSColor (Hex)
- (NSString*)hexadecimalValue;
- (NSString*)RGBAValue;
+ (NSColor*)colorWithHexColorString:(NSString*)hex;
@end

Expand Down
56 changes: 52 additions & 4 deletions atom/browser/ui/cocoa/NSColor+Hex.mm
Expand Up @@ -8,17 +8,65 @@

@implementation NSColor (Hex)

- (NSString*)RGBAValue {
double redFloatValue, greenFloatValue, blueFloatValue, alphaFloatValue;

NSColor* convertedColor =
[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

if (convertedColor) {
[convertedColor getRed:&redFloatValue
green:&greenFloatValue
blue:&blueFloatValue
alpha:&alphaFloatValue];

int redIntValue = redFloatValue * 255.99999f;
int greenIntValue = greenFloatValue * 255.99999f;
int blueIntValue = blueFloatValue * 255.99999f;
int alphaIntValue = alphaFloatValue * 255.99999f;

return
[NSString stringWithFormat:@"%02x%02x%02x%02x", redIntValue,
greenIntValue, blueIntValue, alphaIntValue];
}

return nil;
}

- (NSString*)hexadecimalValue {
double redFloatValue, greenFloatValue, blueFloatValue;

NSColor* convertedColor =
[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

if (convertedColor) {
[convertedColor getRed:&redFloatValue
green:&greenFloatValue
blue:&blueFloatValue
alpha:NULL];

int redIntValue = redFloatValue * 255.99999f;
int greenIntValue = greenFloatValue * 255.99999f;
int blueIntValue = blueFloatValue * 255.99999f;

return [NSString stringWithFormat:@"#%02x%02x%02x", redIntValue,
greenIntValue, blueIntValue];
}

return nil;
}

+ (NSColor*)colorWithHexColorString:(NSString*)inColorString {
unsigned colorCode = 0;
unsigned char redByte, greenByte, blueByte;

if (inColorString) {
NSScanner* scanner = [NSScanner scannerWithString:inColorString];
(void)[scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsigned char)(colorCode >> 16);
greenByte = (unsigned char)(colorCode >> 8);
blueByte = (unsigned char)(colorCode); // masks off high bits

unsigned char redByte = (unsigned char)(colorCode >> 16);
unsigned char greenByte = (unsigned char)(colorCode >> 8);
unsigned char blueByte = (unsigned char)(colorCode); // masks off high bits

return [NSColor colorWithCalibratedRed:(CGFloat)redByte / 0xff
green:(CGFloat)greenByte / 0xff
Expand Down