Jelajahi Sumber

Gestion tray et notification

Arthur Brandao 5 tahun lalu
induk
melakukan
a9ac475418

+ 1 - 0
app/main/application-menu.js

@@ -41,6 +41,7 @@ let template = [
       {
         label: 'Reduire l\'application',
         click: () => {
+          demo.tray();
           console.log('tray+notif');
         }
       }

TEMPAT SAMPAH
app/main/asset/icon.png


TEMPAT SAMPAH
app/main/asset/tray.png


+ 9 - 0
app/main/function/demo-application-menu.js

@@ -1,3 +1,5 @@
+const tray = require('./tray');
+const notif = require('./notif');
 const demoMenu = {};
 
 demoMenu.primeNumberPopup = function () {
@@ -12,4 +14,11 @@ demoMenu.primeNumberResult = function () {
 
 };
 
+demoMenu.tray = function () {
+    // Show notif
+    notif.create('Application minimisé', 'Cliquer sur l\'icone dans la barre des notifications pour réouvrir l\'application');
+    // Add tray
+    tray.active();
+}
+
 module.exports = demoMenu;

+ 32 - 0
app/main/function/notif.js

@@ -0,0 +1,32 @@
+const {ipcMain} = require('electron');
+const path = require('path');
+const window = require('../../../helper/window');
+const notif = {};
+
+let hiddenWindow = null;
+
+notif.create = function (title, content) {
+    // If a notification is already displayed
+    if (hiddenWindow) {
+        return;
+    }
+    // Notification data
+    const data = {
+        title: title,
+        body: content,
+        icon: path.join(__dirname, '../asset/icon.png')
+    }
+    // Wait close information
+    ipcMain.once('notif-close', (event, arg) => {
+        hiddenWindow.close();
+        hiddenWindow = null;
+    })
+    // Create new invisible window to show notif
+    hiddenWindow = window.hidden(path.join(__dirname, '../../renderer/page/notif/notif.html'));
+    // Send information
+    hiddenWindow.on('ready-to-show', () => {
+        hiddenWindow.webContents.send('new-notif', data);
+    });
+}
+
+module.exports = notif;

+ 58 - 0
app/main/function/tray.js

@@ -0,0 +1,58 @@
+const {app, Tray, Menu} = require('electron');
+const path = require('path');
+const window = require('../../../helper/window');
+const tray = {};
+
+tray.openApp = function () {
+    if (appIcon) {
+        appIcon.destroy();
+        // Re create the main window on dashboard page and close the invisible window
+        const newWindow = window.new(path.join(__dirname, '../../renderer/page/dashboard/dashboard.html'));
+        mainWindow.close();
+        mainWindow = newWindow;
+    }
+};
+
+tray.closeApp = function () {
+    if (appIcon) {
+        appIcon.destroy();
+        app.quit();
+    }
+}
+
+tray.active = function () {
+    appIcon = new Tray(path.join(__dirname, '../asset/tray.png'));
+    appIcon.setToolTip('Electronotes');
+    appIcon.setContextMenu(menu);
+    // Hide app
+    const invisibleWindow = window.hidden(); // Create invisible window to avoid closing the app when all window are closed
+    mainWindow.close();
+    mainWindow = invisibleWindow;
+    // Left click => open app
+    appIcon.on('click', (event) => {
+        tray.openApp();
+    });
+};
+
+module.exports = tray;
+
+/* --- Var and event --- */
+
+let appIcon = null;
+app.on('quit', () => {
+    if (appIcon) {
+        appIcon.destroy();
+    }
+});
+
+const template = [
+    {
+        label: 'Ouvrir',
+        click: tray.openApp,
+    },
+    {
+        label: 'Quitter',
+        click: tray.closeApp
+    }
+];
+const menu = Menu.buildFromTemplate(template);

+ 13 - 0
app/renderer/page/notif/notif.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <title>Electronotes</title>
+    </head>
+    <body>         
+        <!-- Load only electronotes.js this page is hidden -->
+        <script type="text/javascript" src="../../common/js/electronotes.js"></script>
+        <!-- Load personnal js -->
+        <script type="text/javascript" src="notif.js"></script>
+    </body>
+</html>

+ 15 - 0
app/renderer/page/notif/notif.js

@@ -0,0 +1,15 @@
+// Wait notification demand
+ipcRenderer.once('new-notif', (event, notification) => {
+    // Check notification data
+    if (notification.title && notification.body && notification.icon) {
+        // Create notification
+        const notif = new window.Notification(notification.title, notification); 
+        // Close notif after 2.5 sec
+        setTimeout(() => {
+            notif.close();
+            ipcRenderer.send('notif-close');
+        }, 2500)   
+    } else {
+        ipcRenderer.send('notif-close');
+    }
+});

+ 15 - 0
helper/window.js

@@ -3,6 +3,21 @@ const { BrowserWindow } = require('electron');
 const path = require('path');
 const window = {};
 
+window.hidden = function (file) {
+    const win = new BrowserWindow({
+        width: 0,
+        height: 0,
+        show: false,
+        webPreferences: {
+            nodeIntegration: true
+        }
+    });
+    if (file) {
+        win.loadFile(file);
+    }
+    return win;
+}
+
 window.new = function (file, width, height) {
     // Default values
     width = width ? width : config.window.width;