forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a tray icon to the application, shown in the system tray bar, that can be used to minimise the application window. This is a common feature on most desktop messaging apps (e.g. Telegram Desktop or Slack) and allows to save space in the system task bar. The tray icon provides a context menu that contains a button to show/hide the application window, and a button to quit the application. When the tray icon is clicked, the visibility of the window is toggled. When the close (x) button of the window is pressed, the application is not terminated but minimised to the tray icon instead (it can be terminated by using the "Quit" entry in the File menu or in the context menu of the tray icon). The tray icon is disabled by default, and two command line arguments are available to enable it: --use-tray-icon: enables the tray icon --start-in-tray: enables the tray icon and the application starts minimised in the tray bar Resolves: signalapp#1480
- Loading branch information
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const electron = require('electron') | ||
const path = require('path'); | ||
|
||
const app = electron.app; | ||
const Menu = electron.Menu; | ||
const Tray = electron.Tray; | ||
|
||
let trayContextMenu = null; | ||
let tray = null; | ||
|
||
function createTrayIcon(getMainWindow, messages) { | ||
|
||
// A smaller icon is needed on macOS | ||
tray = new Tray( | ||
process.platform == "darwin" ? | ||
path.join(__dirname, '..', 'images', 'icon_16.png') : | ||
path.join(__dirname, '..', 'images', 'icon_256.png')); | ||
|
||
tray.toggleWindowVisibility = function () { | ||
var mainWindow = getMainWindow(); | ||
if (mainWindow) { | ||
if (mainWindow.isVisible()) { | ||
mainWindow.hide(); | ||
} else { | ||
mainWindow.show(); | ||
|
||
// On some versions of GNOME the window may not be on top when restored. | ||
// This trick should fix it. | ||
// Thanks to: https://github.com/Enrico204/Whatsapp-Desktop/commit/6b0dc86b64e481b455f8fce9b4d797e86d000dc1 | ||
mainWindow.setAlwaysOnTop(true); | ||
mainWindow.focus(); | ||
mainWindow.setAlwaysOnTop(false); | ||
} | ||
} | ||
tray.updateContextMenu(); | ||
} | ||
|
||
tray.updateContextMenu = function () { | ||
|
||
var mainWindow = getMainWindow(); | ||
|
||
// NOTE: we want to have the show/hide entry available in the tray icon | ||
// context menu, since the 'click' event may not work on all platforms. | ||
// For details please refer to: | ||
// https://github.com/electron/electron/blob/master/docs/api/tray.md. | ||
trayContextMenu = Menu.buildFromTemplate([ | ||
{ | ||
id: 'toggleWindowVisibility', | ||
label: messages[mainWindow.isVisible() ? 'hide' : 'show'].message, | ||
click: tray.toggleWindowVisibility | ||
}, | ||
{ | ||
id: 'quit', | ||
label: messages.quit.message, | ||
click: app.quit.bind(app) | ||
} | ||
]); | ||
|
||
tray.setContextMenu(trayContextMenu); | ||
} | ||
|
||
tray.on('click', tray.toggleWindowVisibility); | ||
|
||
tray.setToolTip(messages.trayTooltip.message); | ||
tray.updateContextMenu(); | ||
|
||
return tray; | ||
} | ||
|
||
module.exports = createTrayIcon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters