forked from bfondu/tulip-upgraded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
209 lines (181 loc) · 8.21 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
const fs = require('fs');
const { Menu, app, BrowserWindow, dialog, ipcMain } = require('electron');
const path = require('path');
require('@electron/remote/main').initialize();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
var printWindow = null;
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function () {
ipcMain.handle('print-pdf', (event, args) => {
printPdf(event, args);
});
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
function createWindow() {
const template = [
{
label: "Tulip",
submenu: [
{ label: "Quit", accelerator: "CmdOrCtrl+Q", click: function () { app.quit(); } },
{ label: "Save", accelerator: "CmdOrCtrl+S", click: function () { mainWindow.webContents.send('save-roadbook'); } },
{ label: "Save As", accelerator: "CmdOrCtrl+Shift+S", click: function () { mainWindow.webContents.send('save-roadbook-as'); } },
{ label: "Open", accelerator: "CmdOrCtrl+O", click: function () { mainWindow.webContents.send('open-roadbook'); } },
]
},
{
label: "Edit",
submenu: [
{ label: "Undo Text", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo Text", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" },
{ type: "separator" },
{
label: "Tracks",
submenu: [
{ label: "Add Track 360", accelerator: "CmdOrCtrl+1", click: function () { mainWindow.webContents.send('add-track-0'); } },
{ label: "Add Track 45", accelerator: "CmdOrCtrl+2", click: function () { mainWindow.webContents.send('add-track-45'); } },
{ label: "Add Track 90", accelerator: "CmdOrCtrl+3", click: function () { mainWindow.webContents.send('add-track-90'); } },
{ label: "Add Track 135", accelerator: "CmdOrCtrl+4", click: function () { mainWindow.webContents.send('add-track-135'); } },
{ label: "Add Track 180", accelerator: "CmdOrCtrl+5", click: function () { mainWindow.webContents.send('add-track-180'); } },
{ label: "Add Track 225", accelerator: "CmdOrCtrl+6", click: function () { mainWindow.webContents.send('add-track-225'); } },
{ label: "Add Track 270", accelerator: "CmdOrCtrl+7", click: function () { mainWindow.webContents.send('add-track-270'); } },
{ label: "Add Track 315", accelerator: "CmdOrCtrl+8", click: function () { mainWindow.webContents.send('add-track-315'); } },
{ type: "separator" },
{ label: "Set Track HP", accelerator: "CmdOrCtrl+Option+1", click: function () { mainWindow.webContents.send('set-track-hp'); } },
{ label: "Set Track P", accelerator: "CmdOrCtrl+Option+2", click: function () { mainWindow.webContents.send('set-track-p'); } },
{ label: "Set Track PP", accelerator: "CmdOrCtrl+Option+3", click: function () { mainWindow.webContents.send('set-track-pp'); } },
{ label: "Set Track RO", accelerator: "CmdOrCtrl+Option+4", click: function () { mainWindow.webContents.send('set-track-ro'); } },
{ label: "Set Track DCW", accelerator: "CmdOrCtrl+Option+5", click: function () { mainWindow.webContents.send('set-track-dcw'); } },
]
},
{ type: "separator" },
{ label: "Add Glyph", accelerator: "CmdOrCtrl+Option+G", click: function () { mainWindow.webContents.send('add-glyph'); } },
]
},
{
label: "Io",
submenu: [
{ label: "Import GPX", accelerator: "CmdOrCtrl+I", click: function () { mainWindow.webContents.send('import-gpx'); } },
{ label: "Export GPX", accelerator: "CmdOrCtrl+E", click: function () { mainWindow.webContents.send('export-gpx'); } },
{ label: "Export OpenRally GPX", click: function () { mainWindow.webContents.send('export-openrally-gpx'); } },
{ label: "Export PDF", accelerator: "CmdOrCtrl+P", click: function () { mainWindow.webContents.send('export-pdf'); } },
]
},
{
label: "View",
submenu: [
{ label: "Reload", accelerator: "CmdOrCtrl+R", click: function () { mainWindow.webContents.send('reload-roadbook'); } },
{ type: "separator" },
{ label: "Toggle Roadbook", accelerator: "CmdOrCtrl+B", click: function () { mainWindow.webContents.send('toggle-roadbook'); } },
{ type: "separator" },
{ label: "Zoom in", accelerator: "CmdOrCtrl+Plus", click: function () { mainWindow.webContents.send('zoom-in'); } },
{ label: "Zoom out", accelerator: "CmdOrCtrl+-", click: function () { mainWindow.webContents.send('zoom-out'); } },
{ type: "separator" },
{
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(focusedWindow) {
if (focusedWindow) focusedWindow.webContents.toggleDevTools()
}
},
]
}
];
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu);
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1500,
height: 1000,
'min-height': 700,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
sandbox: false
}
});
require('@electron/remote/main').enable(mainWindow.webContents);
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');
if (!app.isPackaged) {
mainWindow.webContents.openDevTools();
}
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
printWindow = null;
})
}
/*
IPC LISTENERS
TODO: the below should go in their own folders and be required
*/
var data;
ipcMain.on('ignite-print', (event, arg) => {
printWindow = new BrowserWindow({
width: 650,
height: 700,
'min-height': 700,
'resizable': true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
sandbox: false
}
});
require('@electron/remote/main').enable(printWindow.webContents);
printWindow.loadURL('file://' + __dirname + '/print.html');
data = arg;
printWindow.on('closed', () => {
printWindow = null
})
});
//listens for the browser window to say it's ready to print
ipcMain.on('print-launched', (event) => {
event.sender.send('print-data', data);
});
// NOTE this is about as robust as a wet paper bag and fails just as gracefully
function printPdf(event, arg) {
console.log("print-pdf", arg.opts);
var size = arg.opts.pageSize;
if (arg.opts.pageSize != 'Letter' && arg.opts.pageSize != 'A5') {
size = 'Roll'
}
var filename = arg.filepath.replace('.tlp', size + '.pdf')
printWindow.webContents.printToPDF(arg.opts)
.then((data) => {
fs.writeFile(filename, data, (error) => {
if (error)
throw error;
printWindow.close();
dialog.showMessageBox(mainWindow, { message: "Your PDF has been exported to the same directory you saved your roadbook. Gas a la burra!", buttons: ['ok'] })
});
});
};
//listens for the browser window to ask for the documents folder
ipcMain.on('get-documents-path', (event) => {
event.sender.send('documents-path', app.getPath('documents'));
});