我正在写一个电子应用程序。特点很简单。在我的主进程中,我创建了三个BrowserView,其中一个是应该始终显示的选项卡栏。选项卡栏控制其他两个视图中的哪一个应显示在BrowserWindow中。我使用电子构建器框架。
这是主要的过程。
import { app, BrowserWindow, shell, ipcMain, BrowserView } from 'electron'
import { release } from 'node:os'
import { join } from 'node:path'
// The built directory structure
//
// ├─┬ dist-electron
// │ ├─┬ main
// │ │ └── index.js > Electron-Main
// │ └─┬ preload
// │ └── index.js > Preload-Scripts
// ├─┬ dist
// │ └── index.html > Electron-Renderer
//
process.env.DIST_ELECTRON = join(__dirname, '..')
process.env.DIST = join(process.env.DIST_ELECTRON, '../dist')
process.env.PUBLIC = process.env.VITE_DEV_SERVER_URL
? join(process.env.DIST_ELECTRON, '../public')
: process.env.DIST
// Disable GPU Acceleration for Windows 7
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
// Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
if (!app.requestSingleInstanceLock()) {
app.quit()
process.exit(0)
}
// Remove electron security warnings
// This warning only shows in development mode
// Read more on https://www.electronjs.org/docs/latest/tutorial/security
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
let win: BrowserWindow | null = null
// Here, you can also use other preload
const preload = join(__dirname, '../preload/index.js')
const url = process.env.VITE_DEV_SERVER_URL
const indexHtml = join(process.env.DIST, 'index.html')
const views = {
default: null,
internet: null,
tabbar: null,
}
async function createWindow() {
const defaultView = new BrowserView({
webPreferences: {
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
nodeIntegration: true,
contextIsolation: false,
},
})
const internetView = new BrowserView(
{
webPreferences: {
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
nodeIntegration: true,
contextIsolation: false,
},
},
)
const tabbarView = new BrowserView({
webPreferences: {
preload,
nodeIntegration: true,
contextIsolation: false,
},
})
views.default = defaultView
views.internet = internetView
views.tabbar = tabbarView
win = new BrowserWindow({
title: 'Main window',
icon: join(process.env.PUBLIC, 'favicon.ico'),
webPreferences: {
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
nodeIntegration: true,
contextIsolation: false,
},
})
win.addBrowserView(defaultView)
win.addBrowserView(tabbarView)
if (process.env.VITE_DEV_SERVER_URL) { // electron-vite-vue#298
defaultView.webContents.loadURL(url)
// Open devTool if the app is not packaged
// defaultView.webContents.openDevTools()
} else {
defaultView.webContents.loadFile(indexHtml)
}
// Test actively push message to the Electron-Renderer
defaultView.webContents.on('did-finish-load', () => {
defaultView?.webContents.send('main-process-message', new Date().toLocaleString())
})
// Make all links open with the browser, not with the application
defaultView.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https:')) shell.openExternal(url)
return { action: 'deny' }
})
// win.webContents.on('will-navigate', (event, url) => { }) #344
tabbarView.webContents.loadFile(join(process.env.PUBLIC, 'tabbar.html'))
function handleResize() {
const { width, height } = win.getBounds()
const viewsHeight = height - 50
defaultView.setBounds({x: 0, y: 0, width, height: viewsHeight - 50})
internetView.setBounds({x: 0, y: 0, width, height: viewsHeight - 50})
tabbarView.setBounds({x: 0, y: viewsHeight - 50, width, height: viewsHeight})
}
handleResize()
win.on('resize', () => {
handleResize()
})
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
win = null
if (process.platform !== 'darwin') app.quit()
})
app.on('second-instance', () => {
if (win) {
// Focus on the main window if the user tried to open another
if (win.isMinimized()) win.restore()
win.focus()
}
})
app.on('activate', () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
allWindows[0].focus()
} else {
createWindow()
}
})
ipcMain.on('change-view', (event, viewName) => {
const view = views[viewName]
if (view) {
// don't remove tabbar
win?.removeBrowserView(views.default)
win?.removeBrowserView(views.internet)
win?.addBrowserView(view)
}
})
字符串
它大多是电子制造商的开箱即用。我确实观察到一些非常奇怪的现象。
当我运行应用程序时,它给我一个空白窗口。什么都看不出来。但是如果我调整窗口的大小,我添加到BrowserWindow的BrowserViews就会显示出来。
如果我加上这行代码
tabbarView.webContents.toggleDevTools()
型
仅显示选项卡视图。
其他观点也是如此。除非我做点什么否则它们不会渲染。
有什么问题吗?- 谢谢-谢谢
1条答案
按热度按时间xggvc2p61#
当你创建BrowserView时,你应该在
setBounds
之前先addBrowserView
和setTopBrowserView
。试试这个,看看能不能解决你的问题。