diff --git a/background.js b/background.js index bb3ccaa..0b5ce48 100644 --- a/background.js +++ b/background.js @@ -32,9 +32,9 @@ class IdentityChooser { 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.icMenupopupApi.onMenuClicked.addListener( + (menuitemId, menuId, tabId, info) => this.menuItemClicked(menuitemId, menuId, tabId, info)); + console.debug('IdentityChooser#run: onMenuClicked listener registered'); browser.windows.getCurrent({populate: true}).then((window) => { for (let tab of window.tabs) { @@ -51,6 +51,42 @@ class IdentityChooser { return; } + async menuItemClicked(menuitemId, menuId, tabId, info) { + console.log('background: menuItemClicked'); + + console.log(menuitemId); + console.log(menuId); + console.log(tabId); + console.log(info); + + let identityId = menuitemId; + let messageFormat = await browser.composePrefsApi.getMessageFormat(identityId); + + if(info.includes("Shift")) { + if(messageFormat == "text/plain") { + messageFormat = "text/html"; + } else { + messageFormat = "text/plain"; + } + } + + console.debug('IdentityChooser#menuItemClicked: messageFormat: ', messageFormat); + if(menuId == "ic-new-message-popup") { + console.debug('IdentityChooser#menuItemClicked: open compose editor'); + if(messageFormat == "text/plain") { + browser.compose.beginNew({ + "identityId": identityId, + "isPlainText" : true + }); + } else { + browser.compose.beginNew({ + "identityId": identityId, + "isPlainText" : false + }); + } + } + } + async tabCreated(tab) { console.log('IdentityChooser#tabCreated -- begin:' + tab.id); diff --git a/icmenupopupapi/implementation.js b/icmenupopupapi/implementation.js index 3e49cd5..82dd753 100644 --- a/icmenupopupapi/implementation.js +++ b/icmenupopupapi/implementation.js @@ -1,4 +1,5 @@ var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm"); +var { EventEmitter } = ChromeUtils.import("resource://gre/modules/EventEmitter.jsm"); const xulNS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'; @@ -19,6 +20,7 @@ class Menupopup { let menuPopup = current3Pane.document.createElementNS(xulNS, 'menupopup'); menuPopup.id = menuId; + menuPopup.addEventListener("command", (event) => this.popupClicked(event, menuId, tabId)); popupSet.appendChild(menuPopup); this.addToManagedMenus(tabId, menuPopup, current3Pane); @@ -88,9 +90,44 @@ class Menupopup { this.managedMenus[tabId] = [ menu, current3Pane ]; } } + + popupClicked(event, menuId, tabId) { + console.log("popupClicked"); + console.log(event); + console.log("tabId: " + tabId); + console.log("menuId: " + menuId); + + let src = event.target; + let info = []; + + if(event.shiftKey) { + info.push("Shift"); + } + + if(event.ctrlKey) { + info.push("Ctrl"); + } + + if(event.altKey) { + info.push("Alt"); + } + + if(event.metaKey) { + info.push("Command"); + } + + var menuitemId = src.value; + eventEmitter.emit("menu-command-event", + menuitemId, + menuId, + tabId, + info); + + } } const menuPopup = new Menupopup(); +const eventEmitter = new EventEmitter(); var icMenupopupApi = class extends ExtensionCommon.ExtensionAPI { getAPI(context) { @@ -109,7 +146,21 @@ var icMenupopupApi = class extends ExtensionCommon.ExtensionAPI { console.log('addMenuitem: ' + tabId + '|' + menuId); return menuPopup.addMenuitem(tabId, menuId, menuitemId, label); - } + }, + onMenuClicked: new ExtensionCommon.EventManager({ + context, + name: "icMenupopupApi.onMenuClicked", + register(fire) { + function callback(event, menuitemId, menuId, tabId, info) { + return fire.async(menuitemId, menuId, tabId, info); + } + + eventEmitter.on("menu-command-event", callback); + return function() { + eventEmitter.off("menu-command-event", callback); + }; + }, + }).api() } } } diff --git a/icmenupopupapi/schema.json b/icmenupopupapi/schema.json index afc8a71..d80484d 100644 --- a/icmenupopupapi/schema.json +++ b/icmenupopupapi/schema.json @@ -1,6 +1,22 @@ [ { "namespace": "icMenupopupApi", + "types": [ + { + "id": "IcOnClickData", + "type": "object", + "properties": { + "modifiers": { + "type": "array", + "items": { + "type": "string", + "enum": ["Shift", "Alt", "Command", "Ctrl", "MacCtrl"] + }, + "description": "An array of keyboard modifiers that were held while the menu item was clicked." + } + } + } + ], "functions": [ { "name": "createMenupopup", @@ -48,6 +64,36 @@ } ] } + ], + "events": [ + { + "name": "onMenuClicked", + "description": "fires when the user clicks on a menu item", + "type": "function", + "parameters": [ + { + "name": "menuitemId", + "description": "id of the identity that was chosen", + "type": "string" + }, + { + "name": "menuId", + "description": "id of menu that was clicked on", + "type": "string" + }, + { + "name": "tabId", + "description": "tab where the menu was clicked", + "type": "integer" + }, + { + "name": "info", + "optional": true, + "description": "Information sent when a menu is clicked.", + "$ref": "IcOnClickData" + } + ] + } ] } ]