Merge pull request #8084 from poiru/systempreferences-setuserdefault
Add systemPreferences.setUserDefault for macOS
This commit is contained in:
commit
2c8ab72269
7 changed files with 169 additions and 11 deletions
|
@ -67,6 +67,7 @@ void SystemPreferences::BuildPrototype(
|
||||||
.SetMethod("unsubscribeLocalNotification",
|
.SetMethod("unsubscribeLocalNotification",
|
||||||
&SystemPreferences::UnsubscribeLocalNotification)
|
&SystemPreferences::UnsubscribeLocalNotification)
|
||||||
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
|
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
|
||||||
|
.SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
|
||||||
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
|
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
|
||||||
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
|
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -67,6 +67,9 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
|
||||||
void UnsubscribeLocalNotification(int request_id);
|
void UnsubscribeLocalNotification(int request_id);
|
||||||
v8::Local<v8::Value> GetUserDefault(const std::string& name,
|
v8::Local<v8::Value> GetUserDefault(const std::string& name,
|
||||||
const std::string& type);
|
const std::string& type);
|
||||||
|
void SetUserDefault(const std::string& name,
|
||||||
|
const std::string& type,
|
||||||
|
mate::Arguments* args);
|
||||||
bool IsSwipeTrackingFromScrollEventsEnabled();
|
bool IsSwipeTrackingFromScrollEventsEnabled();
|
||||||
#endif
|
#endif
|
||||||
bool IsDarkMode();
|
bool IsDarkMode();
|
||||||
|
|
|
@ -144,6 +144,91 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SystemPreferences::SetUserDefault(const std::string& name,
|
||||||
|
const std::string& type,
|
||||||
|
mate::Arguments* args) {
|
||||||
|
const auto throwConversionError = [&] {
|
||||||
|
args->ThrowError("Unable to convert value to: " + type);
|
||||||
|
};
|
||||||
|
|
||||||
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
NSString* key = base::SysUTF8ToNSString(name);
|
||||||
|
if (type == "string") {
|
||||||
|
std::string value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[defaults setObject:base::SysUTF8ToNSString(value) forKey:key];
|
||||||
|
} else if (type == "boolean") {
|
||||||
|
bool value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[defaults setBool:value forKey:key];
|
||||||
|
} else if (type == "float") {
|
||||||
|
float value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[defaults setFloat:value forKey:key];
|
||||||
|
} else if (type == "integer") {
|
||||||
|
int value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[defaults setInteger:value forKey:key];
|
||||||
|
} else if (type == "double") {
|
||||||
|
double value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[defaults setDouble:value forKey:key];
|
||||||
|
} else if (type == "url") {
|
||||||
|
GURL value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NSURL* url = net::NSURLWithGURL(value)) {
|
||||||
|
[defaults setURL:url forKey:key];
|
||||||
|
}
|
||||||
|
} else if (type == "array") {
|
||||||
|
base::ListValue value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NSArray* array = ListValueToNSArray(value)) {
|
||||||
|
[defaults setObject:array forKey:key];
|
||||||
|
}
|
||||||
|
} else if (type == "dictionary") {
|
||||||
|
base::DictionaryValue value;
|
||||||
|
if (!args->GetNext(&value)) {
|
||||||
|
throwConversionError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NSDictionary* dict = DictionaryValueToNSDictionary(value)) {
|
||||||
|
[defaults setObject:dict forKey:key];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args->ThrowError("Invalid type: " + type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool SystemPreferences::IsDarkMode() {
|
bool SystemPreferences::IsDarkMode() {
|
||||||
NSString* mode = [[NSUserDefaults standardUserDefaults]
|
NSString* mode = [[NSUserDefaults standardUserDefaults]
|
||||||
stringForKey:@"AppleInterfaceStyle"];
|
stringForKey:@"AppleInterfaceStyle"];
|
||||||
|
|
|
@ -16,13 +16,15 @@ class DictionaryValue;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
NSArray* ListValueToNSArray(const base::ListValue& value);
|
||||||
|
|
||||||
|
std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr);
|
||||||
|
|
||||||
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value);
|
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_
|
||||||
|
|
|
@ -10,6 +10,18 @@
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
NSArray* ListValueToNSArray(const base::ListValue& value) {
|
||||||
|
std::string json;
|
||||||
|
if (!base::JSONWriter::Write(value, &json))
|
||||||
|
return nil;
|
||||||
|
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
|
||||||
|
id obj =
|
||||||
|
[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||||
|
if (![obj isKindOfClass:[NSArray class]])
|
||||||
|
return nil;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
|
std::unique_ptr<base::ListValue> NSArrayToListValue(NSArray* arr) {
|
||||||
if (!arr)
|
if (!arr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
@ -114,16 +114,30 @@ Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificati
|
||||||
|
|
||||||
Get the value of `key` in system preferences.
|
Get the value of `key` in system preferences.
|
||||||
|
|
||||||
This API reads from `NSUserDefaults` on macOS, some popular `key` and `type`s
|
This API uses `NSUserDefaults` on macOS. Some popular `key` and `type`s are:
|
||||||
are:
|
|
||||||
|
|
||||||
* `AppleInterfaceStyle: string`
|
* `AppleInterfaceStyle`: `string`
|
||||||
* `AppleAquaColorVariant: integer`
|
* `AppleAquaColorVariant`: `integer`
|
||||||
* `AppleHighlightColor: string`
|
* `AppleHighlightColor`: `string`
|
||||||
* `AppleShowScrollBars: string`
|
* `AppleShowScrollBars`: `string`
|
||||||
* `NSNavRecentPlaces: array`
|
* `NSNavRecentPlaces`: `array`
|
||||||
* `NSPreferredWebServices: dictionary`
|
* `NSPreferredWebServices`: `dictionary`
|
||||||
* `NSUserDictionaryReplacementItems: array`
|
* `NSUserDictionaryReplacementItems`: `array`
|
||||||
|
|
||||||
|
### `systemPreferences.setUserDefault(key, type, value)` _macOS_
|
||||||
|
|
||||||
|
* `key` String
|
||||||
|
* `type` String - See [`getUserDefault`][#systempreferencesgetuserdefaultkey-type-macos]
|
||||||
|
* `value` String
|
||||||
|
|
||||||
|
Set the value of `key` in system preferences.
|
||||||
|
|
||||||
|
Note that `type` should match actual type of `value`. An exception is thrown
|
||||||
|
if they don't.
|
||||||
|
|
||||||
|
This API uses `NSUserDefaults` on macOS. Some popular `key` and `type`s are:
|
||||||
|
|
||||||
|
* `ApplePressAndHoldEnabled`: `boolean`
|
||||||
|
|
||||||
### `systemPreferences.isAeroGlassEnabled()` _Windows_
|
### `systemPreferences.isAeroGlassEnabled()` _Windows_
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,47 @@ describe('systemPreferences module', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('systemPreferences.setUserDefault(key, type, value)', () => {
|
||||||
|
if (process.platform !== 'darwin') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const KEY = 'SystemPreferencesTest'
|
||||||
|
|
||||||
|
const TEST_CASES = [
|
||||||
|
['string', 'abc'],
|
||||||
|
['boolean', true],
|
||||||
|
['float', 2.5],
|
||||||
|
['double', 10.1],
|
||||||
|
['integer', 11],
|
||||||
|
['url', 'https://github.com/electron'],
|
||||||
|
['array', [1, 2, 3]],
|
||||||
|
['dictionary', {'a': 1, 'b': 2}]
|
||||||
|
]
|
||||||
|
|
||||||
|
it('sets values', () => {
|
||||||
|
for (const [type, value] of TEST_CASES) {
|
||||||
|
systemPreferences.setUserDefault(KEY, type, value)
|
||||||
|
const retrievedValue = systemPreferences.getUserDefault(KEY, type)
|
||||||
|
assert.deepEqual(retrievedValue, value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws when type and value conflict', () => {
|
||||||
|
for (const [type, value] of TEST_CASES) {
|
||||||
|
assert.throws(() => {
|
||||||
|
systemPreferences.setUserDefault(KEY, type, typeof value === 'string' ? {} : 'foo')
|
||||||
|
}, `Unable to convert value to: ${type}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws when type is not valid', () => {
|
||||||
|
assert.throws(() => {
|
||||||
|
systemPreferences.setUserDefault(KEY, 'abc', 'foo')
|
||||||
|
}, 'Invalid type: abc')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('systemPreferences.isInvertedColorScheme()', function () {
|
describe('systemPreferences.isInvertedColorScheme()', function () {
|
||||||
it('returns a boolean', function () {
|
it('returns a boolean', function () {
|
||||||
assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean')
|
assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue