-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
320 lines (280 loc) · 7.27 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const {
app,
BrowserWindow,
ipcMain,
dialog,
Menu,
Tray,
globalShortcut,
nativeTheme,
MenuItem,
Notification,
} = require("electron");
const path = require("path");
const fs = require("fs");
const cp = require("child_process");
// Global vairables
let win = null; // main window
let tray = null; //tray
let commandPopUp = null; //commandInputPopup
let commandPopUpIsOpened = false;
let preferenceObj = null; // mapped application preference
let commandObj = null; // mapped commands object
// Load Preference object
try {
fs.accessSync("preference.json", fs.constants.F_OK);
preferenceObj = JSON.parse(fs.readFileSync("preference.json").toString());
} catch (err) {
preferenceObj = {
closeTrayState: true,
startWithWindow: true,
enableAutoRecommand: true,
};
fs.writeFileSync("preference.json", JSON.stringify(preferenceObj));
}
// Load Mapping command object
try {
fs.accessSync("command.json", fs.constants.F_OK);
commandObj = JSON.parse(fs.readFileSync("command.json").toString());
} catch (err) {
commandObj = {};
fs.writeFileSync("command.json", JSON.stringify({}));
}
app.whenReady().then(() => {
// create Main Window
createWindow();
// create Tray
createTray();
// create cmd popup with show : false option
commandPopUp = createCmdPopUp();
// command input popup Global shortcut Register
globalShortcut.register("CommandOrControl+Shift+R", () => {
if (commandPopUp == null) {
commandPopUp = createCmdPopUp();
commandPopUpIsOpened = true;
} else {
if (commandPopUpIsOpened) {
commandPopUp.hide();
commandPopUpIsOpened = false;
} else {
commandPopUp.show();
commandPopUpIsOpened = true;
}
}
});
// 맥에서는 창이 닫혀도 프로세스는 계속 실행되기 때문에
// 사용가능한 창이 없을 때 앱을 활성화하면 새 창이 열리는 기능을 구현해주어야 함
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
new Notification({
title: "Pandora",
body: "Pandora is running!",
}).show();
});
// 메인 윈도우 생성 ==================
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600,
title: "Pandora",
icon: "./icon.png",
alwaysOnTop: false,
show: preferenceObj.startWithWindow,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, "./view/mainWindowPreload.js"),
},
});
// 화면 html 파일
win.loadFile("./view/mainWindow.html");
// 다크모드 적용
nativeTheme.themeSource = "dark";
// 최소화하고 작업표시줄에서 다시 눌렀을 때
win.on("restore", function (e) {
win.show();
});
// 최소화
win.on("minimize", () => {
win.hide();
});
// 닫기버튼 (x 버튼)
win.on("close", (e) => {
if (preferenceObj.closeTrayState) {
e.preventDefault();
win.hide();
} else {
app.quit();
}
});
// IPC Message Handlers
ipcMain.handle("get-preference", (e) => {
return JSON.stringify(preferenceObj);
});
ipcMain.on("set-close-tray", (e, state) => {
preferenceObj.closeTrayState = state;
savePreference();
});
ipcMain.on("set-start-with-window", (e, state) => {
preferenceObj.startWithWindow = state;
savePreference();
});
ipcMain.on("set-auto-recommand", (e, state) => {
preferenceObj.enableAutoRecommand = state;
savePreference();
if (commandPopUp != null) {
commandPopUp.webContents.send("get-auto-recommand", state);
}
});
ipcMain.handle("new-command", (e, cmd, path) => {
return generateNewCommand(cmd, path);
});
ipcMain.handle("get-cmd-list", (e) => {
return commandObj;
});
ipcMain.handle("dialog:openFile", handleFileOpen);
}
// 트레이 생성 함수 =================
function createTray() {
tray = new Tray("./assets/icon.ico");
const contextMenu = Menu.buildFromTemplate([
{
label: "창 열기",
click: () => {
win.show();
},
},
{
label: "Pandora 종료",
role: "quit",
},
]);
tray.setContextMenu(contextMenu);
// prevent right click window taskbar instead tray icon
tray.on("right-click", (e) => {
e.preventDefault();
});
}
// 명령어 입력 팝업 생성 ================
function createCmdPopUp() {
const cmdPopUp = new BrowserWindow({
width: 800,
height: 80,
frame: false,
transparent: true,
titleBarStyle: "hidden",
show: false,
webPreferences: {
preload: path.join(__dirname, "./view/cmdPopupPreload.js"),
},
});
cmdPopUp.loadFile("./view/cmdPopup.html");
// esc 로 입력창 닫기
let menu = new Menu();
menu.append(
new MenuItem({
accelerator: "esc",
acceleratorWorksWhenHidden: true,
click: () => {
commandPopUp.hide();
commandPopUpIsOpened = false;
},
})
);
cmdPopUp.setMenu(menu);
// 명령 실행 핸들러
ipcMain.on("cmd-execute", function (e, cmdString) {
cmdExecute(cmdString);
commandPopUp.hide();
commandPopUpIsOpened = false;
});
// 명령어 자동완성 기능 플래그
ipcMain.handle("get-auto-recommand-explicit", () => {
return preferenceObj.enableAutoRecommand;
});
// 명령어 자동완성 핸들러
ipcMain.handle("cmd-auto-recommand", (e, cmdString) => {
return cmdAutoRecommand(cmdString);
});
return cmdPopUp;
}
// 세팅파일 저장
function savePreference() {
fs.writeFileSync("preference.json", JSON.stringify(preferenceObj));
}
// 명령어 매핑 json 파일에 저장
function saveCommands() {
fs.writeFileSync("command.json", JSON.stringify(commandObj));
}
// 파일 찾기 다이얼로그 오픈
async function handleFileOpen() {
const { canceled, filePaths } = await dialog.showOpenDialog();
if (canceled) {
return;
} else {
return filePaths[0];
}
}
// 새로운 명령어 등록
function generateNewCommand(cmd, path) {
commandObj[cmd] = path;
if (commandObj[cmd] != undefined) {
saveCommands();
return true;
}
}
// 명령어 실행 함수
function cmdExecute(cmdString) {
// 사용자 설정 예약어
let path = commandObj[cmdString];
if (path != undefined) {
if (path.split(".")[-1] != "exe") {
cp.execFile(path);
return;
}
}
// 콜론 명령어(:)
let colonCmd = null;
let isColon = false;
let colneKeyword = ["text", "txt", "site", "open"];
for (let i of colneKeyword) {
if (cmdString.startsWith(i)) {
isColon = true;
colonCmd = i;
break;
}
}
if (isColon) {
cmdString = cmdString.split(colonCmd)[1].slice(1);
switch (colonCmd) {
case "text":
case "txt": {
cp.exec(`notepad ${cmdString}`);
return;
}
case "site": {
cp.execFile(
`C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe`,
[`${cmdString}`]
);
return;
}
case "open": {
cp.exec(`explorer.exe ${cmdString}`);
return;
}
}
}
// 일반 명령어
cp.exec(cmdString);
return;
}
// 명령어 추천 함수
function cmdAutoRecommand(cmdString) {
let str = [];
let reg = new RegExp(`^${cmdString}`);
for (let i in commandObj) {
if (reg.test(i)) str.push(i);
}
return str.length == 0 ? "" : str.sort()[0].slice(cmdString.length);
}