Last active
September 2, 2020 18:28
-
-
Save CharlieHess/c507468cd40630d04f58c6514100b997 to your computer and use it in GitHub Desktop.
Missing first focus event when focusing window with show: false
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Empty --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Empty */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment