This repository has been archived on 2022-06-29. You can view files and clone it, but cannot push or open issues or pull requests.
yandexmail-desktop/main.js

113 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-01-20 07:22:17 +05:00
const { app, BrowserWindow, Menu, Tray } = require('electron')
const nativeImage = require('electron').nativeImage
const windowStateKeeper = require('electron-window-state');
var path = require('path');
const ipc = require('electron').ipcMain;
let mainWindow;
let tray = null;
let normalIcon = nativeImage.createFromPath(path.join(__dirname, "assets", "icons", "icon.png"));
let unreadIcon = nativeImage.createFromPath(path.join(__dirname, "assets", "icons", "unread.png"));
function createWindow() {
// Window state - load previous session data or fallback to default.
let mainWindowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 700
});
mainWindow = new BrowserWindow({
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
autoHideMenuBar: true,
titleBarStyle: 'hidden',
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: true,
webviewTag: false,
preload: path.resolve(__dirname, 'renderer.js')
}
});
mainWindowState.manage(mainWindow);
2019-01-20 07:59:44 +05:00
//mainWindow.webContents.openDevTools();
2019-01-20 07:22:17 +05:00
mainWindow.loadURL('https://mail.yandex.ru/');
mainWindow.on('closed', function () {
mainWindow = null;
})
mainWindow.setIcon(normalIcon);
// Tray icon. Do not create it on macOS.
2019-01-20 07:52:31 +05:00
if (process.platform != "darwin") {
tray = new Tray(normalIcon);
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open Yandex.Mail window', type: 'normal', click: showHideMainWindow },
{ label: '-', type: 'separator' },
{ label: 'Exit Yandex.Mail wrapper', type: 'normal', click: quitWrapper }
]);
tray.setTitle('Yandex.Mail');
tray.setToolTip('Yandex.Mail');
tray.setContextMenu(contextMenu);
tray.on('click', () => {
showHideMainWindow(null, mainWindow, null);
});
2019-01-20 07:58:52 +05:00
} else {
app.dock.setIcon(normalIcon);
}
2019-01-20 07:22:17 +05:00
mainWindow.Notification = function (title, options) {
options.icon = unreadIcon;
notification = new Notification(title, options);
checkForUnreads();
};
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
quitWrapper();
}
});
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
ipc.on("has-unread", function (event, count) {
2019-01-20 07:22:17 +05:00
mainWindow.setIcon(unreadIcon);
2019-01-20 07:52:31 +05:00
if (process.platform == "darwin") {
2019-01-20 07:58:52 +05:00
app.dock.setIcon(unreadIcon);
} else {
tray.setImage(unreadIcon);
}
2019-01-20 07:22:17 +05:00
});
ipc.on("has-no-unread", function (event, count) {
2019-01-20 07:22:17 +05:00
mainWindow.setIcon(normalIcon);
2019-01-20 07:52:31 +05:00
if (process.platform == "darwin") {
2019-01-20 07:58:52 +05:00
app.dock.setIcon(normalIcon);
} else {
2019-01-20 07:58:52 +05:00
tray.setImage(normalIcon);
}
2019-01-20 07:22:17 +05:00
})
function showHideMainWindow() {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
}
function quitWrapper() {
app.quit();
}