Skip to content

Instantly share code, notes, and snippets.

@CharlieHess
Last active September 2, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlieHess/c507468cd40630d04f58c6514100b997 to your computer and use it in GitHub Desktop.
Save CharlieHess/c507468cd40630d04f58c6514100b997 to your computer and use it in GitHub Desktop.
Missing first focus event when focusing window with show: false
<!-- Empty -->
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
preload: path.resolve(__dirname, './preload.js')
},
show: false,
})
mainWindow.loadURL('https://unixpapa.com/js/testkey.html')
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length === 0) return
allWindows[0].show()
allWindows[0].focus()
})
let focusDiv
const focusStyle = `
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 2px solid yellow
`
const blurStyle = `
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 2px solid gray
`
window.addEventListener('DOMContentLoaded', createElement)
function createElement () {
focusDiv = document.createElement("div")
focusDiv.style = blurStyle
document.body.appendChild(focusDiv)
}
window.addEventListener('focus', () => {
focusDiv.style = focusStyle
})
window.addEventListener('blur', () => {
focusDiv.style = blurStyle
})
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment