-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.js
178 lines (145 loc) · 4.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
const electron = require('electron')
const { app, BrowserWindow, ipcMain: ipc, dialog, Menu } = electron
const contextMenu = require('electron-context-menu')
const { download } = require('electron-dl')
const windowStateKeeper = require('electron-window-state')
const path = require('path')
const fs = require('fs-extra')
let win
let menuTemplate
/**
* IPC Handlers
*/
// handle app exit
ipc.handle('exit', () => {
app.exit()
})
// handle close confirmation dialog
ipc.handle('confirmClose', async (event, fileName) => {
let name = 'your project'
if (fileName) {
name = path.basename(fileName)
}
const result = await dialog.showMessageBox({
type: 'question',
title: 'Confirm',
buttons: ['Save', 'Cancel', 'Don\'t Save'],
message: `Would you like to save the changes you made to ${name} before closing?`
})
return result
})
// handle saving canvas as image
ipc.handle('saveCanvasImage', async (event, data) => {
await download(win, data, { saveAs: true })
})
// handle saving new project with confirmation dialog
ipc.handle('saveProject', async (event, fname, buffer) => {
const { filePath } = await dialog.showSaveDialog({
properties: ['openFile'],
title: 'Choose where to save project...',
defaultPath: process.env.HOME + '/' + fname
})
if (filePath) {
await fs.outputFile(filePath, buffer)
}
return filePath
})
// handle quick overwrite save of project in progress
ipc.handle('saveProjectOverwrite', async (event, file, buffer) => {
await fs.outputFile(file, buffer)
})
// handle enabling of save menu
ipc.handle('enableSaveMenu', () => {
// overrride some renderer keyboard shortcuts in favor of electron accelerators
win.webContents.on('before-input-event', (event, input) => {
// save
if ((input.control || input.meta) && input.key.toLowerCase() === 's') {
event.preventDefault()
win.webContents.send('ping', 'save')
}
// save as
if ((input.control || input.meta) && input.shift && input.key.toLowerCase() === 's') {
event.preventDefault()
win.webContents.send('ping', 'saveas')
}
})
// menu positioning differs depending on the OS
let i = 0
if (process.platform === 'darwin') {
i = 1
}
// enable save buttons in menu template
menuTemplate[i].submenu[0].enabled = true
menuTemplate[i].submenu[1].enabled = true
// rebuild updated menu
const menu = Menu.buildFromTemplate(menuTemplate)
Menu.setApplicationMenu(menu)
})
function createWindow () {
const mainWindowState = windowStateKeeper({
defaultWidth: 1300,
defaultHeight: 900
})
win = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: 800,
minHeight: 640,
icon: process.platform === 'linux' && path.join(__dirname, '/build/icon.png'),
webPreferences: {
preload: path.resolve(app.getAppPath(), 'preload.js'),
contextIsolation: true,
sandbox: true
}
})
win.loadFile('node_modules/Glyphr-Studio/dev/Glyphr_Studio_Electron.html')
// save window position/size across loads
mainWindowState.manage(win)
// create the menubar and get a copy of the template
menuTemplate = require('./menu')(win)
// set up app context menu
contextMenu({
prepend: (defaultActions, parameters, browserWindow) => [
{
label: 'Save Image As...',
visiable: parameters.mediaType === 'canvas',
click: () => {
win.webContents.send('ping', 'saveImage')
}
}
]
})
// configure about app panel
app.setAboutPanelOptions({
applicationName: app.getName(),
applicationVersion: app.getVersion(),
website: 'http://glyphrstudio.com/v1'
})
const webContents = win.webContents
// enable for debugging
// win.webContents.openDevTools()
webContents.on('new-window', async (event, url) => {
const open = await import('open')
event.preventDefault()
open(url)
})
// inform renderer of close event
win.on('close', event => {
event.preventDefault()
win.webContents.send('ping', 'confirmClose')
})
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
app.quit()
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})