nsIMacDockSupport 编辑

widget/nsIMacDockSupport.idlScriptable Provides access to the Dock on Mac OS X. 1.0 66 Introduced Gecko 2.0 Inherits from: nsIMacDockSupport Last changed in Gecko 11.0 (Firefox 11.0 / Thunderbird 11.0 / SeaMonkey 2.8)

Implemented by: @mozilla.org/cookie-monster;1. To create an instance, use:

var dock = Components.classes["@mozilla.org/widget/macdocksupport;1"]
                    .getService(Components.interfaces.nsIMacDockSupport);

See Working with the Mac OS X Dock for details and examples.

Method summary

void activateApplication(in boolean aIgnoreOtherApplications);

Attributes

AttributeTypeDescription
badgeTextAStringText to display in a badge on the application's dock icon. This can be used, for example, to display the number of unread messages in an email client.
dockMenunsIStandaloneMenuThe menu to display when the user right-clicks on the application's icon in the dock.

Methods

activateApplication()

Activates the application, making it the frontmost application. The application should call this to activate itself when one of its dock menu items are selected, since doing so does not automatically activate the application.

void activateApplication(
  in boolean aIgnoreOtherApplications
);
Parameters
aIgnoreOtherApplications
If true, the application is activated regardless of the state of other applications. If false, the application is only activated if other applications are not currently active.

About dockMenu

By default Firefox adds two menu items to the dock menu. The menu items are "New Window" and "New Private Window". If permanent private browsing mode is enabled then the "New Private Window" menu item is hidden. This is created by using the dockMenu attribute of nsIMacDockSupport here: http://mxr.mozilla.org/mozilla-release/source/browser/base/content/browser.js#1562

This is seen here:

Graphic of default native menu created on browser startup

 

If you were to copy and follow that example you would replace the default native menu. This can be done like this:

var dockSupport = Cc['@mozilla.org/widget/macdocksupport;1'].getService(Ci.nsIMacDockSupport);
console.log('dockSupport:', dockSupport);

var win = Services.wm.getMostRecentWindow('navigator:browser');
var macMenu = win.document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menupopup');
macMenu.setAttribute('id', 'myMacMenu');
var macMenuItem = win.document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuitem');
macMenuItem.setAttribute('label', 'Show Most Recent Window');
macMenuItem.setAttribute('id', 'myMacMenuItem');
macMenuItem.addEventListener('command', function(){
  var dockSupport = Cc['@mozilla.org/widget/macdocksupport;1'].getService(Ci.nsIMacDockSupport);
  dockSupport.activateApplication(true);
  Services.wm.getMostRecentWindow(null).focus()
})
macMenu.appendChild(macMenuItem);
var mainPopupSet = win.document.getElementById('mainPopupSet');
mainPopupSet.appendChild(macMenu);

let dockMenuElement = macMenu; //document.getElementById("menu_mac_dockmenu");66
let nativeMenu = Cc["@mozilla.org/widget/standalonenativemenu;1"].createInstance(Ci.nsIStandaloneNativeMenu);

console.log('dockMenuElement:', dockMenuElement);

nativeMenu.init(dockMenuElement);
dockSupport.dockMenu = nativeMenu;

This replaces the default menu with this one menuitem that says "Show Most Recent Window". Notice the activateApplication(true), if this is not done then Firefox will not be activated. The result of this code is shown below.

Graphic of native menu replacement

Notice how the "New Window" and "New Private Window" menuitem's are missing. Therefore, if you would like to add or remove items to the menu it is recommended to manipulate the default menu item which is on the hidden window of Firefox. Note: The hidden window of Firefox (Services.appShel.hiddenDOMWindow) loads on browser startup, so if you would like to access it on startup of the browser make sure to check and wait for the window to be loaded. This can be done by adding a event listenr for load of the hidden window.

This is the recommended way to add to the native menu, just maniuplate the default menu DOM element.

var macMenuItem = Services.appShell.hiddenDOMWindow.document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'menuitem');
macMenuItem.setAttribute('label', 'Show Most Recent Window');
macMenuItem.setAttribute('id', 'myMacMenuItem');
macMenuItem.addEventListener('command', function(){
  var dockSupport = Cc['@mozilla.org/widget/macdocksupport;1'].getService(Ci.nsIMacDockSupport);
  dockSupport.activateApplication(true);
  Services.wm.getMostRecentWindow(null).focus()
})
Services.appShell.hiddenDOMWindow.document.getElementById('menu_mac_dockmenu').appendChild(macMenuItem)

This adds the "Show Most Recent Window" menuitem from the previous example as a third item. Two types of elements:

  • menu(if you would like to have sub menu items, make sure to set a label on the "menu" element)

  • menuitem(like in example above)

Elements can be hidden by adding the hiddenattribute to the menu or menuitem and setting it to true      .

 
 

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:36 次

字数:8119

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文