- BrowserWindow·续2
- 事件: ‘maximize’
- 事件: ‘unmaximize’
- 事件: ‘minimize’
- 事件: ‘restore’
- 事件: ‘will-resize’ macOS Windows
- 事件: ‘resize’
- Event: ‘will-move’ macOS Windows
- 事件: ‘move’
- 事件: ‘moved’ macOS
- 事件: ‘enter-full-screen’
- 事件: ‘leave-full-screen’
- 事件: ‘enter-html-full-screen’
- 事件: ‘leave-html-full-screen’
- Event: ‘always-on-top-changed’
- 事件: ‘app-command’ Windows__Linux
- 事件: ‘scroll-touch-begin’ macOS
- 事件: ‘scroll-touch-end’ macOS
- 事件: ‘scroll-touch-edge’ macOS
- 事件: ‘swipe’ macOS
- Event: ‘rotate-gesture’ macOS
- 事件: ‘sheet-begin’ macOS
- 事件: ‘sheet-end’ macOS
- 事件: ‘new-window-for-tab’ macOS
- 静态方法
- BrowserWindow.getAllWindows()
- BrowserWindow.getFocusedWindow()
- BrowserWindow.fromWebContents(webContents)
- BrowserWindow.fromBrowserView(browserView)
- BrowserWindow.fromId(id)
- BrowserWindow.addExtension(path)
- BrowserWindow.removeExtension(name)
- BrowserWindow.getExtensions()
- BrowserWindow.addDevToolsExtension(path)
- BrowserWindow.removeDevToolsExtension(name)
- BrowserWindow.getDevToolsExtensions()
- 实例属性
- 实例方法
BrowserWindow·续2
Please note that using this event implies that the renderer will be considered “visible” and paint even though show is false. 如果您使用 paintWhenInitiallyHidden: false,此事件将永远不会被触发。
事件: ‘maximize’
窗口最大化时触发
事件: ‘unmaximize’
当窗口从最大化状态退出时触发
事件: ‘minimize’
窗口最小化时触发
事件: ‘restore’
当窗口从最小化状态恢复时触发
事件: ‘will-resize’ macOS Windows
返回:
-
event Event
-
newBounds Rectangle - Size the window is being resized to.
Emitted before the window is resized. Calling event.preventDefault() will prevent the window from being resized.
Note that this is only emitted when the window is being resized manually. Resizing the window with setBounds/setSize will not emit this event.
事件: ‘resize’
调整窗口大小后触发。
Event: ‘will-move’ macOS Windows
返回:
-
event Event
-
newBounds Rectangle - Location the window is being moved to.
Emitted before the window is moved. On Windows, calling event.preventDefault() will prevent the window from being moved.
Note that this is only emitted when the window is being resized manually. Resizing the window with setBounds/setSize will not emit this event.
事件: ‘move’
窗口移动到新位置时触发
注意: 在 macOS 上,此事件是moved的别名.
事件: ‘moved’ macOS
当窗口移动到新位置时触发一次
事件: ‘enter-full-screen’
窗口进入全屏状态时触发
事件: ‘leave-full-screen’
窗口离开全屏状态时触发
事件: ‘enter-html-full-screen’
窗口进入由HTML API 触发的全屏状态时触发
事件: ‘leave-html-full-screen’
窗口离开由HTML API触发的全屏状态时触发
Event: ‘always-on-top-changed’
返回:
-
event Event
-
isAlwaysOnTop Boolean
设置或取消设置窗口总是在其他窗口的顶部显示时触发。
事件: ‘app-command’ Windows__Linux
返回:
-
event Event
-
command String
请求一个应用程序命令.aspx)时触发. 典型的是键盘上的媒体键或浏览器命令, 以及在Windows上的一些鼠标中内置的“后退”按钮。
命令是小写的,下划线替换为连字符,以及APPCOMMAND_ 前缀将被删除。 例如 APPCOMMAND_BROWSER_BACKWARD将被browser-backward触发.
const { BrowserWindow } = require('electron')let win = new BrowserWindow()win.on('app-command', (e, cmd) => { // 当用户点击鼠标返回按钮时,导航窗口会后退 if (cmd === 'browser-backward' && win.webContents.canGoBack()) { win.webContents.goBack() }})
The following app commands are explicitly supported on Linux:
-
browser-backward
-
browser-forward
事件: ‘scroll-touch-begin’ macOS
滚轮事件阶段开始时触发
事件: ‘scroll-touch-end’ macOS
滚轮事件阶段结束时触发
事件: ‘scroll-touch-edge’ macOS
滚轮事件阶段到达元素边缘时触发
事件: ‘swipe’ macOS
返回:
-
event Event
-
direction String
Emitted on 3-finger swipe. Possible directions are up, right, down, left.
The method underlying this event is built to handle older macOS-style trackpad swiping, where the content on the screen doesn’t move with the swipe. Most macOS trackpads are not configured to allow this kind of swiping anymore, so in order for it to emit properly the ‘Swipe between pages’ preference in System Preferences > Trackpad > More Gestures must be set to ‘Swipe with two or three fingers’.
Event: ‘rotate-gesture’ macOS
返回:
-
event Event
-
rotation Float
Emitted on trackpad rotation gesture. Continually emitted until rotation gesture is ended. The rotation value on each emission is the angle in degrees rotated since the last emission. The last emitted event upon a rotation gesture will always be of value 0. Counter-clockwise rotation values are positive, while clockwise ones are negative.
事件: ‘sheet-begin’ macOS
窗口打开sheet(工作表) 时触发
事件: ‘sheet-end’ macOS
窗口关闭sheet(工作表) 时触发
事件: ‘new-window-for-tab’ macOS
当点击了系统的新标签按钮时触发
静态方法
BrowserWindow 类有以下方法:
BrowserWindow.getAllWindows()
返回 BrowserWindow[] - 所有打开的窗口的数组
BrowserWindow.getFocusedWindow()
返回 BrowserWindow | null - 此应用程序中当前获得焦点的窗口,如果无就返回 null.
BrowserWindow.fromWebContents(webContents)
- webContents WebContents
Returns BrowserWindow | null - The window that owns the given webContents or null if the contents are not owned by a window.
BrowserWindow.fromBrowserView(browserView)
- browserView BrowserView
Returns BrowserWindow | null - The window that owns the given browserView. If the given view is not attached to any window, returns null.
BrowserWindow.fromId(id)
- id Integer
返回 BrowserWindow -拥有给定 id 的窗口.
BrowserWindow.addExtension(path)
- path String
添加位于 path的扩展,并且返回扩展名
该方法如果扩展的 manifest 缺失或不完整,该方法不会返回。
注意: 该 API 不能在 app 模块的 ready 事件之前调用.
BrowserWindow.removeExtension(name)
- name 字符串
根据名字删除一个 Chrome 的扩展。
注意: 该 API 不能在 app 模块的 ready 事件之前调用.
BrowserWindow.getExtensions()
Returns Record<String, ExtensionInfo> - The keys are the extension names and each value is an Object containing name and version properties.
注意: 该 API 不能在 app 模块的 ready 事件之前调用.
BrowserWindow.addDevToolsExtension(path)
- path String
添加位于 path的 DevTools 扩展,并且返回扩展名
扩展将被记住, 所以你只需要调用这个API一次, 这个API不是用于编程使用. 如果尝试添加已经加载的扩展, 此方法将不会返回, 而是会向控制台记录警告.
该方法如果扩展的 manifest 缺失或不完整,该方法不会返回。
注意: 该 API 不能在 app 模块的 ready 事件之前调用.
BrowserWindow.removeDevToolsExtension(name)
- name 字符串
根据名字删除一个 DevTools 的扩展。
注意: 该 API 不能在 app 模块的 ready 事件之前调用.
BrowserWindow.getDevToolsExtensions()
Returns Record<string, ExtensionInfo> - The keys are the extension names and each value is an Object containing name and version properties.
要检查是否安装了 DevTools 扩展,您可以运行以下内容:
const { BrowserWindow } = require('electron')let installed = BrowserWindow.getDevToolsExtensions().hasOwnProperty('devtron')console.log(installed)
注意: 该 API 不能在 app 模块的 ready 事件之前调用.
实例属性
使用 new BrowserWindow 创建的对象具有以下属性:
const { BrowserWindow } = require('electron')// 在这个例子中,`win` 是我们的实例let win = new BrowserWindow({ width: 800, height: 600 })win.loadURL('https://github.com')
win.webContents Readonly
A WebContents object this window owns. All web page related events and operations will be done via it.
有关它的方法和事件, 请参见 webContents documentation
win.id Readonly
A Integer property representing the unique ID of the window.
win.autoHideMenuBar
A Boolean property that determines whether the window menu bar should hide itself automatically. Once set, the menu bar will only show when users press the single Alt key.
If the menu bar is already visible, setting this property to true won’t hide it immediately.
win.minimizable
A Boolean property that determines whether the window can be manually minimized by user.
On Linux the setter is a no-op, although the getter returns true.
win.maximizable
A Boolean property that determines whether the window can be manually maximized by user.
On Linux the setter is a no-op, although the getter returns true.
win.fullScreenable
A Boolean property that determines whether the maximize/zoom window button toggles fullscreen mode or maximizes the window.
win.resizable
A Boolean property that determines whether the window can be manually resized by user.
win.closable
A Boolean property that determines whether the window can be manually closed by user.
On Linux the setter is a no-op, although the getter returns true.
win.movable
A Boolean property that determines Whether the window can be moved by user.
On Linux the setter is a no-op, although the getter returns true.
win.excludedFromShownWindowsMenu macOS
A Boolean property that determines whether the window is excluded from the application’s Windows menu. false by default.
const win = new BrowserWindow({ height: 600, width: 600 })const template = [ { role: 'windowmenu' }]win.excludedFromShownWindowsMenu = trueconst menu = Menu.buildFromTemplate(template)Menu.setApplicationMenu(menu)
win.accessibleTitle
A String property that defines an alternative title provided only to accessibility tools such as screen readers. This string is not directly visible to users.
实例方法
使用 new BrowserWindow创建的对象具有以下实例方法:
注意: 某些方法仅在特定的操作系统上可用, 这些方法会被标记出来。
win.destroy()
强制关闭窗口, 除了closed之外,close,unload 和 beforeunload 都不会被触发
win.close()
Try to close the window. This has the same effect as a user manually clicking the close button of the window. The web page may cancel the close though. See the close event.
win.focus()
聚焦于窗口
