xul 创建一个按钮

  1. MDN
  2. Mozilla 产品与私有技术
  3. Mozilla 私有技术
  4. XUL
  5. Toolbars
  6. 添加工具栏按钮 (定制工具栏)

添加工具栏按钮 (定制工具栏)

在本文章中

    1. 创建一个 overlay
    2. 在工具栏添加按钮
    3. 为按键应用风格
      1. 图标大小
      2. CSS 样式表
      3. 应用样式表
    4. 常见错误
    5. 常见工具栏的 overlayed windows
    6. 更多信息

此文章解释如何使用 overlays 为工具包(firefox,Thunderbird 或 Kompozer) 添加工具栏按钮(就是浏览器右上方一系列按钮,home,下载之类的)。适用用户是拥有 XUL 和 CSS 基础知识的 扩展 开发人员。

我们假设您已经会创建基础的火狐插件,并且已经成功创建了 Hello World extension ,另外,还有一份更加完全的初学者示例指南,请查看 自定义工具栏按钮。

创建一个 overlay

The first step is to create an overlay for the document containing the toolbar you wish to enhance. Explaining overlays is beyond the scope of this tutorial -- you can read about them in the XUL Tutorial.

To overlay a document, you need to know its URI. You can find a list of URIs for the most commonly overlaid documents at the bottom of this page.

Note: Some people overlay chrome://messenger/content/mailWindowOverlay.xul. That should cause the button to appear on all windows that mailWindowOverlay.xul is applied to (i.e. Main window and View Message window). This needs to be looked into.

在工具栏添加按钮

Toolkit applications have customizable toolbars; therefore, it‘s common practice for extensions to add their toolbar buttons to the toolbar palette, rather than adding them directly to the toolbar. The latter is possible but is not recommended and is harder to implement.

Adding a button to the toolbar palette is very easy. Just add code like this to your overlay:

<toolbarpalette id="BrowserToolbarPalette">
  <toolbarbutton id="myextension-button" class="toolbarbutton-1"
    label="&toolbarbutton.label;" tooltiptext="&toolbarbutton.tooltip;"
    oncommand="MyExtension.onToolbarButtonCommand(event);"/>
</toolbarpalette>

注意:

  • The id of the palette (BrowserToolbarPalette in the example) depends on the window whose toolbar you wish to insert a button into. See below for the list of common palette IDs.
  • class="toolbarbutton-1" makes the toolbar button appear correctly in Icons and Text mode; it also adjusts padding.
  • If you need to handle middle-click, add this line after the oncommand line.
onclick="checkForMiddleClick(this, event)"
  • you can also handle middle-lick and right-click using onclick handler and check event.button in it. like this:
<toolbarpalette id="BrowserToolbarPalette">
  <toolbarbutton id="myextension-button" class="toolbarbutton-1"
    label="&toolbarbutton.label;" tooltiptext="&toolbarbutton.tooltip;"
    onclick="MyExtension.onclick(event);"/>
</toolbarpalette>
onclick: function(event) {
  switch(event.button) {
    case 0:
      // Left click
      break;
    case 1:
      // Middle click
      break;
    case 2:
      // Right click
      break;
  }
}

To add more buttons, put more <toolbarbutton> elements inside the <toolbarpalette> element. Wrap elements other than <toolbarbutton> in<toolbaritem>.

为按键应用风格

Most toolbar buttons have an icon. To attach an image to the button we use standard Mozilla skinning facilities. If you‘re unfamiliar with how that works, read the skinning section of Jonah Bishop‘s excellent Toolbar Tutorial. Although the article covers creating an entire toolbar, rather than just a button, it has a great explanation of the techniques we‘ll use here.

图标大小

Toolbar buttons can have two different sizes -- big and small. This means you‘ll need to provide two icons for each of your toolbar buttons. The dimensions of the icons in various applications for both modes are summarized in the following table (feel free to add information about other applications):

Application (Theme name) Big icon size Small icon size
Firefox 1.0 (Winstripe) 24x24 16x16
Thunderbird 1.0 (Qute) 24x24 16x16

CSS 样式表

To set the image for your toolbar button, use the following CSS rules:

