commit extjs-2.2.1
[extjs.git] / air / src / SystemTray.js
1 /*\r
2  * Ext JS Library 0.30\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.air.SystemTray\r
11  * @singleton\r
12  *\r
13  * \r
14  *\r
15  */\r
16 Ext.air.SystemTray = function(){\r
17         var app = air.NativeApplication.nativeApplication;\r
18         var icon, isWindows = false, bitmaps;\r
19         \r
20         // windows\r
21         if(air.NativeApplication.supportsSystemTrayIcon) {\r
22                 icon = app.icon;\r
23                 isWindows = true;\r
24         }\r
25     \r
26         // mac\r
27         if(air.NativeApplication.supportsDockIcon) {\r
28             icon = app.icon;\r
29         }\r
30         \r
31         return {\r
32                 /**\r
33                  * Sets the Icon and tooltip for the currently running application in the\r
34                  * SystemTray or Dock depending on the operating system.\r
35                  * @param {String} icon Icon to load with a URLRequest\r
36                  * @param {String} tooltip Tooltip to use when mousing over the icon\r
37                  * @param {Boolean} initWithIcon Boolean to initialize with icon immediately\r
38                  */\r
39                 setIcon : function(icon, tooltip, initWithIcon){\r
40                         if(!icon){ // not supported OS\r
41                                 return;\r
42                         }\r
43                         var loader = new air.Loader();\r
44                         loader.contentLoaderInfo.addEventListener(air.Event.COMPLETE, function(e){\r
45                                 bitmaps = new runtime.Array(e.target.content.bitmapData);\r
46                                 if (initWithIcon) {\r
47                                         icon.bitmaps = bitmaps;\r
48                                 }\r
49                         });\r
50                         \r
51                         loader.load(new air.URLRequest(icon));\r
52                         if(tooltip && air.NativeApplication.supportsSystemTrayIcon) {\r
53                                 app.icon.tooltip = tooltip;\r
54                         }\r
55                 },\r
56                 \r
57                 /**\r
58                  * Bounce the OS X dock icon. Accepts a priority to notify the user\r
59                  * whether the event which has just occurred is informational (single bounce)\r
60                  * or critcal (continual bounce).\r
61                  * @param priority {air.NotificationType} The priorities are air.NotificationType.INFORMATIONAL and air.NotificationType.CRITICAL.\r
62                  */\r
63                 bounce : function(priority){\r
64                         icon.bounce(priority);\r
65                 },\r
66                 \r
67                 on : function(eventName, fn, scope){\r
68                         icon.addEventListener(eventName, function(){\r
69                                 fn.apply(scope || this, arguments);\r
70                         });\r
71                 },\r
72                 \r
73                 /**\r
74                  * Hide the custom icon\r
75                  */\r
76                 hideIcon : function(){\r
77                         if(!icon){ // not supported OS\r
78                                 return;\r
79                         }\r
80                         icon.bitmaps = [];\r
81                 },\r
82                 \r
83                 /**\r
84                  * Show the custom icon\r
85                  */\r
86                 showIcon : function(){\r
87                         if(!icon){ // not supported OS\r
88                                 return;\r
89                         }\r
90                         icon.bitmaps = bitmaps;\r
91                 },\r
92                 \r
93                 /**\r
94                  * Sets a menu for the icon\r
95                  * @param {Array} actions Configurations for Ext.air.MenuItem's\r
96                  */\r
97                 setMenu: function(actions, _parentMenu){\r
98                         if(!icon){ // not supported OS\r
99                                 return;\r
100                         }\r
101                         var menu = new air.NativeMenu();\r
102                         \r
103                         for (var i = 0, len = actions.length; i < len; i++) {\r
104                                 var a = actions[i];\r
105                                 if(a == '-'){\r
106                                         menu.addItem(new air.NativeMenuItem("", true));\r
107                                 }else{\r
108                                         var item = menu.addItem(Ext.air.MenuItem(a));\r
109                                         if(a.menu || (a.initialConfig && a.initialConfig.menu)){\r
110                                                 item.submenu = Ext.air.SystemTray.setMenu(a.menu || a.initialConfig.menu, menu);\r
111                                         }\r
112                                 }\r
113                                 \r
114                                 if(!_parentMenu){\r
115                                         icon.menu = menu;\r
116                                 }\r
117                         }\r
118                         \r
119                         return menu;\r
120                 }\r
121         };      \r
122 }();