原文:https://github.com/electron/electron/blob/master/docs/api/system-preferences.md
譯者:Lin
獲取系統(tǒng)偏好設(shè)置
進(jìn)程:主進(jìn)程
const {systemPreferences} = require('electron')
console.log(systemPreferences.isDarkMode())
事件
systemPreferences
對(duì)象分發(fā)下面事件:
事件: 'accent-color-changed' Windows
返回值:
-
event
Event類型 -
newColor
String類型 - 用戶指定作為他們系統(tǒng)顏色的新的RGBA格式的顏色续挟。
事件: 'color-changed' Windows
返回值:
-
event
Event類型
事件: 'inverted-color-scheme-changed' Windows
返回值:
-
event
Event類型 -
invertedColorScheme
Boolean類型 - 如果是反向配色方案,例如一個(gè)高對(duì)比度的主題將要被使用,則這個(gè)值為true
碍岔。否則這個(gè)值為false
勇垛。
方法
systemPreferences.isDarkMode()
MacOS
返回值為Boolean
類型 - 系統(tǒng)是否是深色模式胜卤。
systemPreferences.isSwipeTrackingFromScrollEventsEnabled()
MacOS
返回值為Boolean
類型 - 是否開啟頁(yè)面之間的切換丧靡。
systemPreferences.postNotification(event, userInfo)
MacOS
-
event
String類型 -
userInfo
Object類型
將event
作為MacOS的本地通知發(fā)送祖能。userInfo
是一個(gè)和通知一起發(fā)送的包含了用戶信息字典的對(duì)象歉秫。
systemPreferences.postLocalNotification(event, userInfo)
MacOS
-
event
String類型 -
userInfo
Object類型
將event
作為MacOS的本地通知發(fā)送。userInfo
是一個(gè)和通知一起發(fā)送的包含了用戶信息字典的對(duì)象养铸。
systemPreferences.subscribeNotification(event, callback)
MacOS
-
event
String類型 -
callback
Function類型-
event
String類型 -
userInfo
Object類型
-
訂閱MacOS的本地通知雁芙,當(dāng)對(duì)應(yīng)的event
發(fā)生的時(shí)候callback
將會(huì)使用callback(event, userInfo)
被調(diào)用。userInfo
是一個(gè)和通知一起發(fā)送的包含了用戶信息字典的對(duì)象钞螟。
返回訂閱器的id
兔甘,可以用來(lái)取消訂閱event
。
這個(gè)接口的本質(zhì)是訂閱NSDistributedNotificationCenter
鳞滨,例如event
的值可以是:
AppleInterfaceThemeChangedNotification
AppleAquaColorVariantChanged
AppleColorPreferencesChangedNotification
AppleShowScrollBarsSettingChanged
systemPreferences.unsubscribeNotification(id)
MacOS
-
id
Integer類型
通過(guò)id
移除訂閱器洞焙。
systemPreferences.subscribeLocalNotification(event, callback)
MacOS
-
event
String類型 -
callback
Function類型-
event
String類型 -
userInfo
Object類型
-
同subscribeNotification
一樣,不過(guò)本地是默認(rèn)使用NSNotificationCenter
拯啦。這個(gè)對(duì)于事件是必要的澡匪,例如NSUserDefaultsDidChangeNotification
systemPreferences.unsubscribeLocalNotification(id)
MacOS
-
id
Integer類型
同unsubscribeNotification
一樣,不過(guò)是從NSNotificationCenter
中移除訂閱器褒链。
systemPreferences.getUserDefault(key, type)
MacOS
-
key
String類型 -
type
String類型 - 可以是string
唁情、boolean
、integer
甫匹、float
甸鸟、double
、url
兵迅、array
哀墓、dictionary
通過(guò)key
來(lái)獲取系統(tǒng)偏好設(shè)置里面對(duì)應(yīng)的值。
這個(gè)接口在MacOS中使用NSUserDefaults
喷兼。一些常用的key
和type
有:
-
AppleInterfaceStyle
:string
-
AppleAquaColorVariant
:integer
-
AppleHighlightColor
:string
-
AppleShowScrollBars
:string
-
NSNavRecentPlaces
:array
-
NSPreferredWebServices
:dictionary
-
NSUserDictionaryReplacementItems
:array
systemPreferences.setUserDefault(key, type, value)
MacOS
-
key
String類型 -
type
String類型 - 查看getUserDefault -
value
String類型
通過(guò)key
來(lái)設(shè)置系統(tǒng)偏好設(shè)置里面對(duì)應(yīng)的值篮绰。
請(qǐng)注意,type
應(yīng)該和value
的實(shí)際類型匹配季惯。如果不匹配將會(huì)拋出一個(gè)錯(cuò)誤吠各。An exception is thrown if they don't.
這個(gè)接口在MacOS中使用NSUserDefaults
。一些常用的key
和type
有:
-
ApplePressAndHoldEnabled
:boolean
systemPreferences.isAeroGlassEnabled()
Windows
如果DWM composition(毛玻璃效果)被激活勉抓,那么這個(gè)方法將返回true
贾漏,否則將返回false
。
一個(gè)使用這個(gè)接口的例子藕筋,來(lái)決定是否你需要?jiǎng)?chuàng)建一個(gè)透明窗口(當(dāng)DWM composition
被禁用的時(shí)候透明窗口將不能正常的工作):
const {BrowserWindow, systemPreferences} = require('electron')
let browserOptions = {width: 1000, height: 800}
// Make the window transparent only if the platform supports it.
if (process.platform !== 'win32' || systemPreferences.isAeroGlassEnabled()) {
browserOptions.transparent = true
browserOptions.frame = false
}
// Create the window.
let win = new BrowserWindow(browserOptions)
// Navigate.
if (browserOptions.transparent) {
win.loadURL(`file://${__dirname}/index.html`)
} else {
// No transparency, so we load a fallback that uses basic styles.
win.loadURL(`file://${__dirname}/fallback.html`)
}
systemPreferences.getAccentColor()
Windows
返回值為String
類型 - 十六進(jìn)制RGBA格式的用戶當(dāng)前系統(tǒng)顏色偏好纵散。
const color = systemPreferences.getAccentColor() // `"aabbccdd"`
const red = color.substr(0, 2) // "aa"
const green = color.substr(2, 2) // "bb"
const blue = color.substr(4, 2) // "cc"
const alpha = color.substr(6, 2) // "dd"
systemPreferences.getColor(color)
Windows
-
color
String類型 - 使用以下值之一:-
3d-dark-shadow
- 顯示的3D元素的深色陰影顏色。 -
3d-face
- 顯示的3D元素和對(duì)話框背景的表面顏色。 -
3d-highlight
- 顯示的3D元素的高亮顏色伍掀。 -
3d-light
- 顯示的3D元素的光亮的顏色掰茶。 -
3d-shadow
- 顯示的3D元素的陰影的顏色。 -
active-border
- 活動(dòng)窗口的邊框的顏色蜜笤。 -
active-caption
- 活動(dòng)窗口的標(biāo)題欄濒蒋。如果開啟了顏色梯度變化,那么這個(gè)值是活動(dòng)窗口標(biāo)題欄最左側(cè)的顏色把兔。 -
active-caption-gradient
- 活動(dòng)窗口標(biāo)題欄最右側(cè)的顏色沪伙。 -
app-workspace
- 多文檔界面(MDI)應(yīng)用的背景顏色。 -
button-text
- 推送按鈕的文字的顏色县好。 -
caption-text
- 標(biāo)題欄的文字围橡、縮放框和滾動(dòng)條剪頭框的顏色。 -
desktop
- 桌面背景顏色缕贡。 -
disabled-text
- 灰色(無(wú)效的)文本的顏色某饰。 -
highlight
- 控件中選中項(xiàng)的顏色。 -
highlight-text
- 控件中選中項(xiàng)的文本的顏色善绎。 -
hotlight
- 超鏈接或熱跟蹤項(xiàng)的顏色黔漂。 -
inactive-border
- 非活動(dòng)窗口邊框的顏色。 -
inactive-caption
- 非活動(dòng)窗口的標(biāo)題欄的顏色禀酱。如果開啟了顏色梯度變化炬守,那么這個(gè)值是活動(dòng)窗口標(biāo)題欄最左側(cè)的顏色。 -
inactive-caption-gradient
- 非活動(dòng)窗口標(biāo)題欄最右側(cè)的顏色剂跟。 -
inactive-caption-text
- 非活動(dòng)標(biāo)題欄的文字的顏色减途。 -
info-background
- 工具提示控件的背景顏色。 -
info-text
- 工具提示控件的文本顏色曹洽。 -
menu
- 菜單背景顏色鳍置。 -
menu-highlight
- 菜單欄為平面菜單時(shí)的菜單項(xiàng)的高亮顏色。 -
menubar
- 菜單欄為平面菜單時(shí)的背景顏色送淆。 -
menu-text
- 菜單的文字顏色税产。 -
scrollbar
- 滾動(dòng)條灰色區(qū)域的顏色。 -
window
- 窗口背景色偷崩。 -
window-frame
- 窗口框架顏色辟拷。 -
window-text
- 窗口內(nèi)的文本顏色。
-
返回值為String
類型 - 十六進(jìn)制的RGB格式(#ABCDEF
)的系統(tǒng)顏色設(shè)置阐斜。查看Windows docs獲取更詳細(xì)信息衫冻。
systemPreferences.isInvertedColorScheme()
Windows
返回值為Boolean
類型 - 如果是反向配色方案,例如一個(gè)高對(duì)比度的主題將要被使用谒出,則這個(gè)值為true
隅俘。否則這個(gè)值為false
邻奠。