1

I've built a small Thunderbird addon that adds a toolbar button. It works as expected.

image

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.

image

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);
maliayas
  • 2,674
  • 1
  • 18
  • 17
  • Download addon [DOMInspector](https://addons.mozilla.org/en-US/seamonkey/addon/dom-inspector-6622/) and [ElementInspector](https://addons.mozilla.org/en-US/firefox/addon/element-inspector/) and shift+right click on button that is disabled. You will probably find an attribute `disabled=true` then you do the same to disable your button. btw turkish huh? cool :) tarkan! – Noitidart Aug 03 '14 at 21:36
  • 1
    Thanks, I can already do that (line 3 in the above code). What I can't do is running that code whenever email selection changes (see `setInterval` usage at the bottom). How can I do that? Is there a special event for that? – maliayas Aug 03 '14 at 22:08

1 Answers1

0

The mail toolbar updates by listening to the notifications generated by UpdateMailToolbar(). There are two notifications:

  1. A <commandset commandupdater="true" events="mail-toolbar" oncommandupdate="..."> will fire a command update. The commandset in mailWindowOverlay.xul contains a bunch of <command> elements and updates them using goUpdateCommand for each one. If you are not using a command controller then you can create your own <commandset> element with its own update logic.
  2. There is also an observer notification for the "mail:updateToolbarItems" topic. The subject of the notification is the window whose toolbar needs updating. (If your code already lives in a window, you should check that the subject is the current window before doing any unnecessary work.) You may find that this notification is easier to hook into.
Neil
  • 54,642
  • 8
  • 60
  • 72