Thunderbird插件:以编程方式禁用工具栏按钮

时间:2014-08-03 14:34:06

标签: javascript firefox-addon mozilla thunderbird-addon

我已经构建了一个小Thunderbird addon,它添加了一个工具栏按钮。它按预期工作。

image

但是,有些情况下按钮应该看起来已禁用。这类似于"回复"和#34;全部回复"雷鸟的按钮。如果没有选择电子邮件,则这些按钮看起来已禁用。

image

我想对我的插件做同样的事情。我已经编写了算法来刷新按钮,但我不知道如何触发它。如何在电子邮件列表中更改选择时触发它?

代码经过测试并正常运行:

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);

1 个答案:

答案 0 :(得分:0)

通过收听UpdateMailToolbar()生成的通知来更新邮件工具栏。有两个通知:

  1. <commandset commandupdater="true" events="mail-toolbar" oncommandupdate="...">将触发命令更新。 mailWindowOverlay.xul中的命令集包含一堆<command>元素,并使用goUpdateCommand为每个元素更新它们。如果您没有使用命令控制器,那么您可以使用自己的更新逻辑创建自己的<commandset>元素。
  2. 还有"mail:updateToolbarItems"主题的观察者通知。通知的主题是其工具栏需要更新的窗口。 (如果您的代码已经存在于窗口中,则应该在进行任何不必要的工作之前检查主题是否是当前窗口。)您可能会发现此通知更容易被挂钩。
相关问题