Skip to content

Commit

Permalink
Add support for Windows for .openSystemPreferences() API (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
xpirt authored and sindresorhus committed May 8, 2021
1 parent b1deb86 commit 4557c29
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 15 deletions.
173 changes: 168 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ Menu.setApplicationMenu(menu);
*/
export function appMenu(menuItems?: readonly MenuItemConstructorOptions[]): MenuItemConstructorOptions;

export interface SystemPreferencesPanes {
export interface SystemPreferencesMacOsPanes {
universalaccess:
| 'Captioning'
| 'Hearing'
Expand Down Expand Up @@ -580,12 +580,174 @@ export interface SystemPreferencesPanes {
| 'Services_ScreenSharing';
}

export type SystemPreferencesWindowsPane =
/**
System
*/
'display'
| 'sound' // Build 17063+
| 'notifications'
| 'quiethours' // Build 17074+
| 'powersleep'
| 'batterysaver'
| 'storagesense'
| 'tabletmode'
| 'multitasking'
| 'project'
| 'crossdevice'
| 'clipboard' // Build 17666+
| 'remotedesktop'
| 'about'
/**
Devices
*/
| 'bluetooth'
| 'connecteddevices'
| 'printers'
| 'mousetouchpad'
| 'devices-touchpad'
| 'typing'
| 'wheel'
| 'pen'
| 'autoplay'
| 'usb'
| 'mobile-devices' // Build 16251+
/**
Network & Internet
*/
| 'network'
| 'network-status'
| 'network-cellular'
| 'network-wifi'
| 'network-wificalling'
| 'network-ethernet'
| 'network-dialup'
| 'network-vpn'
| 'network-airplanemode'
| 'network-mobilehotspot'
| 'nfctransactions'
| 'datausage'
| 'network-proxy'
/**
Personalization
*/
| 'personalization'
| 'personalization-background'
| 'personalization-colors'
| 'lockscreen'
| 'themes'
| 'fonts' // Build 17083+
| 'personalization-start'
| 'taskbar'
/**
Apps
*/
| 'appsfeatures'
| 'optionalfeatures'
| 'defaultapps'
| 'maps'
| 'appsforwebsites'
| 'videoplayback' // Build 16215+
| 'startupapps' // Build 17017+
/**
Accounts
*/
| 'yourinfo'
| 'emailandaccounts'
| 'signinoptions'
| 'workplace'
| 'otherusers'
| 'sync'
/**
Time & language
*/
| 'dateandtime'
| 'regionformatting'
| 'regionlanguage'
| 'speech'
/**
Gaming
*/
| 'gaming-gamebar'
| 'gaming-gamedvr'
| 'gaming-broadcasting'
| 'gaming-gamemode'
| 'gaming-xboxnetworking' // Build 16226+
/**
Ease of Access
*/
| 'easeofaccess-display' // Build 17025+
| 'easeofaccess-cursorandpointersize' // Build 17040+
| 'easeofaccess-cursor'
| 'easeofaccess-magnifier'
| 'easeofaccess-colorfilter' // Build 17025+
| 'easeofaccess-highcontrast'
| 'easeofaccess-narrator'
| 'easeofaccess-audio' // Build 17035+
| 'easeofaccess-closedcaptioning'
| 'easeofaccess-speechrecognition' // Build 17035+
| 'easeofaccess-keyboard'
| 'easeofaccess-mouse'
| 'easeofaccess-eyecontrol' // Build 17035+
/**
Search & Cortana
*/
| 'search-permissions' // Version 1903+
| 'cortana-windowssearch' // Version 1903+
| 'cortana' // Build 16188+
| 'cortana-talktocortana' // Build 16188+
| 'cortana-permissions' // Build 16188+
/**
Privacy
*/
| 'privacy'
| 'privacy-speech'
| 'privacy-speechtyping'
| 'privacy-feedback'
| 'privacy-activityhistory' // Build 17040+
| 'privacy-location'
| 'privacy-webcam'
| 'privacy-microphone'
| 'privacy-voiceactivation'
| 'privacy-notifications'
| 'privacy-accountinfo'
| 'privacy-contacts'
| 'privacy-calendar'
| 'privacy-phonecalls'
| 'privacy-callhistory'
| 'privacy-email'
| 'privacy-eyetracker'
| 'privacy-tasks'
| 'privacy-messaging'
| 'privacy-radios'
| 'privacy-customdevices'
| 'privacy-backgroundapps'
| 'privacy-appdiagnostics'
| 'privacy-automaticfiledownloads'
| 'privacy-documents'
| 'privacy-pictures'
| 'privacy-videos'
| 'privacy-broadfilesystemaccess'
/**
Update & security
*/
| 'windowsupdate'
| 'delivery-optimization'
| 'windowsdefender'
| 'backup'
| 'troubleshoot'
| 'recovery'
| 'activation'
| 'findmydevice'
| 'developers'
| 'windowsinsider';

/**
Open the System Preferences on macOS.
Open the System Preferences on macOS and Windows 10.
This method does nothing on other systems.
This method does nothing on Linux.
Optionally provide a pane and section.
On macOS, optionally provide a pane and section.
@example
```
Expand All @@ -602,4 +764,5 @@ openSystemPreferences('security', 'Firewall');
@param section - The section within that pane.
@returns A Promise that resolves when the preferences window is opened.
*/
export const openSystemPreferences: <T extends keyof SystemPreferencesPanes>(pane?: T, section?: SystemPreferencesPanes[T]) => Promise<void>;
export function openSystemPreferences(pane?: SystemPreferencesWindowsPane): Promise<void>;
export function openSystemPreferences<T extends keyof SystemPreferencesMacOsPanes>(pane?: T, section?: SystemPreferencesMacOsPanes[T]): Promise<void>;
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ expectType<MenuItemConstructorOptions>(appMenu([

expectType<Promise<void>>(openSystemPreferences());
expectType<Promise<void>>(openSystemPreferences('security', 'Privacy_Microphone'));
expectType<Promise<void>>(openSystemPreferences('windowsupdate'));
expectError(openSystemPreferences('security', 'Bad_Section'));
14 changes: 8 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ console.log(is.macos && is.main);
- [`aboutMenuItem()`](#aboutmenuitemoptions-linux-windows)
- [`debugInfo()`](#debuginfo)
- [`appMenu()`](#appmenumenuitems-macos)
- [`openSystemPreferences()`](#opensystempreferencespanel-section-promise-macos)
- [`openSystemPreferences()`](#opensystempreferencespane-section-promise-macos-windows)

### api

Expand Down Expand Up @@ -492,15 +492,17 @@ const menu = Menu.buildFromTemplate([
Menu.setApplicationMenu(menu);
```

### openSystemPreferences(panel?, section?): Promise<void> <sup>*macOS*</sup>
### openSystemPreferences(pane?, section?): Promise<void> <sup>*macOS*</sup> <sup>*Windows*</sup>

Type: `Function`

Open the System Preferences on macOS.
Open the System Preferences on macOS and Windows 10.

This method does nothing on other systems.
This method does nothing on Linux.

Optionally provide a pane and section. A list of available options can be found [here](https://github.com/sindresorhus/electron-util/blob/4215b51e741b7ea50a0637abe919e2e7d61b34ac/index.d.ts#L515-L561).
A list of available options can be found [here](https://github.com/sindresorhus/electron-util/blob/b1deb86cf1cba9a89869b277f8dfbdc7b0ddc888/index.d.ts#L531-L743).

On macOS, optionally provide a pane and section.

#### pane

Expand All @@ -514,7 +516,7 @@ const {openSystemPreferences} = require('electron-util');
openSystemPreferences('security');
```

#### section
#### section <sup>*macOS*</sup>

Type: `string`

Expand Down
8 changes: 4 additions & 4 deletions source/open-system-preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const api = require('./api');
const is = require('./is');

module.exports = async (pane, section) => {
if (!is.macos) {
return;
if (is.macos) {
await api.shell.openExternal(`x-apple.systempreferences:com.apple.preference.${pane}${section ? `?${section}` : ''}`);
} else if (is.windows) {
await api.shell.openExternal(`ms-settings:${pane}`);
}

await api.shell.openExternal(`x-apple.systempreferences:com.apple.preference.${pane}${section ? `?${section}` : ''}`);
};

0 comments on commit 4557c29

Please sign in to comment.