This repository was archived by the owner on Sep 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
120 lines (101 loc) · 3.27 KB
/
main.js
File metadata and controls
120 lines (101 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const {app, BrowserWindow, ipcMain, Menu, shell} = require('electron');
const storage = require('electron-json-storage-sync');
const appMenu = require('./menu');
const tray = require('./tray');
const minimist = require('minimist');
const path = require('path');
const argv = minimist(process.argv.slice(1));
// A global reference of the window object, to prevent window being closed
// when the JavaScript object is garbage collected.
let mainWindow;
// flag that force quits the application
let forceQuit = false;
// Context menu. Handled with package electron-context-menu
require('electron-context-menu')({
prepend: (params, browserWindow) => [{
// Only show save image when right-clicking images
visible: params.mediaType === 'image'
}],
// Add or hide inspect element entry based on environment
showInspectElement: argv.devtools
});
// IPC
ipcMain.on('notification-click', () => {
mainWindow.show();
});
ipcMain.on('display-badge', () => {
tray.setBadgeIcon(true);
});
function createWindow () {
// if configuration doesn't exist, create defaults
const lastWindowStateResult = storage.get('lastWindowState');
if (lastWindowStateResult.status)
var lastWindowState = lastWindowStateResult.data;
else
var lastWindowState = {x: 0, y:0, width: 980, height: 620}
const launchOnStartupResult = storage.get('launchOnStartup');
if (!launchOnStartupResult.status)
storage.set('launchOnStartup', false);
const mainURL = 'https://web.whatsapp.com';
const title = 'WhatsApp';
mainWindow = new BrowserWindow(
{
title: app.getName(),
x: lastWindowState.x,
y: lastWindowState.y,
width: lastWindowState.width,
height: lastWindowState.height,
minWidth: 980,
minHeight: 620,
titleBarStyle: 'hidden-inset',
autoHideMenuBar: true,
icon: path.join(__dirname, 'images/icon96.png'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
}
});
mainWindow.loadURL(mainURL);
// Open devtools in dev mode
if (argv.devtools) {
mainWindow.webContents.openDevTools();
}
// On show, unset unread-badge icon
mainWindow.on('show', function(e){
tray.setBadgeIcon(false);
});
// Hide on close if not forceQuit
mainWindow.on('close', function(e){
if(!forceQuit){
e.preventDefault();
mainWindow.hide();
}
else {
storage.set('lastWindowState', mainWindow.getBounds());
}
});
// Handle opening of links in default browser
mainWindow.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
});
return mainWindow
};
// Create systray
app.on('ready', () => {
Menu.setApplicationMenu(appMenu);
appWindow = createWindow();
tray.create(appWindow);
});
app.on('before-quit', function() {
forceQuit = true;
});
app.on('will-quit', () => {
mainWindow = null;
});
app.on('activate', function () {
// On OS X 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();
}
});