-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
123 lines (101 loc) · 3.06 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
const { app, BrowserWindow, ipcMain } = require('electron');
const fs = require('fs');
const path = require('path');
const Store = require('./store');
const basePath = app.getPath('userData');
const store = new Store(basePath)
var mainWindow;
var imageWindows = [];
function createWindow () {
mainWindow = new BrowserWindow({
titleBarStyle: 'hidden',
trafficLightPosition: {x: 20, y: 20},
width: 1200,
height: 1000,
webPreferences: {
sandbox: true,
scrollBounce: false,
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(__dirname, 'board-preload.js')
}
})
mainWindow.loadFile(path.join(__dirname, "front/index.html"))
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// Board
ipcMain.on('base-path', (event, arg) => {
event.returnValue = basePath;
});
ipcMain.on('fetch-images', (event, options) => {
event.returnValue = store.fetchImages(options);
})
ipcMain.handle('create-image', async (event, filePath) => {
return await store.createImage(filePath);
})
ipcMain.on('get-image', (event, id) => {
event.returnValue = store.getImage(id);
})
ipcMain.on('update-image', (event, image) => {
store.updateImage(image);
event.returnValue = true;
})
// ImageView
ipcMain.on('open-image', (event, image) => {
if (imageWindows[image.id]) {
imageWindows[image.id].focus()
event.returnValue = true
return;
}
const mainBounds = mainWindow.getNormalBounds();
const imageWidth = Math.min(Math.floor(image.width / 2), 1600);
const imageHeight = Math.floor(image.height * (imageWidth / image.width));
const windowHeight = Math.min(imageHeight, 1200);
const imageWindow = new BrowserWindow({
titleBarStyle: 'hidden',
x: mainBounds.x + 100,
y: mainBounds.y + 50,
useContentSize: true, // actual content size without window frame
width: imageWidth,
height: windowHeight,
fullscreenable: false,
webPreferences: {
sandbox: true,
scrollBounce: false,
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(__dirname, 'image-view-preload.js')
}
})
if (imageHeight <= 1200) {
imageWindow.setAspectRatio(image.width/image.height);
}
imageWindow.loadFile(path.join(__dirname, "front/image-view.html"))
imageWindow.once('ready-to-show', () => {
imageWindow.webContents.executeJavaScript(`load(${image.id},'${basePath + "/" + image.originalPath + "/" + image.fileName}')`)
imageWindow.show();
imageWindow.focus();
})
imageWindow.on('close', (event) => {
imageWindows[image.id] = null;
event.returnValue = false;
})
imageWindows[image.id] = imageWindow;
event.returnValue = true;
})
ipcMain.on('close-image', (event, imageId) => {
if (imageWindows[imageId]) {
imageWindows[imageId].close();
}
event.returnValue = true;
});