UNPKG

backsplash-app

Version:
160 lines (137 loc) 5.48 kB
import { ipcMain, BrowserWindow } from "electron"; import { AVAILABLE_MODES_DATA } from "@/types/wallpaperModeTypes"; import { WallpaperService } from "./services/wallpaperService"; import { WallpaperChannels } from "./channels/wallpaperChannels"; import { ServerChannels } from "@/ipc/channels/serverChannels"; // Track if handlers have been initialized to prevent duplicates let wallpaperHandlersInitialized = false; /** * Clean up existing wallpaper IPC handlers */ export const cleanupWallpaperIpcHandlers = () => { try { // Remove all wallpaper-related handlers ipcMain.removeHandler(WallpaperChannels.SET_WALLPAPER); ipcMain.removeHandler(WallpaperChannels.GET_MAC_SCREENS); ipcMain.removeHandler(ServerChannels.GENERATE_WALLPAPER); ipcMain.removeHandler(WallpaperChannels.UPSCALE_WALLPAPER); ipcMain.removeHandler("get-wallpaper-modes"); wallpaperHandlersInitialized = false; console.log("[Wallpaper IPC] Cleaned up existing IPC handlers"); } catch (error) { // Ignore errors if handlers don't exist console.debug("[Wallpaper IPC] No existing handlers to clean up"); } }; // Initialize the filesystemStorageService // Helper function to send status updates to all renderer processes export const sendStatusUpdate = (status: { type: string; message: string; data?: any }) => { const windows = BrowserWindow.getAllWindows(); windows.forEach((window) => { if (!window.isDestroyed()) { window.webContents.send(WallpaperChannels.WALLPAPER_STATUS_UPDATE, status); } }); }; /** * Registers IPC handlers related to wallpaper modes and generation. */ export const registerWallpaperIpc = (wallpaperService: WallpaperService) => { // Prevent duplicate handler registration if (wallpaperHandlersInitialized) { console.warn("[Wallpaper IPC] Handlers already initialized, skipping registration..."); return; } // Clean up any existing handlers first cleanupWallpaperIpcHandlers(); /** * Sets a wallpaper from a remote URL * Downloads it first to the local filesystem, then sets it as the wallpaper */ ipcMain.handle(WallpaperChannels.SET_WALLPAPER, async (event, objectKey) => { // Send status update: Starting upscale process sendStatusUpdate({ type: "upscale-start", message: `Starting wallpaper upscale for ${objectKey}`, }); // Process the wallpaper const result = await wallpaperService.setWallpaper(objectKey); // Send status update: Completed sendStatusUpdate({ type: "upscale-complete", message: `Completed wallpaper upscale for ${objectKey}`, data: { isUpscaled: result.isUpscaled, success: result.success }, }); return result; }); /** * Gets available screens for wallpaper setting (macOS) */ ipcMain.handle(WallpaperChannels.GET_MAC_SCREENS, async () => { try { const wallpaperModule = await import("wallpaper"); const screens = await wallpaperModule.screens(); return screens; } catch (error) { console.error("Error getting screens:", error); throw error; } }); /** * Generates a new wallpaper. Mode and parameters are fetched from StoreService by GenerationService. * Automatically sets it as the current wallpaper if generation is successful (handled by services). */ ipcMain.handle(ServerChannels.GENERATE_WALLPAPER, async () => { // Send status update: Starting generation sendStatusUpdate({ type: "generation-start", message: `Starting wallpaper generation...`, }); // Generate the wallpaper. wallpaperService.generateWallpaper now pulls params from the store. const result = await wallpaperService.generateWallpaper(); // Send status update: Completed sendStatusUpdate({ type: "generation-complete", message: `Completed wallpaper generation`, data: { success: result.success, objectKey: result.success ? (result as any).objectKey : null }, }); return result; }); /** * Upscales an existing wallpaper. * Channel: WallpaperChannels.UPSCALE_WALLPAPER */ ipcMain.handle(WallpaperChannels.UPSCALE_WALLPAPER, async (_event, objectKey: string) => { if (!objectKey || typeof objectKey !== "string") { const errorMsg = "Invalid objectKey provided for upscaling."; // Send status update immediately for client-side error feedback sendStatusUpdate({ type: "upscale-error", // Consider adding 'upscale-error' to WallpaperStatusType if not there message: errorMsg, data: { error: errorMsg, objectKey }, }); return { success: false, error: errorMsg }; } sendStatusUpdate({ type: "upscale-start", message: `Starting wallpaper upscale for ${objectKey}`, data: { objectKey }, }); const result = await wallpaperService.upscaleWallpaper(objectKey); // Send completion/error status update sendStatusUpdate({ type: result.success ? "upscale-complete" : "upscale-error", message: result.success ? `Completed wallpaper upscale for ${objectKey}` : result.error || `Upscale failed for ${objectKey}`, data: { ...result, objectKey }, }); return result; }); // Provide access to wallpaper modes constants ipcMain.handle("get-wallpaper-modes", async () => { console.log("[Wallpaper IPC] Providing wallpaper modes list."); return AVAILABLE_MODES_DATA; }); wallpaperHandlersInitialized = true; };