Skip to content

Commit

Permalink
chore: remove deprecated systemPreferences APIs (electron#39696)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Sep 11, 2023
1 parent d182794 commit aceb432
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 147 deletions.
26 changes: 0 additions & 26 deletions docs/api/system-preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ This API is only available on macOS 10.14 Mojave or newer.
* `window-frame` - Window frame.
* `window-text` - Text in windows.
* On **macOS**
* `alternate-selected-control-text` - The text on a selected surface in a list or table. _Deprecated_
* `control-background` - The background of a large interface element, such as a browser or table.
* `control` - The surface of a control.
* `control-text` -The text of a control that isn’t disabled.
Expand Down Expand Up @@ -339,21 +338,6 @@ Returns `string` - Can be `dark`, `light` or `unknown`.
Gets the macOS appearance setting that is currently applied to your application,
maps to [NSApplication.effectiveAppearance](https://developer.apple.com/documentation/appkit/nsapplication/2967171-effectiveappearance?language=objc)

### `systemPreferences.getAppLevelAppearance()` _macOS_ _Deprecated_

Returns `string` | `null` - Can be `dark`, `light` or `unknown`.

Gets the macOS appearance setting that you have declared you want for
your application, maps to [NSApplication.appearance](https://developer.apple.com/documentation/appkit/nsapplication/2967170-appearance?language=objc).
You can use the `setAppLevelAppearance` API to set this value.

### `systemPreferences.setAppLevelAppearance(appearance)` _macOS_ _Deprecated_

* `appearance` string | null - Can be `dark` or `light`

Sets the appearance setting for your application, this should override the
system default and override the value of `getEffectiveAppearance`.

### `systemPreferences.canPromptTouchID()` _macOS_

Returns `boolean` - whether or not this device has the ability to use Touch ID.
Expand Down Expand Up @@ -417,16 +401,6 @@ Returns an object with system animation settings.

## Properties

### `systemPreferences.appLevelAppearance` _macOS_ _Deprecated_

A `string` property that can be `dark`, `light` or `unknown`. It determines the macOS appearance setting for
your application. This maps to values in: [NSApplication.appearance](https://developer.apple.com/documentation/appkit/nsapplication/2967170-appearance?language=objc). Setting this will override the
system default as well as the value of `getEffectiveAppearance`.

Possible values that can be set are `dark` and `light`, and possible return values are `dark`, `light`, and `unknown`.

This property is only available on macOS 10.14 Mojave or newer.

### `systemPreferences.effectiveAppearance` _macOS_ _Readonly_

A `string` property that can be `dark`, `light` or `unknown`.
Expand Down
35 changes: 35 additions & 0 deletions docs/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,41 @@ w.webContents.getPrintersAsync().then((printers) => {
})
```

### Removed: `systemPreferences.{get,set}AppLevelAppearance` and `systemPreferences.appLevelAppearance`

The `systemPreferences.getAppLevelAppearance` and `systemPreferences.setAppLevelAppearance`
methods have been removed, as well as the `systemPreferences.appLevelAppearance` property.
Use the `nativeTheme` module instead.

```js
// Removed
systemPreferences.getAppLevelAppearance()
// Replace with
nativeTheme.shouldUseDarkColors

// Removed
systemPreferences.appLevelAppearance
// Replace with
nativeTheme.shouldUseDarkColors

// Removed
systemPreferences.setAppLevelAppearance('dark')
// Replace with
nativeTheme.themeSource = 'dark'
```

### Removed: `alternate-selected-control-text` value for `systemPreferences.getColor`

The `alternate-selected-control-text` value for `systemPreferences.getColor`
has been removed. Use `selected-content-background` instead.

```js
// Removed
systemPreferences.getColor('alternate-selected-control-text')
// Replace with
systemPreferences.getColor('selected-content-background')
```

## Planned Breaking API Changes (26.0)

### Deprecated: `webContents.getPrinters`
Expand Down
28 changes: 0 additions & 28 deletions lib/browser/api/system-preferences.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
import * as deprecate from '@electron/internal/common/deprecate';

const { systemPreferences } = process._linkedBinding('electron_browser_system_preferences');

if ('getAppLevelAppearance' in systemPreferences) {
const nativeALAGetter = systemPreferences.getAppLevelAppearance;
const nativeALASetter = systemPreferences.setAppLevelAppearance;
const warnALA = deprecate.warnOnce('appLevelAppearance');
const warnALAGetter = deprecate.warnOnce('getAppLevelAppearance function');
const warnALASetter = deprecate.warnOnce('setAppLevelAppearance function');
Object.defineProperty(systemPreferences, 'appLevelAppearance', {
get: () => {
warnALA();
return nativeALAGetter.call(systemPreferences);
},
set: (appearance) => {
warnALA();
nativeALASetter.call(systemPreferences, appearance);
}
});
systemPreferences.getAppLevelAppearance = () => {
warnALAGetter();
return nativeALAGetter.call(systemPreferences);
};
systemPreferences.setAppLevelAppearance = (appearance) => {
warnALASetter();
nativeALASetter.call(systemPreferences, appearance);
};
}

if ('getEffectiveAppearance' in systemPreferences) {
const nativeEAGetter = systemPreferences.getEffectiveAppearance;
Object.defineProperty(systemPreferences, 'effectiveAppearance', {
Expand Down
4 changes: 0 additions & 4 deletions shell/browser/api/electron_api_system_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ gin::ObjectTemplateBuilder SystemPreferences::GetObjectTemplateBuilder(
&SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled)
.SetMethod("getEffectiveAppearance",
&SystemPreferences::GetEffectiveAppearance)
.SetMethod("getAppLevelAppearance",
&SystemPreferences::GetAppLevelAppearance)
.SetMethod("setAppLevelAppearance",
&SystemPreferences::SetAppLevelAppearance)
.SetMethod("getSystemColor", &SystemPreferences::GetSystemColor)
.SetMethod("canPromptTouchID", &SystemPreferences::CanPromptTouchID)
.SetMethod("promptTouchID", &SystemPreferences::PromptTouchID)
Expand Down
2 changes: 0 additions & 2 deletions shell/browser/api/electron_api_system_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ class SystemPreferences
// TODO(MarshallOfSound): Write tests for these methods once we
// are running tests on a Mojave machine
v8::Local<v8::Value> GetEffectiveAppearance(v8::Isolate* isolate);
v8::Local<v8::Value> GetAppLevelAppearance(v8::Isolate* isolate);
void SetAppLevelAppearance(gin::Arguments* args);
#endif
v8::Local<v8::Value> GetAnimationSettings(v8::Isolate* isolate);

Expand Down
24 changes: 1 addition & 23 deletions shell/browser/api/electron_api_system_preferences_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,7 @@ AVMediaType ParseMediaType(const std::string& media_type) {
std::string SystemPreferences::GetColor(gin_helper::ErrorThrower thrower,
const std::string& color) {
NSColor* sysColor = nil;
if (color == "alternate-selected-control-text") {
sysColor = [NSColor alternateSelectedControlTextColor];
EmitWarning(
node::Environment::GetCurrent(thrower.isolate()),
"'alternate-selected-control-text' is deprecated as an input to "
"getColor. Use 'selected-content-background' instead.",
"electron");
} else if (color == "control-background") {
if (color == "control-background") {
sysColor = [NSColor controlBackgroundColor];
} else if (color == "control") {
sysColor = [NSColor controlColor];
Expand Down Expand Up @@ -605,19 +598,4 @@ AVMediaType ParseMediaType(const std::string& media_type) {
isolate, [NSApplication sharedApplication].effectiveAppearance);
}

v8::Local<v8::Value> SystemPreferences::GetAppLevelAppearance(
v8::Isolate* isolate) {
return gin::ConvertToV8(isolate,
[NSApplication sharedApplication].appearance);
}

void SystemPreferences::SetAppLevelAppearance(gin::Arguments* args) {
NSAppearance* appearance;
if (args->GetNext(&appearance)) {
[[NSApplication sharedApplication] setAppearance:appearance];
} else {
args->ThrowError();
}
}

} // namespace electron::api
18 changes: 1 addition & 17 deletions spec/api-native-theme-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { expect } from 'chai';
import { nativeTheme, systemPreferences, BrowserWindow, ipcMain } from 'electron/main';
import { nativeTheme, BrowserWindow, ipcMain } from 'electron/main';
import { once } from 'node:events';
import * as path from 'node:path';
import { setTimeout } from 'node:timers/promises';

import { expectDeprecationMessages } from './lib/deprecate-helpers';
import { ifdescribe } from './lib/spec-helpers';
import { closeAllWindows } from './lib/window-helpers';

describe('nativeTheme module', () => {
Expand Down Expand Up @@ -59,20 +57,6 @@ describe('nativeTheme module', () => {
expect(called).to.equal(false);
});

ifdescribe(process.platform === 'darwin')('on macOS', () => {
it('should update appLevelAppearance when set', async () => {
await expectDeprecationMessages(
() => {
nativeTheme.themeSource = 'dark';
expect(systemPreferences.appLevelAppearance).to.equal('dark');
nativeTheme.themeSource = 'light';
expect(systemPreferences.appLevelAppearance).to.equal('light');
},
"(electron) 'appLevelAppearance' is deprecated and will be removed."
);
});
});

const getPrefersColorSchemeIsDark = async (w: Electron.BrowserWindow) => {
const isDark: boolean = await w.webContents.executeJavaScript(
'matchMedia("(prefers-color-scheme: dark)").matches'
Expand Down
47 changes: 0 additions & 47 deletions spec/api-system-preferences-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'chai';
import { systemPreferences } from 'electron/main';
import { expectDeprecationMessages } from './lib/deprecate-helpers';
import { ifdescribe } from './lib/spec-helpers';

describe('systemPreferences module', () => {
Expand Down Expand Up @@ -215,52 +214,6 @@ describe('systemPreferences module', () => {
const sysColor = systemPreferences.getColor(color);
expect(sysColor).to.be.a('string');
}

await expectDeprecationMessages(
() => {
const sysColor = systemPreferences.getColor('alternate-selected-control-text');
expect(sysColor).to.be.a('string');
},
"'alternate-selected-control-text' is deprecated as an input to getColor. Use 'selected-content-background' instead."
);
});
});

ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => {
const options = ['dark', 'light', 'unknown', null];
describe('with properties', () => {
it('returns a valid appearance', () => {
const appearance = systemPreferences.appLevelAppearance;
expect(options).to.include(appearance);
});

it('can be changed', () => {
systemPreferences.appLevelAppearance = 'dark';
expect(systemPreferences.appLevelAppearance).to.eql('dark');
});
});

describe('with functions', () => {
it('returns a valid appearance', async () => {
await expectDeprecationMessages(
() => {
const appearance = systemPreferences.getAppLevelAppearance();
expect(options).to.include(appearance);
},
"(electron) 'getAppLevelAppearance function' is deprecated and will be removed."
);
});

it('can be changed', async () => {
await expectDeprecationMessages(
() => {
systemPreferences.setAppLevelAppearance('dark');
const appearance = systemPreferences.getAppLevelAppearance();
expect(appearance).to.eql('dark');
},
"(electron) 'setAppLevelAppearance function' is deprecated and will be removed."
);
});
});
});

Expand Down
8 changes: 8 additions & 0 deletions spec/ts-smoke/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ if (process.platform === 'darwin') {
console.log(value);
const value2 = systemPreferences.getUserDefault('Foo', 'boolean');
console.log(value2);
// @ts-expect-error Removed API
console.log(systemPreferences.getAppLevelAppearance());
// @ts-expect-error Removed API
systemPreferences.setAppLevelAppearance('dark');
// @ts-expect-error Removed API
console.log(systemPreferences.appLevelAppearance);
// @ts-expect-error Removed API
console.log(systemPreferences.getColor('alternate-selected-control-text'));
}

// Create the window.
Expand Down

0 comments on commit aceb432

Please sign in to comment.