use keyup listeners instead of a globalShortcut for the enhancements menu toggle

This commit is contained in:
dragonwocky 2020-10-11 16:02:45 +11:00
parent 3d64a5e8c8
commit 4886bbbd3e
3 changed files with 27 additions and 14 deletions

View File

@ -13,11 +13,18 @@ module.exports = (store, __exports) => {
notionIpc = require(`${helpers.__notion.replace(
/\\/g,
'/'
)}/app/helpers/notionIpc.js`);
)}/app/helpers/notionIpc.js`),
{ toKeyEvent } = require('keyboardevent-from-electron-accelerator');
// additional hotkeys
document.defaultView.addEventListener('keyup', (event) => {
if (event.code === 'F5') location.reload();
// open menu on hotkey toggle
const hotkey = toKeyEvent(store().menu_toggle);
let triggered = true;
for (let prop in hotkey)
if (hotkey[prop] !== event[prop]) triggered = false;
if (triggered) electron.ipcRenderer.send('enhancer:open-menu');
});
const attempt_interval = setInterval(enhance, 500);

View File

@ -10,7 +10,8 @@ const store = require('../../pkg/store.js'),
helpers = require('../../pkg/helpers.js'),
fs = require('fs-extra'),
path = require('path'),
electron = require('electron');
electron = require('electron'),
{ toKeyEvent } = require('keyboardevent-from-electron-accelerator');
window['__start'] = async () => {
const buttons = require('./buttons.js')(() => ({
@ -141,9 +142,22 @@ window['__start'] = async () => {
document.addEventListener('keyup', (event) => {
if (
$popup.classList.contains('visible') &&
[13, 27].includes(event.keyCode)
['Enter', 'Escape'].includes(event.key)
)
$popup.classList.remove('visible');
// close window on hotkey toggle
console.log();
const hotkey = toKeyEvent(
store('0f0bf8b6-eae6-4273-b307-8fc43f2ee082', {
menu_toggle: modules.loaded
.find((m) => m.id === '0f0bf8b6-eae6-4273-b307-8fc43f2ee082')
.options.find((o) => o.key === 'menu_toggle').value,
}).menu_toggle
);
let triggered = true;
for (let prop in hotkey)
if (hotkey[prop] !== event[prop]) triggered = false;
if (triggered) electron.remote.getCurrentWindow().close();
});
let colorpicker_target = null;
const $colorpicker = colorjoe

View File

@ -32,6 +32,9 @@ module.exports = (store, __exports) => {
// menu
electron.ipcMain.on('enhancer:open-menu', (event, arg) => {
openExtensionMenu();
});
electron.ipcMain.on('enhancer:set-menu-theme', (event, arg) => {
if (!enhancer_menu) return;
enhancer_menu.webContents.send('enhancer:set-menu-theme', arg);
@ -115,17 +118,6 @@ module.exports = (store, __exports) => {
});
}
electron.globalShortcut.register(store().menu_toggle, () => {
if (
electron.BrowserWindow.getAllWindows()
.filter((win) => win.getTitle() !== 'notion-enhancer menu')
.some((win) => win.isFocused())
) {
openExtensionMenu();
} else if (enhancer_menu && enhancer_menu.isFocused())
enhancer_menu.close();
});
// tray
const contextMenu = electron.Menu.buildFromTemplate([