Skip to content

Commit a5f56d7

Browse files
committed
Notification Icon
1 parent 77ae186 commit a5f56d7

File tree

1 file changed

+69
-49
lines changed

1 file changed

+69
-49
lines changed

src/index.ts

+69-49
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import { app, BrowserWindow, Tray, Menu, NativeImage, nativeImage, Notification } from 'electron';
1+
import {
2+
app,
3+
BrowserWindow,
4+
Tray,
5+
Menu,
6+
NativeImage,
7+
nativeImage,
8+
Notification,
9+
} from 'electron';
210
import * as path from 'path';
3-
import rpc from "./lib/rpc";
11+
import rpc from './lib/rpc';
412

5-
import socket from "./lib/socket";
13+
import socket from './lib/socket';
614

715
const WebSocket = socket();
816

917
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
1018
if (require('electron-squirrel-startup')) {
11-
// eslint-disable-line global-require
12-
app.quit();
19+
// eslint-disable-line global-require
20+
app.quit();
1321
}
1422

1523
let stopped = false;
@@ -19,34 +27,44 @@ let tray: Tray | null;
1927
let mainWindow: BrowserWindow | null = null;
2028

2129
const createWindow = (): void => {
22-
// Create the browser window.
23-
if(mainWindow) {
24-
mainWindow.show();
25-
return
26-
}
27-
mainWindow = new BrowserWindow({
28-
height: 600,
29-
width: 800,
30-
darkTheme: true,
31-
backgroundColor: '#000000',
32-
});
33-
34-
mainWindow.setMenu(null);
35-
36-
mainWindow.on("close", () => {
37-
mainWindow = null;
38-
if(stopped == true) return;
39-
new Notification({
40-
title: "Mcat-Dc",
41-
body: "Mcat-Dc is still running in the background! Check your system tray to open it again or quit the app.",
42-
silent: true,
43-
timeoutType: "default",
44-
subtitle: "Info"
45-
}).show()
46-
})
47-
48-
// and load the index.html of the app.
49-
mainWindow.loadFile(path.join(__dirname, '../src/index.html'));
30+
// Create the browser window.
31+
if (mainWindow) {
32+
mainWindow.show();
33+
return;
34+
}
35+
mainWindow = new BrowserWindow({
36+
height: 600,
37+
width: 800,
38+
darkTheme: true,
39+
backgroundColor: '#000000',
40+
});
41+
42+
mainWindow.setMenu(null);
43+
44+
mainWindow.on('close', () => {
45+
mainWindow = null;
46+
if (stopped == true) return;
47+
new Notification({
48+
title: 'Mcat-Dc',
49+
body:
50+
'Mcat-Dc is still running in the background! Check your system tray to open it again or quit the app.',
51+
silent: true,
52+
timeoutType: 'default',
53+
subtitle: 'Info',
54+
icon: path.join(__dirname, '../src/mcat.png'),
55+
})
56+
.on('action', (event, i) => {
57+
if (i == 0) {
58+
createWindow();
59+
} else {
60+
app.quit();
61+
}
62+
})
63+
.show();
64+
});
65+
66+
// and load the index.html of the app.
67+
mainWindow.loadFile(path.join(__dirname, '../src/index.html'));
5068
};
5169

5270
// This method will be called when Electron has finished
@@ -55,8 +73,8 @@ const createWindow = (): void => {
5573
app.on('ready', createWindow);
5674

5775
app.whenReady().then(() => {
58-
createWindow();
59-
runTray();
76+
createWindow();
77+
runTray();
6078
});
6179

6280
// Quit when all windows are closed, except on macOS. There, it's common
@@ -67,34 +85,36 @@ app.on('window-all-closed', (e: any) => {
6785
});
6886

6987
app.on('activate', () => {
70-
// On OS X it's common to re-create a window in the app when the
71-
// dock icon is clicked and there are no other windows open.
72-
if (BrowserWindow.getAllWindows().length === 0) {
73-
createWindow();
74-
}
88+
// On OS X it's common to re-create a window in the app when the
89+
// dock icon is clicked and there are no other windows open.
90+
if (BrowserWindow.getAllWindows().length === 0) {
91+
createWindow();
92+
}
7593
});
7694

7795
// In this file you can include the rest of your app's specific main process
7896
// code. You can also put them in separate files and import them here.
7997

8098
function runTray() {
81-
const icon = nativeImage.createFromPath(path.join(__dirname, "../src/mcat.png")).resize({ width: 32, height: 32 });
99+
const icon = nativeImage
100+
.createFromPath(path.join(__dirname, '../src/mcat.png'))
101+
.resize({ width: 32, height: 32 });
82102
tray = new Tray(icon);
83103
const contextMenu = Menu.buildFromTemplate([
84104
{ label: 'Mcat-Dc', type: 'normal', enabled: false },
85105
{
86106
label: 'Quit',
87107
type: 'normal',
88108
click: () => {
89-
stopped = true;
109+
stopped = true;
90110
app.quit();
91-
}
92-
}
111+
},
112+
},
93113
]);
94114

95-
tray.on("click", () => {
96-
createWindow();
97-
})
115+
tray.on('click', () => {
116+
createWindow();
117+
});
98118

99-
tray.setContextMenu(contextMenu);
100-
}
119+
tray.setContextMenu(contextMenu);
120+
}

0 commit comments

Comments
 (0)