Add loading page when starting the app + fix electron refresh error
This commit is contained in:
1 parent
0b1a1d989f
commit
31d38d8da9
13 files changed
+397
-50
No files matched your search
@@ -3,13 +3,13 @@ const { app, BrowserWindow } = require('electron');
|
||||
const path = require('path');
|
||||
const url = require('url');
|
||||
|
||||
let expressProcess;
|
||||
let win;
|
||||
let backendExpressProcess;
|
||||
let mainWindow;
|
||||
|
||||
|
||||
const { createMediaFolders } = require('./backend/services/mediaService');
|
||||
|
||||
|
||||
// app.on('ready', createWindow);
|
||||
app.on('ready', () => {
|
||||
// ! Start the Express server loop
|
||||
startExpressServer();
|
||||
@@ -19,6 +19,13 @@ app.on('ready', () => {
|
||||
createWindow();
|
||||
});
|
||||
|
||||
app.on('before-quit', () => {
|
||||
if (backendExpressProcess) {
|
||||
backendExpressProcess.kill();
|
||||
console.log('Killing backend process.');
|
||||
}
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
@@ -26,43 +33,47 @@ app.on('window-all-closed', () => {
|
||||
});
|
||||
|
||||
app.on('activate', () => {
|
||||
if (win === null) {
|
||||
if (mainWindow === null) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
|
||||
const { dialog, ipcMain } = require('electron');
|
||||
const { createMediaFolders } = require('./backend/services/mediaService');
|
||||
// const { dialog, ipcMain } = require('electron');
|
||||
|
||||
ipcMain.on('select-dirs', async (event, arg) => {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ['openDirectory']
|
||||
})
|
||||
console.log('directories selected', result.filePaths)
|
||||
})
|
||||
// ipcMain.on('select-dirs', async (event, arg) => {
|
||||
// const result = await dialog.showOpenDialog(mainWindow, {
|
||||
// properties: ['openDirectory']
|
||||
// })
|
||||
// console.log('directories selected', result.filePaths)
|
||||
// })
|
||||
|
||||
function startExpressServer() {
|
||||
let parrentProcess = {
|
||||
"electron_path": app.getPath('userData')
|
||||
}
|
||||
// Start the Express server
|
||||
expressProcess = spawn('node', ['backend/server.js'], { env: { PARENT_PROCESS: JSON.stringify(parrentProcess) } });
|
||||
expressProcess.stdout.on('data', (data) => {
|
||||
backendExpressProcess = spawn('node', ['backend/server.js'], { env: { PARENT_PROCESS: JSON.stringify(parrentProcess) } });
|
||||
|
||||
backendExpressProcess.stdout.on('data', (data) => {
|
||||
console.log(`Express stdout: ${data}`);
|
||||
if (data.toString().includes('Server started')) { // Adjust to match backend's startup message
|
||||
mainWindow.webContents.send('backend-ready'); // Send event to frontend
|
||||
}
|
||||
});
|
||||
expressProcess.stderr.on('data', (data) => {
|
||||
|
||||
backendExpressProcess.stderr.on('data', (data) => {
|
||||
console.error(`Express stderr: ${data}`);
|
||||
});
|
||||
|
||||
// Restart the server if it crashes
|
||||
expressProcess.on('exit', (code, signal) => {
|
||||
backendExpressProcess.on('exit', (code, signal) => {
|
||||
console.log(`Express process exited with code ${code} and signal ${signal}. Restarting...`);
|
||||
startExpressServer();
|
||||
});
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
win = new BrowserWindow({
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 720,
|
||||
webPreferences: {
|
||||
@@ -75,7 +86,7 @@ function createWindow() {
|
||||
});
|
||||
|
||||
|
||||
win.loadURL(
|
||||
mainWindow.loadURL(
|
||||
url.format({
|
||||
pathname: path.join(__dirname, 'dist', 'showy', 'browser', 'index.html'),
|
||||
protocol: 'file:',
|
||||
@@ -83,7 +94,19 @@ function createWindow() {
|
||||
})
|
||||
);
|
||||
|
||||
win.on('closed', () => {
|
||||
win = null;
|
||||
mainWindow.on('closed', () => {
|
||||
mainWindow = null;
|
||||
});
|
||||
|
||||
mainWindow.webContents.on('will-navigate', (event, url) => {
|
||||
event.preventDefault();
|
||||
console.log('hiiiiiiiii');
|
||||
mainWindow.loadURL(`file://${path.join(__dirname, 'dist/showy/index.html')}`);
|
||||
});
|
||||
|
||||
mainWindow.webContents.on('did-fail-load', (event, url) => {
|
||||
// event.preventDefault();
|
||||
console.log('yoooo');
|
||||
mainWindow.loadURL(`file://${path.join(__dirname, 'dist/showy/browser/index.html')}`);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user