-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
š global shortcuts are app-specific (don't hijack OS-wide keyboard evā¦
ā¦ents) Signed-off-by: Marc Nuri <[email protected]>
- Loading branch information
Showing
12 changed files
with
124 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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 contains 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 contains 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 contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2022 Marc Nuri San Felix | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
describe('Main :: Global Keyboard Shortcuts module test suite', () => { | ||
let electron; | ||
let browserWindow; | ||
let inputEvent; | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
jest.mock('electron', () => require('../../__tests__').mockElectronInstance()); | ||
electron = require('electron'); | ||
browserWindow = require('../../__tests__').mockBrowserWindowInstance(); | ||
require('../').registerAppShortcuts({}, browserWindow.webContents); | ||
inputEvent = { | ||
preventDefault: jest.fn() | ||
}; | ||
}); | ||
test.each([ | ||
[{key: 'Escape', appEvent: 'appMenuClose'}], [{key: 'Escape', appEvent: 'closeDialog'}], | ||
[{key: 'F11', appEvent: 'fullscreenToggle'}] | ||
])('Key "$key" triggers "$appEvent" app event', ({key, appEvent}) => { | ||
browserWindow.listeners['before-input-event'](inputEvent, {key}); | ||
expect(electron.ipcMain.emit).toHaveBeenCalledWith(appEvent); | ||
}); | ||
describe('preventDefault', () => { | ||
test('calls preventDefault if key is registered', () => { | ||
browserWindow.listeners['before-input-event'](inputEvent, {key: 'Escape'}); | ||
expect(inputEvent.preventDefault).toHaveBeenCalled(); | ||
}); | ||
test('doesn\'t call preventDefault if key is not registered', () => { | ||
browserWindow.listeners['before-input-event'](inputEvent, {}); | ||
expect(inputEvent.preventDefault).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains 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 contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2022 Marc Nuri San Felix | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
const {ipcMain: eventBus} = require('electron'); | ||
const {APP_EVENTS} = require('../constants'); | ||
|
||
const eventKey = ({key, shift = false, control = false, alt = false, meta = false}) => | ||
`${key}-${shift}-${control}-${alt}-${meta}`; | ||
|
||
const EVENTS = new Map(); | ||
|
||
EVENTS.set(eventKey({key: 'Escape'}), () => { | ||
eventBus.emit(APP_EVENTS.appMenuClose); | ||
eventBus.emit(APP_EVENTS.closeDialog); | ||
}); | ||
|
||
EVENTS.set(eventKey({key: 'F11'}), () => { | ||
eventBus.emit(APP_EVENTS.fullscreenToggle); | ||
}); | ||
|
||
const registerAppShortcuts = (_, webContents) => { | ||
webContents.on('before-input-event', (event, {key, shift, control, alt, meta}) => { | ||
const func = EVENTS.get(eventKey({key, shift, control, alt, meta})); | ||
if (func) { | ||
event.preventDefault(); | ||
func(); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = {registerAppShortcuts}; |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 contains 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