Merge pull request #5833 from CharlieHess/osx-global-defaults

Support array / dictionary types in getUserDefault
This commit is contained in:
Cheng Zhao 2016-06-03 08:36:44 +00:00 committed by GitHub
commit f59eecb1cc
5 changed files with 40 additions and 10 deletions

View file

@ -9,6 +9,7 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include "atom/browser/mac/dict_util.h" #include "atom/browser/mac/dict_util.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/values.h" #include "base/values.h"
@ -66,8 +67,8 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* key = base::SysUTF8ToNSString(name); NSString* key = base::SysUTF8ToNSString(name);
if (type == "string") { if (type == "string") {
return mate::StringToV8( return mate::StringToV8(isolate(),
isolate(), base::SysNSStringToUTF8([defaults stringForKey:key])); base::SysNSStringToUTF8([defaults stringForKey:key]));
} else if (type == "boolean") { } else if (type == "boolean") {
return v8::Boolean::New(isolate(), [defaults boolForKey:key]); return v8::Boolean::New(isolate(), [defaults boolForKey:key]);
} else if (type == "float") { } else if (type == "float") {
@ -77,8 +78,14 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
} else if (type == "double") { } else if (type == "double") {
return v8::Number::New(isolate(), [defaults doubleForKey:key]); return v8::Number::New(isolate(), [defaults doubleForKey:key]);
} else if (type == "url") { } else if (type == "url") {
return mate::ConvertToV8( return mate::ConvertToV8(isolate(),
isolate(), net::GURLWithNSURL([defaults URLForKey:key])); net::GURLWithNSURL([defaults URLForKey:key]));
} else if (type == "array") {
return mate::ConvertToV8(isolate(),
*NSArrayToListValue([defaults arrayForKey:key]));
} else if (type == "dictionary") {
return mate::ConvertToV8(isolate(),
*NSDictionaryToDictionaryValue([defaults dictionaryForKey:key]));
} else { } else {
return v8::Undefined(isolate()); return v8::Undefined(isolate());
} }

View file

@ -10,7 +10,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
namespace base { namespace base {
class Value; class ListValue;
class DictionaryValue; class DictionaryValue;
} }
@ -21,6 +21,8 @@ NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value);
std::unique_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue( std::unique_ptr<base::DictionaryValue> NSDictionaryToDictionaryValue(
NSDictionary* dict); NSDictionary* dict);
std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr);
} // namespace atom } // namespace atom
#endif // ATOM_BROWSER_MAC_DICT_UTIL_H_ #endif // ATOM_BROWSER_MAC_DICT_UTIL_H_

View file

@ -10,8 +10,6 @@
namespace atom { namespace atom {
namespace {
std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) { std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
if (!arr) if (!arr)
return nullptr; return nullptr;
@ -51,8 +49,6 @@ std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
return result; return result;
} }
} // namespace
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value) { NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value) {
std::string json; std::string json;
if (!base::JSONWriter::Write(value, &json)) if (!base::JSONWriter::Write(value, &json))

View file

@ -39,7 +39,7 @@ Removes the subscriber with `id`.
* `key` String * `key` String
* `type` String - Can be `string`, `boolean`, `integer`, `float`, `double`, * `type` String - Can be `string`, `boolean`, `integer`, `float`, `double`,
`url`. `url`, `array`, `dictionary`
Get the value of `key` in system preferences. Get the value of `key` in system preferences.
@ -50,6 +50,9 @@ are:
* `AppleAquaColorVariant: integer` * `AppleAquaColorVariant: integer`
* `AppleHighlightColor: string` * `AppleHighlightColor: string`
* `AppleShowScrollBars: string` * `AppleShowScrollBars: string`
* `NSNavRecentPlaces: array`
* `NSPreferredWebServices: dictionary`
* `NSUserDictionaryReplacementItems: array`
### `systemPreferences.isAeroGlassEnabled()` _Windows_ ### `systemPreferences.isAeroGlassEnabled()` _Windows_

View file

@ -0,0 +1,22 @@
const assert = require('assert')
const {remote} = require('electron')
const {systemPreferences} = remote
describe('systemPreferences module', function () {
if (process.platform !== 'darwin') {
return
}
describe('systemPreferences.getUserDefault(key, type)', function () {
it('returns values for known user defaults', function () {
let locale = systemPreferences.getUserDefault('AppleLocale', 'string')
assert.notEqual(locale, null)
assert(locale.length > 0)
let languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert.notEqual(languages, null)
assert(languages.length > 0)
})
})
})