当我调用win.loadFile(..)时,Electron BrowserView正在失去焦点

当我调用win.loadFile(..)时,Electron BrowserView正在失去焦点,electron,focus,Electron,Focus,在我的Electron设置中,一旦应用程序加载,我就会显示一个基于web的登录屏幕。它被安置在一个浏览器视图中,浏览器视图填满了应用程序的屏幕空间。通过这种方法,我可以禁用登录页面的nodeIntegration(因为它需要jQuery,当nodeIntegration为true时,jQuery不可用),同时为我的主应用程序保持启用状态 但我注意到从关闭登录视图到加载主应用程序都有延迟。e、 可能需要2-5秒 为了消除这个延迟,我已经开始使用'did finish load'事件预加载主应用程序

在我的Electron设置中,一旦应用程序加载,我就会显示一个基于web的登录屏幕。它被安置在一个浏览器视图中,浏览器视图填满了应用程序的屏幕空间。通过这种方法,我可以禁用登录页面的nodeIntegration(因为它需要jQuery,当nodeIntegration为true时,jQuery不可用),同时为我的主应用程序保持启用状态

但我注意到从关闭登录视图到加载主应用程序都有延迟。e、 可能需要2-5秒

为了消除这个延迟,我已经开始使用'did finish load'事件预加载主应用程序。这意味着当登录屏幕出现时,我可以在“后台”加载主应用程序

然而,我遇到了一个问题,BrowserView失去了焦点。这意味着用户需要手动单击登录输入。这是在我将调用添加到
win.loadFile('index.html')
之后开始发生的

到目前为止,我已经能够通过在主应用加载后将焦点传递回BrowserView来缓解这种情况。但这并不完美,因为有一个短暂的死区,在此期间键盘输入被忽略

有更好的解决办法吗?我的代码如下

const win = new BrowserWindow({
    width: 605, height: 550,
    minWidth: 605, minHeight: 550,
    useContentSize: true,
    maximizable: false,
    title: "My Window"
})

const view = new BrowserView({
    webPreferences: {
      // Enables support for jQuery and others by disabling nodeIntegration in this BrowserView
      nodeIntegration: false,
    }
})

win.setBrowserView(view)
view.setAutoResize({ width: true, height: true})
view.setBounds({ x: 0, y: 0, width: win.getContentBounds().width, height: win.getContentBounds().height })

view.webContents.loadURL('my login url')

view.webContents.once('did-finish-load', () => {
    // Load the main view in the background. This means it'll be available immediately when the login view closes.
    // But it also steals focus from the view so...
    win.loadFile('index.html')

    // Best fix so far but not perfect
    win.webContents.on('did-finish-load', () => view.webContents.focus())
})

view.webContents.on('login-complete-made-up-event', async (event) => {
    // Close the login page to show the main view
    view.destroy()
    // Set the focus onto the main view
    win.webContents.focus()
})

解决办法相当简单。今天早上5分钟内偶然发现的

view.webContents.once('did-finish-load', () => {
    // Load the main view in the background. This means it'll be available immediately when the login view closes.
    win.loadFile('index.html')

    // Place focus on the view for keyboard input. i.e. Otherwise the user needs to click.
    // Do this after loading index.html as the loadFile(..) steals focus
    view.webContents.focus()
})

“因为它需要jQuery,而当nodeIntegration为真时,jQuery是不可用的”-我认为至少在我的经验中是相反的。相信我是正确的-请阅读>由于Electron的Node.js集成,有一些额外的符号插入到DOM模块中,如exports,require。这会导致某些库出现问题,因为它们希望插入具有相同名称的符号。