-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
163 lines (157 loc) · 4.13 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
const {
app,
BrowserWindow,
Menu,
ipcMain,
dialog,
shell,
Notification,
} = require("electron");
const fs = require("fs");
const os = require("os");
const path = require("path");
const resizeImg = require("resize-img");
const isMac = process.platform == "darwin";
let mainWin;
const createMainWindow = () => {
mainWin = new BrowserWindow({
width: 800,
height: 600,
title: "Modi",
fullscreenable: true,
focusable: true,
hasShadow: true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
nodeIntegration: true,
enableRemoteModule: true,
},
});
mainWin.loadFile("./Renderer/index.html");
mainWin.once("ready-to-show", () => {
mainWin.show();
});
//remove mainWindow from memory after closing
mainWin.on("closed", () => {
mainWin = null;
});
};
app.whenReady().then(() => {
createMainWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createMainWindow();
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});
//menu template
const template = [
{
label: "Application",
submenu: [
isMac ? { role: "close" } : { role: "quit" },
{ role: "togglefullscreen" },
{ type: "separator" },
{ role: "zoomIn" },
{ role: "zoomOut" },
],
},
{
label: "About",
submenu: [
{
label: "About Modi",
click: () => {
const win = new BrowserWindow({
width: 300,
height: 300,
title: "About Modi",
fullscreenable: false,
maximizable: false,
focusable: true,
hasShadow: true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
win.loadFile("./Renderer/about.html");
win.once("ready-to-show", () => {
win.show();
app.setAppUserModelId("Modi");
});
},
},
],
},
{
role: "Help",
submenu: [
{
label: "Learn about Modi",
click: async () => {
const { shell } = require("electron");
await shell.openExternal("https://electronjs.org");
},
},
],
},
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
ipcMain.on("sent-from-renderer", async (event, data) => {
try {
const imgW = data.width;
const imgH = data.height;
const savedDir = path.join(os.homedir(), "Modi");
const resizedImg = await resizeImg(fs.readFileSync(data.imgPath), {
width: +imgW,
height: +imgH,
});
console.log(resizedImg, "rrrrrrrrrr");
console.log(savedDir);
dialog
.showSaveDialog({
title: "Select directory to save",
defaultPath: savedDir,
buttonLabel: "Save",
filters: [
{
name: "Image",
extensions: ["gif", "jpg", "png"],
},
],
properties: [
{ dontAddToRecent: true, showOverwriteConfirmation: true },
],
})
.then((file) => {
console.log(file.canceled);
if (!file.canceled) {
const savedFilePath = file.filePath;
mainWin.webContents.send("received-from-main", savedFilePath);
shell.openPath(file.filePath.toString());
fs.writeFile(file.filePath.toString(), resizedImg, function (err) {
if (err) throw err;
console.log("Saved!");
});
//pop notification
new Notification({
title: "Thanks for using Modi!",
subtitle: "Hope you enjoyed the service 🙂",
body: `Your image has been stored at: s${savedFilePath} `,
// silent: false,
// hasReply: true,
// timeoutType: "never",
// replyPlaceholder: "Drop a feedback...",
// sound: Api.join("C:/Users/Oluwaloju/Modi", "../Assets/notify.mp3"),
// urgency: "critical",
}).show();
console.log("hi");
}
})
.catch((e) => console.log(e, "Error writing file to dir"));
} catch (error) {
console.log(error);
}
});