forked from RocketChat/Rocket.Chat
-
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.
Merge pull request RocketChat#315 from alexbrazier/feature/notifications
Fix issues with Windows 7 notifications and improve design
- Loading branch information
Showing
5 changed files
with
243 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { BrowserWindow, ipcMain, screen } from 'electron'; | ||
|
||
export default class Toaster { | ||
constructor (mainWindow, maxNotifications = 3, debug = false) { | ||
this.mainWindow = mainWindow; | ||
this.debug = debug; | ||
this.maxNotifications = maxNotifications; | ||
this.windows = []; | ||
} | ||
|
||
toast (msg, callback) { | ||
const window = new BrowserWindow({ | ||
width: msg.width, | ||
height: 75, | ||
useContentSize: true, | ||
transparent: true, | ||
frame: false, | ||
show : false, | ||
alwaysOnTop: true, | ||
skipTaskbar: true, | ||
resizeable: false | ||
}); | ||
|
||
if (this.debug) { | ||
window.openDevTools(); | ||
} | ||
|
||
this.windows.push(window); | ||
|
||
ipcMain.once(`notification-${msg.tag}`, callback); | ||
|
||
window.on('closed', () => { | ||
this.windows = this.windows.filter((win) => win && !win.isDestroyed() && win !== window); | ||
this.windows.forEach((win, i) => this._setPosition(win, i + 1)); | ||
}); | ||
|
||
const htmlFile = `${msg.htmlFile}?` + | ||
`title=${encodeURIComponent(msg.title || '')}&` + | ||
`message=${encodeURIComponent(msg.message || '')}&` + | ||
`timeout=${msg.timeout}&` + | ||
`icon=${msg.icon}&` + | ||
`tag=${msg.tag}`; | ||
|
||
window.loadURL(htmlFile); | ||
|
||
window.webContents.on('did-finish-load', () => { | ||
this._setPosition(window, this.windows.length); | ||
}); | ||
} | ||
|
||
_setPosition (window, index) { | ||
const width = window.getSize()[0]; | ||
const height = window.getSize()[1]; | ||
const pos = this.mainWindow.getPosition(); | ||
const display = screen.getDisplayNearestPoint({x:pos[0], y:pos[1]}); | ||
const notificationDistance = height + 5; | ||
const x = display.workAreaSize.width - width - 4; | ||
const y = display.workAreaSize.height - (notificationDistance * index); | ||
|
||
window.setPosition(x, y); | ||
|
||
if (index <= this.maxNotifications) { | ||
window.show(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.