aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api
diff options
context:
space:
mode:
authorLibravatar Stefan Malzner <stefan@adlk.io>2017-10-13 12:29:40 +0200
committerLibravatar Stefan Malzner <stefan@adlk.io>2017-10-13 12:29:40 +0200
commit58cda9cc7fb79ca9df6746de7f9662bc08dc156a (patch)
tree1211600c2a5d3b5f81c435c6896618111a611720 /src/electron/ipc-api
downloadferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.gz
ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.zst
ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.zip
initial commit
Diffstat (limited to 'src/electron/ipc-api')
-rw-r--r--src/electron/ipc-api/appIndicator.js80
-rw-r--r--src/electron/ipc-api/autoUpdate.js54
-rw-r--r--src/electron/ipc-api/index.js9
-rw-r--r--src/electron/ipc-api/settings.js10
-rw-r--r--src/electron/ipc-api/tray.js48
5 files changed, 201 insertions, 0 deletions
diff --git a/src/electron/ipc-api/appIndicator.js b/src/electron/ipc-api/appIndicator.js
new file mode 100644
index 000000000..576234d25
--- /dev/null
+++ b/src/electron/ipc-api/appIndicator.js
@@ -0,0 +1,80 @@
1import { app, ipcMain, Tray, Menu } from 'electron';
2import path from 'path';
3
4const INDICATOR_TRAY_PLAIN = 'tray';
5const INDICATOR_TRAY_UNREAD = 'tray-unread';
6const INDICATOR_TASKBAR = 'taskbar';
7
8const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png';
9let trayIcon;
10
11function getAsset(type, asset) {
12 return path.join(
13 __dirname, '..', '..', 'assets', 'images', type, process.platform, `${asset}.${FILE_EXTENSION}`,
14 );
15}
16
17export default (params) => {
18 trayIcon = new Tray(getAsset('tray', INDICATOR_TRAY_PLAIN));
19 const trayMenuTemplate = [
20 {
21 label: 'Show Franz',
22 click() {
23 params.mainWindow.show();
24 },
25 }, {
26 label: 'Quit Franz',
27 click() {
28 app.quit();
29 },
30 },
31 ];
32
33 const trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
34 trayIcon.setContextMenu(trayMenu);
35
36 trayIcon.on('click', () => {
37 params.mainWindow.show();
38 });
39
40 ipcMain.on('updateAppIndicator', (event, args) => {
41 // Update badge
42 if (process.platform === 'darwin'
43 && typeof (args.indicator) === 'string') {
44 app.dock.setBadge(args.indicator);
45 }
46
47 if ((process.platform === 'darwin'
48 || process.platform === 'linux')
49 && typeof (args.indicator) === 'number'
50 ) {
51 app.setBadgeCount(args.indicator);
52 }
53
54 if (process.platform === 'win32') {
55 if (typeof args.indicator === 'number'
56 && args.indicator !== 0) {
57 params.mainWindow.setOverlayIcon(
58 getAsset('taskbar', `${INDICATOR_TASKBAR}-${(args.indicator >= 10 ? 10 : args.indicator)}`),
59 '',
60 );
61 } else if (typeof args.indicator === 'string') {
62 params.mainWindow.setOverlayIcon(
63 getAsset('taskbar', `${INDICATOR_TASKBAR}-alert`),
64 '',
65 );
66 } else {
67 params.mainWindow.setOverlayIcon(null, '');
68 }
69 }
70
71 // Update system tray
72 trayIcon.setImage(getAsset('tray', args.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN));
73
74 if (process.platform === 'darwin') {
75 trayIcon.setPressedImage(
76 getAsset('tray', `${args.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`),
77 );
78 }
79 });
80};
diff --git a/src/electron/ipc-api/autoUpdate.js b/src/electron/ipc-api/autoUpdate.js
new file mode 100644
index 000000000..7bc193e2d
--- /dev/null
+++ b/src/electron/ipc-api/autoUpdate.js
@@ -0,0 +1,54 @@
1import { app, ipcMain } from 'electron';
2import { autoUpdater } from 'electron-updater';
3
4export default (params) => {
5 if (process.platform === 'darwin' || process.platform === 'win32') {
6 // autoUpdater.setFeedURL(updateUrl);
7 ipcMain.on('autoUpdate', (event, args) => {
8 try {
9 autoUpdater.allowPrerelease = Boolean(params.settings.get('beta'));
10 if (args.action === 'check') {
11 autoUpdater.checkForUpdates();
12 } else if (args.action === 'install') {
13 console.log('install update');
14 autoUpdater.quitAndInstall();
15 // we need to send a quit event
16 setTimeout(() => {
17 app.quit();
18 }, 20);
19 }
20 } catch (e) {
21 console.error(e);
22 event.sender.send('autoUpdate', { error: true });
23 }
24 });
25
26 autoUpdater.on('update-not-available', () => {
27 console.log('update-not-available');
28 params.mainWindow.webContents.send('autoUpdate', { available: false });
29 });
30
31 autoUpdater.on('update-available', () => {
32 console.log('update-available');
33 params.mainWindow.webContents.send('autoUpdate', { available: true });
34 });
35
36 autoUpdater.on('download-progress', (progressObj) => {
37 let logMessage = `Download speed: ${progressObj.bytesPerSecond}`;
38 logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`;
39 logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`;
40
41 console.log(logMessage);
42 });
43
44 autoUpdater.on('update-downloaded', () => {
45 console.log('update-downloaded');
46 params.mainWindow.webContents.send('autoUpdate', { downloaded: true });
47 });
48
49 autoUpdater.on('error', () => {
50 console.log('update-error');
51 params.mainWindow.webContents.send('autoUpdate', { error: true });
52 });
53 }
54};
diff --git a/src/electron/ipc-api/index.js b/src/electron/ipc-api/index.js
new file mode 100644
index 000000000..4ea6d1475
--- /dev/null
+++ b/src/electron/ipc-api/index.js
@@ -0,0 +1,9 @@
1import autoUpdate from './autoUpdate';
2import settings from './settings';
3import appIndicator from './appIndicator';
4
5export default (params) => {
6 settings(params);
7 autoUpdate(params);
8 appIndicator(params);
9};
diff --git a/src/electron/ipc-api/settings.js b/src/electron/ipc-api/settings.js
new file mode 100644
index 000000000..1d7eafa6c
--- /dev/null
+++ b/src/electron/ipc-api/settings.js
@@ -0,0 +1,10 @@
1import { ipcMain } from 'electron';
2
3export default (params) => {
4 if (process.platform === 'darwin' || process.platform === 'win32') {
5 // eslint-disable-next-line
6 ipcMain.on('settings', (event, args) => {
7 params.settings.set(args);
8 });
9 }
10};
diff --git a/src/electron/ipc-api/tray.js b/src/electron/ipc-api/tray.js
new file mode 100644
index 000000000..43364c0ed
--- /dev/null
+++ b/src/electron/ipc-api/tray.js
@@ -0,0 +1,48 @@
1import { Tray, Menu, ipcMain } from 'electron';
2import path from 'path';
3
4const INDICATOR_PLAIN = 'franz-taskbar';
5const INDICATOR_UNREAD = 'franz-taskbar-unread';
6
7const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png';
8
9let trayIcon;
10
11function getAsset(asset) {
12 return path.join(
13 __dirname, '..', '..', 'assets', 'images', 'tray', process.platform, `${asset}.${FILE_EXTENSION}`,
14 );
15}
16
17export default (params) => {
18 // if (process.platform === 'win32' || process.platform === 'linux') {
19 trayIcon = new Tray(getAsset(INDICATOR_PLAIN));
20 const trayMenuTemplate = [
21 {
22 label: 'Show Franz',
23 click() {
24 params.mainWindow.show();
25 },
26 }, {
27 label: 'Quit Franz',
28 click() {
29 params.app.quit();
30 },
31 },
32 ];
33
34 const trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
35 trayIcon.setContextMenu(trayMenu);
36
37 trayIcon.on('click', () => {
38 params.mainWindow.show();
39 });
40
41 ipcMain.on('updateTrayIconIndicator', (event, args) => {
42 trayIcon.setImage(getAsset(args.count !== 0 ? INDICATOR_UNREAD : INDICATOR_PLAIN));
43
44 if (process.platform === 'darwin') {
45 trayIcon.setPressedImage(getAsset(`${args.count !== 0 ? INDICATOR_UNREAD : INDICATOR_PLAIN}-active`));
46 }
47 });
48};