nw_sys/
tray.rs

1//!
2//! Creation and management of the System Tray Icons and Menus.
3//!
4//!
5//! # Synopsis
6//! ```
7//! let options = nw_sys::tray::Options::new()
8//!     //.title("My App")
9//!     .icon("resources/icons/tray-icon@2x.png")
10//!     .icons_are_templates(false);
11//!
12//! let tray = nw_sys::Tray::new(&options);
13//! tray.title();
14//! tray.tooltip();
15//! tray.icon();
16//! tray.alticon();// (Mac)
17//! tray.icons_are_templates();// (Mac)
18//! tray.menu();
19//! //tray.remove();
20//!
21//! let menu = nw_sys::Menu::new();
22//! menu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Menu 1")));
23//! tray.set_menu(&menu);
24//!
25//! ```
26//!
27
28use crate::menu::Menu;
29use crate::options::OptionsTrait;
30use js_sys::{Function, Object};
31use wasm_bindgen::prelude::*;
32
33#[wasm_bindgen]
34extern "C" {
35    ///
36    /// Interface for creating system tray icons and menus. For usage example please refer to [nw_sys::tray](self)
37    ///
38    /// Tray is an abstraction of different controls on different platforms,
39    /// usually it’s a small icon shown on the OS’s notification area.
40    /// On Mac OS X it’s called Status Item, on GTK it’s Status Icon,
41    /// and on Windows it’s System Tray Icon.
42    ///
43    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#tray)
44    ///
45    #[wasm_bindgen(js_namespace=nw, js_name = Tray)]
46    #[derive(Debug, Clone)]
47    pub type Tray;
48
49    #[wasm_bindgen(constructor, js_namespace=["nw"])]
50    /// # Synopsis
51    /// ```
52    /// //Create a tray icon
53    /// let tray = nw_sys::Tray::new(&nw_sys::tray::Options::new().title("Tray").icon("img/icon.png"));
54    /// ```
55    ///
56    /// Create a new Tray, option is an contains initial settings for the Tray.
57    /// Every field has its own property in the Tray,
58    /// see documentation of each property for details.
59    ///
60    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#tray)
61    ///
62    pub fn new(options: &Options) -> Tray;
63
64    #[wasm_bindgen(method, getter, js_name=title)]
65    /// Get the title of the tray.
66    ///
67    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traytitle)
68    pub fn title(this: &Tray) -> String;
69
70    #[wasm_bindgen(method, setter, js_name=title)]
71    /// Set the title of the tray.
72    ///
73    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traytitle)
74    pub fn set_title(this: &Tray, title: &str);
75
76    #[wasm_bindgen(method, getter, js_name=tooltip)]
77    /// Get the tooltip of the tray.
78    ///
79    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traytooltip)
80    pub fn tooltip(this: &Tray) -> String;
81
82    #[wasm_bindgen(method, setter, js_name=tooltip)]
83    /// Set the tooltip of the tray. tooltip shows when you hover the Tray with mouse.
84    /// On Mac OS X title will be showed on status bar along with its icon,
85    /// but it doesn’t have effects on GTK and Windows, since the latter
86    /// two platforms only support tray to be showed as icons.
87    ///
88    /// Note:
89    ///
90    /// tooltip is showed on all three platforms. Should be set as Tray property
91    /// rather from option object constructor.
92    ///
93    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traytooltip)
94    pub fn set_tooltip(this: &Tray, tooltip: &str);
95
96    #[wasm_bindgen(method, getter, js_name=icon)]
97    /// Get the icon of the tray.
98    ///
99    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayicon)
100    pub fn icon(this: &Tray) -> String;
101
102    #[wasm_bindgen(method, setter, js_name=icon)]
103    /// Set the icon of the tray. Icon must a path to your icon file.
104    /// It can be a relative path which points to an icon in your app,
105    /// or an absolute path pointing to a file in user’s system.
106    ///
107    ///
108    /// Mac OS X caveat: when used in notification context,
109    /// png icon is not sized down like in windows notification area,
110    /// it is rather displayed in 1:1 ratio.
111    ///
112    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayicon)
113    pub fn set_icon(this: &Tray, icon: &str);
114
115    #[wasm_bindgen(method, getter, js_name=alticon)]
116    /// (Mac) Get the alternate (active) tray icon.
117    ///
118    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayalticon-mac)
119    pub fn alticon(this: &Tray) -> String;
120
121    #[wasm_bindgen(method, setter, js_name=alticon)]
122    /// (Mac) Set the alternate (active) tray icon.
123    ///
124    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayalticon-mac)
125    pub fn set_alticon(this: &Tray, alticon: &str);
126
127    #[wasm_bindgen(method, getter, js_name=iconsAreTemplates)]
128    /// (Mac) Get whether icon and alticon images are treated as “templates”
129    /// (true by default). When the property is set to true the images are
130    /// treated as “templates” and the system automatically ensures proper
131    /// styling according to the various states of the status item
132    /// (e.g. dark menu, light menu, etc.).
133    /// Template images should consist only of black and clear colours and
134    /// can use the alpha channel in the image to adjust the opacity of
135    /// black content.
136    ///
137    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayiconsaretemplates-mac)
138    pub fn icons_are_templates(this: &Tray) -> bool;
139
140    #[wasm_bindgen(method, setter, js_name=alticon)]
141    /// (Mac) Set whether icon and alticon images are treated as “templates”
142    /// (true by default).
143    ///
144    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayalticon-mac)
145    pub fn set_icons_are_templates(this: &Tray, value: bool);
146
147    #[wasm_bindgen(method, getter, js_name = menu)]
148    /// Get the menu of the tray
149    ///
150    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traymenu)
151    pub fn menu(this: &Tray) -> Option<Menu>;
152
153    #[wasm_bindgen(method, setter, js_name = menu)]
154    /// Set the menu of the tray, menu will be showed when you click on the tray icon.
155    ///
156    /// ```rust
157    /// let menu = nw_sys::Menu::new();
158    /// menu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Menu 1")));
159    /// tray.set_menu(&menu);
160    /// ```
161    ///
162    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traymenu)
163    pub fn set_menu(this: &Tray, menu: &Menu);
164
165    #[wasm_bindgen(method)]
166    /// Remove the tray
167    ///
168    ///
169    /// tray = null;<---- TODO
170    ///
171    /// Once removed, you will not be able to show it again and you should set
172    /// your tray variable to null to make it garbage collected.
173    /// There is no way temporarily hide a tray icon.
174    ///
175    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayremove)
176    pub fn remove(this: &Tray);
177
178    #[wasm_bindgen(method, js_name=on)]
179    /// Event handling: Click
180    ///
181    /// Emitted when user clicks the menu item with left mouse button.
182    ///
183    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#event-click)
184    pub fn on(this: &Tray, event: &str, callback: &Function);
185
186    #[wasm_bindgen(extends = Object)]
187    #[derive(Debug, Clone, PartialEq, Eq)]
188    pub type Options;
189}
190
191impl OptionsTrait for Options {}
192
193impl Options {
194    /// Set the title of the tray.
195    ///
196    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traytitle)
197    pub fn title(self, title: &str) -> Self {
198        self.set("title", JsValue::from(title))
199    }
200
201    /// Set the tooltip of the tray. tooltip shows when you hover the Tray with mouse.
202    ///
203    /// Note: tooltip is showed on all three platforms.
204    /// Should be set as Tray property rather from option object constructor.
205    ///
206    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traytooltip)
207    pub fn tooltip(self, tooltip: &str) -> Self {
208        self.set("tooltip", JsValue::from(tooltip))
209    }
210
211    /// Set the icon of the tray, icon must a path to your icon file.
212    /// It can be a relative path which points to an icon in your app,
213    /// or an absolute path pointing to a file in user’s system.
214    ///
215    /// Mac OS X caveat: when used in notification context,
216    /// png icon is not sized down like in windows notification area,
217    /// it is rather displayed in 1:1 ratio.
218    ///
219    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayicon)
220    pub fn icon(self, icon: &str) -> Self {
221        self.set("icon", JsValue::from(icon))
222    }
223
224    /// (Mac) Set the alternate (active) tray icon.
225    ///
226    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayalticon-mac)
227    pub fn alticon(self, alticon: &str) -> Self {
228        self.set("alticon", JsValue::from(alticon))
229    }
230
231    /// (Mac) Set whether icon and alticon images are treated as "templates" (true by default).
232    /// When the property is set to true the images are treated as “templates”
233    /// and the system automatically ensures proper styling according to the various
234    /// states of the status item (e.g. dark menu, light menu, etc.).
235    /// Template images should consist only of black and clear colours
236    /// and can use the alpha channel in the image to adjust the opacity of black content.
237    ///
238    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#trayiconsaretemplates-mac)
239    pub fn icons_are_templates(self, icons_are_templates: bool) -> Self {
240        self.set("iconsAreTemplates", JsValue::from(icons_are_templates))
241    }
242
243    /// Set the menu of the tray, menu will be showed when you click on the tray icon.
244    ///
245    /// On Mac OS X the menu will be showed when you click on the
246    /// tray (which is the only action available for tray icons on Mac OS X).
247    /// On Windows and Linux, the menu will be showed when you single click on the
248    /// tray with right mouse button, clicking with left mouse button sends the click
249    /// event and does not show a menu.
250    ///
251    /// In order to reduce differences from different platforms, setting menu property
252    /// is the only way to bind a menu to tray, there’s no way to popup a menu with
253    /// left mouse button click on Linux and Windows.
254    ///
255    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Tray/#traymenu)
256    pub fn menu(self, menu: &Menu) -> Self {
257        self.set("menu", JsValue::from(menu))
258    }
259}