-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
192 lines (166 loc) · 5.04 KB
/
main.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const path = require('path');
const os = require('os');
const fs = require('fs');
const { app, BrowserWindow, Menu, ipcMain, shell } = require('electron');
const ResizeImg = require('resize-img');
// the below one is used to check if the app is in production or development
const isMac = process.platform === 'darwin';
const isDev = process.env.NODE_ENV !== 'production';
// the below one is used to check if the app is in production or development
let mainWindow;
// the below one is used to create a window
function createMainWindow() {
mainWindow = new BrowserWindow({
title: 'SnapScale',
width: isDev ? 1000 : 500,
height: 600,
icon: 'icon.ico',
webPreferences: {
contextIsolation: true,
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js'),
}
})
// Open devtools if in dev environment0
if (isDev) {
mainWindow.webContents.openDevTools();
}
// The below one can be used to load a file it can also load a url if we use loadurl() e.x :- twitter.com
mainWindow.loadFile(path.join(__dirname, 'renderer/index.html'));
}
// the below one is used to create a about window
function createAboutWindow() {
const aboutWindow = new BrowserWindow({
title: 'About SnapScale',
width: 300,
height: 300,
icon: 'icon.ico',
})
aboutWindow.loadFile(path.join(__dirname, 'renderer/about.html'));
}
// the below one is used to create a menu
const menu = [
...(isMac ? [{
label: app.name,
submenu: [
{
label: 'About',
click: createAboutWindow
}
]
}] : []),
{
label: 'file',
submenu: [
{
label: 'Quit',
accelerator: 'CmdOrCtrl+W',
click: () => app.quit()
},
{
label: 'minimize',
accelerator: 'CmdOrCtrl+M',
click: () => mainWindow.minimize()
},
{
label: 'maximize',
accelerator: 'CmdOrCtrl+X',
click: () => mainWindow.maximize()
},
{
label: 'restore',
accelerator: 'CmdOrCtrl+R',
click: () => mainWindow.restore()
},
{
label: 'reload',
accelerator: 'CmdOrCtrl+L',
click: () => mainWindow.reload()
}
]
},
...(!isMac ? [
{
label: 'Help',
submenu: [
{
label: 'About',
click: createAboutWindow
},
{
label: 'Creator',
click: () => shell.openExternal('https://rajeshportfolio.me')
}
]
}
] : [
])
]
let splash;
function createSplashWindow() {
splash = new BrowserWindow({
width: 600,
height: 400,
frame: false,
icon: 'icon.ico',
alwaysOnTop: true,
});
splash.loadFile(path.join(__dirname, 'renderer/splash.html'))
splash.show();
}
// or you can use the below one to create a window when the app is ready it returns a promise
app.whenReady().then(() => {
createSplashWindow();
splash.once('ready-to-show', () => {
setTimeout(() => {
splash.close();
splash = null;
createMainWindow();
}, 3000);
})
const mainMenu = Menu.buildFromTemplate(menu);
Menu.setApplicationMenu(mainMenu);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});
// Remove mainwindow from memory when closed
if (BrowserWindow.getAllWindows().length === 0) {
mainWindow = null;
}
});
// Respond to ipcRenderer resize
ipcMain.on('image:resize', (e, options) => {
options.dest = path.join(os.homedir(), 'SnapScaleImages');
resizeImage(options);
});
// the below one is used to resize the image
async function resizeImage({ imgPath, width, height, dest }) {
try {
const newPath = await ResizeImg(fs.readFileSync(imgPath), {
width: parseInt(width),
height: parseInt(height),
});
// Creating filename
const filename = path.basename(imgPath);
// Creating destination folder if it does not exists
if(!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
// witing file to destination folder
fs.writeFileSync(path.join(dest, filename), newPath);
// Sending success message to renderer
mainWindow.webContents.send('image:done', 'Image resized successfully');
// Open dest folder
shell.openPath(dest);
} catch (error) {
console.log(error)
}
}
// the below one is used to quit the app when all the windows are closed except on mac as mac works differently
app.on('window-all-closed', () => {
if (!isMac) {
app.quit();
}
});