-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
436 lines (385 loc) · 14.4 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
const {app, Menu, MenuItem, dialog, ipcMain, BrowserWindow, nativeTheme} = require('electron')
const path = require('path');
const fs = require('fs');
const Store = require('electron-store');
const store = new Store()
//------------------------------------
// 定数
//------------------------------------
// ウィンドウのデフォルトサイズ
const DEFAULT_SIZE = {
width: 800,
height: 600
}
// ウィンドウのデフォルトポジション
const DEFAULT_POS = {
x: 0,
y: 0
}
// ダークモードのデフォルト
const DEFAULT_DARK_MODE = false
// https://qiita.com/umamichi/items/8781e426e9cd4a88961b
let pdf_win;
const PDFWindow = require('electron-pdf-window');
let dark_menu;
function createPDFWindow() {
if (pdf_win === undefined) {
const pos = store.get('pdf.window.pos') || [DEFAULT_POS.x, DEFAULT_POS.y];
const size = store.get('pdf.window.size') || [DEFAULT_SIZE.width, DEFAULT_SIZE.height];
pdf_win = new PDFWindow({
width: size[0],
height: size[1],
x: pos[0],
y: pos[1],
show: false
});
pdf_win.on('close', (e) => {
store.set('pdf.window.pos', pdf_win.getPosition()) // ウィンドウの座標を記録
store.set('pdf.window.size', pdf_win.getSize()) // ウィンドウのサイズを記録
pdf_win.hide();
e.preventDefault();
})
const pdf_contents = pdf_win.webContents
pdf_contents.on('did-finish-load', () => {
if (pdf_contents.dark_css_key)
delete pdf_contents.dark_css_key
set_pdf_dark(pdf_contents, dark_menu.checked)
})
}
}
function set_pdf_dark(pdf_contents, flag) {
if (flag) {
// dark mode に設定する
if (!pdf_contents.dark_css_key) {
// 設定がないので設定する
const dark_css = 'embed {filter: brightness(0.91) grayscale(0.95) invert(0.95) sepia(0.55) hue-rotate(180deg);}'
pdf_contents.insertCSS(dark_css).then((key) => {
pdf_contents.dark_css_key = key
})
// ':root[theme="normal"] embed {filter: brightness(1) grayscale(0) invert(0) sepia(0) hue-rotate(0deg);}'
}
} else {
// dark mode を解除する
if (pdf_contents.dark_css_key) {
// すでに設定がされているので解除する.
pdf_contents.removeInsertedCSS(pdf_contents.dark_css_key).then(() => {
delete pdf_contents.dark_css_key
})
}
}
}
let main_win;
function app_main() {
const mainMenu = new Menu();
// mac original
const appMenu = new Menu();
appMenu.append(new MenuItem({role: "hideOthers"}));
appMenu.append(new MenuItem({role: "unhide"}));
appMenu.append(new MenuItem({role: "quit"}));
mainMenu.append(new MenuItem({role: "appMenu", submenu: appMenu}));
const fileMenu = new Menu();
fileMenu.append(new MenuItem({
label: 'Open', accelerator: 'CommandOrControl+O', click: (m, w) => {
if ((pdf_win !== undefined) && (w === pdf_win)) return;
if ((main_win !== undefined) && (w === main_win)) {
selectfile(w)
} else {
main_win = createMainWindow(null)
}
}
}));
fileMenu.append(new MenuItem({role: 'recentdocuments', submenu: [new MenuItem({role: 'clearrecentdocuments'})]}));
fileMenu.append(new MenuItem({
role: 'close', click: () => {
if (main_win !== undefined) {
force_hide(pdf_win)
main_win.close()
}
}
}));
mainMenu.append(new MenuItem({role: 'fileMenu', submenu: fileMenu}));
const editMenu = new Menu();
editMenu.append(new MenuItem({role: 'cut'}));
editMenu.append(new MenuItem({role: 'copy'}));
editMenu.append(new MenuItem({role: 'paste'}));
editMenu.append(new MenuItem({
label: 'Search', accelerator: 'CommandOrControl+f', click: () => {
const win = BrowserWindow.getFocusedWindow();
search_main_openDialog(win.webContents)
}
}));
mainMenu.append(new MenuItem({role: 'editMenu', submenu: editMenu}));
const viewMenu = new Menu();
dark_menu = new MenuItem({
label: 'DarkMode', type: 'checkbox',
checked: store.get('dark_mode') || DEFAULT_DARK_MODE,
click: (menuItem) => {
set_pdf_dark(pdf_win.webContents, menuItem.checked)
if (menuItem.checked) {
nativeTheme.themeSource = 'dark'
} else {
nativeTheme.themeSource = 'system'
}
store.set('dark_mode',menuItem.checked)
}
})
if (dark_menu.checked) {
nativeTheme.themeSource = 'dark'
} else {
nativeTheme.themeSource = 'system'
}
viewMenu.append(dark_menu);
mainMenu.append(new MenuItem({role: 'viewMenu', submenu: viewMenu}));
const winMenu = new Menu();
winMenu.append(new MenuItem({'role': 'minimize'}));
winMenu.append(new MenuItem({
label: 'Reload Sub-window', accelerator: 'Cmd+R', click: () => {
const win = BrowserWindow.getFocusedWindow();
if (win !== null) {
win.closeFilePreview();
open_student_file(win);
}
}
}));
winMenu.append(new MenuItem({type: "separator"}));
winMenu.append(new MenuItem({role: "front"}));
mainMenu.append(new MenuItem({role: "windowmenu", submenu: winMenu}));
const debugMenu = new Menu();
debugMenu.append(new MenuItem({role: 'toggleDevTools'}));
debugMenu.append(new MenuItem({
label: 'Dialog', click: () => {
dialog.showOpenDialog({defaultPath: "/"})
}
}));
mainMenu.append(new MenuItem({label: 'debug', submenu: debugMenu}));
Menu.setApplicationMenu(mainMenu);
ipcMain.on('req_row_data', (e, args) => {
const mfile = e.sender.manabaXSLX;
const next = mfile.set_row_data(args);
const next_data = mfile.get_row_data(next);
next_data['flag'] = true;
BrowserWindow.fromWebContents(e.sender).closeFilePreview();
e.sender.send('rep_row_data', next_data);
open_student_file(BrowserWindow.fromWebContents(e.sender));
})
main_win = createMainWindow(null);
}
function createMainWindow(filepath) {
const pos = store.get('main.window.pos') || [DEFAULT_POS.x, DEFAULT_POS.y];
const size = store.get('main.window.size') || [DEFAULT_SIZE.width, DEFAULT_SIZE.height];
const win = new BrowserWindow({
width: size[0],
height: size[1],
x: pos[0],
y: pos[1],
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.resolve(__dirname, 'preload4score.js'),
}
});
if (filepath == null) {
selectfile(win, {
'callback': () => {
win.close()
}
});
} else {
showScoreWindow(win, filepath)
}
search_main_setup(win.webContents)
function _local_save() {
store.set('main.window.pos', main_win.getPosition()) // ウィンドウの座標を記録
store.set('main.window.size', main_win.getSize()) // ウィンドウのサイズを記録
}
win.on('close', () => {
_local_save()
if (pdf_win !== undefined)
pdf_win.destroy()
})
win.on('resized', () => {
_local_save()
})
win.on('moved', () => {
_local_save()
})
return win
}
function selectfile(_win, options) {
dialog.showOpenDialog(_win, {
title: "Please select score file",
message: "Please select score file",
properties: ['openFile']
}).then((args) => {
if (!args['canceled']) {
if (showScoreWindow(_win, args['filePaths'][0]))
return;
}
if (options.callback !== undefined)
options['callback']();
})
}
// このメソッドは、Electron が初期化処理と
// browser window の作成準備が完了した時に呼び出されます。
// 一部のAPIはこのイベントが発生した後にのみ利用できます。
app.whenReady().then(app_main)
// 全てのウィンドウが閉じられた時に終了します。
// app.on('window-all-closed', () => {
// // macOSでは、ユーザが Cmd + Q で明示的に終了するまで、
// // アプリケーションとそのメニューバーは有効なままにするのが一般的です。
// if (process.platform !== 'darwin') {
// app.quit()
// }
// })
//
// app.on('activate', () => {
// // macOSでは、ユーザがドックアイコンをクリックしたとき、
// // そのアプリのウインドウが無かったら再作成するのが一般的です。
// if (BrowserWindow.getAllWindows().length === 0) {
// createMainWindow(null);
// }
// })
app.on('open-file', (e, filepath) => {
if (main_win !== undefined) {
main_win = createMainWindow(filepath);
} else {
showScoreWindow(main_win, filepath)
}
})
// このファイル内には、
// 残りのアプリ固有のメインプロセスコードを含めることができます。
// 別々のファイルに分割してここで require することもできます。
function search_main_setup(webcontents) {
let queue = {}
// contents への found_in_page の listener を設定する.
// このリスナーは renderer に通知する
webcontents.on('found-in-page', (event, results) => {
if (results.finalUpdate) {
queue[results.requestId].send('res_search',
{state: 'update', text: `${results.activeMatchOrdinal - 1}/${results.matches - 1}`})
delete queue[results.requestId]
}
})
// renderer からのイベントの listener を設定する.
// このリスナーは webcontents の fineinpage AP を操作する.
ipcMain.on('req_search', (e, args) => {
let id;
switch (args['state']) {
case 'start':
id = webcontents.findInPage(args['text'])
queue[id] = e.sender
break
case 'forward':
id = webcontents.findInPage(args['text'], {findNext: true})
queue[id] = e.sender
break
case 'backward':
id = webcontents.findInPage(args['text'], {forward: false, findNext: true})
queue[id] = e.sender
break
case 'close':
webcontents.stopFindInPage('clearSelection')
break
}
})
// renderer への通知
// 1) dialog の open
// 2) dialog への 設定(全体の数と,現在の位置)
// renderer からの通知
// 1) 最初の検索 (文字列)
// 2) 前方向への次の検索
// 3) 後方向への次の検索
// 4) dialog の close/検索の終了
// renderer 側でセットアップルーチンを作成する必要あり
// 1) document の中に dialog 要素をつくる.
// 2) dialog 内のボタン等からのイベントの listener を設定する.
// このリスナーは main に通知する.
// 開始,前,後,閉じる のボタン
// 3) main からのイベントの listener の設定をする.
// このリスナーは,dialog の設定をする.
// オープン,現在の位置等の設定
}
function search_main_openDialog(contents) {
// search を行う際に dialog をオープンさせるときにこれを呼び出す.
contents.send('res_search', {state: 'open', text: '0/0'})
}
function force_hide(_win) {
if ((_win !== undefined) && !(_win.isDestroyed())) _win.hide();
}
const manabaXSLX = require("./manabaXSLX");
function showScoreWindow(win, filepath) {
// open file dialog を開いて
const mfile = new manabaXSLX(filepath);
if (!mfile.isAvairable()) return false;
app.addRecentDocument(filepath);
const data = mfile.get_status();
win.webContents['manabaXSLX'] = mfile;
createPDFWindow();
ipcMain.once('req_init_data', (e) => {
//e.sender.send('rep_init_data', data)
let row_data;
for (let i = data['min']; i <= data['max']; i++) {
row_data = mfile.get_row_data(i);
row_data['flag'] = false;
e.sender.send('rep_row_data', row_data);
}
row_data = mfile.get_row_data(data['min']);
row_data['flag'] = true;
e.sender.send('rep_row_data', row_data);
e.sender.send('rep_init_data', data)
open_student_file(BrowserWindow.fromWebContents(e.sender));
});
win.loadFile("score.html");
return true;
}
function open_student_dir(win, filepath) {
dialog.showOpenDialog({
title: "Please select preview file",
message: "Please select preview file",
defaultPath: filepath,
properties: ['openFile']
}).then((args) => {
if (!args.canceled) open_student_one_file(win, args.filePaths[0])
})
}
function open_student_one_file(win, filepath) {
if (/^\.pdf$/i.test(path.extname(filepath))) {
pdf_win.loadFile(filepath);
pdf_win.showInactive();
} else {
pdf_win.hide();
win.closeFilePreview();// これだとうまくいかない。
win.previewFile(filepath);
}
}
function open_student_file(win) {
if ((win === undefined) || (win === null)) return;
const filepath = win.webContents.manabaXSLX.get_student_file_path();
if (filepath !== undefined) {
fs.stat(filepath, (error, stats) => {
if (error) {
pdf_win.hide();
} else if (stats.isDirectory()) {
const files = get_file_in_dir(filepath);
if (files.length === 1) {
open_student_one_file(win, path.join(filepath, files[0]))
} else {
open_student_dir(win, filepath)
}
} else if (stats.isFile()) {
open_student_one_file(win, filepath)
} else {
pdf_win.hide();
}
});
} else {
force_hide(pdf_win);
}
}
function get_file_in_dir(pathname) {
let array = [];
for (let dirent of fs.readdirSync(pathname, {withFileTypes: true}))
if (dirent.isFile())
array.push(dirent.name)
return array;
}