-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
239 lines (213 loc) · 7.78 KB
/
Copy pathmain.js
File metadata and controls
239 lines (213 loc) · 7.78 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Import necessary modules from Electron
const { app, BrowserWindow, Menu, shell, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs');
// Function to log messages to the renderer process console
function logToRenderer(win, message) {
win.webContents.executeJavaScript(`console.log(${JSON.stringify(message)});`);
}
// Function to create the main application window
function createWindow() {
// Create a new BrowserWindow instance
const win = new BrowserWindow({
width: 800, // Set the width of the window
height: 600, // Set the height of the window
webPreferences: {
nodeIntegration: true, // Enable Node.js integration in the renderer process
},
icon: __dirname + `/assets/icon.ico`
});
// Load the unified interface
win.loadFile('src/unified-interface.html');
// Log to the renderer process console
win.webContents.on('did-finish-load', () => {
logToRenderer(win, "Main process started");
});
}
// Function to open documentation files in external application
function openDocumentationFile(filename, title) {
const filePath = path.join(__dirname, filename);
// Check if file exists
if (!fs.existsSync(filePath)) {
console.error(`Documentation file not found: ${filename}`);
return;
}
// Open with default application (markdown viewer, text editor, etc.)
shell.openExternal(`file://${filePath}`).catch(error => {
console.error(`Could not open ${title}:`, error);
// Fallback: try opening with system default
shell.openPath(filePath).catch(fallbackError => {
console.error(`Fallback failed for ${title}:`, fallbackError);
});
});
}
// Create a custom menu template
const menuTemplate = [
{
label: 'File',
submenu: [
{
label: 'New Graph',
accelerator: 'F5',
role: 'reload'
},
{ type: 'separator' },
{
label: 'Restart Application',
accelerator: 'CmdOrCtrl+Shift+R',
click: () => {
console.log('Menu: Restart Application clicked');
app.relaunch();
app.quit();
}
},
{ label: 'Exit', role: 'quit' }
]
},
{
label: 'View',
submenu: [
{
label: 'Fit Graph to View',
accelerator: 'CmdOrCtrl+0',
click: () => {
const win = BrowserWindow.getFocusedWindow();
if (win) {
// Execute the fit operation directly in the renderer
win.webContents.executeJavaScript(`
if (window.graphApp && window.graphApp.networks && window.graphApp.networks.editor) {
window.graphApp.networks.editor.fit();
console.log('Graph fitted to view via menu');
} else {
console.log('Graph network not available for fitting');
}
`);
}
}
},
{ type: 'separator' },
{ label: 'Toggle Developer Tools', role: 'toggledevtools' }
]
},
{
label: 'Window',
submenu: [
{ label: 'Minimize', role: 'minimize' },
{ label: 'Close', role: 'close' }
]
},
{
label: 'Tools',
submenu: [
{
label: 'Open Plugins Folder',
click: () => {
const pluginsDir = global.pluginsDirectory || initializePluginDirectories();
shell.openPath(pluginsDir);
}
}
]
},
{
label: 'Help',
submenu: [
{
label: 'Help',
click: () => openDocumentationFile('HELP.md', 'Help')
},
{ type: 'separator' },
{
label: 'README',
click: () => openDocumentationFile('README.md', 'README')
},
{
label: 'Plugin Development Guide',
click: () => openDocumentationFile('PLUGIN.md', 'Plugin Development Guide')
},
{
label: 'Plugin Specification',
click: () => openDocumentationFile('PLUGIN_SPEC.md', 'Plugin Specification')
},
{ type: 'separator' },
{
label: 'Roadmap',
click: () => openDocumentationFile('ROADMAP.md', 'Roadmap')
},
{
label: 'Changelog',
click: () => openDocumentationFile('CHANGELOG.md', 'Changelog')
}
]
}
];
// Set the application menu
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
// Initialize plugin directories
function initializePluginDirectories() {
const userDataPath = app.getPath('userData');
const pluginsDir = path.join(userDataPath, 'plugins');
// Create plugins directory if it doesn't exist
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, { recursive: true });
console.log('Created user plugins directory:', pluginsDir);
}
// Copy built-in plugins to user directory (first time only)
const builtinPluginsDir = path.join(__dirname, 'py', 'plugins');
if (fs.existsSync(builtinPluginsDir)) {
const builtinPlugins = fs.readdirSync(builtinPluginsDir);
for (const pluginName of builtinPlugins) {
const srcDir = path.join(builtinPluginsDir, pluginName);
const destDir = path.join(pluginsDir, pluginName);
// Only copy if destination doesn't exist (don't overwrite user modifications)
if (!fs.existsSync(destDir) && fs.statSync(srcDir).isDirectory()) {
try {
copyDirectory(srcDir, destDir);
console.log(`Copied built-in plugin: ${pluginName}`);
} catch (error) {
console.error(`Failed to copy plugin ${pluginName}:`, error);
}
}
}
}
// Store plugin directory path for renderer process
global.pluginsDirectory = pluginsDir;
return pluginsDir;
}
// Helper function to copy directories recursively
function copyDirectory(src, dest) {
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirectory(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
// Add IPC handler for getting plugins directory
ipcMain.handle('get-plugins-directory', () => {
return global.pluginsDirectory || initializePluginDirectories();
});
// Event listener for when the app is ready to create the window
app.whenReady().then(() => {
initializePluginDirectories();
createWindow();
});
// Event listener for when all windows are closed
app.on('window-all-closed', () => {
// On non-macOS platforms, quit the app
if (process.platform !== 'darwin') {
app.quit(); // Quit the application
}
});
// Event listener for macOS to recreate a window when the app is activated
app.on('activate', () => {
// If there are no open windows, create a new one
if (BrowserWindow.getAllWindows().length === 0) {
createWindow(); // Call the function to create a new window
}
});