做者:Kurosakijavascript
本节旨在汇总在开发Electron 窗口可能遇到的问题,作一个汇总,后续遇到问题会持续更新。html
const { BrowserWindow } = require('electron'); const win = new BrowserWindow(); win.loadURL('https://github.com');
使用new BrowserWindow() 建立出窗口,若是不做任何配置的话,窗口就会出现,默认是白色的;这个时候使用win.loadURL('https://github.com'),加载远程资源,窗口从新渲染,从而致使窗口出现闪烁。前端
解决方法:java
const { BrowserWindow } = require('electron'); const win = new BrowserWindow({ show:false }); win.loadURL('https://github.com'); win.once('ready-to-show',()=>{ win.show(); })
公司业务开发的Electron应用,是给老师用的,有些老师是那种老版本Window7,而且关闭了自动更新。node
解决办法:
安装最新版的.NET Framework
官方下载地址:https://dotnet.microsoft.com/download/dotnet-framework
相关issue: https://github.com/electron/electron/issues/25186git
macOS 关机会把全部的窗口关闭,若是存在github
// 窗口注册close事件 win.on('close',(event)=>{ event.preventDefault() // 阻止窗口关闭 })
这种代码,会致使阻止系统关机。web
Window
系统下,使用 hide()
方法,可能致使页面挂住不能用。致使这种场景能够是由于调用 hide()
以后不调用 show()
方法,而只是调用 restore()
方法,会致使页面挂住不能用。因此得在 restore()
方法以后添加 show()
方法macos
切换成新版的Electron,new BrowserWindow(options)配置更新,webPreferences中配置enableRemoteModule:truenpm
在建立窗口时,配置一下preload.js,代码以下:
function initTopDrag() { const topDiv = document.createElement('div') // 建立节点 topDiv.style.position = 'fixed' // 一直在顶部 topDiv.style.top = '0' topDiv.style.left = '0' topDiv.style.height = '20px' // 顶部20px才可拖动 topDiv.style.width = '100%' // 宽度100% topDiv.style.zIndex = '9999' // 悬浮于最外层 topDiv.style.pointerEvents = 'none' // 用于点击穿透 topDiv.style['-webkit-user-select'] = 'none' // 禁止选择文字 topDiv.style['-webkit-app-region'] = 'drag' // 拖动 document.body.appendChild(topDiv) // 添加节点 } window.addEventListener('DOMContentLoaded', function onDOMContentLoaded() { initTopDrag() })
Window系统下,菜单长的很丑,有2种方案能够隐藏菜单
配置preload.js,将通讯方法挂载到window
/** * 这个是用于窗口通讯例子的preload, * preload执行顺序在窗口js执行顺序以前 */ import { ipcRenderer, remote } from 'electron' const { argv } = require('yargs') const { BrowserWindow } = remote // 父窗口监听子窗口事件 ipcRenderer.on('communication-to-parent', (event, msg) => { alert(msg) }) const { parentWindowId } = argv if (parentWindowId !== 'undefined') { const parentWindow = BrowserWindow.fromId(parentWindowId as number) // 挂载到window // @ts-ignore window.send = (params: any) => { parentWindow.webContents.send('communication-to-parent', params) } }
建立窗口传入id
browserWindow.webContents.on('new-window', (event, url, frameName, disposition) => { event.preventDefault() // 在经过BrowserWindow建立窗口 const win = new BrowserWindow({ show:false, webPreferences: { preload:preload.js, additionalArguments:[`--parentWindow=${browserWindow.id}`] // 把父窗口的id传过去 } }); win.loadURl(url); win.once('ready-to-show',()=>{ win.show() }) })
没有上述那么麻烦,配置preload就能够,具体实现
import { remote, ipcRenderer } from 'electron' // 父窗口监听子窗口事件 ipcRenderer.on('communication-to-parent', (event, msg) => { alert(msg) }) const parentWindow = remote.getCurrentWindow().getParentWindow() // @ts-ignore window.sendToParent = (params: any) => parentWindow.webContents.send('communication-to-parent', params)
渲染进程通讯很简单,经过window.open,window.open会返回一个
windowObjectReference
经过postMessage就能够通讯了。而且window.open 支持传preload等配置。
使用按钮全屏和退出全屏是能够的,可是先点击左上角🚥全屏,再使用按钮退出全屏,是不行的。由于没法知道当前的状态是全屏,仍是不是全屏。
配置preload,使用Electron自带的全屏API
import { remote } from 'electron' const setFullScreen = remote.getCurrentWindow().setFullScreen const isFullScreen = remote.getCurrentWindow().isFullScreen window.setFullScreen = setFullScreen window.isFullScreen = isFullScreen
在macOS下,咱们启动Electron应用,是在Application文件夹的外面,运行在一个只读的disk image。基于安全的缘由,须要存在用户本身受权,electron-util作了一个很好的封装。可使用 electron-util
中的 enforceMacOSAppLocation
方法。该文档electron-util。
const { app, BrowserWindow } = require('electron') const { enforceMacOSAppLocation } = require('electron-util') function createWindow() { enforceMacOSAppLocation() const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, }, }) mainWindow.loadFile('index.html') } app.on('ready', createWindow) app.on('window-all-closed', function () { if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } })
enforceMacOSAppLocation 方法封装
'use strict'; const api = require('./api'); const is = require('./is'); module.exports = () => { if (is.development || !is.macos) { return; } if (api.app.isInApplicationsFolder()) { return; } const appName = 'name' in api.app ? api.app.name : api.app.getName(); const clickedButtonIndex = api.dialog.showMessageBoxSync({ type: 'error', message: 'Move to Applications folder?', detail: `${appName} must live in the Applications folder to be able to run correctly.`, buttons: [ 'Move to Applications folder', `Quit ${appName}` ], defaultId: 0, cancelId: 1 }); if (clickedButtonIndex === 1) { api.app.quit(); return; } api.app.moveToApplicationsFolder({ conflictHandler: conflict => { if (conflict === 'existsAndRunning') { // Can't replace the active version of the app api.dialog.showMessageBoxSync({ type: 'error', message: `Another version of ${api.app.getName()} is currently running. Quit it, then launch this version of the app again.`, buttons: [ 'OK' ] }); api.app.quit(); } return true; } }); };
若是你遇到Electron相关的问题,能够评论一下,咱们共同解决,一块儿成长。
对 Electron 感兴趣?请关注咱们的开源项目 Electron Playground,带你极速上手 Electron。
咱们每周五会精选一些有意思的文章和消息和你们分享,来掘金关注咱们的 晓前端周刊。
咱们是好将来 · 晓黑板前端技术团队。
咱们会常常与你们分享最新最酷的行业技术知识。
欢迎来 知乎、掘金、Segmentfault、CSDN、简书、开源中国、博客园 关注咱们。