mirror of
https://github.com/zen-browser/desktop.git
synced 2026-05-04 13:05:12 +00:00
Refactor keyboard shortcut handling to improve key replacement logic and streamline event management
This commit is contained in:
@@ -226,14 +226,14 @@ class KeyShortcutModifiers {
|
|||||||
if (this.#control) {
|
if (this.#control) {
|
||||||
str += 'control,';
|
str += 'control,';
|
||||||
}
|
}
|
||||||
if (this.#alt) {
|
if (this.#accel) {
|
||||||
str += 'alt,';
|
str += 'accel,';
|
||||||
}
|
}
|
||||||
if (this.#shift) {
|
if (this.#shift) {
|
||||||
str += 'shift,';
|
str += 'shift,';
|
||||||
}
|
}
|
||||||
if (this.#accel) {
|
if (this.#alt) {
|
||||||
str += 'accel,';
|
str += 'alt,';
|
||||||
}
|
}
|
||||||
if (this.#meta) {
|
if (this.#meta) {
|
||||||
str += 'meta,';
|
str += 'meta,';
|
||||||
@@ -385,13 +385,19 @@ class KeyShortcut {
|
|||||||
|
|
||||||
toXHTMLElement(window) {
|
toXHTMLElement(window) {
|
||||||
let key = window.document.createXULElement('key');
|
let key = window.document.createXULElement('key');
|
||||||
|
return this.replaceWithChild(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceWithChild(key) {
|
||||||
key.id = this.#id;
|
key.id = this.#id;
|
||||||
if (this.#keycode) {
|
if (this.#keycode) {
|
||||||
key.setAttribute('keycode', this.#keycode);
|
key.setAttribute('keycode', this.#keycode);
|
||||||
|
key.removeAttribute('key');
|
||||||
} else {
|
} else {
|
||||||
// note to "mr. macos": Better use setAttribute, because without it, there's a
|
// note to "mr. macos": Better use setAttribute, because without it, there's a
|
||||||
// risk of malforming the XUL element.
|
// risk of malforming the XUL element.
|
||||||
key.setAttribute('key', this.#key);
|
key.setAttribute('key', this.#key);
|
||||||
|
key.removeAttribute('keycode');
|
||||||
}
|
}
|
||||||
key.setAttribute('group', this.#group);
|
key.setAttribute('group', this.#group);
|
||||||
|
|
||||||
@@ -899,6 +905,8 @@ class ZenKeyboardShortcutsVersioner {
|
|||||||
window.removeEventListener('zen-devtools-keyset-added', listener);
|
window.removeEventListener('zen-devtools-keyset-added', listener);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// We need to load after an event because the devtools keyset is not in the DOM yet
|
||||||
|
// and we need to wait for it to be added.
|
||||||
gZenKeyboardShortcutsManager._hasToLoadDefaultDevtools = true;
|
gZenKeyboardShortcutsManager._hasToLoadDefaultDevtools = true;
|
||||||
window.addEventListener('zen-devtools-keyset-added', listener);
|
window.addEventListener('zen-devtools-keyset-added', listener);
|
||||||
}
|
}
|
||||||
@@ -910,24 +918,6 @@ var gZenKeyboardShortcutsManager = {
|
|||||||
loader: new ZenKeyboardShortcutsLoader(),
|
loader: new ZenKeyboardShortcutsLoader(),
|
||||||
_hasToLoadDevtools: false,
|
_hasToLoadDevtools: false,
|
||||||
|
|
||||||
devtoolsShortcutListener: {
|
|
||||||
//async onKey(window, key) {
|
|
||||||
// try {
|
|
||||||
// // Record the timing at which this event started in order to compute later in
|
|
||||||
// // gDevTools.showToolbox, the complete time it takes to open the toolbox.
|
|
||||||
// // i.e. especially take `initDevTools` into account.
|
|
||||||
// const startTime = Cu.now();
|
|
||||||
// const require = this.initDevTools("KeyShortcut", key);
|
|
||||||
// const {
|
|
||||||
// gDevToolsBrowser,
|
|
||||||
// } = require("devtools/client/framework/devtools-browser");
|
|
||||||
// await gDevToolsBrowser.onKeyShortcut(window, key, startTime);
|
|
||||||
// } catch (e) {
|
|
||||||
// console.error(`Exception while trigerring key ${key}: ${e}\n${e.stack}`);
|
|
||||||
// }
|
|
||||||
//},
|
|
||||||
},
|
|
||||||
|
|
||||||
beforeInit() {
|
beforeInit() {
|
||||||
if (!this.inBrowserView) {
|
if (!this.inBrowserView) {
|
||||||
return;
|
return;
|
||||||
@@ -1090,29 +1080,21 @@ var gZenKeyboardShortcutsManager = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let devtoolsKeyset = browser.gZenKeyboardShortcutsManager.getZenDevtoolsKeyset(browser);
|
let devtoolsKeyset = browser.gZenKeyboardShortcutsManager.getZenDevtoolsKeyset(browser);
|
||||||
const remainingChildren = [];
|
|
||||||
for (let i = devtoolsKeyset.children.length - 1; i >= 0; i--) {
|
|
||||||
const key = devtoolsKeyset.children[i];
|
|
||||||
if (ZenKeyboardShortcutsLoader.IGNORED_DEVTOOLS_SHORTCUTS.includes(key.id)) {
|
|
||||||
remainingChildren.push(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
devtoolsKeyset.innerHTML = '';
|
|
||||||
for (let key of this._currentShortcutList) {
|
for (let key of this._currentShortcutList) {
|
||||||
if (key.getGroup() != 'devTools') {
|
if (key.getGroup() != 'devTools') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let child = key.toXHTMLElement(browser);
|
if (ZenKeyboardShortcutsLoader.IGNORED_DEVTOOLS_SHORTCUTS.includes(key.getID())) {
|
||||||
// child.addEventListener('command', event);
|
continue;
|
||||||
devtoolsKeyset.appendChild(child);
|
}
|
||||||
}
|
const originalKey = browser.document.getElementById(key.getID());
|
||||||
|
// We do not want to remove and create a new key in these cases,
|
||||||
for (let key of remainingChildren) {
|
// because it will lose the event listeners.
|
||||||
devtoolsKeyset.appendChild(key);
|
key.replaceWithChild(originalKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const mainKeyset = browser.document.getElementById(ZEN_MAIN_KEYSET_ID);
|
const mainKeyset = browser.document.getElementById(ZEN_MAIN_KEYSET_ID);
|
||||||
|
mainKeyset.after(devtoolsKeyset);
|
||||||
mainKeyset.before(devtoolsKeyset);
|
mainKeyset.before(devtoolsKeyset);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user