2015-09-01 02:13:27 +00:00
|
|
|
# Desktop Environment Integration
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
Different operating systems provide different features for integrating desktop
|
|
|
|
applications into their desktop environments. For example, on Windows,
|
|
|
|
applications can put shortcuts in the JumpList of task bar, and on Mac,
|
2014-11-17 13:40:07 +00:00
|
|
|
applications can put a custom menu in the dock menu.
|
|
|
|
|
2015-06-13 14:58:18 +00:00
|
|
|
This guide explains how to integrate your application into those desktop
|
2015-04-16 03:31:12 +00:00
|
|
|
environments with Electron APIs.
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
## Notifications (Windows, Linux, macOS)
|
2015-11-10 09:43:29 +00:00
|
|
|
|
|
|
|
All three operating systems provide means for applications to send notifications
|
|
|
|
to the user. Electron conveniently allows developers to send notifications with
|
|
|
|
the [HTML5 Notification API](https://notifications.spec.whatwg.org/), using
|
|
|
|
the currently running operating system's native notification APIs to display it.
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2016-03-31 05:17:50 +00:00
|
|
|
**Note:** Since this is an HTML5 API it is only available in the renderer process.
|
2016-03-25 22:26:14 +00:00
|
|
|
|
2015-11-10 07:28:14 +00:00
|
|
|
```javascript
|
2016-05-04 18:11:51 +00:00
|
|
|
let myNotification = new Notification('Title', {
|
2015-11-10 07:28:14 +00:00
|
|
|
body: 'Lorem Ipsum Dolor Sit Amet'
|
|
|
|
});
|
|
|
|
|
2016-05-10 17:15:09 +00:00
|
|
|
myNotification.onclick = () => {
|
2016-05-10 16:57:55 +00:00
|
|
|
console.log('Notification clicked');
|
|
|
|
};
|
2015-11-10 07:28:14 +00:00
|
|
|
```
|
|
|
|
|
2015-12-08 05:13:45 +00:00
|
|
|
While code and user experience across operating systems are similar, there
|
2015-11-10 09:43:29 +00:00
|
|
|
are fine differences.
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
### Windows
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
* On Windows 10, notifications "just work".
|
|
|
|
* On Windows 8.1 and Windows 8, a shortcut to your app, with a [Application User
|
|
|
|
Model ID][app-user-model-id], must be installed to the Start screen. Note,
|
|
|
|
however, that it does not need to be pinned to the Start screen.
|
|
|
|
* On Windows 7 and below, notifications are not supported. You can however send
|
2016-01-13 16:52:07 +00:00
|
|
|
"balloon notifications" using the [Tray API][tray-balloon].
|
2015-11-10 09:43:29 +00:00
|
|
|
|
2016-01-11 06:41:38 +00:00
|
|
|
Furthermore, the maximum length for the notification body is 250 characters,
|
2015-11-10 09:43:29 +00:00
|
|
|
with the Windows team recommending that notifications should be kept to 200
|
|
|
|
characters.
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
### Linux
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
Notifications are sent using `libnotify`, it can show notifications on any
|
|
|
|
desktop environment that follows [Desktop Notifications
|
|
|
|
Specification][notification-spec], including Cinnamon, Enlightenment, Unity,
|
|
|
|
GNOME, KDE.
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
### macOS
|
2015-11-10 09:43:29 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
Notifications are straight-forward on macOS, you should however be aware of
|
2016-04-22 13:53:26 +00:00
|
|
|
[Apple's Human Interface guidelines regarding notifications](https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/OSXHIGuidelines/NotificationCenter.html).
|
2015-11-10 09:43:29 +00:00
|
|
|
|
|
|
|
Note that notifications are limited to 256 bytes in size - and will be truncated
|
|
|
|
if you exceed that limit.
|
2015-11-10 07:28:14 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
## Recent documents (Windows & macOS)
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
Windows and macOS provide easy access to a list of recent documents opened by
|
2015-09-01 02:13:27 +00:00
|
|
|
the application via JumpList or dock menu, respectively.
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
__JumpList:__
|
|
|
|
|
|
|
|
![JumpList Recent Files](http://i.msdn.microsoft.com/dynimg/IC420538.png)
|
|
|
|
|
|
|
|
__Application dock menu:__
|
|
|
|
|
2014-11-18 03:27:08 +00:00
|
|
|
<img src="https://cloud.githubusercontent.com/assets/639601/5069610/2aa80758-6e97-11e4-8cfb-c1a414a10774.png" height="353" width="428" >
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
To add a file to recent documents, you can use the
|
2014-11-18 02:16:17 +00:00
|
|
|
[app.addRecentDocument][addrecentdocument] API:
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
```javascript
|
2015-04-16 03:31:12 +00:00
|
|
|
app.addRecentDocument('/Users/USERNAME/Desktop/work.type');
|
2014-11-17 13:40:07 +00:00
|
|
|
```
|
|
|
|
|
2015-09-01 21:15:21 +00:00
|
|
|
And you can use [app.clearRecentDocuments][clearrecentdocuments] API to empty
|
2014-11-18 02:16:17 +00:00
|
|
|
the recent documents list:
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
app.clearRecentDocuments();
|
|
|
|
```
|
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
### Windows Notes
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
In order to be able to use this feature on Windows, your application has to be
|
2015-06-13 14:58:18 +00:00
|
|
|
registered as a handler of the file type of the document, otherwise the file
|
|
|
|
won't appear in JumpList even after you have added it. You can find everything
|
|
|
|
on registering your application in [Application Registration][app-registration].
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
When a user clicks a file from the JumpList, a new instance of your application
|
|
|
|
will be started with the path of the file added as a command line argument.
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
### macOS Notes
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
When a file is requested from the recent documents menu, the `open-file` event
|
2015-09-01 02:13:27 +00:00
|
|
|
of `app` module will be emitted for it.
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
## Custom Dock Menu (macOS)
|
2014-11-17 13:40:07 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
macOS enables developers to specify a custom menu for the dock, which usually
|
2014-11-17 13:40:07 +00:00
|
|
|
contains some shortcuts for commonly used features of your application:
|
|
|
|
|
|
|
|
__Dock menu of Terminal.app:__
|
|
|
|
|
|
|
|
<img src="https://cloud.githubusercontent.com/assets/639601/5069962/6032658a-6e9c-11e4-9953-aa84006bdfff.png" height="354" width="341" >
|
|
|
|
|
|
|
|
To set your custom dock menu, you can use the `app.dock.setMenu` API, which is
|
2016-06-18 13:26:26 +00:00
|
|
|
only available on macOS:
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
```javascript
|
2015-11-12 13:20:09 +00:00
|
|
|
const electron = require('electron');
|
|
|
|
const app = electron.app;
|
|
|
|
const Menu = electron.Menu;
|
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
const dockMenu = Menu.buildFromTemplate([
|
2016-05-10 17:15:09 +00:00
|
|
|
{ label: 'New Window', click() { console.log('New Window'); } },
|
2014-11-17 13:40:07 +00:00
|
|
|
{ label: 'New Window with Settings', submenu: [
|
|
|
|
{ label: 'Basic' },
|
2015-06-09 16:15:30 +00:00
|
|
|
{ label: 'Pro'}
|
2014-11-17 13:40:07 +00:00
|
|
|
]},
|
2015-06-09 16:15:30 +00:00
|
|
|
{ label: 'New Command...'}
|
2014-11-17 13:40:07 +00:00
|
|
|
]);
|
|
|
|
app.dock.setMenu(dockMenu);
|
|
|
|
```
|
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
## User Tasks (Windows)
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
On Windows you can specify custom actions in the `Tasks` category of JumpList,
|
|
|
|
as quoted from MSDN:
|
|
|
|
|
|
|
|
> Applications define tasks based on both the program's features and the key
|
|
|
|
> things a user is expected to do with them. Tasks should be context-free, in
|
|
|
|
> that the application does not need to be running for them to work. They
|
|
|
|
> should also be the statistically most common actions that a normal user would
|
|
|
|
> perform in an application, such as compose an email message or open the
|
|
|
|
> calendar in a mail program, create a new document in a word processor, launch
|
|
|
|
> an application in a certain mode, or launch one of its subcommands. An
|
|
|
|
> application should not clutter the menu with advanced features that standard
|
|
|
|
> users won't need or one-time actions such as registration. Do not use tasks
|
|
|
|
> for promotional items such as upgrades or special offers.
|
|
|
|
>
|
|
|
|
> It is strongly recommended that the task list be static. It should remain the
|
|
|
|
> same regardless of the state or status of the application. While it is
|
|
|
|
> possible to vary the list dynamically, you should consider that this could
|
|
|
|
> confuse the user who does not expect that portion of the destination list to
|
|
|
|
> change.
|
|
|
|
|
|
|
|
__Tasks of Internet Explorer:__
|
|
|
|
|
|
|
|
![IE](http://i.msdn.microsoft.com/dynimg/IC420539.png)
|
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
Unlike the dock menu in macOS which is a real menu, user tasks in Windows work
|
2015-09-01 02:13:27 +00:00
|
|
|
like application shortcuts such that when user clicks a task, a program will be
|
2014-11-17 13:40:07 +00:00
|
|
|
executed with specified arguments.
|
|
|
|
|
2014-11-18 02:16:17 +00:00
|
|
|
To set user tasks for your application, you can use
|
|
|
|
[app.setUserTasks][setusertaskstasks] API:
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
app.setUserTasks([
|
|
|
|
{
|
|
|
|
program: process.execPath,
|
|
|
|
arguments: '--new-window',
|
|
|
|
iconPath: process.execPath,
|
|
|
|
iconIndex: 0,
|
2015-04-30 19:40:30 +00:00
|
|
|
title: 'New Window',
|
2015-06-09 16:15:30 +00:00
|
|
|
description: 'Create a new window'
|
2014-11-17 13:40:07 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
To clean your tasks list, just call `app.setUserTasks` with an empty array:
|
2014-11-17 13:40:07 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
app.setUserTasks([]);
|
|
|
|
```
|
|
|
|
|
|
|
|
The user tasks will still show even after your application closes, so the icon
|
|
|
|
and program path specified for a task should exist until your application is
|
|
|
|
uninstalled.
|
|
|
|
|
2015-08-02 05:09:42 +00:00
|
|
|
## Thumbnail Toolbars
|
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
On Windows you can add a thumbnail toolbar with specified buttons in a taskbar
|
|
|
|
layout of an application window. It provides users a way to access to a
|
|
|
|
particular window's command without restoring or activating the window.
|
2015-08-02 05:09:42 +00:00
|
|
|
|
|
|
|
From MSDN, it's illustrated:
|
|
|
|
|
|
|
|
> This toolbar is simply the familiar standard toolbar common control. It has a
|
|
|
|
> maximum of seven buttons. Each button's ID, image, tooltip, and state are defined
|
|
|
|
> in a structure, which is then passed to the taskbar. The application can show,
|
|
|
|
> enable, disable, or hide buttons from the thumbnail toolbar as required by its
|
|
|
|
> current state.
|
|
|
|
>
|
|
|
|
> For example, Windows Media Player might offer standard media transport controls
|
|
|
|
> such as play, pause, mute, and stop.
|
|
|
|
|
|
|
|
__Thumbnail toolbar of Windows Media Player:__
|
|
|
|
|
|
|
|
![player](https://i-msdn.sec.s-msft.com/dynimg/IC420540.png)
|
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
You can use [BrowserWindow.setThumbarButtons][setthumbarbuttons] to set
|
|
|
|
thumbnail toolbar in your application:
|
2015-08-02 05:09:42 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
```javascript
|
2016-05-10 16:26:38 +00:00
|
|
|
const {BrowserWindow} = require('electron');
|
2015-11-12 13:20:09 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2016-05-04 18:11:51 +00:00
|
|
|
let win = new BrowserWindow({
|
2015-08-02 05:09:42 +00:00
|
|
|
width: 800,
|
|
|
|
height: 600
|
|
|
|
});
|
2016-05-04 18:11:51 +00:00
|
|
|
|
2015-08-02 05:09:42 +00:00
|
|
|
win.setThumbarButtons([
|
|
|
|
{
|
2016-05-10 09:42:11 +00:00
|
|
|
tooltip: 'button1',
|
2015-08-02 05:09:42 +00:00
|
|
|
icon: path.join(__dirname, 'button1.png'),
|
2016-06-16 01:32:58 +00:00
|
|
|
click() { console.log('button1 clicked'); }
|
2015-08-02 05:09:42 +00:00
|
|
|
},
|
|
|
|
{
|
2016-05-10 09:42:11 +00:00
|
|
|
tooltip: 'button2',
|
2015-08-02 05:09:42 +00:00
|
|
|
icon: path.join(__dirname, 'button2.png'),
|
2016-05-10 17:15:09 +00:00
|
|
|
flags: ['enabled', 'dismissonclick'],
|
|
|
|
click() { console.log('button2 clicked.'); }
|
2015-08-02 05:09:42 +00:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
|
|
|
To clean thumbnail toolbar buttons, just call `BrowserWindow.setThumbarButtons`
|
|
|
|
with an empty array:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
win.setThumbarButtons([]);
|
|
|
|
```
|
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
## Unity Launcher Shortcuts (Linux)
|
2014-11-17 14:00:56 +00:00
|
|
|
|
2015-11-10 09:43:29 +00:00
|
|
|
In Unity, you can add custom entries to its launcher via modifying the
|
|
|
|
`.desktop` file, see [Adding Shortcuts to a Launcher][unity-launcher].
|
2014-11-17 14:00:56 +00:00
|
|
|
|
2014-11-18 02:16:17 +00:00
|
|
|
__Launcher shortcuts of Audacious:__
|
|
|
|
|
|
|
|
![audacious](https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles?action=AttachFile&do=get&target=shortcuts.png)
|
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
## Progress Bar in Taskbar (Windows, macOS, Unity)
|
2014-11-18 02:54:08 +00:00
|
|
|
|
2015-09-01 02:13:27 +00:00
|
|
|
On Windows a taskbar button can be used to display a progress bar. This enables
|
2015-06-13 14:58:18 +00:00
|
|
|
a window to provide progress information to the user without the user having to
|
2014-11-18 02:54:08 +00:00
|
|
|
switch to the window itself.
|
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
On macOS the progress bar will be displayed as a part of the dock icon.
|
2016-03-05 12:20:02 +00:00
|
|
|
|
2015-06-09 14:49:21 +00:00
|
|
|
The Unity DE also has a similar feature that allows you to specify the progress
|
|
|
|
bar in the launcher.
|
2014-11-18 02:54:08 +00:00
|
|
|
|
|
|
|
__Progress bar in taskbar button:__
|
|
|
|
|
|
|
|
![Taskbar Progress Bar](https://cloud.githubusercontent.com/assets/639601/5081682/16691fda-6f0e-11e4-9676-49b6418f1264.png)
|
|
|
|
|
|
|
|
To set the progress bar for a Window, you can use the
|
2014-11-18 03:27:08 +00:00
|
|
|
[BrowserWindow.setProgressBar][setprogressbar] API:
|
2014-11-18 02:54:08 +00:00
|
|
|
|
|
|
|
```javascript
|
2016-05-10 17:16:32 +00:00
|
|
|
let win = new BrowserWindow({...});
|
|
|
|
win.setProgressBar(0.5);
|
2014-11-18 02:54:08 +00:00
|
|
|
```
|
|
|
|
|
2016-02-13 08:03:58 +00:00
|
|
|
## Icon Overlays in Taskbar (Windows)
|
|
|
|
|
|
|
|
On Windows a taskbar button can use a small overlay to display application
|
|
|
|
status, as quoted from MSDN:
|
|
|
|
|
|
|
|
> Icon overlays serve as a contextual notification of status, and are intended
|
|
|
|
> to negate the need for a separate notification area status icon to communicate
|
|
|
|
> that information to the user. For instance, the new mail status in Microsoft
|
|
|
|
> Outlook, currently shown in the notification area, can now be indicated
|
|
|
|
> through an overlay on the taskbar button. Again, you must decide during your
|
|
|
|
> development cycle which method is best for your application. Overlay icons are
|
|
|
|
> intended to supply important, long-standing status or notifications such as
|
|
|
|
> network status, messenger status, or new mail. The user should not be
|
|
|
|
> presented with constantly changing overlays or animations.
|
|
|
|
|
|
|
|
__Overlay on taskbar button:__
|
|
|
|
|
|
|
|
![Overlay on taskbar button](https://i-msdn.sec.s-msft.com/dynimg/IC420441.png)
|
|
|
|
|
|
|
|
To set the overlay icon for a window, you can use the
|
|
|
|
[BrowserWindow.setOverlayIcon][setoverlayicon] API:
|
|
|
|
|
|
|
|
```javascript
|
2016-05-10 17:16:32 +00:00
|
|
|
let win = new BrowserWindow({...});
|
|
|
|
win.setOverlayIcon('path/to/overlay.png', 'Description for overlay');
|
2016-02-13 08:03:58 +00:00
|
|
|
```
|
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
## Represented File of Window (macOS)
|
2014-11-18 03:27:08 +00:00
|
|
|
|
2016-06-18 13:26:26 +00:00
|
|
|
On macOS a window can set its represented file, so the file's icon can show in
|
2015-10-13 00:17:06 +00:00
|
|
|
the title bar and when users Command-Click or Control-Click on the title a path
|
2014-11-18 03:27:08 +00:00
|
|
|
popup will show.
|
|
|
|
|
2015-06-13 14:58:18 +00:00
|
|
|
You can also set the edited state of a window so that the file icon can indicate
|
|
|
|
whether the document in this window has been modified.
|
2014-11-18 03:27:08 +00:00
|
|
|
|
|
|
|
__Represented file popup menu:__
|
|
|
|
|
|
|
|
<img src="https://cloud.githubusercontent.com/assets/639601/5082061/670a949a-6f14-11e4-987a-9aaa04b23c1d.png" height="232" width="663" >
|
|
|
|
|
|
|
|
To set the represented file of window, you can use the
|
|
|
|
[BrowserWindow.setRepresentedFilename][setrepresentedfilename] and
|
|
|
|
[BrowserWindow.setDocumentEdited][setdocumentedited] APIs:
|
|
|
|
|
|
|
|
```javascript
|
2016-05-10 17:16:32 +00:00
|
|
|
let win = new BrowserWindow({...});
|
|
|
|
win.setRepresentedFilename('/etc/passwd');
|
|
|
|
win.setDocumentEdited(true);
|
2014-11-18 03:27:08 +00:00
|
|
|
```
|
|
|
|
|
2016-02-15 08:36:01 +00:00
|
|
|
[addrecentdocument]: ../api/app.md#appaddrecentdocumentpath-os-x-windows
|
|
|
|
[clearrecentdocuments]: ../api/app.md#appclearrecentdocuments-os-x-windows
|
|
|
|
[setusertaskstasks]: ../api/app.md#appsetusertaskstasks-windows
|
|
|
|
[setprogressbar]: ../api/browser-window.md#winsetprogressbarprogress
|
2016-02-13 08:03:58 +00:00
|
|
|
[setoverlayicon]: ../api/browser-window.md#winsetoverlayiconoverlay-description-windows-7
|
2016-02-15 08:36:01 +00:00
|
|
|
[setrepresentedfilename]: ../api/browser-window.md#winsetrepresentedfilenamefilename-os-x
|
|
|
|
[setdocumentedited]: ../api/browser-window.md#winsetdocumenteditededited-os-x
|
2014-11-18 03:27:08 +00:00
|
|
|
[app-registration]: http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx
|
|
|
|
[unity-launcher]: https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles#Adding_shortcuts_to_a_launcher
|
2016-02-15 08:36:01 +00:00
|
|
|
[setthumbarbuttons]: ../api/browser-window.md#winsetthumbarbuttonsbuttons-windows-7
|
2015-11-10 09:43:29 +00:00
|
|
|
[tray-balloon]: ../api/tray.md#traydisplayballoonoptions-windows
|
|
|
|
[app-user-model-id]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd378459(v=vs.85).aspx
|
|
|
|
[notification-spec]: https://developer.gnome.org/notification-spec/
|