获取系统首选项
获取系统首选项
进程: 主进程 例子:
const {systemPreferences} = require('electron')
console.log(systemPreferences.isDarkMode())事件列表
systemPreferences 对象提供以下事件:
事件: 'accent-color-changed' Windows
触发:用户在个性化中更改了系统基本色调时
eventEventnewColorString
事件: 'color-changed' Windows
触发:用户在个性化中更改了系统颜色时
eventEvent
事件: 'inverted-color-scheme-changed' Windows
触发:用户切换为反色方案(高对比度主题等)时
eventEventinvertedColorSchemeBoolean -true代表的是正在使用反色方案, 否则为false.
方法列表
systemPreferences.isDarkMode() macOS
systemPreferences.isDarkMode() macOS用途:判断系统是否处于深色或黑暗模式(
Boolean)
systemPreferences.isSwipeTrackingFromScrollEventsEnabled() macOS
systemPreferences.isSwipeTrackingFromScrollEventsEnabled() macOS用途:判断系统是否开启页面间滑动功能(
Boolean)
systemPreferences.postNotification(event, userInfo) macOS
systemPreferences.postNotification(event, userInfo) macOS用途:在 macOS 上使用系统通知来发布
event
eventStringuserInfoObject 包含了用户向通知发送的信息字典
systemPreferences.postLocalNotification(event, userInfo) macOS
systemPreferences.postLocalNotification(event, userInfo) macOS用途:在 macOS 上使用系统本地通知来发布
event
eventStringuserInfoObject 包含了用户向通知发送的信息字典
systemPreferences.subscribeNotification(event, callback) macOS
systemPreferences.subscribeNotification(event, callback) macOS用途:订阅macOS的本地通知并调用
callback(event, userInfo)返回订阅者的id
eventStringcallbackFunctioneventStringuserInfoObject 包含了用户向通知发送的信息字典
订阅者的 id 可用于取消订阅 event 。
这个API在底层上订阅于 NSDistributedNotificationCenter, event 的参考值如下:
AppleInterfaceThemeChangedNotificationAppleAquaColorVariantChangedAppleColorPreferencesChangedNotificationAppleShowScrollBarsSettingChanged
systemPreferences.unsubscribeNotification(id) macOS
systemPreferences.unsubscribeNotification(id) macOS用途:使用
id删除订阅
idInteger
systemPreferences.subscribeLocalNotification(event, callback) macOS
systemPreferences.subscribeLocalNotification(event, callback) macOS用途:订阅macOS的本地通知并调用
callback(event, userInfo)返回订阅者的id,但从NSNotificationCenter订阅
eventStringcallbackFunctioneventStringuserInfoObject
对于诸如 NSUserDefaultsDidChangeNotification的事件是必需的.
systemPreferences.unsubscribeLocalNotification(id) macOS
systemPreferences.unsubscribeLocalNotification(id) macOS用途:使用
id删除订阅,但从NSNotificationCenter删除
idInteger
systemPreferences.getUserDefault(key, type) macOS
systemPreferences.getUserDefault(key, type) macOS用途:获取系统偏好中相应
key的值
keyStringtypeString - 类型,可选string,boolean,integer,float,double,url,array,dictionary
这个API在macOS上使用的是 NSUserDefaults 。以下是部分常用的 key 和 type:
AppleInterfaceStyle:stringAppleAquaColorVariant:integerAppleHighlightColor:stringAppleShowScrollBars:stringNSNavRecentPlaces:arrayNSPreferredWebServices:dictionaryNSUserDictionaryReplacementItems:array
systemPreferences.setUserDefault(key, type, value) macOS
systemPreferences.setUserDefault(key, type, value) macOS用途:设置系统偏好中相应
key的值
keyStringtypeString - 类型,可选string,boolean,integer,float,double,url,array,dictionaryvalueString
需要注意的是 type 需要与实际的类型的 value 对应。不然会抛出一个异常。
这个API在macOS上使用的是 NSUserDefaults 。以下是部分常用的 key 和 type:
ApplePressAndHoldEnabled:boolean
systemPreferences.isAeroGlassEnabled() Windows
systemPreferences.isAeroGlassEnabled() Windows用途:判断是否开启了毛玻璃效果(
Boolean)
使用它来确定是否可创建透明窗口的示例(因为DWM组合被禁用时,透明窗口将无法正常工作):
systemPreferences.getAccentColor() Windows
systemPreferences.getAccentColor() Windows用途:获取当前系统范围的16进制 RGBA 色值(
String)
systemPreferences.getColor(color) Windows
systemPreferences.getColor(color) Windows用途:获取当前系统范围的16进制 RGB 色值.如
#ABCDEF(String)
colorString - 可选值:3d-dark-shadow- 3D暗影颜色。3d-face- 3D边框或对话框背景色。3d-highlight- 高亮的3D元素颜色。3d-light- 3D 元素亮色。3d-shadow- 3D 元素的阴影颜色。active-border- 活动窗口边框。active-caption- 活动窗口标题栏。如果启用渐变效果,则指定活动窗口标题栏的颜色渐变中的左侧颜色。active-caption-gradient- 活动窗口标题栏颜色渐变中的右侧颜色。app-workspace- 多文档(MDI)应用界面背景颜色。button-text- 按钮上的文本。caption-text- 标题栏,尺寸框,滚动条尖头框上的文本。desktop- 桌面背景颜色。disabled-text- 灰色(禁止的)文本。highlight- 在控件中选择的项目。highlight-text- 在控件中选择的项目的文本。hotlight- 超链接或热跟踪项的颜色。inactive-border- 非活动窗口边框。inactive-caption- 非活动窗口标题。如果启用渐变效果,则指定非活动窗口标题栏的颜色渐变中的左侧颜色。非活动窗口的标题栏。inactive-caption-text- 无效标题文本的颜色。info-background- 工具提示控件的背景颜色。info-text- 工具提示控件的文本颜色。menu- 菜单背景。menu-highlight- 当菜单作为平面菜单时高亮的菜单颜色。menubar- 当菜单作为平面菜单时菜单栏背景颜色。menu-text- 菜单中的文本。scrollbar- 滚动条灰色区域。window- 窗口背景。window-frame- 窗口框架。window-text- 在窗口中的文本。
详见 [Windows docs][windows-colors] 获取更多细节。
systemPreferences.isInvertedColorScheme() Windows
systemPreferences.isInvertedColorScheme() Windows用途:判断系统是否处于反色方案(高对比度主题等)(
Boolean)
[windows-colors]:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724371(v=vs.85).aspx
Last updated
Was this helpful?