Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions src/main/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,24 @@ const init = async options => {

crossover.resetPosition()

// Set lock state, timeout makes it pretty
setTimeout( () => {
// Set lock state after renderer is ready
// Using did-finish-load ensures the renderer's IPC listeners are registered
// before we send lock_window. The 400ms timeout was unreliable on Windows (#480).
let lockStateInitialized = false
const setupLockState = () => {

// Todo: We shouldn't need a timeout here
// Keyboard shortcuts - delay fixes an unbreakable loop on reset, continually triggering resets
if ( lockStateInitialized ) {

return

}

lockStateInitialized = true

// Keyboard shortcuts (delay from did-finish-load avoids the reset loop)
crossover.registerKeyboardShortcuts()

// Show or hide window
// Restore lock state
crossover.lockWindow( preferences.value( 'hidden.locked' ) )

ipcMain.once( 'init', () => {
Expand All @@ -76,7 +86,39 @@ const init = async options => {

} )

}, 400 )
}

if ( windows.win && windows.win.webContents ) {

if ( options?.triggeredByReset ) {

// Reset case: page is already loaded, small delay to avoid the reset loop
setTimeout( setupLockState, 100 )

} else {

const scheduleSetup = () => setTimeout( setupLockState, 50 )

if ( windows.win.webContents.isLoading() ) {

// First boot: wait for renderer to finish loading
windows.win.webContents.once( 'did-finish-load', scheduleSetup )

} else {

// Page already loaded
scheduleSetup()

}

}

} else {

// Fallback
setTimeout( setupLockState, 400 )

}

// Spawn chooser window (if resetting it may exist)
if ( !windows.chooserWindow ) {
Expand Down
21 changes: 20 additions & 1 deletion src/main/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,32 @@ const appEvents = () => {
// See https://github.com/electron/electron/issues/5273
app.on( 'before-quit', () => {

// Unlock all windows before quitting so they can properly close on Windows
// When locked, closable=false prevents the window from being destroyed,
// leaving a zombie process (see #480)
const makeWindowClosable = win => {

if ( win && !win.isDestroyed() ) {

win.closable = true
win.setFocusable( true )
win.setIgnoreMouseEvents( false )

}

}

makeWindowClosable( windows.win )
windows.shadowWindows.forEach( makeWindowClosable )

app.releaseSingleInstanceLock()

} )

app.on( 'will-quit', () => {

// Unregister all shortcuts.
// Save lock state before cleanup so it persists for next launch
// Then unregister all hooks and shortcuts
iohook.unregisterIOHook()
keyboard.unregisterShortcuts()
// process.exit( EXIT_CODES.SUCCESS )
Expand Down