-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectron.ts
103 lines (84 loc) · 2.67 KB
/
electron.ts
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
// eslint-disable-next-line import/no-extraneous-dependencies
import { app, BrowserWindow, ipcMain } from 'electron';
import * as isDev from 'electron-is-dev';
import * as path from 'path';
import * as fs from 'fs';
let mainWindow: BrowserWindow;
function createWindow() {
mainWindow = new BrowserWindow({
autoHideMenuBar: true,
darkTheme: true,
icon: path.join(__dirname, 'public/favicon.ico'),
title: 'Ma Gallery',
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
webSecurity: !isDev,
},
});
const url = isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`;
mainWindow.loadURL(url);
mainWindow.on('closed', () => { mainWindow = null; });
}
app.allowRendererProcessReuse = true;
app.on('ready', createWindow);
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } });
app.on('activate', () => { if (mainWindow === null) { createWindow(); } });
function isAllowedFileExtension(filename: string) {
const allowedExtensions = [
'.apng',
'.png',
'.bmp',
'.gif',
'.ico',
'.cur',
'.jpg',
'.jpeg',
'.jfif',
'.pjpeg',
'.pjp',
'.svg',
'.tif',
'.tiff',
'.webp',
];
return allowedExtensions.includes(path.extname(filename).toLowerCase());
}
function filterFileExtensions(filenames: string[]) {
return (filenames.filter((file) => isAllowedFileExtension(file)));
}
function listAvailableImages(directory: string) {
const filenames = fs.readdirSync(directory);
const filteredNames = filterFileExtensions(filenames);
return filteredNames;
}
ipcMain.on('opened-file-request', (event, userRequest: boolean, userFile: string) => {
let filename: string;
if (userRequest) {
filename = userFile;
} else if (isDev) {
filename = 'sample_images\\gif.gif';
} else if (process.argv[1]) {
[, filename] = process.argv;
} else {
const picturesPath = app.getPath('pictures');
const filenames = listAvailableImages(picturesPath);
const firstFile = path.join(picturesPath, filenames[0]);
filename = firstFile;
}
event.returnValue = filename;
});
ipcMain.on('opened-file-directory', (event, userRequest: boolean, filename: string) => {
let files: string[];
if (isDev && !userRequest) {
const directory = path.dirname(path.join(__dirname, 'public/sample_images/gif.gif'));
files = listAvailableImages(directory)
.map((file) => path.join('sample_images', file));
} else {
const directory = path.dirname(filename);
files = listAvailableImages(directory)
.map((file) => path.join(directory, file));
}
event.sender.send('opened-file-directory-reply', files);
});