Skip to content

Commit

Permalink
NEW: React on clicks on new message button and open editor for chosen
Browse files Browse the repository at this point in the history
identity.
  • Loading branch information
speedball2001 committed Jul 26, 2023
1 parent 588db7a commit 9efea96
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
42 changes: 39 additions & 3 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down
53 changes: 52 additions & 1 deletion icmenupopupapi/implementation.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions icmenupopupapi/schema.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
]
}
]
}
]

0 comments on commit 9efea96

Please sign in to comment.