
正文
node-webkit教程(9)native api 之Tray(托盘)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
/* Font Definitions */
@font-face
{font-family:Helvetica;
panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:微软雅黑;
panose-1:2 11 5 3 2 2 4 2 2 4;}
@font-face
{font-family:华文隶书;
panose-1:2 1 8 0 4 1 1 1 1 1;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
{font-family:华文楷体;
panose-1:2 1 6 0 4 1 1 1 1 1;}
@font-face
{font-family:"\@微软雅黑";
panose-1:2 11 5 3 2 2 4 2 2 4;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"\@华文楷体";
panose-1:2 1 6 0 4 1 1 1 1 1;}
@font-face
{font-family:"\@华文隶书";
panose-1:2 1 8 0 4 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
h2
{mso-style-link:"标题 2 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-align:center;
text-indent:10.0pt;
line-height:115%;
background:#DBE5F1;
border:none;
padding:0cm;
font-size:14.0pt;
font-family:"Calibri","sans-serif";
text-transform:uppercase;
letter-spacing:.75pt;
font-weight:normal;}
h3
{mso-style-link:"标题 3 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
color:#243F60;
letter-spacing:.75pt;
font-weight:normal;}
h4
{mso-style-name:"标题 4\,代码";
mso-style-link:"标题 4 Char\,代码 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:24.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";
font-weight:normal;}
h5
{mso-style-link:"标题 5 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
color:#244061;
letter-spacing:.5pt;
font-weight:bold;}
h6
{mso-style-link:"标题 6 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;
font-weight:normal;}
p.MsoHeading7, li.MsoHeading7, div.MsoHeading7
{mso-style-link:"标题 7 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;}
p.MsoHeading8, li.MsoHeading8, div.MsoHeading8
{mso-style-link:"标题 8 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
font-size:9.0pt;
font-family:"Calibri","sans-serif";
text-transform:uppercase;
letter-spacing:.5pt;}
p.MsoHeading9, li.MsoHeading9, div.MsoHeading9
{mso-style-link:"标题 9 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
font-size:9.0pt;
font-family:"Calibri","sans-serif";
text-transform:uppercase;
letter-spacing:.5pt;
font-style:italic;}
p.MsoToc1, li.MsoToc1, div.MsoToc1
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoToc2, li.MsoToc2, div.MsoToc2
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:21.0pt;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoToc3, li.MsoToc3, div.MsoToc3
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:42.0pt;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{mso-style-link:"页眉 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-align:center;
text-indent:10.0pt;
line-height:115%;
layout-grid-mode:char;
border:none;
padding:0cm;
font-size:9.0pt;
font-family:"Calibri","sans-serif";}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{mso-style-link:"页脚 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
layout-grid-mode:char;
font-size:9.0pt;
font-family:"Calibri","sans-serif";}
p.MsoCaption, li.MsoCaption, div.MsoCaption
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
font-size:8.0pt;
font-family:"Calibri","sans-serif";
color:#365F91;
font-weight:bold;}
p.MsoTitle, li.MsoTitle, div.MsoTitle
{mso-style-link:"标题 Char";
margin-top:36.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
font-size:26.0pt;
font-family:"Calibri","sans-serif";
color:#4F81BD;
text-transform:uppercase;
letter-spacing:.5pt;}
p.MsoSubtitle, li.MsoSubtitle, div.MsoSubtitle
{mso-style-link:"副标题 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:50.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri","sans-serif";
color:#595959;
text-transform:uppercase;
letter-spacing:.5pt;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
em
{color:#243F60;
text-transform:uppercase;
letter-spacing:.25pt;
font-style:normal;}
p.MsoDocumentMap, li.MsoDocumentMap, div.MsoDocumentMap
{mso-style-link:"文档结构图 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
font-size:9.0pt;
font-family:宋体;}
p
{margin-right:0cm;
margin-left:0cm;
font-size:12.0pt;
font-family:宋体;}
code
{font-family:宋体;}
pre
{mso-style-link:"HTML 预设格式 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:宋体;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-link:"批注框文本 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:9.0pt;
font-family:"Calibri","sans-serif";}
p.MsoNoSpacing, li.MsoNoSpacing, div.MsoNoSpacing
{mso-style-link:"无间隔 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:36.0pt;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle
{margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast
{margin-top:0cm;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:36.0pt;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
p.MsoQuote, li.MsoQuote, div.MsoQuote
{mso-style-link:"引用 Char";
margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
text-indent:10.0pt;
line-height:115%;
font-size:12.0pt;
font-family:"Calibri","sans-serif";
font-style:italic;}
p.MsoIntenseQuote, li.MsoIntenseQuote, div.MsoIntenseQuote
{mso-style-link:"明显引用 Char";
margin-top:10.0pt;
margin-right:57.6pt;
margin-bottom:0cm;
margin-left:64.8pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:10.0pt;
line-height:115%;
border:none;
padding:0cm;
font-size:12.0pt;
font-family:"Calibri","sans-serif";
color:#4F81BD;
font-style:italic;}
span.MsoSubtleEmphasis
{color:#243F60;
font-style:italic;}
span.MsoIntenseEmphasis
{color:#243F60;
text-transform:uppercase;
letter-spacing:.5pt;
font-weight:bold;}
span.MsoSubtleReference
{color:#4F81BD;
font-weight:bold;}
span.MsoIntenseReference
{color:#4F81BD;
text-transform:uppercase;
font-weight:bold;
font-style:italic;}
span.MsoBookTitle
{letter-spacing:.45pt;
font-weight:bold;
font-style:italic;}
p.MsoTocHeading, li.MsoTocHeading, div.MsoTocHeading
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-align:center;
text-indent:10.0pt;
line-height:115%;
background:#4F81BD;
border:none;
padding:0cm;
font-size:24.0pt;
font-family:"Calibri","sans-serif";
color:white;
text-transform:uppercase;
letter-spacing:.75pt;
font-weight:bold;}
span.Char
{mso-style-name:"页眉 Char";
mso-style-link:页眉;}
span.Char0
{mso-style-name:"页脚 Char";
mso-style-link:页脚;}
span.1Char
{mso-style-name:"标题 1 Char";
mso-style-link:"标题 1";
font-family:"微软雅黑","sans-serif";
color:white;
text-transform:uppercase;
letter-spacing:.75pt;
background:#4F81BD;
font-weight:bold;}
span.Char1
{mso-style-name:"文档结构图 Char";
mso-style-link:文档结构图;
font-family:宋体;}
span.2Char
{mso-style-name:"标题 2 Char";
mso-style-link:"标题 2";
font-family:华文隶书;
text-transform:uppercase;
letter-spacing:.75pt;
background:#DBE5F1;}
span.3Char
{mso-style-name:"标题 3 Char";
mso-style-link:"标题 3";
color:#243F60;
letter-spacing:.75pt;}
span.4Char
{mso-style-name:"标题 4 Char\,代码 Char";
mso-style-link:"标题 4\,代码";}
span.5Char
{mso-style-name:"标题 5 Char";
mso-style-link:"标题 5";
color:#244061;
letter-spacing:.5pt;
font-weight:bold;}
span.6Char
{mso-style-name:"标题 6 Char";
mso-style-link:"标题 6";
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;}
span.7Char
{mso-style-name:"标题 7 Char";
mso-style-link:"标题 7";
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;}
span.8Char
{mso-style-name:"标题 8 Char";
mso-style-link:"标题 8";
text-transform:uppercase;
letter-spacing:.5pt;}
span.9Char
{mso-style-name:"标题 9 Char";
mso-style-link:"标题 9";
text-transform:uppercase;
letter-spacing:.5pt;
font-style:italic;}
span.Char2
{mso-style-name:"标题 Char";
mso-style-link:标题;
color:#4F81BD;
text-transform:uppercase;
letter-spacing:.5pt;}
span.Char3
{mso-style-name:"副标题 Char";
mso-style-link:副标题;
color:#595959;
text-transform:uppercase;
letter-spacing:.5pt;}
span.Char4
{mso-style-name:"无间隔 Char";
mso-style-link:无间隔;}
span.Char5
{mso-style-name:"引用 Char";
mso-style-link:引用;
font-style:italic;}
span.Char6
{mso-style-name:"明显引用 Char";
mso-style-link:明显引用;
color:#4F81BD;
font-style:italic;}
span.Char7
{mso-style-name:"批注框文本 Char";
mso-style-link:批注框文本;}
span.HTMLChar
{mso-style-name:"HTML 预设格式 Char";
mso-style-link:"HTML 预设格式";
font-family:宋体;}
span.Char8
{mso-style-name:"编号 Char";
mso-style-link:编号;
font-family:"Times New Roman","serif";}
p.a, li.a, div.a
{mso-style-name:编号;
mso-style-link:"编号 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:0cm;
line-height:150%;
font-size:10.5pt;
font-family:"Times New Roman","serif";}
span.sentence
{mso-style-name:sentence;}
p.a0, li.a0, div.a0
{mso-style-name:图;
mso-style-link:"图 Char";
margin:0cm;
margin-bottom:.0001pt;
text-align:center;
text-indent:24.0pt;
font-size:12.0pt;
font-family:"Calibri","sans-serif";}
span.Char9
{mso-style-name:"图 Char";
mso-style-link:图;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
span.cp
{mso-style-name:cp;}
span.nt
{mso-style-name:nt;}
span.nb
{mso-style-name:nb;}
span.p
{mso-style-name:p;}
span.nx
{mso-style-name:nx;}
span.s2
{mso-style-name:s2;}
span.kc
{mso-style-name:kc;}
span.mi
{mso-style-name:mi;}
span.kd
{mso-style-name:kd;}
span.o
{mso-style-name:o;}
span.na
{mso-style-name:na;}
span.s
{mso-style-name:s;}
span.s1
{mso-style-name:s1;}
p.code, li.code, div.code
{mso-style-name:code;
mso-style-link:"code Char";
margin-top:5.25pt;
margin-right:0cm;
margin-bottom:5.25pt;
margin-left:0cm;
text-indent:20.15pt;
line-height:10.0pt;
background:#F8F8F8;
border:none;
padding:0cm;
font-size:10.0pt;
font-family:Consolas;
color:#333333;
font-weight:bold;}
span.k
{mso-style-name:k;}
span.codeChar
{mso-style-name:"code Char";
mso-style-link:code;
font-family:Consolas;
color:#333333;
background:#F8F8F8;
font-weight:bold;}
span.c1
{mso-style-name:c1;}
.MsoChpDefault
{font-size:11.0pt;}
.MsoPapDefault
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
line-height:115%;}
/* Page Definitions */
@page WordSection1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
layout-grid:15.6pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
node-webkit教程(9)native api 之Tray(托盘)
文/玄魂
目录
node-webkit教程(9)native api 之Tray(托盘)
前言
9.1 Tray简介
9.2 tray的属性
9.3 tray 的构造函数
9.4 初始化一个tray
9.5 删除tray
9.6 小结
前言
几个月前,要开发一个简易的展示应用,要求支持离线播放(桌面应用)和在线播放(web应用)。
当时第一想到的是flex,同一套代码(或者只需少量的更改)就可以同时运行在桌面和浏览器上。由于很多展现效果要全新开发,我想到了impress.js(https://github.com/bartaz/impress.js/)。如果选择impress.js,就意味着要将html5作为桌面应用,当时想到要封装webkit,但是本人对这方面也不是很熟悉,时间也很有限,就又沿着这个方向搜索,找到了node-webkit(https://github.com/rogerwang/node-webkit)。
node-webkit 解决了我通过 html 和 js 来编写桌面应用的难题 。
至于node-webkit的定义,按照作者的说法:
“ 基于node.js和chromium的应用程序实时运行环境,可运行通过HTML(5)、CSS(3)、Javascript来编写的本地应用程序。node.js和webkit的结合体,webkit提供DOM操作,node.js提供本地化操作;且将二者的context完全整合,可在HTML代码中直接使用node.js的API。”
从本篇文章开始,为您介绍Platform Services些列的API,本系列由以下类别:
· App – 每个应用运行时全局api
· Clipboard – 剪贴板
· Tray – 状态栏图标,消息通知
· File dialogs-文件选择对话框
· Shell – 桌面相关
· Handling files and arguments-处理文件和相关参数
9.1 Tray简介
Tray在不同的平台下的展现形式不一样,通常以一个ICON的形式展现在操作系统状态通知的位置。在Mac下称之为Status Item,GTK环境下称为Status Icon,windows叫系统托盘。
新建tray.html 和package.json作为本文的示例程序。
tray.html内容如下:
<html>
<head>
<title>trayDemo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body >
<h1>Tray 测试</h1>
<script>
// Load native UI library
var gui = require('nw.gui');
</script>
</body>
</html>
package.json内容如下:
{
"name": "tray-demo",
"main": "tray.html",
"nodejs":true,
"window": {
"title": "trayDemo",
"toolbar": true,
"width": 800,
"height": 600,
"resizable":true,
"show_in_taskbar":true,
"frame":true,
"kiosk":false,
"icon": "2655716405282662783.png"
},
"webkit":{
"plugin":true
}
}
9.2 tray的属性
Tray包含title、tooltip、icon、menu、alticon五个属性。
title属性只在mac系统下有效,会和icon图标一起显示在状态栏。
tooltip是当鼠标移动到tray上方时显示的提示语,在所有平台下都有效。
icon是tray显示在托盘中的图标。
menu是托盘中的菜单,是一个 gui.Menu对象(参考:node-webkit教程6native-ui-api-之menu菜单)。
alticon只有在mac下起作用,配置切换效果icon图标。
9.3 tray 的构造函数
new Tray(option)
option中用来初始化tray的属性值,但是只能配置title, tooltip, icon 和menu四个属性。如:
var tray = new gui.Tray({ title: 'Tray', icon: 'img/icon.png' });
所有的属性都可以通过对象直接获取或赋值,如:
tray.menu = menu;
9.4 初始化一个tray
现在我们修改tray.html:
<script>
var isShowWindow = true;
// Load native UI library
var gui = require('nw.gui');
var win = gui.Window.get();
var tray = new gui.Tray({ title: '玄魂的软件', icon: '2655716405282662783.png' });
tray.tooltip = '点此打开';
//添加一个菜单
var menu = new gui.Menu();
menu.append(new gui.MenuItem({ type: 'checkbox', label: '选择我' }));
tray.menu = menu;
//click事件
tray.on('click',
function()
{
if(isShowWindow)
{
win.hide();
isShowWindow = false;
}
else
{
win.show();
isShowWindow = true;
}
}
);
</script>
运行效果如下:
点击托盘中的图标程序的窗体会相应的隐藏或者显示。
9.5 删除tray
很可惜的是,现在还没有办法临时隐藏Tray,只能删除它。
在删除需要调用remove方法,然后设置为null。如:
tray.remove();
tray = null;
9.6 小结
本文内容主要参考node-webkit的官方英文文档,做了适当的调整(https://github.com/rogerwang/node-webkit/wiki/Tray)。
下一篇文章,介绍Tray。
鄙视不标明出处的转载,更多相关内容,欢迎访问玄魂的博客(www.xuanhun521.com)
更多相关内容,欢迎访问 玄魂的博客 ( 更多 node-webkit 相关内容 http://www.xuanhun521.com/Blog/Tag/node-webkit )
ps:nw.js,electron交流群 313717550








