电子浏览器视图未呈现

编程入门 行业动态 更新时间:2024-10-03 10:47:11

电子浏览器<a href=https://www.elefans.com/category/jswz/34/1769510.html style=视图未呈现"/>

电子浏览器视图未呈现

我正在写一个电子应用程序。特点很简单。在我的主要进程中,我创建了三个浏览器视图,其中之一是一个应该始终显示的标签栏。标签栏控制其他两个视图中的哪一个应该显示在 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 
// 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 
      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 
        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 
      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)
  }
})

它大部分是开箱即用的 electron builder。我确实观察到一些非常奇怪的东西。

当我运行该应用程序时,它会给我一个空白窗口。没有显示。但是如果我调整窗口大小,我添加到 BrowserWindow 的 BrowserViews 就会出现。

如果我添加这行代码

tabbarView.webContents.toggleDevTools()

只有标签视图显示。

其他视图也一样。除非我做点什么,否则它们不会渲染。

问题是什么?谢谢。

回答如下:

更多推荐

电子浏览器视图未呈现

本文发布于:2024-05-30 04:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   浏览器   电子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!