forked from JD-P/minihf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
138 lines (115 loc) · 3.53 KB
/
Copy pathmain.js
File metadata and controls
138 lines (115 loc) · 3.53 KB
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
const { app, BrowserWindow, ipcMain, dialog, Menu, MenuItem } = require('electron');
const fs = require('fs');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
// Get the existing menu template
const existingMenuTemplate = Menu.getApplicationMenu().items.map(item => {
return {
label: item.label,
submenu: item.submenu.items.map(subItem => {
return {
label: subItem.label,
role: subItem.role, // preserve other roles
type: subItem.type, // preserve type (e.g., separator)
// ...other properties as needed
};
})
};
});
// Define new items for the File menu
const fileMenuItems = [
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
click() {
mainWindow.webContents.send('invoke-action', 'save-file');
}
},
{
label: 'Load',
accelerator: 'CmdOrCtrl+O',
click() {
mainWindow.webContents.send('invoke-action', 'load-file');
}
},
{ type: 'separator' }, // Separator
];
// Find the File menu in the existing template
const fileMenuIndex = existingMenuTemplate.findIndex(item => item.label === 'File');
if (fileMenuIndex >= 0) {
// If File menu exists, append new items to it
existingMenuTemplate[fileMenuIndex].submenu = fileMenuItems.concat(existingMenuTemplate[fileMenuIndex].submenu);
} else {
// If File menu doesn't exist, add it
existingMenuTemplate.unshift({
label: 'File',
submenu: fileMenuItems
});
}
// Build and set the new menu
const newMenu = Menu.buildFromTemplate(existingMenuTemplate);
Menu.setApplicationMenu(newMenu);
mainWindow.loadFile('index.html');
mainWindow.on('closed', function () {
mainWindow = null;
});
}
let autoSavePath = null;
ipcMain.handle('save-file', async (event, data) => {
let filePath;
if (autoSavePath) {
filePath = autoSavePath;
} else {
const { filePath: chosenPath } = await dialog.showSaveDialog(mainWindow, {
title: 'Save File',
filters: [{ name: 'JSON Files', extensions: ['json'] }],
});
filePath = chosenPath;
autoSavePath = chosenPath; // Update auto-save path
}
if (filePath) {
fs.writeFileSync(filePath, JSON.stringify(data));
}
});
ipcMain.handle('load-file', async (event) => {
const { filePaths } = await dialog.showOpenDialog(mainWindow, {
title: 'Load File',
filters: [{ name: 'JSON Files', extensions: ['json'] }],
properties: ['openFile'],
});
if (filePaths && filePaths.length > 0) {
const content = fs.readFileSync(filePaths[0], 'utf8');
autoSavePath = filePaths[0]; // Update auto-save path
return JSON.parse(content);
}
});
ipcMain.handle('auto-save', (event, data) => {
if (autoSavePath) {
fs.writeFileSync(autoSavePath, JSON.stringify(data));
}
});
app.whenReady().then(createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', function () {
if (mainWindow === null) createWindow();
});
ipcMain.on('show-context-menu', (event) => {
const contextMenu = Menu.buildFromTemplate([
{ label: 'Cut', role: 'cut' },
{ label: 'Copy', role: 'copy' },
{ label: 'Paste', role: 'paste' },
{ type: 'separator' },
{ label: 'Select All', role: 'selectAll' },
]);
contextMenu.popup(BrowserWindow.fromWebContents(event.sender));
});