Skip to content

Commit

Permalink
NEW: Add identites as menu item to new message menu popup.
Browse files Browse the repository at this point in the history
  • Loading branch information
speedball2001 committed Jul 24, 2023
1 parent 4957220 commit 34acfd5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 20 deletions.
73 changes: 58 additions & 15 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,36 @@ class IdentityChooser {
async run2() {
console.debug("IdentityChooser#run2 -- begin");

try {
await this.icOptions.setupDefaultOptions();
} catch (error) {
//
// Workaround. Several users report issues with Cardboox and
// Identity Chooser accessing the browser.local store
// (https://github.com/speedball2001/identitychooser-mx/issues/18:
//
// 20:30:33.873 TransactionInactiveError: A request was placed
// against a transaction which is currently not active, or which
// is finished. IndexedDB.jsm:101:46
//
// Assuming that this error is caused by a timing issue while
// accessing the store concurrently, we simply try to circumvent this by
// reloading ourselves

console.debug("Caught exception while reading settings. Reloading extension.", error);
browser.runtime.reload();
}

browser.icApi.onIdentityChosen.addListener(
(identityId, action, windowId, info) => this.identityChosen(identityId, action, windowId, info));
console.debug('IdentityChooser#run: onIdentityChosen listener registered');

browser.windows.getCurrent({populate: true}).then((window) => {
for (let tab of window.tabs) {
this.tabCreated(tab);
}
});

// browser.windows.onCreated.addListener(this.windowCreated);
// browser.windows.onRemoved.addListener(this.windowRemoved);

browser.tabs.onCreated.addListener(this.tabCreated);
browser.tabs.onUpdated.addListener(this.tabUpdated);
browser.tabs.onRemoved.addListener(this.tabRemoved);
Expand All @@ -36,7 +57,7 @@ class IdentityChooser {
console.log(tab);

if(tab.type == 'mail' && tab.status == 'complete') {
self.initNormalTab(tab);
self.init3PaneTab(tab);
} else if(tab.type == 'messageDisplay' && tab.status == 'complete') {
self.initMessageDisplayTab(tab);
}
Expand All @@ -48,7 +69,7 @@ class IdentityChooser {
console.log('IdentityChooser#tabUpdated -- begin: ' + tabId);

if(tab.type == 'mail' && changeInfo.status == 'complete') {
self.initNormalTab(tab);
self.init3PaneTab(tab);
} else if(tab.type == 'messageDisplay' && changeInfo.status == 'complete') {
self.initMessageDisplayTab(tab);
}
Expand All @@ -62,10 +83,38 @@ class IdentityChooser {
console.log('IdentityChooser#tabRemoved: ' + tabId);
}

async initNormalTab(tab) {
let menuId = await browser.icMenupopupApi.createMenupopup(tab.id, 'icMenu1');
let menuId2 = await browser.icMenupopupApi.createMenupopup(tab.id, 'icMenu2');
let menuId3 = await browser.icMenupopupApi.createMenupopup(tab.id, 'icMenu3');
async init3PaneTab(tab) {
console.debug('IdentityChooser#run: iterate over accounts and identities');
var icIdentities = new IcIdentities(this.icOptions);
var identities = await icIdentities.getIdentities();

let menuId = await browser.icMenupopupApi.createMenupopup(tab.id,
'ic-new-message-popup');

for (const identity of identities) {
if (identity !== undefined){
console.debug(`IdentityChooser#run: found ${identity.accountId}, ${identity.id}`);
if (identity.showInMenu) {
var icIdentity = {
"id": identity.id,
"label": identity.label
}

var isEnabledComposeMessage =
await this.icOptions.isEnabledComposeMessage();
if (isEnabledComposeMessage) {
console.debug('IdentityChooser#run: add identity ',
icIdentity,
'to compose');
let menuId =
await browser.icMenupopupApi.addMenuitem(tab.id,
'ic-new-message-popup',
identity.id,
identity.label);
}
}
}
}
}

async initMessageDisplayTab(tab) {
Expand All @@ -75,12 +124,6 @@ class IdentityChooser {
async run() {
console.debug("IdentityChooser#run -- begin");

browser.windows.getCurrent().then((window) => this.initMenupopup(window));

return;

// console.debug(browser.document.getElementById("folderPaneWriteMessage"));

try {
await this.icOptions.setupDefaultOptions();
} catch (error) {
Expand Down
54 changes: 49 additions & 5 deletions icmenupopupapi/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const xulNS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';

class Menupopup {
constructor() {
this.managedMenus = {};
this.managedMenus = [];
}

async createMenupopup(menuId, tabId, realTab, realTabWindow) {
Expand All @@ -21,13 +21,52 @@ class Menupopup {
menuPopup.id = menuId;
popupSet.appendChild(menuPopup);

this.addToManagedMenus(tabId, menuPopup);
this.addToManagedMenus(tabId, menuPopup, current3Pane);

console.log(this.managedMenus);

let newMsgButton = current3Pane.document.querySelector('#folderPaneWriteMessage');
let newMsgButtonParent = newMsgButton.parentNode;

// We register ourselves at the parent of the new message button
// and fire during the capture phase in order to make sure to
// "overwrite" the default click handler
newMsgButtonParent.addEventListener("click", event => {
console.log('newmsgparent click -- start');

console.log(event.target == newMsgButton);
if(event.target == newMsgButton) {
menuPopup.openPopup(event.target, "after_start", 0, 0, true);

event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
}

console.log(event);

console.log('newmsgparent click -- end');
}, true);

console.log(newMsgButton);
// console.log(top);

return menuId;
}


async addMenuitem(tabId, menuId, menuitemId, label) {
let [ menuPopup, current3Pane ] = this.managedMenus[tabId];

let menuItem = current3Pane.document.createElementNS(xulNS,
'menuitem');
menuItem.id = menuitemId;
menuItem.setAttribute('label', label);

menuPopup.appendChild(menuItem);
}


async ensure3PaneLoaded(current3Pane) {
// ensure 3pane is completely loaded
if(current3Pane.document.readyState != "complete") {
Expand All @@ -39,13 +78,13 @@ class Menupopup {
return current3Pane;
}

async addToManagedMenus(tabId, menu) {
async addToManagedMenus(tabId, menu, current3Pane) {
if(tabId in this.managedMenus) {
let menuList = this.managedMenus[tabId];
menuList.push(menu);
menuList.push([ menu, current3Pane ]);

} else {
this.managedMenus[tabId] = [ menu ];
this.managedMenus[tabId] = [ menu, current3Pane ];
}
}
}
Expand All @@ -64,6 +103,11 @@ var icMenupopupApi = class extends ExtensionCommon.ExtensionAPI {
let realTabWindow = tabObject.window;

return menuPopup.createMenupopup(menuId, tabId, realTab, realTabWindow);
},
async addMenuitem(tabId, menuId, menuitemId, label) {
console.log('addMenuitem: ' + tabId + '|' + menuId);

return menuPopup.addMenuitem(tabId, menuId, menuitemId, label);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions icmenupopupapi/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@
"type": "any"
}
]
},
{
"name": "addMenuitem",
"description": "add a sub menu popup structure to DOM",
"type": "function",
"async": true,
"parameters": [
{
"name": "tabId",
"description": "tab id",
"type": "any"
},
{
"name": "menuId",
"description": "menu id",
"type": "any"
},
{
"name": "menuitemId",
"description": "menu item id",
"type": "any"
},
{
"name": "label",
"description": "menu label",
"type": "any"
}
]
}
]
}
Expand Down

0 comments on commit 34acfd5

Please sign in to comment.