#220 stricter keyboard shortcut listeners

This commit is contained in:
dragonwocky 2020-11-12 00:05:03 +11:00
parent 9597dcc4d5
commit c067d93a74
Signed by: dragonwocky
GPG Key ID: C7A48B7846AA706D
3 changed files with 94 additions and 45 deletions

View File

@ -23,7 +23,13 @@ module.exports = (store, __exports) => {
if (event.key === 'F5') location.reload(); if (event.key === 'F5') location.reload();
// open menu on hotkey toggle // open menu on hotkey toggle
if (store().menu_toggle) { if (store().menu_toggle) {
const hotkey = toKeyEvent(store().menu_toggle); const hotkey = {
ctrlKey: false,
metaKey: false,
altKey: false,
shiftKey: false,
...toKeyEvent(store().menu_toggle),
};
let triggered = true; let triggered = true;
for (let prop in hotkey) for (let prop in hotkey)
if ( if (
@ -37,7 +43,13 @@ module.exports = (store, __exports) => {
const tabStore = () => store('e1692c29-475e-437b-b7ff-3eee872e1a42'); const tabStore = () => store('e1692c29-475e-437b-b7ff-3eee872e1a42');
if (tabStore().select_modifier) { if (tabStore().select_modifier) {
// switch between tabs via key modifier // switch between tabs via key modifier
const select_tab_modifier = toKeyEvent(select_tab_modifier); const select_tab_modifier = {
ctrlKey: false,
metaKey: false,
altKey: false,
shiftKey: false,
...toKeyEvent(tabStore().select_modifier),
};
let triggered = true; let triggered = true;
for (let prop in select_tab_modifier) for (let prop in select_tab_modifier)
if (select_tab_modifier[prop] !== event[prop]) triggered = false; if (select_tab_modifier[prop] !== event[prop]) triggered = false;
@ -61,14 +73,26 @@ module.exports = (store, __exports) => {
} }
if (tabStore().new_tab) { if (tabStore().new_tab) {
// create/close tab keybindings // create/close tab keybindings
const new_tab_keybinding = toKeyEvent(tabStore().new_tab); const new_tab_keybinding = {
ctrlKey: false,
metaKey: false,
altKey: false,
shiftKey: false,
...toKeyEvent(tabStore().new_tab),
};
let triggered = true; let triggered = true;
for (let prop in new_tab_keybinding) for (let prop in new_tab_keybinding)
if (new_tab_keybinding[prop] !== event[prop]) triggered = false; if (new_tab_keybinding[prop] !== event[prop]) triggered = false;
if (triggered) electron.ipcRenderer.sendToHost('enhancer:new-tab'); if (triggered) electron.ipcRenderer.sendToHost('enhancer:new-tab');
} }
if (tabStore().close_tab) { if (tabStore().close_tab) {
const close_tab_keybinding = toKeyEvent(tabStore().close_tab); const close_tab_keybinding = {
ctrlKey: false,
metaKey: false,
altKey: false,
shiftKey: false,
...toKeyEvent(tabStore().close_tab),
};
let triggered = true; let triggered = true;
for (let prop in close_tab_keybinding) for (let prop in close_tab_keybinding)
if (close_tab_keybinding[prop] !== event[prop]) triggered = false; if (close_tab_keybinding[prop] !== event[prop]) triggered = false;

View File

@ -129,7 +129,13 @@ window['__start'] = async () => {
$popup.classList.remove('visible'); $popup.classList.remove('visible');
// close window on hotkey toggle // close window on hotkey toggle
if (coreStore().menu_toggle) { if (coreStore().menu_toggle) {
const hotkey = toKeyEvent(coreStore().menu_toggle); const hotkey = {
ctrlKey: false,
metaKey: false,
altKey: false,
shiftKey: false,
...toKeyEvent(coreStore().menu_toggle),
};
let triggered = true; let triggered = true;
for (let prop in hotkey) for (let prop in hotkey)
if ( if (

View File

@ -168,46 +168,65 @@ module.exports = (store, __exports) => {
}); });
document.addEventListener('keyup', (event) => { document.addEventListener('keyup', (event) => {
if (!electron.remote.getCurrentWindow().isFocused()) return; if (!electron.remote.getCurrentWindow().isFocused()) return;
// switch between tabs via key modifier const tabStore = () => store('e1692c29-475e-437b-b7ff-3eee872e1a42');
const select_tab_modifier = toKeyEvent( if (tabStore().select_modifier) {
store('e1692c29-475e-437b-b7ff-3eee872e1a42').select_modifier // switch between tabs via key modifier
); const select_tab_modifier = {
let triggered = true; ctrlKey: false,
for (let prop in select_tab_modifier) metaKey: false,
if (select_tab_modifier[prop] !== event[prop]) triggered = false; altKey: false,
if ( shiftKey: false,
triggered && ...toKeyEvent(tabStore().select_modifier),
[ };
'1', let triggered = true;
'2', for (let prop in select_tab_modifier)
'3', if (select_tab_modifier[prop] !== event[prop]) triggered = false;
'4', if (
'5', triggered &&
'6', [
'7', '1',
'8', '2',
'9', '3',
'ArrowRight', '4',
'ArrowLeft', '5',
].includes(event.key) '6',
) '7',
this.selectTab(event.key); '8',
// create/close tab keybindings '9',
const new_tab_keybinding = toKeyEvent( 'ArrowRight',
store('e1692c29-475e-437b-b7ff-3eee872e1a42').new_tab 'ArrowLeft',
); ].includes(event.key)
triggered = true; )
for (let prop in new_tab_keybinding) this.selectTab(event.key);
if (new_tab_keybinding[prop] !== event[prop]) triggered = false; }
if (triggered) this.newTab(); if (tabStore().new_tab) {
const close_tab_keybinding = toKeyEvent( // create/close tab keybindings
store('e1692c29-475e-437b-b7ff-3eee872e1a42').close_tab const new_tab_keybinding = {
); ctrlKey: false,
triggered = true; metaKey: false,
for (let prop in close_tab_keybinding) altKey: false,
if (close_tab_keybinding[prop] !== event[prop]) triggered = false; shiftKey: false,
if (triggered && document.querySelector('.tab.current .close')) ...toKeyEvent(tabStore().new_tab),
document.querySelector('.tab.current .close').click(); };
let triggered = true;
for (let prop in new_tab_keybinding)
if (new_tab_keybinding[prop] !== event[prop]) triggered = false;
if (triggered) this.newTab();
}
if (tabStore().close_tab) {
const close_tab_keybinding = {
ctrlKey: false,
metaKey: false,
altKey: false,
shiftKey: false,
...toKeyEvent(tabStore().close_tab),
};
let triggered = true;
for (let prop in close_tab_keybinding)
if (close_tab_keybinding[prop] !== event[prop]) triggered = false;
if (triggered && document.querySelector('.tab.current .close'))
document.querySelector('.tab.current .close').click();
}
}); });
electron.ipcRenderer.on('enhancer:close-tab', (event, tab) => { electron.ipcRenderer.on('enhancer:close-tab', (event, tab) => {
this.closeTab(tab); this.closeTab(tab);