backsplash-app
Version:
An AI powered wallpaper app.
116 lines (93 loc) • 3.61 kB
text/typescript
import { app, BrowserWindow, ipcMain, Menu, shell } from "electron";
import { MenuChannels } from "@/ipc/channels/menuChannels";
// Track if handlers have been initialized to prevent duplicates
let menuHandlersInitialized = false;
/**
* Clean up existing menu IPC handlers
*/
export const cleanupMenuIpcHandlers = () => {
try {
ipcMain.removeHandler(MenuChannels.WINDOW_MINIMIZE);
ipcMain.removeHandler(MenuChannels.WINDOW_MAXIMIZE);
ipcMain.removeHandler(MenuChannels.WINDOW_TOGGLE_MAXIMIZE);
ipcMain.removeHandler(MenuChannels.WINDOW_CLOSE);
ipcMain.removeHandler(MenuChannels.WEB_TOGGLE_DEVTOOLS);
ipcMain.removeHandler(MenuChannels.WEB_ACTUAL_SIZE);
ipcMain.removeHandler(MenuChannels.WEB_ZOOM_IN);
ipcMain.removeHandler(MenuChannels.WEB_ZOOM_OUT);
ipcMain.removeHandler(MenuChannels.WEB_TOGGLE_FULLSCREEN);
ipcMain.removeHandler(MenuChannels.OPEN_GITHUB_PROFILE);
// Clean up event listeners (these use .on instead of .handle)
ipcMain.removeAllListeners(MenuChannels.EXECUTE_MENU_ITEM_BY_ID);
ipcMain.removeAllListeners(MenuChannels.SHOW_CONTEXT_MENU);
menuHandlersInitialized = false;
console.log("[MenuIPC] Cleaned up existing IPC handlers");
} catch (error) {
// Ignore errors if handlers don't exist
console.debug("[MenuIPC] No existing handlers to clean up");
}
};
export const registerMenuIpc = (mainWindow: BrowserWindow) => {
// Prevent duplicate handler registration
if (menuHandlersInitialized) {
console.warn("[MenuIPC] Handlers already initialized, skipping registration...");
return;
}
// Clean up any existing handlers first
cleanupMenuIpcHandlers();
ipcMain.on(MenuChannels.EXECUTE_MENU_ITEM_BY_ID, (event, id) => {
const currentMenu = Menu.getApplicationMenu();
if (currentMenu === null) {
return;
}
const menuItem = currentMenu.getMenuItemById(id);
if (menuItem) {
const window = BrowserWindow.fromWebContents(event.sender) || undefined;
menuItem.click(null, window, event.sender);
}
});
ipcMain.on(MenuChannels.SHOW_CONTEXT_MENU, (event, template) => {
const menu = Menu.buildFromTemplate(template);
const window = BrowserWindow.fromWebContents(event.sender);
if (window) {
menu.popup({ window });
}
});
ipcMain.handle(MenuChannels.WINDOW_MINIMIZE, () => {
mainWindow.minimize();
});
ipcMain.handle(MenuChannels.WINDOW_MAXIMIZE, () => {
mainWindow.maximize();
});
ipcMain.handle(MenuChannels.WINDOW_TOGGLE_MAXIMIZE, () => {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
});
ipcMain.handle(MenuChannels.WINDOW_CLOSE, () => {
mainWindow.close();
app.quit();
});
ipcMain.handle(MenuChannels.WEB_TOGGLE_DEVTOOLS, () => {
mainWindow.webContents.toggleDevTools();
});
ipcMain.handle(MenuChannels.WEB_ACTUAL_SIZE, () => {
mainWindow.webContents.setZoomLevel(0);
});
ipcMain.handle(MenuChannels.WEB_ZOOM_IN, () => {
mainWindow.webContents.setZoomLevel(mainWindow.webContents.zoomLevel + 0.5);
});
ipcMain.handle(MenuChannels.WEB_ZOOM_OUT, () => {
mainWindow.webContents.setZoomLevel(mainWindow.webContents.zoomLevel - 0.5);
});
ipcMain.handle(MenuChannels.WEB_TOGGLE_FULLSCREEN, () => {
mainWindow.setFullScreen(!mainWindow.fullScreen);
});
ipcMain.handle(MenuChannels.OPEN_GITHUB_PROFILE, (_event, id) => {
shell.openExternal(`https://github.com/${id}`);
});
menuHandlersInitialized = true;
console.log("[MenuIPC] All handlers registered successfully");
};