-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
274 lines (214 loc) · 7.22 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
'use strict';
const electron = require('electron');
const events = require('events');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const ipcMain = electron.ipcMain;
const globalShortcut = electron.globalShortcut;
const clipboard = electron.clipboard;
const jsonfile = require('jsonfile');
const getYoutubeUrl = require('youtube-url');
const appConfig = require('./user-data/appConfig.json');
const userGuide = require('./user-data/userGuide.json');
const lastPlayerSize = process.env.HOT ? require('./user-data/playerSize.development.json') : require('./user-data/playerSize.production.json');
electron.crashReporter.start();
let guideWindow = null;
let playerWindow = null;
let playListWindow = null;
let youtubeWindow = null;
let trayIcon = null;
let onlineStatusWindow = null;
let indexFile = process.env.HOT ? 'index-dev' : 'index';
let playerWidth = lastPlayerSize.width; // 412
let playerHeight = lastPlayerSize.height; // 232
let playerFullScreen = null;
// ---
// app
//
var mini = new events.EventEmitter();
mini.app = app;
app.on('window-all-closed', function() {
app.quit();
});
app.on('will-quit', function() {
if (globalShortcut.isRegistered(appConfig.hotKeyTogglePlayer)) {
globalShortcut.unregister(appConfig.hotKeyTogglePlayer); // 단축키의 등록을 해제 한다.
}
});
const appBootStrap = function() {
mini.setPlayListWindow(); // 메인 윈도우.
mini.setHotKey(); // 단축키.
mini.setIpcMain(); // ipc 통신.
};
app.on('ready', appBootStrap);
// ---
// Windows.
//
mini.setPlayListWindow = function() {
playListWindow = new BrowserWindow({
width : 928,
height : 605,
show : true,
frame : true,
titleBarStyle : 'hidden-inset',
resizable : true,
fullscreen : false
});
playListWindow.loadURL('file://' + __dirname + '/app/' + indexFile + '.html#/playlist');
// playListWindow.setAlwaysOnTop(true);
playListWindow.on('focus', function() {
var checkeVideo = mini.clipboardChecke();
if (checkeVideo) playListWindow.webContents.send('클립보드-발견', mini.clipboardChecke());
playListWindow.webContents.send('검색박스-포커스', 1);
});
playListWindow.on('closed', function() {
playListWindow = null;
});
playListWindow.webContents.on('did-finish-load', function() {
playListWindow.webContents.send('dispatch-playlist-to-playlist', 1);
});
};
mini.setPlayerWindow = function() {
playerWindow = new BrowserWindow({
width : playerWidth,
height : playerHeight,
show : true,
frame : false,
// darkTheme : true,
resizable : appConfig.playerResizable,
acceptFirstMouse : true
});
playerWindow.loadURL('file://' + __dirname + '/app/' + indexFile + '.html#/');
if (appConfig.aspectRatio == '16/9') {
playerWindow.setAspectRatio(16/9, [ 0, 0 ]);
} else {
playerWindow.setAspectRatio(4/3, [ 0, 0 ]);
};
// playerWindow.setAlwaysOnTop(true);
playerWindow.on('resize', function() {
mini.playerLastSize(); // 플레이어 사이즈 저장.
});
playerWindow.on('moved', function() {
mini.playerLastSize(); // 플레이어 사이즈 저장.
});
playerWindow.on('restore', function() {
playerWindow.hide(); // 최소창에서 돌아오면 화면이 안보인다. _-_;
playerWindow.show(); // 뭐 이런 버그가..
});
// playerWindow.on('enter-full-screen', function() {});
playerWindow.on('leave-full-screen', function() {
playerWindow.show();
playerFullScreen = false;
});
playerWindow.on('closed', function() {
playerWindow = null;
});
playerWindow.webContents.on('did-finish-load', function() {
if (onlineStatusWindow == null) mini.setOnlineStatusCheckerWindow();
onlineStatusWindow.webContents.send('check-online-status', function(){})
});
};
mini.setOnlineStatusCheckerWindow = function() {
onlineStatusWindow = new BrowserWindow({
width : 0,
height : 0,
show : false
});
onlineStatusWindow.loadURL('file://' + __dirname + '/app/online-status.html');
onlineStatusWindow.on('closed', function() {
onlineStatusWindow = null;
});
};
// ---
// Functions.
//
mini.setHotKey = function() {
var ret = globalShortcut.register(appConfig.hotKeyTogglePlayer, function() {
mini.togglePlayerWindow();
});
if (!ret) console.log(appConfig.hotKeyTogglePlayer + ' 등록 실패');
if (globalShortcut.isRegistered(appConfig.hotKeyTogglePlayer)) {
console.log('단축키 ' + appConfig.hotKeyTogglePlayer + ' 가 등록 되었습니다.');
}
};
mini.togglePlayerWindow = function(state) {
if (!playerWindow) return mini.setPlayerWindow();
if (state != 'show' && playerWindow && playerWindow.isVisible()) {
playerWindow.hide();
mini.emit('playerHide');
return;
};
playerWindow.show();
};
mini.showPlayListWindow = function() {
if (!playListWindow) mini.setPlayListWindow();
playListWindow.show();
};
mini.clipboardChecke = function() {
return getYoutubeUrl.extractId(clipboard.readText());
};
mini.playerLastSize = function() {
// TODO: 위치저장.
console.log(playerWindow.getBounds());
if (playerWindow.isFullScreen() === false) {
const file = process.env.HOT ? './user-data/playerSize.development.json' : './user-data/playerSize.production.json';
const newSize = {
width: playerWindow.getSize()[0],
height: playerWindow.getSize()[1]
};
jsonfile.writeFile(file, newSize, function (err) {
if(err) return console.error(err);
});
playerWidth = playerWindow.getSize()[0];
playerHeight = playerWindow.getSize()[1];
}
};
mini.setIpcMain = function() {
ipcMain.on('ready-player', function(event) {
mini.emit('readyPlayer');
});
ipcMain.on('show-controller', function(event, state) {
playerWindow.webContents.send('show-controller-to-player', state);
});
ipcMain.on('close-player', function(event) {
playerWindow.close();
if (onlineStatusWindow) onlineStatusWindow.close();
});
ipcMain.on('minimize-player', function(event) {
playerWindow.minimize();
});
ipcMain.on('fullscreen-player', function(event) {
var f = playerWindow.isFullScreen();
if (f) playerFullScreen = true;
playerWindow.setFullScreen(f ? false : true);
});
ipcMain.on('always-on-top-player', function(event) {
var t = playerWindow.isAlwaysOnTop();
playerWindow.setAlwaysOnTop(t ? false : true);
});
ipcMain.on('play-video', function(event, videoIndex) {
var sender = function(){
playerWindow.webContents.send('play-video-to-player', videoIndex);
};
if (!playerWindow) {
mini.setPlayerWindow();
mini.once('readyPlayer', sender);
return;
}
mini.togglePlayerWindow('show');
sender();
});
ipcMain.on('open-playlist', function(event) {
mini.showPlayListWindow();
});
ipcMain.on('refresh-playlist', function(event, status) {
playListWindow.webContents.send('dispatch-playlist-to-playlist', status);
});
ipcMain.on('change-online-status', function(event, status) {
if (status == 'offline' && playerWindow) {
playerWindow.webContents.send('offline', status);
} else {
playerWindow.webContents.send('online', status);
}
});
};