All we really need to do is make getUserDefault support array / object types.

This commit is contained in:
Charlie Hess 2016-06-01 19:21:17 -07:00
parent 587dd2fe51
commit fa3b17444f
5 changed files with 17 additions and 35 deletions

View file

@ -54,7 +54,6 @@ void SystemPreferences::BuildPrototype(
.SetMethod("unsubscribeNotification",
&SystemPreferences::UnsubscribeNotification)
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
.SetMethod("getGlobalDefault", &SystemPreferences::GetGlobalDefault)
#endif
.SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
}

View file

@ -39,7 +39,6 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
void UnsubscribeNotification(int id);
v8::Local<v8::Value> GetUserDefault(const std::string& name,
const std::string& type);
v8::Local<v8::Value> GetGlobalDefault(const std::string& name);
#endif
bool IsDarkMode();

View file

@ -67,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") {
@ -78,28 +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]));
} else {
return v8::Undefined(isolate());
}
}
v8::Local<v8::Value> SystemPreferences::GetGlobalDefault(const std::string& name) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* key = base::SysUTF8ToNSString(name);
NSDictionary* globalPrefs = [defaults persistentDomainForName:NSGlobalDomain];
id value = [globalPrefs objectForKey:key];
if ([value isKindOfClass:[NSString class]]) {
return mate::StringToV8(isolate(), base::SysNSStringToUTF8(value));
} else if ([value isKindOfClass:[NSNumber class]]) {
return v8::Integer::New(isolate(), [value integerValue]);
} else if ([value isKindOfClass:[NSArray class]]) {
return mate::ConvertToV8(isolate(), *NSArrayToListValue(value));
} else if ([value isKindOfClass:[NSDictionary class]]) {
return mate::ConvertToV8(isolate(), *NSDictionaryToDictionaryValue(value));
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

@ -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

@ -7,17 +7,12 @@ describe('systemPreferences module', function () {
return
}
it('returns user defaults', function () {
assert.notEqual(systemPreferences.getUserDefault('AppleInterfaceStyle', 'string'), null)
assert.notEqual(systemPreferences.getUserDefault('AppleAquaColorVariant', 'integer'), null)
})
it('returns defaults under the global domain', function () {
let locale = systemPreferences.getGlobalDefault('AppleLocale')
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.getGlobalDefault('AppleLanguages')
let languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert.notEqual(languages, null)
assert(languages.length > 0)
})