/*  skin/toolbar-button.css  */

#myextension-button {
  list-style-image: url("chrome://myextension/skin/btn_large.png");
}

toolbar[iconsize="small"] #myextension-button {
  list-style-image: url("chrome://myextension/skin/btn_small.png");
}

应用样式表

Remember to attach the stylesheet you created to both the overlay file and the Customize Toolbar window. To attach it to the overlay, put this processing instruction (PI) at the top of the overlay file:

<?xml-stylesheet href="chrome://myextension/skin/toolbar-button.css" type="text/css"?>

Note: The CSS file with your toolbar styles needs to be included in the overlay file, as you would expect, but also in the chrome.manifest file. This is very important because the toolbar customization dialog won‘t work correctly without this.

To include the style on your chrome.manifest file:

style chrome://global/content/customizeToolbar.xul chrome://myextension/skin/toolbar-button.css

If you are developing for Firefox 1.0, attach it to the Customize Toolbar window (chrome://global/content/customizeToolbar.xul) usingskin/contents.rdf. The code looks like this:

<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:chrome="http://www.mozilla.org/rdf/chrome#">

  <Seq about="urn:mozilla:skin:root">
    <li resource="urn:mozilla:skin:classic/1.0"/>
  </Seq>

  <Description about="urn:mozilla:skin:classic/1.0">
    <chrome:packages>
      <Seq about="urn:mozilla:skin:classic/1.0:packages">
        <li resource="urn:mozilla:skin:classic/1.0:myextension"/>
      </Seq>
    </chrome:packages>
  </Description>

  <Seq about="urn:mozilla:stylesheets">
    <li resource="chrome://global/content/customizeToolbar.xul"/>
  </Seq>

  <Seq about="chrome://global/content/customizeToolbar.xul">
    <li>chrome://myextension/skin/toolbar-button.css</li>
  </Seq>
</RDF>

The skin/contents.rdf file is denigrated in developing for later releases of Firefox. Extensions for Firefox/Thunderbird 1.5 and above should instead use something like this in their chrome.manifest:

skin	myextension	classic/1.0	chrome/skin/
style	chrome://global/content/customizeToolbar.xul	chrome://myextension/skin/toolbar-button.css
ia

Take note of the Packaging section in this article; you may need to include .jar references if you are delivering your extension as a .xpi file.

常见错误

This is a list of the most common mistakes made by extension authors, including both symptoms and solutions.

Problem: The whole set of default buttons is painted on the toolbar or in the Customize Toolbars window, instead of your own icon.

Caused by: Malformed or not applied stylesheet.

Solution: Check to be sure your stylesheet is correct, make sure your contents.rdf (or chrome.manifest) is correct, and be sure you didn‘t forget to apply the stylesheet to customizeToolbar.xul.

常见工具栏的 overlayed windows

URL Application and affected window(s) Palette id
chrome://browser/content/browser.xul Firefox - Main window BrowserToolbarPalette
chrome://navigator/content/navigator.xul SeaMonkey 2.0 - Browser window BrowserToolbarPalette
chrome://messenger/content/messenger.xul Thunderbird - Main window MailToolbarPalette
chrome://messenger/content/messenger...gercompose.xul Thunderbird - Compose window MsgComposeToolbarPalette
chrome://messenger/content/addressbo...ddressbook.xul Thunderbird - Address book AddressBookToolbarPalette
chrome://editor/content/editor.xul Kompozer - Main window NvuToolbarPalette
chrome://calendar/content/calendar.xul Sunbird - Main window calendarToolbarPalette

更多信息

时间: 2024-07-31 14:25:58

xul 创建一个按钮的相关文章

jQuery EasyUI使用教程之创建一个链接按钮

<jQuery EasyUI最新版下载> 本教程主要为大家展示如何使用jQuery EasyUI创建一个链接按钮.通常情况下,使用"button/"元素来创建一个按钮:使用"a/"元素来创建链接按钮.所以事实上一个链接按钮是一个显示为按钮样式"a/"元素. 查看演示 为了创建一个链接按钮,你所需要做的就是添加一个名为'easyui-linkbutton'的类属性到"a/"元素中: < div style =

【细解】如何基于bootstrap创建一个响应式的导航条

本地下载 最终实现效果如下: 首先你得引入bootstrap与jquery 推荐一个CDN:http://cdn.gbtags.com/index.html 然后就是开始编写HTML代码.如果你不想更改显示效果的话实际上CSS都免去写了2333 因为HTML代码比较多 这里分为三个部分 然后最后再上一份整体HTML代码 首先如上图所示的,实现这个效果需要了解bootstrap的以下几个组件 导航条 按钮 表单 下拉菜单 实际上以上几个组件的样式有很多.我们只需要了解一部分即可 如需了解更多的请转

python Tkinter 基础添加一个按钮,在按钮上显示文字,并设置文字颜色

code; # Tkinter 基础添加一个按钮,在按钮上显示文字,并设置文字颜色 import tkinter as tk # 面向对象编程 class App(): def __init__(self, master): frame = tk.Frame(master) # 框架??不是很理解 frame.pack() # 创建一个按钮,fg前景色:蓝色 self.testButton = tk.Button(frame, text = "hello", fg = "bl

iOS开发---UIButton 1 //创建一个可以显示图片的按钮。

1 //创建一个可以显示图片的按钮. 2 -(void)creatImageBtn{ 3 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 4 btn.frame = CGRectMake(100, 100, 100, 100); 5 UIImage *icon1 = [UIImage imageNamed:@"icon1.png"]; 6 UIImage *icon2 = [UIImage imageNamed

利用Zynq Soc创建一个嵌入式工程

英文题目:Using the Zynq SoC Processing System,参考自ADI的ug1165文档. 利用Zynq Soc创建一个嵌入式工程,该工程总体上包括五个步骤: 步骤一.新建空白工程 步骤二.创建一个Embedded Processor工程 步骤三.Zynq7 Processing System的管理 步骤四.综合仿真.编译运行.生成二进制文件 步骤五.Exporting Hardware to SDK 步骤一.新建工程 1. 点击Vivado图标启动软件,Create

搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)

搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating-a-wpf-chat-client-server-application/ 注意:本教程是相当广泛的,如果你是在短请也看到我们的东西 开始和 如何在几分钟内创建一个客户端服务器应用程序教程. 注2:本例中包括,明显延长进一步证明功能,在包中包含的示例 包下载. 在我们开始之前确保您已经安装了Vis

使用ABP创建一个《电话簿项目》-MPA版本

此篇博客是属于半教程博客,为什么说是半教程呢.因为我不会打算说什么理论性的东西,没必要.要看理论性的资料以及基础信息,请前往tkb至简和@阳光铭睿的博客查看文档资料. 扫个小盲: MPA:为多页面,通过服务器端进行渲染razor进行绑定数据的方式(等其他情况. SPA:则为单页面,可以用作前后端分离如:vue,angular这些前端技术,而后端只需要提供webapi就可以了(当然还有其他方式. 1.首先创建一个模板解决方案"PhoneBook" 打开ABP的官方网站:http://ww

Xcode 6 如何创建一个用于纯手写UI界面的Empty Application?

1. 运行Xcode6, 创建一个Single View Application工程. 2. 随后删除Main.storyboard和LaunchScreen.xib,扔进废纸篓. 3.打开Info.plist,把Launch screen interface file base name,以及Main storyboard file base name两项,删除(点击旁边的减号即可) 4. 打开工程项目属性文件,点击Target下面的第一项,再选择General选项卡,向下找到Use Asse

Maven和Eclipse:m2eclipse -- 创建一个简单的Maven项目

陈科肇-欢迎转载,转载请注明出来,谢谢! ================== 在Maven中,我们使用archetype来创建项目.而在Ecelipse中,我们通过新建项目向导来创建项目.Eeclipse中的新建项目向导为我们提供了大量的创建项目的模板.m2eclipse为这个向导添加了如下的功能: 1.从SCM仓库签出一个Maven项目 2.使用Maven archetype(Maven 原型,即模板)创建一个Maven项目 3.创建一个Maven POM文件 4.实操-例子 =======