Add systemPreferences.removeUserDefault()
This can be used to restore the default or global value of a `key` previously set with `setUserDefault`.
This commit is contained in:
parent
30abdbccf8
commit
06d782279c
5 changed files with 35 additions and 4 deletions
|
@ -67,6 +67,7 @@ void SystemPreferences::BuildPrototype(
|
||||||
&SystemPreferences::UnsubscribeLocalNotification)
|
&SystemPreferences::UnsubscribeLocalNotification)
|
||||||
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
|
.SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
|
||||||
.SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
|
.SetMethod("setUserDefault", &SystemPreferences::SetUserDefault)
|
||||||
|
.SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault)
|
||||||
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
|
.SetMethod("isSwipeTrackingFromScrollEventsEnabled",
|
||||||
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
|
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -76,6 +76,7 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences>
|
||||||
void SetUserDefault(const std::string& name,
|
void SetUserDefault(const std::string& name,
|
||||||
const std::string& type,
|
const std::string& type,
|
||||||
mate::Arguments* args);
|
mate::Arguments* args);
|
||||||
|
void RemoveUserDefault(const std::string& name);
|
||||||
bool IsSwipeTrackingFromScrollEventsEnabled();
|
bool IsSwipeTrackingFromScrollEventsEnabled();
|
||||||
#endif
|
#endif
|
||||||
bool IsDarkMode();
|
bool IsDarkMode();
|
||||||
|
|
|
@ -229,6 +229,11 @@ void SystemPreferences::SetUserDefault(const std::string& name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SystemPreferences::RemoveUserDefault(const std::string& name) {
|
||||||
|
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
[defaults removeObjectForKey:base::SysUTF8ToNSString(name)];
|
||||||
|
}
|
||||||
|
|
||||||
bool SystemPreferences::IsDarkMode() {
|
bool SystemPreferences::IsDarkMode() {
|
||||||
NSString* mode = [[NSUserDefaults standardUserDefaults]
|
NSString* mode = [[NSUserDefaults standardUserDefaults]
|
||||||
stringForKey:@"AppleInterfaceStyle"];
|
stringForKey:@"AppleInterfaceStyle"];
|
||||||
|
|
|
@ -112,9 +112,9 @@ Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificati
|
||||||
* `type` String - Can be `string`, `boolean`, `integer`, `float`, `double`,
|
* `type` String - Can be `string`, `boolean`, `integer`, `float`, `double`,
|
||||||
`url`, `array`, `dictionary`
|
`url`, `array`, `dictionary`
|
||||||
|
|
||||||
Returns `any` - The value of `key` in system preferences.
|
Returns `any` - The value of `key` in `NSUserDefaults`.
|
||||||
|
|
||||||
This API uses `NSUserDefaults` on macOS. Some popular `key` and `type`s are:
|
Some popular `key` and `type`s are:
|
||||||
|
|
||||||
* `AppleInterfaceStyle`: `string`
|
* `AppleInterfaceStyle`: `string`
|
||||||
* `AppleAquaColorVariant`: `integer`
|
* `AppleAquaColorVariant`: `integer`
|
||||||
|
@ -130,15 +130,22 @@ This API uses `NSUserDefaults` on macOS. Some popular `key` and `type`s are:
|
||||||
* `type` String - See [`getUserDefault`][#systempreferencesgetuserdefaultkey-type-macos]
|
* `type` String - See [`getUserDefault`][#systempreferencesgetuserdefaultkey-type-macos]
|
||||||
* `value` String
|
* `value` String
|
||||||
|
|
||||||
Set the value of `key` in system preferences.
|
Set the value of `key` in `NSUserDefaults`.
|
||||||
|
|
||||||
Note that `type` should match actual type of `value`. An exception is thrown
|
Note that `type` should match actual type of `value`. An exception is thrown
|
||||||
if they don't.
|
if they don't.
|
||||||
|
|
||||||
This API uses `NSUserDefaults` on macOS. Some popular `key` and `type`s are:
|
Some popular `key` and `type`s are:
|
||||||
|
|
||||||
* `ApplePressAndHoldEnabled`: `boolean`
|
* `ApplePressAndHoldEnabled`: `boolean`
|
||||||
|
|
||||||
|
### `systemPreferences.removeUserDefault(key)` _macOS_
|
||||||
|
|
||||||
|
* `key` String
|
||||||
|
|
||||||
|
Removes the `key` in `NSUserDefaults`. This can be used to restore the default
|
||||||
|
or global value of a `key` previously set with `setUserDefault`.
|
||||||
|
|
||||||
### `systemPreferences.isAeroGlassEnabled()` _Windows_
|
### `systemPreferences.isAeroGlassEnabled()` _Windows_
|
||||||
|
|
||||||
Returns `Boolean` - `true` if [DWM composition][dwm-composition] (Aero Glass) is
|
Returns `Boolean` - `true` if [DWM composition][dwm-composition] (Aero Glass) is
|
||||||
|
|
|
@ -100,6 +100,23 @@ describe('systemPreferences module', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('systemPreferences.setUserDefault(key, type, value)', () => {
|
||||||
|
if (process.platform !== 'darwin') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
it('removes keys', () => {
|
||||||
|
const KEY = 'SystemPreferencesTest'
|
||||||
|
systemPreferences.setUserDefault(KEY, 'string', 'foo')
|
||||||
|
systemPreferences.removeUserDefault(KEY)
|
||||||
|
assert.equal(systemPreferences.getUserDefault(KEY, 'string'), '')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not throw for missing keys', () => {
|
||||||
|
systemPreferences.removeUserDefault('some-missing-key')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
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
Reference in a new issue