Yet another attempt into fixing macos shortcuts

This commit is contained in:
mauro-balades 2024-10-08 20:31:31 +02:00
parent ccfbac35f3
commit bfb5e8fc94
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB

View file

@ -96,6 +96,9 @@ const fixedL10nIds = {
'History:RestoreLastClosedTabOrWindowOrSession': 'zen-restore-last-closed-tab-shortcut', 'History:RestoreLastClosedTabOrWindowOrSession': 'zen-restore-last-closed-tab-shortcut',
}; };
const ZEN_MAIN_KEYSET_ID = 'mainKeyset';
const ZEN_KEYSET_ID = 'zenKeyset';
const ZEN_COMPACT_MODE_SHORTCUTS_GROUP = 'zen-compact-mode'; const ZEN_COMPACT_MODE_SHORTCUTS_GROUP = 'zen-compact-mode';
const ZEN_WORKSPACE_SHORTCUTS_GROUP = 'zen-workspace'; const ZEN_WORKSPACE_SHORTCUTS_GROUP = 'zen-workspace';
const ZEN_OTHER_SHORTCUTS_GROUP = 'zen-other'; const ZEN_OTHER_SHORTCUTS_GROUP = 'zen-other';
@ -537,7 +540,7 @@ function zenGetDefaultShortcuts() {
// and increment the version number. // and increment the version number.
console.info('Zen CKS: Loading default shortcuts...'); console.info('Zen CKS: Loading default shortcuts...');
let keySet = document.getElementById('mainKeyset'); let keySet = document.getElementById(ZEN_MAIN_KEYSET_ID);
let newShortcutList = []; let newShortcutList = [];
// Firefox's standard keyset. Reverse order to keep the order of the keys // Firefox's standard keyset. Reverse order to keep the order of the keys
@ -755,10 +758,18 @@ class ZenKeyboardShortcutsVersioner {
var gZenKeyboardShortcutsManager = { var gZenKeyboardShortcutsManager = {
loader: new ZenKeyboardShortcutsLoader(), loader: new ZenKeyboardShortcutsLoader(),
async init() { beforeInit() {
if (window.location.href == 'chrome://browser/content/browser.xhtml') { if (!this.inBrowserView) {
console.info('Zen CKS: Initializing shortcuts'); return;
}
// Create the main keyset before calling the async init function,
// This is because other browser-sets needs this element and the JS event
// handled wont wait for the async function to finish.
void(this.zenKeyset);
},
async init() {
if (this.inBrowserView) {
const loadedShortcuts = await this._loadSaved(); const loadedShortcuts = await this._loadSaved();
this._currentShortcutList = this.versioner.migrateIfNeeded(loadedShortcuts); this._currentShortcutList = this.versioner.migrateIfNeeded(loadedShortcuts);
@ -770,6 +781,10 @@ var gZenKeyboardShortcutsManager = {
} }
}, },
get inBrowserView() {
return window.location.href == 'chrome://browser/content/browser.xhtml';
},
async _loadSaved() { async _loadSaved() {
var innerLoad = async () => { var innerLoad = async () => {
let data = await this.loader.load(); let data = await this.loader.load();
@ -790,25 +805,39 @@ var gZenKeyboardShortcutsManager = {
return loadedShortcuts; return loadedShortcuts;
}, },
get zenKeyset() {
if (!this._zenKeyset) {
this._zenKeyset = document.createXULElement('keyset');
this._zenKeyset.id = ZEN_KEYSET_ID;
const mainKeyset = document.getElementById(ZEN_MAIN_KEYSET_ID);
mainKeyset.after(this._zenKeyset);
}
return this._zenKeyset;
},
clearMainKeyset(element) {
for (let key of element.children) {
if (key.getAttribute('internal') == 'true') {
continue;
}
key.remove();
}
},
_applyShortcuts() { _applyShortcuts() {
for (const browser of ZenMultiWindowFeature.browsers) { for (const browser of ZenMultiWindowFeature.browsers) {
let mainKeyset = browser.document.getElementById('mainKeyset'); let mainKeyset = browser.document.getElementById(ZEN_MAIN_KEYSET_ID);
if (!mainKeyset) { if (!mainKeyset) {
throw new Error('Main keyset not found'); throw new Error('Main keyset not found');
} }
let parent = mainKeyset.parentElement; let parent = mainKeyset.parentElement;
//mainKeyset.remove(); this.clearMainKeyset(mainKeyset);
const keyset = this.zenKeyset;
this.clearMainKeyset(keyset);
const children = mainKeyset.children;
for (let i = children.length - 1; i >= 0; i--) {
let key = children[i];
// Do NOT remove the internal keys
if (key.getAttribute('internal') == 'true') {
continue;
}
key.remove();
}
// We dont check this anymore since we are skiping internal keys // We dont check this anymore since we are skiping internal keys
//if (mainKeyset.children.length > 0) { //if (mainKeyset.children.length > 0) {
// throw new Error('Child list not empty'); // throw new Error('Child list not empty');
@ -819,10 +848,10 @@ var gZenKeyboardShortcutsManager = {
continue; continue;
} }
let child = key.toXHTMLElement(browser); let child = key.toXHTMLElement(browser);
mainKeyset.appendChild(child); keyset.appendChild(child);
} }
parent.prepend(mainKeyset); parent.prepend(keyset);
console.debug('Shortcuts applied...'); console.debug('Shortcuts applied...');
} }
}, },
@ -897,6 +926,8 @@ var gZenKeyboardShortcutsManager = {
}, },
}; };
window.addEventListener("MozBeforeInitialXULLayout", async () => { document.addEventListener("MozBeforeInitialXULLayout", () => {
await gZenKeyboardShortcutsManager.init(); gZenKeyboardShortcutsManager.beforeInit();
}); // Async init
gZenKeyboardShortcutsManager.init();
}, { once: true });