Changing the ipcMain File in Electron

Viewed 157

Hello i'm new to Electron and i want to split my code into separate files and i really would like to move my ipcMain.on Fucntions to another file from main.ts . How can i do that because i don't know if i move it to another file, how should i call it? and is there any way to use mainWindow.webContents.send() inside another file?

main.ts

import { app, BrowserWindow, ipcMain } from 'electron';
import isDev from 'electron-is-dev';
import path from 'path';
import db from './database/database';

let mainWindow: BrowserWindow;

const createWindow = () => {
  mainWindow = new BrowserWindow({
    minWidth: 980,
    minHeight: 600,
    webPreferences: {
      nodeIntegration: true,
    },
    frame: false,
  });

  mainWindow.loadURL(
    isDev
      ? 'http://localhost:3000'
      : `file://${path.join(__dirname, "../build/index.html")}`
  );
  mainWindow.webContents.openDevTools();
};

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

ipcMain.on('mainWindow:close', () => {
  db.close();
  app.quit();
});

ipcMain.on('mainWindow:min', () => {
  mainWindow.minimize();
});

ipcMain.on('mainWindow:max', () => {
  if (mainWindow.isMaximized()) {
    mainWindow.unmaximize();
  }
  else {
    mainWindow.maximize();
  }
});
0 Answers
Related