Skip to content

Commit

Permalink
Add dropdown menu for move/copy actions to buttons
Browse files Browse the repository at this point in the history
Button click triggers the default action as configured in add-on preferences (same behavior as before).
  • Loading branch information
moisseev committed Jul 27, 2024
1 parent 81e9b02 commit 37cf545
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 32 deletions.
45 changes: 39 additions & 6 deletions experiments/trainButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ var trainButtons = class extends ExtensionCommon.ExtensionAPI {
const toolbarbuttonId = "rspamdSpamnessButton" + cls;
if (document.getElementById(toolbarbuttonId)) return;

// The button container
const toolbarbutton = document.createXULElement("toolbarbutton");
toolbarbutton.setAttribute("is", "toolbarbutton-menu-button");
toolbarbutton.setAttribute("type", majorVersion < 111 ? "menu-button" : "menu");
toolbarbutton.id = toolbarbuttonId;
toolbarbutton.classList
.add("toolbarbutton-1", majorVersion < 100 ? "msgHeaderView-button" : "message-header-view-button");
Expand All @@ -43,15 +46,44 @@ var trainButtons = class extends ExtensionCommon.ExtensionAPI {
.localizeMessage("spamness.buttonTrain" + cls + ".tooltip")
);

const primaryButton = document.createXULElement("toolbarbutton");
primaryButton.classList.add("box-inherit", "toolbarbutton-menubutton-button");

const img = document.createElement("img");
img.src = imageURL;
toolbarbutton.appendChild(img);
primaryButton.appendChild(img);

const label = document.createElement("label");
if (majorVersion < 110) label.style.display = "-moz-inline-box";
label.innerHTML = context.extension.localeData
.localizeMessage("spamness.buttonTrain" + cls + ".label");
toolbarbutton.appendChild(label);
label.setAttribute("value", "label");
primaryButton.appendChild(label);

toolbarbutton.appendChild(primaryButton);

const dropmarker = document.createXULElement("dropmarker");
dropmarker.classList.add("toolbarbutton-menubutton-dropmarker");
toolbarbutton.appendChild(dropmarker);

const menupopup = document.createXULElement("menupopup");

const moveItem = document.createXULElement("menuitem");
moveItem.setAttribute("label", "Move");
moveItem.setAttribute("data-action", "move");

const copyItem = document.createXULElement("menuitem");
copyItem.setAttribute("label", "Copy");
copyItem.setAttribute("data-action", "copy");

menupopup.appendChild(moveItem);
menupopup.appendChild(copyItem);
toolbarbutton.appendChild(menupopup);

dropmarker.addEventListener("click", (event) => {
event.stopPropagation();
menupopup.openPopup(toolbarbutton, "after_end", 0, 0, true, false);
});

toolbar.insertBefore(toolbarbutton, toolbar.firstChild);
targetIds.push(toolbarbuttonId);
Expand All @@ -77,14 +109,15 @@ var trainButtons = class extends ExtensionCommon.ExtensionAPI {
context,
name: "trainButtons.onButtonCommand",
register(fire, targetId, windowId, tabIndex) {
function callback(event) {
return fire.async(event.target.id);
function handleButtonClick(event) {
const selectedAction = event.target.dataset.action;
return fire.async(selectedAction);
}
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
const target = document.getElementById(targetId);
target.addEventListener("command", callback);
target.addEventListener("command", handleButtonClick);
return function () {
target.removeEventListener("command", callback);
target.removeEventListener("command", handleButtonClick);
};
}
}).api(),
Expand Down
58 changes: 32 additions & 26 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ function sortSymbols(order) {
messageHeader.updateHeaders();
}

async function moveMessage(buttonId, windowId, tabIndex) {
async function moveMessage(buttonId, windowId, tabIndex, selectedAction) {
// eslint-disable-next-line no-useless-assignment
let ids = [];
// eslint-disable-next-line no-useless-assignment
let message = null;
const tabs = await browser.tabs.query({active: true, currentWindow: true});

Expand Down Expand Up @@ -58,7 +60,7 @@ async function moveMessage(buttonId, windowId, tabIndex) {
return;
}
const folders = path.trim().replace(/(^\/|\/$)/g, "").split("/");
const destination = folders.reduce(function (prev, curr, i, arr) {
const destination = folders.reduce((prev, curr, i, arr) => {
const folder = (i) ? prev.subFolders : prev.folders;
const subFolder =
folder.find((f) => f.path.replace(/.*[/]/, "") === decodeURIComponent(curr) || f.name === curr);
Expand All @@ -72,36 +74,40 @@ async function moveMessage(buttonId, windowId, tabIndex) {
return;
}

browser.storage.local.get(["trainingButtonHam-defaultAction", "trainingButtonSpam-defaultAction"])
.then((localStorage) => {
const action = (buttonId === "rspamdSpamnessButtonHam" &&
(localStorage["trainingButtonHam-defaultAction"] === "copy") ||
buttonId === "rspamdSpamnessButtonSpam" &&
(localStorage["trainingButtonSpam-defaultAction"] === "copy"))
? "copy"
: "move";
async function getDefaultAction() {
const localStorage =
await browser.storage.local.get(["trainingButtonHam-defaultAction", "trainingButtonSpam-defaultAction"]);
return (buttonId === "rspamdSpamnessButtonHam" &&
(localStorage["trainingButtonHam-defaultAction"] === "copy") ||
buttonId === "rspamdSpamnessButtonSpam" &&
(localStorage["trainingButtonSpam-defaultAction"] === "copy"))
? "copy"
: "move";
}

if (action === "move" && message.external) {
libBackground.displayNotification("spamness.alertText.moveNotPermittedForExternalMessages");
return;
}
const action = selectedAction || await getDefaultAction();

browser.messages[action](ids, destination).catch(function (error) {
libBackground.error(error);
if ((/^Unexpected error (copy|mov)ing messages: 2147500037$/).test(error.message)) {
libBackground.displayNotification(
"spamness.alertText.error_2147500037_workaround",
error.message + "\n\n"
);
}
});
});
if (action === "move" && message.external) {
libBackground.displayNotification("spamness.alertText.moveNotPermittedForExternalMessages");
return;
}

browser.messages[action](ids, destination).catch(function (error) {
libBackground.error(error);
if ((/^Unexpected error (copy|mov)ing messages: 2147500037$/).test(error.message)) {
libBackground.displayNotification(
"spamness.alertText.error_2147500037_workaround",
error.message + "\n\n"
);
}
});
}

function addTrainButtonsToWindow(windowId, tabIndex) {
browser.trainButtons.addButtonsToWindowById(windowId, tabIndex).then((targetIds) => {
targetIds.forEach(function (targetId) {
browser.trainButtons.onButtonCommand.addListener((buttonId) => {
moveMessage(buttonId, windowId, tabIndex);
browser.trainButtons.onButtonCommand.addListener((selectedAction) => {
moveMessage(targetId, windowId, tabIndex, selectedAction);
}, targetId, windowId, tabIndex);
});
});
Expand Down

0 comments on commit 37cf545

Please sign in to comment.