I've built a small Thunderbird addon that adds a toolbar button. It works as expected.
However there are cases that the button should look disabled. This is similar to "Reply" and "Reply All" buttons of Thunderbird. If there is no email selected, than those buttons look disabled.
I want to do the same with my addon. I've written the algorithm to refresh the button but I don't know how to trigger it. How can I trigger it when the selection changes in the emails list?
The code is tested and working:
var OpenConversation = {
refresh: function () {
document.getElementById("open-conversation").disabled = ! OpenConversation.isEnabled()
},
// Based on: https://github.com/mozilla/releases-comm-central/blob/9ba3a1faeb6db90254d7e67d9d0dd630fd1a90be/mail/base/content/mail3PaneWindowCommands.js#L330-L360
isEnabled: function () {
let numSelected = GetNumSelectedMessages();
if (numSelected == 1) {
if (! gFolderDisplay.getCommandStatus(nsMsgViewCommandType.cmdRequiringMsgBody))
return false;
// Check if we have a collapsed thread selected and are summarizing it.
// If so, selectedIndices.length won't match numSelected. Also check
// that we're not displaying a message, which handles the case
// where we failed to summarize the selection and fell back to
// displaying a message.
if (gFolderDisplay.selectedIndices.length != numSelected &&
command != "cmd_applyFiltersToSelection" &&
gDBView && gDBView.currentlyDisplayedMessage == nsMsgViewIndex_None)
return false;
return true;
}
return false;
}
};
/*
* Instead of this, events should be used. Whenever user selects/deselects mails in
* the list, `OpenConversation.refresh()` should be triggered.
*/
window.setInterval(OpenConversation.refresh, 50);