BrowserWindow
BrowserWindow
创建和控制浏览器窗口。
进程:主进程
// 在主进程中.const { BrowserWindow } = require('electron')// 或者从渲染进程中使用 `remote`.// const { BrowserWindow } = require('electron').remotelet win = new BrowserWindow({ width: 800, height: 600 })win.on('closed', () => { win = null})// 加载远程URLwin.loadURL('https://github.com')// 或加载本地HTML文件win.loadURL(`file://${__dirname}/app/index.html`)
无边框窗口
如果想创建一个无边框或者任意形状的透明窗口,你可以使用Frameless Window 的API
优雅地显示窗口
当页面在窗口中直接加载时,用户会看到未完成的页面,这不是一个好的原生应用的体验。为了让画面显示时没有视觉闪烁,有两种不同的解决方案。
使用ready-to-show事件
在加载页面时,渲染进程第一次完成绘制时,如果窗口还没有被显示,渲染进程会发出 ready-to-show 事件 。 在此事件后显示窗口将没有视觉闪烁:
const { BrowserWindow } = require('electron')let win = new BrowserWindow({ show: false })win.once('ready-to-show', () => { win.show()})
这个事件通常在 did-finish-load 事件之后发出,但是页面有许多远程资源时,它可能会在 did-finish-load之前发出事件。
Please note that using this event implies that the renderer will be considered “visible” and paint even though show is false. 如果您使用 paintWhenInitiallyHidden: false,此事件将永远不会被触发。
设置 backgroundColor
对于一个复杂的应用,ready-to-show 可能发出的太晚,会让应用感觉缓慢。 在这种情况下,建议立刻显示窗口,并使用接近应用程序背景的 backgroundColor
const { BrowserWindow } = require('electron')let win = new BrowserWindow({ backgroundColor: '#2e2c29' })win.loadURL('https://github.com')
请注意,即使是使用 ready-to-show 事件的应用程序,仍建议使用设置 backgroundColor 使应用程序感觉更原生。
父子窗口
通过使用 parent 选项,你可以创建子窗口:
const { BrowserWindow } = require('electron')let top = new BrowserWindow()let child = new BrowserWindow({ parent: top })child.show()top.show()
child 窗口将总是显示在 top 窗口的顶部.
模态窗口
模态窗口是禁用父窗口的子窗口,创建模态窗口必须设置 parent 和 modal 选项:
const { BrowserWindow } = require('electron')let child = new BrowserWindow({ parent: top, modal: true, show: false })child.loadURL('https://github.com')child.once('ready-to-show', () => { child.show()})
页面可见性
页面可见性 API 的工作方式如下:
-
在所有平台上, 可见性状态与窗口是否隐藏/最小化相关。
-
此外, 在 macOS 上, 可见性状态还会跟踪窗口的遮挡状态。 如果窗口被另一个窗口完全遮挡了,可见性状态为hidden. 在其他平台上,可见性状态只有在使用 win.hide()使窗口最小化或者隐藏时才为 hidden
-
如果创建BrowserWindow 时带有 show: false的参数, 最初的可见性状态将为visible 尽管窗口实际上是隐藏的。
-
如果backgroundThrottling被禁用,可见性状态将保持为visible 即使窗口被最小化、遮挡或隐藏。
推荐您在可见性状态为 hidden 时暂停消耗资源的操作以便减少电力消耗。
平台相关的提示
-
在 macOS 上,modal 窗口将显示为附加到父窗口的工作表。
-
在 macOS 上,子窗口将保持与父窗口的相对位置。而在 Windows 和 Linux 中,当父窗口移动时子窗口不会移动。
-
在Linux上,模态窗口的类型将更改为 dialog.
-
在Linux上,许多桌面环境不支持隐藏模态窗口。
Class: BrowserWindow
创建和控制浏览器窗口。
进程:主进程
BrowserWindow 是一个EventEmitter.
通过 options 可以创建一个具有原生属性的 BrowserWindow 。
new BrowserWindow([options])
-
options Object (optional)
-
width Integer (optional) - Window’s width in pixels. Default is 800.
-
height Integer (optional) - Window’s height in pixels. Default is 600.
-
x Integer (optional) - (required if y is used) Window’s left offset from screen. Default is to center the window.
-
y Integer (optional) - (required if x is used) Window’s top offset from screen. Default is to center the window.
-
useContentSize Boolean (可选) - width 和 height 将设置为 web 页面的尺寸(译注: 不包含边框), 这意味着窗口的实际尺寸将包括窗口边框的大小,稍微会大一点。 默认值为 false.
-
center Boolean (可选) - 窗口在屏幕居中.
-
minWidth Integer (optional) - Window’s minimum width. Default is 0.
-
minHeight Integer (optional) - Window’s minimum height. Default is 0.
-
maxWidth Integer (optional) - Window’s maximum width. Default is no limit.
-
maxHeight Integer (optional) - Window’s maximum height. Default is no limit.
-
resizable Boolean (optional) - Whether window is resizable. 默认值为 true。
-
movable Boolean (optional) - Whether window is movable. This is not implemented on Linux. 默认值为 true。
-
minimizable Boolean (optional) - Whether window is minimizable. This is not implemented on Linux. 默认值为 true。
-
maximizable Boolean (optional) - Whether window is maximizable. This is not implemented on Linux. 默认值为 true。
-
closable Boolean (optional) - Whether window is closable. This is not implemented on Linux. 默认值为 true。
-
focusable Boolean (可选) - 窗口是否可以聚焦. 默认值为 true。 在 Windows 中设置 focusable: false 也意味着设置了skipTaskbar: true. 在 Linux 中设置 focusable: false 时窗口停止与 wm 交互, 并且窗口将始终置顶。
-
alwaysOnTop Boolean (optional) - Whether the window should always stay on top of other windows. 默认值为 false.
-
fullscreen Boolean (可选) - 窗口是否全屏. 当明确设置为 false 时,在 macOS 上全屏的按钮将被隐藏或禁用. 默认值为 false.
-
fullscreenable Boolean (可选) - 窗口是否可以进入全屏状态. 在 macOS上, 最大化/缩放按钮是否可用 默认值为 true。
-
simpleFullscreen Boolean (optional) - Use pre-Lion fullscreen on macOS. 默认值为 false.
-
skipTaskbar Boolean (optional) - Whether to show the window in taskbar. Default is false.
-
kiosk Boolean (optional) - The kiosk mode. 默认值为 false.
-
titleString(可选) - 默认窗口标题 默认为"Electron"。 如果由loadURL()加载的HTML文件中含有标签<title>,此属性将被忽略。
-
icon (NativeImage | String) (可选) - 窗口的图标. 在 Windows 上推荐使用 ICO 图标来获得最佳的视觉效果, 默认使用可执行文件的图标.
-
show Boolean (optional) - Whether window should be shown when created. 默认值为 true。
-
paintWhenInitiallyHiddenBoolean(可选) - 当show为false并且渲染器刚刚被创建时,它是否应激活。 为了让document.visibilityState 在show: false的情况下第一次加载时正确地工作,你应该把这个设置成false. 设置为 false 将会导致ready-to-show 事件不触发。 默认值为 true。
-
frame Boolean (optional) - Specify false to create a Frameless Window. 默认值为 true。
-
parent BrowserWindow (optional) - Specify parent window. Default is null.
-
modal Boolean (optional) - Whether this is a modal window. This only works when the window is a child window. 默认值为 false.
-
acceptFirstMouse Boolean (optional) - Whether the web view accepts a single mouse-down event that simultaneously activates the window. Default is false.
-
disableAutoHideCursor Boolean (optional) - Whether to hide cursor when typing. 默认值为 false.
-
autoHideMenuBar Boolean (optional) - Auto hide the menu bar unless the Alt key is pressed. 默认值为 false.
-
enableLargerThanScreen Boolean (可选) - 是否允许改变窗口的大小使之大于屏幕的尺寸. 仅适用于 macOS,因为其它操作系统默认允许 大于屏幕的窗口。 默认值为 false.
-
backgroundColor String(可选) - 窗口的背景颜色为十六进制值,例如#66CD00, #FFF, #80FFFFFF (设置transparent为true方可支持alpha属性,格式为#AARRGGBB)。 默认值为 #FFF(白色)。
-
hasShadow Boolean (optional) - Whether window should have a shadow. 默认值为 true。
-
opacity Number (optional) - Set the initial opacity of the window, between 0.0 (fully transparent) and 1.0 (fully opaque). This is only implemented on Windows and macOS.
-
darkTheme Boolean (optional) - Forces using dark theme for the window, only works on some GTK+3 desktop environments. 默认值为 false.
-
transparent Boolean (optional) - Makes the window transparent. 默认值为 false. On Windows, does not work unless the window is frameless.
-
type String (optional) - The type of window, default is normal window. See more about this below.
-
titleBarStyle String (optional) - The style of window title bar. Default is default. Possible values are:
-
default - 标准灰色不透明的Mac标题栏
-
hidden - 隐藏标题栏, 内容充满整个窗口, 但它依然在左上角, 仍然受标准窗口控制.
-
hiddenInset - 隐藏标题栏, 显示小的控制按钮在窗口边缘
-
customButtonsOnHover Boolean (可选) - 在macOS的无框窗口上绘制自定义的关闭与最小化按钮. 除非鼠标悬停到窗口的左上角, 否则这些按钮不会显示出来. 这些自定义的按钮能防止, 与发生于标准的窗口工具栏按钮处的鼠标事件相关的问题. 注意: 此选项目前是实验性的。
-
trafficLightPosition Point (optional) - Set a custom position for the traffic light buttons. Can only be used with titleBarStyle set to hidden
-
fullscreenWindowTitle Boolean (optional) - Shows the title in the title bar in full screen mode on macOS for all titleBarStyle options. 默认值为 false.
-
thickFrame Boolean(可选)-对 Windows 上的无框窗口使用WS_THICKFRAME 样式,会增加标准窗口框架。 设置为 false 时将移除窗口的阴影和动画. 默认值为 true。
-
vibrancy String (可选) - 窗口是否使用 vibrancy 动态效果, 仅 macOS 中有效. Can be appearance-based, light, dark, titlebar, selection, menu, popover, sidebar, medium-light, ultra-dark, header, sheet, window, hud, fullscreen-ui, tooltip, content, under-window, or under-page. Please note that using frame: false in combination with a vibrancy value requires that you use a non-default titleBarStyle as well. Also note that appearance-based, light, dark, medium-light, and ultra-dark have been deprecated and will be removed in an upcoming version of macOS.
