This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from teetrinkers/trayicon
Show a tray icon if enabled in the config.
- Loading branch information
Showing
18 changed files
with
209 additions
and
4 deletions.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,95 @@ | ||
var electron = require('electron'); | ||
var Menu = electron.Menu; | ||
var MenuItem = electron.MenuItem; | ||
var Tray = electron.Tray; | ||
var nativeImage = electron.nativeImage; | ||
var ipc = electron.ipcMain; | ||
var app = electron.app; | ||
var path = require('path'); | ||
|
||
var defaultIcon = nativeImage.createFromPath( | ||
path.join(__dirname, '../resources/tray.png')); | ||
var unreadMessagesIcon = nativeImage.createFromPath( | ||
path.join(__dirname, '../resources/tray-unread.png')); | ||
var mentionsIcon = nativeImage.createFromPath( | ||
path.join(__dirname, '../resources/tray-mention.png')); | ||
|
||
var tray; | ||
var mainWindow; | ||
|
||
var unreadCount = 0; | ||
var mentionsCount = 0; | ||
|
||
// Returns true if the tray icon is enabled. | ||
function isEnabled() { | ||
return tray != undefined; | ||
} | ||
|
||
function createMenu() { | ||
var menu = new Menu(); | ||
|
||
menu.append(new MenuItem({ | ||
label: 'Show', | ||
click: function(item) { | ||
mainWindow.restore(); | ||
mainWindow.show(); | ||
} | ||
})); | ||
menu.append(new MenuItem({ | ||
label: 'Quit', | ||
click: function(item) { | ||
app.quit(); | ||
} | ||
})); | ||
|
||
return menu; | ||
} | ||
|
||
function updateIcon() { | ||
if (mentionsCount > 0) { | ||
tray.setImage(mentionsIcon); | ||
} else if (unreadCount > 0) { | ||
tray.setImage(unreadMessagesIcon); | ||
} else { | ||
tray.setImage(defaultIcon); | ||
} | ||
} | ||
|
||
function handleUnreadChange(event, arg) { | ||
if (arg !== unreadCount) { | ||
unreadCount = arg; | ||
updateIcon(); | ||
} | ||
} | ||
|
||
function handleMentionsChange(event, arg) { | ||
if (arg !== mentionsCount) { | ||
mentionsCount = arg; | ||
updateIcon(); | ||
} | ||
} | ||
|
||
// Shows the tray icon. | ||
function enable(mainBrowserWindow) { | ||
mainWindow = mainBrowserWindow; | ||
|
||
tray = new Tray(defaultIcon); | ||
tray.setToolTip(app.getName()); | ||
|
||
var tray_menu = createMenu(); | ||
tray.setContextMenu(tray_menu); | ||
|
||
tray.on('click', function() { | ||
mainBrowserWindow.restore(); | ||
mainBrowserWindow.show(); | ||
}); | ||
|
||
ipc.on('mention', handleMentionsChange); | ||
ipc.on('unread', handleUnreadChange); | ||
} | ||
|
||
|
||
module.exports = { | ||
enable: enable, | ||
isEnabled: isEnabled | ||
}; |
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,40 @@ | ||
/** | ||
* Returns a partial stub of the electron api. | ||
*/ | ||
function create() { | ||
return { | ||
app: { | ||
getName: function() {} | ||
}, | ||
|
||
ipcMain: { | ||
on: function() {} | ||
}, | ||
|
||
nativeImage : { | ||
'createFromPath': function() { | ||
return {}; | ||
} | ||
}, | ||
|
||
Tray: function() { | ||
var Tray = function() {}; | ||
Tray.prototype.setToolTip = function() {}; | ||
Tray.prototype.setContextMenu = function() {}; | ||
Tray.prototype.on = function() {}; | ||
return Tray; | ||
}(), | ||
|
||
Menu: function() { | ||
var Menu = function() {}; | ||
Menu.prototype.append = function() {}; | ||
return Menu; | ||
}(), | ||
|
||
MenuItem: function() {} | ||
} | ||
} | ||
|
||
module.exports = { | ||
create: create | ||
}; |
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,38 @@ | ||
var sinon = require("sinon"); | ||
var proxyquire = require("proxyquire").noPreserveCache().noCallThru(); | ||
var electronStub = require("./electron-stub.js"); | ||
|
||
describe('tray', function() { | ||
|
||
var electron; | ||
var trayModule; | ||
|
||
beforeEach(function() { | ||
electron = electronStub.create(); | ||
electron.Tray = sinon.spy(electron.Tray); | ||
}); | ||
|
||
it('is initially disabled', function() { | ||
trayModule = proxyquire('../src/tray.js', {electron: electron}); | ||
expect(electron.Tray).to.not.have.been.called; | ||
expect(trayModule.isEnabled()).to.be.false; | ||
}); | ||
|
||
it('creates a tray icon on enable()', function() { | ||
trayModule = proxyquire('../src/tray.js', {electron: electron}); | ||
trayModule.enable({}); | ||
expect(electron.Tray).to.have.been.calledOnce; | ||
expect(trayModule.isEnabled()).to.be.true; | ||
}); | ||
|
||
it('sets a tooltip for the tray icon', function() { | ||
electron.app.getName = function() {return "foo";} | ||
electron.Tray.prototype.setToolTip = sinon.spy(); | ||
trayModule = proxyquire('../src/tray.js', {electron: electron}); | ||
|
||
trayModule.enable({}); | ||
expect(electron.Tray.prototype.setToolTip).to.have.been.calledOnce; | ||
expect(electron.Tray.prototype.setToolTip).to.have.been.calledWith("foo"); | ||
}); | ||
|
||
}); |