fix: macOS getColor inconsistencies (#20611)

This commit is contained in:
Shelley Vohr 2019-10-21 14:31:03 -07:00 committed by GitHub
parent 9a5cd4c8d2
commit aa26e8b946
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 12 deletions

View file

@ -291,7 +291,7 @@ This API is only available on macOS 10.14 Mojave or newer.
* `window-frame` - Window frame. * `window-frame` - Window frame.
* `window-text` - Text in windows. * `window-text` - Text in windows.
* On **macOS** * On **macOS**
* `alternate-selected-control-text` - The text on a selected surface in a list or table. * `alternate-selected-control-text` - The text on a selected surface in a list or table. _deprecated_
* `control-background` - The background of a large interface element, such as a browser or table. * `control-background` - The background of a large interface element, such as a browser or table.
* `control` - The surface of a control. * `control` - The surface of a control.
* `control-text` -The text of a control that isnt disabled. * `control-text` -The text of a control that isnt disabled.
@ -310,7 +310,7 @@ This API is only available on macOS 10.14 Mojave or newer.
* `selected-content-background` - The background for selected content in a key window or view. * `selected-content-background` - The background for selected content in a key window or view.
* `selected-control` - The surface of a selected control. * `selected-control` - The surface of a selected control.
* `selected-control-text` - The text of a selected control. * `selected-control-text` - The text of a selected control.
* `selected-menu-item` - The text of a selected menu. * `selected-menu-item-text` - The text of a selected menu.
* `selected-text-background` - The background of selected text. * `selected-text-background` - The background of selected text.
* `selected-text` - Selected text. * `selected-text` - Selected text.
* `separator` - A separator between different sections of content. * `separator` - A separator between different sections of content.
@ -328,6 +328,8 @@ This API is only available on macOS 10.14 Mojave or newer.
Returns `String` - The system color setting in RGB hexadecimal form (`#ABCDEF`). Returns `String` - The system color setting in RGB hexadecimal form (`#ABCDEF`).
See the [Windows docs][windows-colors] and the [MacOS docs][macos-colors] for more details. See the [Windows docs][windows-colors] and the [MacOS docs][macos-colors] for more details.
The following colors are only available on macOS 10.14: `find-highlight`, `selected-content-background`, `separator`, `unemphasized-selected-content-background`, `unemphasized-selected-text-background`, and `unemphasized-selected-text`.
[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
[macos-colors]:https://developer.apple.com/design/human-interface-guidelines/macos/visual-design/color#dynamic-system-colors [macos-colors]:https://developer.apple.com/design/human-interface-guidelines/macos/visual-design/color#dynamic-system-colors

View file

@ -13,6 +13,7 @@
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "shell/browser/api/event_emitter_deprecated.h" #include "shell/browser/api/event_emitter_deprecated.h"
#include "shell/common/gin_helper/error_thrower.h" #include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/node_includes.h"
#include "shell/common/promise_util.h" #include "shell/common/promise_util.h"
#if defined(OS_WIN) #if defined(OS_WIN)
@ -52,7 +53,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
#if defined(OS_WIN) || defined(OS_MACOSX) #if defined(OS_WIN) || defined(OS_MACOSX)
std::string GetAccentColor(); std::string GetAccentColor();
std::string GetColor(const std::string& color, mate::Arguments* args); std::string GetColor(gin_helper::ErrorThrower thrower,
const std::string& color);
#endif #endif
#if defined(OS_WIN) #if defined(OS_WIN)
bool IsAeroGlassEnabled(); bool IsAeroGlassEnabled();

View file

@ -26,6 +26,7 @@
#include "shell/browser/mac/atom_application.h" #include "shell/browser/mac/atom_application.h"
#include "shell/browser/mac/dict_util.h" #include "shell/browser/mac/dict_util.h"
#include "shell/browser/ui/cocoa/NSColor+Hex.h" #include "shell/browser/ui/cocoa/NSColor+Hex.h"
#include "shell/common/deprecate_util.h"
#include "shell/common/native_mate_converters/gurl_converter.h" #include "shell/common/native_mate_converters/gurl_converter.h"
#include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/native_mate_converters/value_converter.h"
#include "ui/native_theme/native_theme.h" #include "ui/native_theme/native_theme.h"
@ -503,18 +504,23 @@ bool SystemPreferences::IsTrustedAccessibilityClient(bool prompt) {
return AXIsProcessTrustedWithOptions((CFDictionaryRef)options); return AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
} }
std::string SystemPreferences::GetColor(const std::string& color, std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
mate::Arguments* args) { const std::string& color) {
NSColor* sysColor = nil; NSColor* sysColor = nil;
if (color == "alternate-selected-control-text") { if (color == "alternate-selected-control-text") {
sysColor = [NSColor alternateSelectedControlTextColor]; sysColor = [NSColor alternateSelectedControlTextColor];
EmitDeprecationWarning(
node::Environment::GetCurrent(thrower.isolate()),
"'alternate-selected-control-text' is deprecated as an input to "
"getColor. Use 'selected-content-background' instead.",
"electron");
} else if (color == "control-background") { } else if (color == "control-background") {
sysColor = [NSColor controlBackgroundColor]; sysColor = [NSColor controlBackgroundColor];
} else if (color == "control") { } else if (color == "control") {
sysColor = [NSColor controlColor]; sysColor = [NSColor controlColor];
} else if (color == "control-text") { } else if (color == "control-text") {
sysColor = [NSColor controlTextColor]; sysColor = [NSColor controlTextColor];
} else if (color == "disabled-control") { } else if (color == "disabled-control-text") {
sysColor = [NSColor disabledControlTextColor]; sysColor = [NSColor disabledControlTextColor];
} else if (color == "find-highlight") { } else if (color == "find-highlight") {
if (@available(macOS 10.14, *)) if (@available(macOS 10.14, *))
@ -580,11 +586,12 @@ std::string SystemPreferences::GetColor(const std::string& color,
} else if (color == "window-frame-text") { } else if (color == "window-frame-text") {
sysColor = [NSColor windowFrameTextColor]; sysColor = [NSColor windowFrameTextColor];
} else { } else {
args->ThrowError("Unknown color: " + color); thrower.ThrowError("Unknown color: " + color);
return "";
} }
if (sysColor)
return base::SysNSStringToUTF8([sysColor hexadecimalValue]); return base::SysNSStringToUTF8([sysColor hexadecimalValue]);
return "";
} }
std::string SystemPreferences::GetMediaAccessStatus( std::string SystemPreferences::GetMediaAccessStatus(

View file

@ -46,8 +46,8 @@ std::string SystemPreferences::GetAccentColor() {
return hexColorDWORDToRGBA(color); return hexColorDWORDToRGBA(color);
} }
std::string SystemPreferences::GetColor(const std::string& color, std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
mate::Arguments* args) { const std::string& color) {
int id; int id;
if (color == "3d-dark-shadow") { if (color == "3d-dark-shadow") {
id = COLOR_3DDKSHADOW; id = COLOR_3DDKSHADOW;
@ -110,7 +110,7 @@ std::string SystemPreferences::GetColor(const std::string& color,
} else if (color == "window-text") { } else if (color == "window-text") {
id = COLOR_WINDOWTEXT; id = COLOR_WINDOWTEXT;
} else { } else {
args->ThrowError("Unknown color: " + color); thrower.ThrowError("Unknown color: " + color);
return ""; return "";
} }

View file

@ -134,6 +134,58 @@ describe('systemPreferences module', () => {
}) })
}) })
ifdescribe(process.platform === 'darwin')('systemPreferences.getColor(color)', () => {
it('throws on invalid colors', () => {
const color = 'bad-color'
expect(() => {
systemPreferences.getColor(color as any)
}).to.throw(`Unknown color: ${color}`)
})
it('returns a valid color', () => {
const colors = [
'alternate-selected-control-text',
'control-background',
'control',
'control-text',
'disabled-control-text',
'find-highlight',
'grid',
'header-text',
'highlight',
'keyboard-focus-indicator',
'label',
'link',
'placeholder-text',
'quaternary-label',
'scrubber-textured-background',
'secondary-label',
'selected-content-background',
'selected-control',
'selected-control-text',
'selected-menu-item-text',
'selected-text-background',
'selected-text',
'separator',
'shadow',
'tertiary-label',
'text-background',
'text',
'under-page-background',
'unemphasized-selected-content-background',
'unemphasized-selected-text-background',
'unemphasized-selected-text',
'window-background',
'window-frame-text'
]
colors.forEach(color => {
const sysColor = systemPreferences.getColor(color as any)
expect(sysColor).to.be.a('string')
})
})
})
ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => { ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => {
it('has an appLevelAppearance property', () => { it('has an appLevelAppearance property', () => {
expect(systemPreferences).to.have.property('appLevelAppearance') expect(systemPreferences).to.have.property('appLevelAppearance')