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>
#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 "base/strings/sys_string_conversions.h"
#include "base/values.h"
@ -66,8 +67,8 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* key = base::SysUTF8ToNSString(name);
if (type == "string") {
return mate::StringToV8(
isolate(), base::SysNSStringToUTF8([defaults stringForKey:key]));
return mate::StringToV8(isolate(),
base::SysNSStringToUTF8([defaults stringForKey:key]));
} else if (type == "boolean") {
return v8::Boolean::New(isolate(), [defaults boolForKey:key]);
} else if (type == "float") {
@ -77,8 +78,14 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
} else if (type == "double") {
return v8::Number::New(isolate(), [defaults doubleForKey:key]);
} else if (type == "url") {
return mate::ConvertToV8(
isolate(), net::GURLWithNSURL([defaults URLForKey:key]));
return mate::ConvertToV8(isolate(),
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 {
return v8::Undefined(isolate());
}

View file

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

View file

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

View file

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