Skip to content
Open
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
37 changes: 34 additions & 3 deletions src/main/crossover.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@ const registerKeyboardShortcuts = () => {
// Register all shortcuts
const { keybinds } = Preferences.getDefaults()
const custom = preferences.value( 'keybinds' ) // Defaults
const failed = []

for ( const shortcut of keyboardShortcuts() ) {

let accelerator
let registered

// Custom shortcuts
if ( custom[shortcut.action] === '' ) {

Expand All @@ -196,19 +201,45 @@ const registerKeyboardShortcuts = () => {

// If a custom shortcut exists for this action
log.info( `Custom keybind for ${shortcut.action}` )
keyboard.registerShortcut( custom[shortcut.action], shortcut.fn )
accelerator = custom[shortcut.action]
registered = keyboard.registerShortcut( accelerator, shortcut.fn )

} else if ( keybinds[shortcut.action] ) {

// Set default keybind
keyboard.registerShortcut( keybinds[shortcut.action], shortcut.fn )
accelerator = keybinds[shortcut.action]
registered = keyboard.registerShortcut( accelerator, shortcut.fn )

} else {

// Fallback to internal bind - THIS SHOULDNT HAPPEN
// if it does you forgot to add a default keybind for this shortcut
log.info( 'ERROR - you likely forgot to add a default keybind for this shortcut: ', shortcut )
keyboard.registerShortcut( shortcut.keybind, shortcut.fn )
accelerator = shortcut.keybind
registered = keyboard.registerShortcut( accelerator, shortcut.fn )

}

if ( accelerator && registered === false ) {

failed.push( accelerator )

}

}

if ( failed.length > 0 ) {

log.warn( `Shortcut registration failed for: ${failed.join( ', ' )}` )

// Notify the user — only if the renderer window is ready
if ( windows.win && !windows.win.isDestroyed() ) {

const notification = require( './notification' )
notification( {
title: 'Keyboard Shortcut Conflict',
body: `Could not register: ${failed.join( ', ' )}. Another app may be using this combo. Try rebinding in Settings.`,
} )

}

Expand Down
14 changes: 13 additions & 1 deletion src/main/keyboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { globalShortcut } = require( 'electron' )
const log = require( './log' )
const windows = require( './windows' )

const escapeAction = () => {
Expand All @@ -22,7 +23,18 @@ const registerEscape = ( action = keyboard.escapeAction ) => {

}

const registerShortcut = ( ...args ) => globalShortcut.register( ...args )
const registerShortcut = ( accelerator, fn ) => {

const registered = globalShortcut.register( accelerator, fn )
if ( !registered ) {

log.warn( `globalShortcut.register failed for: ${accelerator} (another app may have claimed this combo)` )

}
Comment on lines +29 to +33

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using console.warn bypasses the application's configured logging system (which might write to files, format logs, or handle log levels differently). It is better to import and use the custom log module, which also removes the need for the eslint-disable-next-line comment.

Suggested change
if ( !registered ) {
// eslint-disable-next-line no-console
console.warn( `[CrossOver] globalShortcut.register failed for: ${accelerator} (another app may have claimed this combo)` )
}
if ( !registered ) {
const log = require( './log' )
log.warn( '[CrossOver] globalShortcut.register failed for: ' + accelerator + ' (another app may have claimed this combo)' )
}


return registered

}

const unregisterShortcut = ( ...args ) => globalShortcut.unregister( ...args )

Expand Down