【问题标题】:Thunderbird addon: Disable toolbar button programmaticallyThunderbird 插件:以编程方式禁用工具栏按钮
【发布时间】:2014-08-03 14:34:05
【问题描述】:

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

但是,在某些情况下,按钮应该看起来是禁用的。这类似于 Thunderbird 的“回复”和“全部回复”按钮。如果没有选择电子邮件,则这些按钮看起来已禁用。

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

代码已经过测试并且可以运行:

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

【问题讨论】:

  • 下载插件 DOMInspectorElementInspector 并按住 shift+右键单击禁用的按钮。您可能会找到一个属性disabled=true,然后您执行相同的操作来禁用您的按钮。顺便说一句土耳其语吧?酷:) tarkan!
  • 谢谢,我已经可以做到了(上面代码中的第 3 行)。我不能做的是在电子邮件选择更改时运行该代码(请参阅底部的setInterval 用法)。我怎样才能做到这一点?有什么特别活动吗?

标签: javascript firefox-addon mozilla thunderbird-addon


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    相关资源
    最近更新 更多