From 4557c29be9f94625c00a9e945db2fb26010990e6 Mon Sep 17 00:00:00 2001 From: Andrei Conache Date: Sat, 8 May 2021 12:14:49 +0200 Subject: [PATCH] Add support for Windows for `.openSystemPreferences()` API (#48) --- index.d.ts | 173 +++++++++++++++++++++++++++++- index.test-d.ts | 1 + readme.md | 14 +-- source/open-system-preferences.js | 8 +- 4 files changed, 181 insertions(+), 15 deletions(-) diff --git a/index.d.ts b/index.d.ts index c2e0613..f208cbc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -528,7 +528,7 @@ Menu.setApplicationMenu(menu); */ export function appMenu(menuItems?: readonly MenuItemConstructorOptions[]): MenuItemConstructorOptions; -export interface SystemPreferencesPanes { +export interface SystemPreferencesMacOsPanes { universalaccess: | 'Captioning' | 'Hearing' @@ -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 ``` @@ -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: (pane?: T, section?: SystemPreferencesPanes[T]) => Promise; +export function openSystemPreferences(pane?: SystemPreferencesWindowsPane): Promise; +export function openSystemPreferences(pane?: T, section?: SystemPreferencesMacOsPanes[T]): Promise; diff --git a/index.test-d.ts b/index.test-d.ts index e42942f..7a2a11a 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -69,4 +69,5 @@ expectType(appMenu([ expectType>(openSystemPreferences()); expectType>(openSystemPreferences('security', 'Privacy_Microphone')); +expectType>(openSystemPreferences('windowsupdate')); expectError(openSystemPreferences('security', 'Bad_Section')); diff --git a/readme.md b/readme.md index 7cab6a6..5597483 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -492,15 +492,17 @@ const menu = Menu.buildFromTemplate([ Menu.setApplicationMenu(menu); ``` -### openSystemPreferences(panel?, section?): Promise *macOS* +### openSystemPreferences(pane?, section?): Promise *macOS* *Windows* 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 @@ -514,7 +516,7 @@ const {openSystemPreferences} = require('electron-util'); openSystemPreferences('security'); ``` -#### section +#### section *macOS* Type: `string` diff --git a/source/open-system-preferences.js b/source/open-system-preferences.js index a8aebab..f5c1350 100644 --- a/source/open-system-preferences.js +++ b/source/open-system-preferences.js @@ -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}` : ''}`); };