-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
140 lines (118 loc) · 3.68 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
const {app, BrowserWindow, Menu, isMac, globalShortcut, ipcMain} = require('electron');
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminOptipng = require('imagemin-optipng');
const slash = require('slash');
const log = require('electron-log')
const ENVIRONMENTS = {
local: 1,
production: 2,
}
let mainWindow;
let aboutWindow;
let environment = ENVIRONMENTS.local;
function createBrowserWindow() {
const browserWindow = new BrowserWindow({
title: app.name,
width: environment !== ENVIRONMENTS.local ? 500 : 1000,
height: 600,
icon: `${__dirname}/app/assets/icons/application-icon__256x256.png`,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
browserWindow.loadFile(`./app/index.html`);
if (environment === ENVIRONMENTS.local) {
browserWindow.webContents.openDevTools();
}
return browserWindow;
}
function createAboutWindow() {
const browserWindow = new BrowserWindow({
title: `About ${app.name}`,
width: 300,
height: 400,
resizable: false,
icon: `${__dirname}/app/assets/icons/application-icon__256x256.png`
});
browserWindow.loadFile(`./app/about.html`);
return browserWindow;
}
function createApplicationMenu() {
const fileMenu = {
role: 'fileMenu'
};
const developerMenu = {
label: "Developer",
submenu: [
{role: 'reload'},
{role: 'forcereload'},
{type: 'separator'},
{role: 'toggleDevTools'}
]
};
const helpMenu = {
role: "help",
submenu: [
{
label: 'About',
click: () => {
aboutWindow = createAboutWindow();
}
}
]
}
const createDefaultMenu = () => [
fileMenu,
developerMenu,
helpMenu
];
const createMacOsMenu = () => [
{
role: 'appMenu'
},
fileMenu,
developerMenu
]
const menuTemplate = !isMac ? createDefaultMenu() : createMacOsMenu();
return Menu.buildFromTemplate(menuTemplate)
}
async function onStartUp() {
await app.whenReady()
app.applicationMenu = createApplicationMenu();
globalShortcut.register("CmdOrCtrl+R", () => mainWindow.reload())
globalShortcut.register("CmdOrCtrl+Shift+I", () => mainWindow.toggleDevTools())
mainWindow = createBrowserWindow();
}
ipcMain.on('image/shrink?request', (event, {path, quality, outputFolderPath}) => {
log.log('Received "image/shrink" request.', {path, quality, outputFolderPath});
shrinkImage(path, quality, outputFolderPath)
});
async function shrinkImage(sourcePath, quality, outputFolderPath) {
try {
const optimizationLevel = Math.floor(quality / 8);
const sourcePathWithBackslashes = slash(sourcePath);
const outputFolderPathWithBackslashes = slash(outputFolderPath)
const shrunkPaths = await imagemin(
[sourcePathWithBackslashes],
{
destination: outputFolderPathWithBackslashes,
plugins: [
imageminOptipng({
optimizationLevel
}),
imageminMozjpeg({
quality
})
]
}
);
const destinationPath = shrunkPaths[0].destinationPath;
log.log(shrunkPaths)
mainWindow.webContents.send('image/shrink?response', {path: destinationPath});
} catch (error) {
log.error('Could not shrink image file.', error);
}
}
onStartUp()