From 2fb8839d3143b3861c0a18630c98e51cb8e14193 Mon Sep 17 00:00:00 2001 From: longbotao Date: Tue, 20 Dec 2016 12:03:59 +0800 Subject: [PATCH 1/2] [ci skip]add systemPreferences API Simplified Chinese translation --- docs-translations/zh-CN/README.md | 2 +- .../zh-CN/api/system-preferences.md | 215 ++++++++++++++++++ 2 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 docs-translations/zh-CN/api/system-preferences.md diff --git a/docs-translations/zh-CN/README.md b/docs-translations/zh-CN/README.md index e46ce19c2a00..e223c020ec5e 100644 --- a/docs-translations/zh-CN/README.md +++ b/docs-translations/zh-CN/README.md @@ -61,7 +61,7 @@ * [powerSaveBlocker](api/power-save-blocker.md) * [protocol](api/protocol.md) * [session](api/session.md) -* [systemPreferences](api/system-preferences.md) 未翻译 +* [systemPreferences](api/system-preferences.md) * [Tray](api/tray.md) * [webContents](api/web-contents.md) diff --git a/docs-translations/zh-CN/api/system-preferences.md b/docs-translations/zh-CN/api/system-preferences.md new file mode 100644 index 000000000000..7182e33e7614 --- /dev/null +++ b/docs-translations/zh-CN/api/system-preferences.md @@ -0,0 +1,215 @@ +# systemPreferences + +> 获取系统偏好设置. + +可使用的进程: [主进程](../tutorial/quick-start.md#main-process) + +```javascript +const {systemPreferences} = require('electron') +console.log(systemPreferences.isDarkMode()) +``` + +## 事件 + +`systemPreferences` 对象会触发以下事件: + +### Event: 'accent-color-changed' _Windows_ + +返回: + +* `event` Event +* `newColor` String - 用户给系统颜色设置的新的 RGBA 色值。 + +### Event: 'color-changed' _Windows_ + +返回: + +* `event` Event + +### Event: 'inverted-color-scheme-changed' _Windows_ + +返回: + +* `event` Event +* `invertedColorScheme` Boolean - 如果一个反色的配色方案正在被使用,比如一个高对比度的主题,则返回 `true` ,否则返回 `false` 。 + +## 方法 + +### `systemPreferences.isDarkMode()` _macOS_ + +返回 `Boolean` - 系统是否处于深色模式. + +### `systemPreferences.isSwipeTrackingFromScrollEventsEnabled()` _macOS_ + +返回 `Boolean` - 是否开启页面间滑动功能。 + +### `systemPreferences.postNotification(event, userInfo)` _macOS_ + +* `event` String +* `userInfo` Object + +在 macOS 上使用原生系统通知来发布 `event` 。`userInfo` 是包含了用户向通知发送的信息字典的一个对象。 + +### `systemPreferences.postLocalNotification(event, userInfo)` _macOS_ + +* `event` String +* `userInfo` Object + +在 macOS 上使用原生系统通知来发布 `event` 。`userInfo` 是包含了用户向通知发送的信息字典的一个对象。 + +### `systemPreferences.subscribeNotification(event, callback)` _macOS_ + +* `event` String +* `callback` Function + * `event` String + * `userInfo` Object + +在 macOS 上订阅一个原生消息。当相关 `event` 发生, `callback` 将会被 `callback(event, userInfo)` 调用。`userInfo` 是包含了用户向通知发送的信息字典的一个对象。 + +订阅者的 `id` 将会被返回,它能够用于取消订阅相关 `event` 。 + +这个API在底层上订阅于 `NSDistributedNotificationCenter`, + `event` 的一些实例值如下: + +* `AppleInterfaceThemeChangedNotification` +* `AppleAquaColorVariantChanged` +* `AppleColorPreferencesChangedNotification` +* `AppleShowScrollBarsSettingChanged` + +### `systemPreferences.unsubscribeNotification(id)` _macOS_ + +* `id` Integer + +移除当前 `id` 下的订阅。 + +### `systemPreferences.subscribeLocalNotification(event, callback)` _macOS_ + +* `event` String +* `callback` Function + * `event` String + * `userInfo` Object + +大体上如同 `subscribeNotification` ,但默认使用本地 `subscribeNotification` 。一些事件必须使用它,比如 `NSUserDefaultsDidChangeNotification` 。 + +### `systemPreferences.unsubscribeLocalNotification(id)` _macOS_ + +* `id` Integer + +大体上如同 `unsubscribeNotification` , 但是是从 `NSNotificationCenter` 中移除订阅者。 + +### `systemPreferences.getUserDefault(key, type)` _macOS_ + +* `key` String +* `type` String - Can be `string`, `boolean`, `integer`, `float`, `double`, + `url`, `array`, `dictionary` + +获取系统偏好中相应的 `key` 的值。 + +这个 API 在 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`][#systempreferencesgetuserdefaultkey-type-macos] +* `value` String + +设置系统偏好中相应的 `key` 的值。 + +需要注意的是 `type` 需要与实际的类型的 `value` 对应。不然会抛出一个异常。 + +这个 API 在 macOS 系统中使用的是 `NSUserDefaults` 。以下是一些热门的 `key` 和 `type`: + +* `ApplePressAndHoldEnabled`: `boolean` + +### `systemPreferences.isAeroGlassEnabled()` _Windows_ + +如果 [DWM composition][dwm-composition] (毛玻璃效果)开启则会返回 `true` 否则返回 `false` 。 + +以下是一个实例去使用它来确定是否应该创建一个透明的窗口(透明的窗口在 DWM composition 禁止的情况下无法正确运行): + +```javascript +const {BrowserWindow, systemPreferences} = require('electron') +let browserOptions = {width: 1000, height: 800} + +// 如果平台支持的话便新建一个透明的窗口。 +if (process.platform !== 'win32' || systemPreferences.isAeroGlassEnabled()) { + browserOptions.transparent = true + browserOptions.frame = false +} + +// 创建窗口。 +let win = new BrowserWindow(browserOptions) + +// 区分环境进行加载。 +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`) +} +``` + +[dwm-composition]:https://msdn.microsoft.com/en-us/library/windows/desktop/aa969540.aspx + +### `systemPreferences.getAccentColor()` _Windows_ + +返回 `String` - 用户当前系统颜色偏好的16进制 RGBA 色值。 + +```js +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 肤色,以及对话框背景。 + * `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-gradient` - 不活跃窗口标题栏中右侧色彩梯度。 + * `inactive-caption-text` - 不活跃的标题文本。 + * `info-background` - 工具栏背景。 + * `info-text` - 工具栏文本。 + * `menu` - 菜单背景。 + * `menu-highlight` - 当菜单作为平面菜单时高亮的菜单颜色。 + * `menubar` - 当菜单作为平面菜单时菜单栏背景。 + * `menu-text` - 菜单文本。 + * `scrollbar` - 滚动条灰色区域。 + * `window` - 窗口背景。 + * `window-frame` - 窗口框架。 + * `window-text` - 窗口内文本。 + +返回 `String` - 系统设置色值的16进制形式(`#ABCDEF`)。 +详见 [Windows docs][windows-colors] 获取更多细节。 + +### `systemPreferences.isInvertedColorScheme()` _Windows_ + +返回 `Boolean` - 如果一个反色的配色方案正在被使用,比如一个高对比度的主题,则返回 `true` ,否则返回 `false` 。 + +[windows-colors]:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724371(v=vs.85).aspx From e3b23842dd9685816f67a902c1e757e12436629e Mon Sep 17 00:00:00 2001 From: x-yao Date: Tue, 20 Dec 2016 17:03:32 +0800 Subject: [PATCH 2/2] [ci skip]add net API Simplified Chinese translation --- docs-translations/zh-CN/README.md | 1 + docs-translations/zh-CN/api/net.md | 54 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 docs-translations/zh-CN/api/net.md diff --git a/docs-translations/zh-CN/README.md b/docs-translations/zh-CN/README.md index e223c020ec5e..8c1921f8fd2f 100644 --- a/docs-translations/zh-CN/README.md +++ b/docs-translations/zh-CN/README.md @@ -57,6 +57,7 @@ * [ipcMain](api/ipc-main.md) * [Menu](api/menu.md) * [MenuItem](api/menu-item.md) +* [net](api/net.md) * [powerMonitor](api/power-monitor.md) * [powerSaveBlocker](api/power-save-blocker.md) * [protocol](api/protocol.md) diff --git a/docs-translations/zh-CN/api/net.md b/docs-translations/zh-CN/api/net.md new file mode 100644 index 000000000000..a7575086f8d8 --- /dev/null +++ b/docs-translations/zh-CN/api/net.md @@ -0,0 +1,54 @@ +# net + +> 使用原生 Chromium 的 networking 库来解决 HTTP/HTTPS 请求问题。 + +可使用的进程: [主进程](../tutorial/quick-start.md#main-process) + +`net` 模块是一个客户端 API 用于解决 HTTP(S) 请求问题。它和 Node.js 中的 [HTTP](https://nodejs.org/api/http.html) 和 [HTTPS](https://nodejs.org/api/https.html) 模块比较相似,但是它使用了 Chromium 的原生 API 来替代 Node.js 的方案 ,相对而言更加适合 web 端的请求处理。 + +以下是部分简要的原因来阐述为什么你需要考虑使用 `net` 来替代 Node.js 的原生模块: + +* 自动管理系统代理设置,支持 wpad 协议和 pac 代理配置文件。 +* 自动使用隧道通过 HTTPS 请求。 +* 支持 basic, digest, NTLM, Kerberos or + negotiate 等身份验证模式作为身份验证代理。 +* 支持 traffic monitoring proxies: 类似于 Fiddler 的代理用于监控和操作网络请求。 + +`net` 模块的 API 在设计上特别的模仿了 Node.js 的 API 从而达到比较接近的体验。在 API 组件中包含了类,方法,属性还有事件等名称,这些都像是在使用 Node.js 的 API。 + +例如下面一个例子就快速展示了如何使用 `net` API : + +```javascript +const {app} = require('electron') +app.on('ready', () => { + const {net} = require('electron') + const request = net.request('https://github.com') + request.on('response', (response) => { + console.log(`STATUS: ${response.statusCode}`) + console.log(`HEADERS: ${JSON.stringify(response.headers)}`) + response.on('data', (chunk) => { + console.log(`BODY: ${chunk}`) + }) + response.on('end', () => { + console.log('No more data in response.') + }) + }) + request.end() +}) +``` + +也就是说,它对于你使用过 Node.js 的 [HTTP](https://nodejs.org/api/http.html)/[HTTPS](https://nodejs.org/api/https.html) 模块来说是非常的相似的。 + +`net` API 只有在应用触发 `ready` 事件后才可以使用。如果再 `ready` 事件前尝试使用会抛出一个错误。 + +## 方法 + +`net` 模块拥有以下方法: + +### `net.request(options)` + +* `options` (Object | String) - `ClientRequest` 的构造参数。 + +返回 `ClientRequest` + +创建一个 [`ClientRequest`](./client-request.md) 使用提供的参数来作为构造器。`net.request` 方法会根据设置中的相应的协议方来处理处理安全和不受信任的 HTTP 请求。 \ No newline at end of file