显示和隐藏Electron应用程序时会发生事件吗?

时间:2018-10-08 20:05:21

标签: javascript electron

我一直在寻找显示或隐藏应用程序的电子app事件。我在文档中看到有'browser-window-blur''browser-window-focus',但它们并不能满足我的要求。

我想知道用户何时切换到另一个应用程序或切换回我的应用程序。如果用户在浏览器窗口之间切换(包括``开发人员的工具''窗口),则会触发上述事件。


main.js

中的代码
app.on('browser-window-focus', () => {
    if (mainWindow) {
        console.log('browser-window-focus');

        mainWindow.webContents.send('projectMsg', { "event": "focus" });
    }
});

app.on('browser-window-blur', () => {
    console.log('browser-window-blur');
    if (mainWindow) {
        mainWindow.webContents.send('projectMsg', { "event": "blur" });
    }
});

3 个答案:

答案 0 :(得分:1)

还有showhide,尽管您必须使用Product::find($id);win.show()显式显示/隐藏应用才能触发这些事件。

答案 1 :(得分:0)

检查以下BrowserWindow的事件:

Event: 'blur':当窗口失去焦点时发出。

Event: 'show':显示窗口时发出。

例如:

    for(let i = 0; i < 3; i++) {
        // list has arrays inside containing coordinates in a 2d plane, following a triangular grid. Each value looks like this [0, 2, -1]
        // j = next modulus value in base 3 given i
        // k = next modulus value in base 3 given j
        let j = (i + 1) % 3;
        let k = (i + 2) % 3;

        list[i] = [...old_list].sort((a, b) => { // sort list
            // This provides 3 sorted lists which are sorted by xy, yz, and zx
            if(a[i] === b[i]) {
                if( a[j] === b[j] && Math.abs(a[k] - b[k]) ) {
                    my_function(a, b, i, j, k);
                }
                return a[j] - b[j];
            }
            return a[i] - b[i];
        });
    }

    my_function(a, b, i, j, k) {
        let modules = universe.modules;
        let dir = b[k] - a[k];

        a = {x: undefined, y: undefined};
        b = {x: undefined, y: undefined};

        if(k === 0 && dir === -1) { a.x = dir *  module.constraints[k].x; a.y = dir *  module.constraints[k].y; } // (0, -1) => (0)
        if(k === 0 && dir ===  1) { a.x = dir *  module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (0,  1) => (1)

        if(k === 1 && dir === -1) { a.x = dir * -module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (1, -1) => (2)
        if(k === 1 && dir ===  1) { a.x = dir * -module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (1,  1) => (2)

        if(k === 2 && dir === -1) { a.x = dir * -module.constraints[j].x; a.y = dir *  module.constraints[j].y; } // (2, -1) => (1)
        if(k === 2 && dir ===  1) { a.x = dir * -module.constraints[i].x; a.y = dir *  module.constraints[i].y; } // (2,  1) => (0)



        if(k === 0 && dir === -1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (0, -1) => (1)
        if(k === 0 && dir ===  1) { b.x = dir * -module.constraints[k].x; b.y = dir * -module.constraints[k].y; } // (0,  1) => (0)

        if(k === 1 && dir === -1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (1, -1) => (2)
        if(k === 1 && dir ===  1) { b.x = dir * -module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (1,  1) => (2)

        if(k === 2 && dir === -1) { b.x = dir *  module.constraints[i].x; b.y = dir * -module.constraints[i].y; } // (2, -1) => (0)
        if(k === 2 && dir ===  1) { b.x = dir *  module.constraints[j].x; b.y = dir * -module.constraints[j].y; } // (2,  1) => (1)

        // doing some stuff with Matter.js(physics library) to decide where to add constraints between two bodies
    }

希望获得帮助。

答案 2 :(得分:0)

在我看来,它的工作原理与您描述的完全相同,因此要求可能有所不同。

此代码

const {app, BrowserWindow} = require('electron')

app.on('browser-window-focus', (event, win) => {
  console.log('browser-window-focus', win.webContents.id)
})
app.on('browser-window-blur', (event, win) => {
  if (win.webContents.isDevToolsFocused()) {
    console.log('Ignore this case')
  } else {
    console.log('browser-window-blur', win.webContents.id)
  }
})
app.once('ready', () => {
  new BrowserWindow()
  new BrowserWindow().webContents.openDevTools({detach: true})
})
假设最初没有重点关注,则

采用以下方式(在3.0.3中):

  • 单击窗口1会打印browser-window-focus 1
  • 单击窗口2会打印browser-window-blur 1 browser-window-focus 2
  • 单击devtools窗口可打印 browser-window-blur 2 Ignore this case

据我所知,这些事件中未包括devtool,对于任何其他以窗口为中心的窗口(包括devtool),窗口变得越来越模糊