-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
173 lines (145 loc) · 4.67 KB
/
main.js
File metadata and controls
173 lines (145 loc) · 4.67 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
// Add command line arguments for development BEFORE requiring electron
const isDev = process.env.NODE_ENV === 'development';
// Import electron
const { app, BrowserWindow } = require('electron');
const fs = require('fs');
const path = require('path');
// Set command line switches before app is ready
if (isDev) {
app.commandLine.appendSwitch('--no-sandbox');
app.commandLine.appendSwitch('--disable-web-security');
app.commandLine.appendSwitch('--disable-gpu-sandbox');
app.commandLine.appendSwitch('--disable-software-rasterizer');
}
const next = require('next');
const getPort = require('get-port').default;
// Set persistent user data directory
const userDataPath = isDev
? path.join(__dirname, 'electron-dev-data')
: app.getPath('userData');
if (isDev) {
app.setPath('userData', userDataPath);
console.log('📁 Setting dev userData path:', userDataPath);
} else {
console.log('📁 Using production userData path:', userDataPath);
}
// Ensure the directory exists (only in development mode)
if (isDev) {
if (!fs.existsSync(userDataPath)) {
console.log('📂 Creating dev userData directory:', userDataPath);
fs.mkdirSync(userDataPath, { recursive: true });
console.log('✅ Dev userData directory created');
} else {
console.log('📂 Dev userData directory already exists');
}
} else {
console.log('📂 Using production userData path (Electron will handle creation):', userDataPath);
}
// List what's in the directory (development only)
if (isDev) {
try {
const files = fs.readdirSync(userDataPath);
console.log('📁 Contents of dev userData directory:', files);
} catch (e) {
console.log('📁 Could not read dev userData directory contents:', e.message);
}
}
// Initialize Next.js
const nextApp = next({
dev: isDev,
dir: __dirname
});
let mainWindow;
let server;
let port;
async function createWindow() {
// Get an available port starting from 3000
port = await getPort({ port: 3000 });
// Prepare Next.js
await nextApp.prepare();
// Start the Next.js server
server = require('http').createServer(nextApp.getRequestHandler());
await new Promise((resolve) => {
server.listen(port, (err) => {
if (err) throw err;
console.log(`Study Buddy server ready on http://localhost:${port}`);
resolve();
});
});
// Create the browser window
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webSecurity: false, // Disable for development
partition: 'persist:study-buddy', // Use persistent session
sandbox: false // Disable sandbox for Linux compatibility
},
titleBarStyle: 'default',
show: true // Show immediately for debugging
});
console.log('Created BrowserWindow, loading URL...');
// Load the Next.js app
try {
console.log(`Loading URL: http://localhost:${port}`);
await mainWindow.loadURL(`http://localhost:${port}`);
console.log('URL loaded successfully');
} catch (error) {
console.error('Failed to load URL:', error);
}
// Show window when ready to prevent visual flash
mainWindow.once('ready-to-show', () => {
console.log('Window ready-to-show event fired');
mainWindow.show();
console.log('Window should now be visible');
// Open DevTools in development
if (isDev) {
mainWindow.webContents.openDevTools();
}
});
// Additional debugging
mainWindow.once('show', () => {
console.log('Window show event fired');
});
mainWindow.webContents.once('did-finish-load', () => {
console.log('WebContents did-finish-load event fired');
});
// Handle window closed
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// App event handlers
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
// Close the server when all windows are closed
if (server) {
server.close();
}
// On macOS, keep app running even when all windows are closed
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS, re-create window when dock icon is clicked
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// Security: Prevent new window creation
app.on('web-contents-created', (event, contents) => {
contents.on('new-window', (event, navigationUrl) => {
event.preventDefault();
// Could open in external browser if needed
// require('electron').shell.openExternal(navigationUrl);
});
});
// Handle app protocol for better security
app.setAsDefaultProtocolClient('studybuddy');
// Export for potential IPC communication later
module.exports = { app, mainWindow };