Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Previous Window Focus / Electron

Tags:

electron

Currently scratching my head on a tough one. I'm just starting with electron and so far so good. However, when the window gets hidden (it's a popup showing with a shortcut, that goes away when you press enter), I'd like to give the focus back to the previous window.

I'm using Mac, and the menu bar shows my previous app's name so it looks like the focus is given back to the app, but not entirely since the window is not selected.

Any idea how to fix that?

Thanks!

like image 331
Michel Vermeulen Avatar asked Jun 01 '18 11:06

Michel Vermeulen


1 Answers

For Linux: I found browserWindow.hide() correctly restores focus.

For Windows: browserWindow.minimize() correctly restores focus.

For Mac: app.hide() correctly restores focus. Note: calling app.hide() unfortunately hides all windows. There is no known way of keeping some windows open without hiding all with app.hide().

This works both on Mac, Linux and Windows:

hide() {
    this.window.minimize();
    this.window.hide();
    if (process.platform == "darwin") this.app.hide()
}

show() {
    this.window.show();
    this.window.restore();
}
like image 117
junvar Avatar answered Jan 01 '23 22:01

junvar