Skip to content

Commit

Permalink
Revert webview. Load ComfyUI in main window. (#159)
Browse files Browse the repository at this point in the history
* Revert webview. Load ComfyUI in main process.

* Prettier.
  • Loading branch information
robinjhuang authored Oct 31, 2024
1 parent be0cbf4 commit 6836432
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 144 deletions.
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export const IPC_CHANNELS = {
OPEN_DIALOG: 'open-dialog',
FIRST_TIME_SETUP_COMPLETE: 'first-time-setup-complete',
DEFAULT_INSTALL_LOCATION: 'default-install-location',
GET_COMFYUI_URL: 'get-comfyui-url',
COMFYUI_READY: 'comfyui-ready',
GET_PRELOAD_SCRIPT: 'get-preload-script',
OPEN_DEVTOOLS: 'open-devtools',
OPEN_LOGS_FOLDER: 'open-logs-folder',
Expand Down
61 changes: 4 additions & 57 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IPCChannel,
SENTRY_URL_ENDPOINT,
} from './constants';
import { app, BrowserWindow, dialog, screen, ipcMain, Menu, MenuItem, globalShortcut, shell } from 'electron';
import { app, BrowserWindow, dialog, screen, ipcMain, Menu, MenuItem, shell } from 'electron';
import log from 'electron-log/main';
import * as Sentry from '@sentry/electron/main';
import Store from 'electron-store';
Expand All @@ -20,7 +20,6 @@ import { graphics } from 'systeminformation';
import { createModelConfigFiles, readBasePathFromConfig } from './config/extra_model_config';
import { WebSocketServer } from 'ws';
import { StoreType } from './store';
import { createReadStream, watchFile } from 'node:fs';
import todesktop from '@todesktop/runtime';
import { PythonEnvironment } from './pythonEnvironment';
import { DownloadManager } from './models/DownloadManager';
Expand Down Expand Up @@ -69,9 +68,6 @@ app.on('before-quit', async () => {
log.error(error);
}

closeWebSocketServer();
globalShortcut.unregisterAll();

app.exit();
});

Expand Down Expand Up @@ -112,8 +108,6 @@ if (!gotTheLock) {

graphics()
.then((graphicsInfo) => {
log.info('GPU Info: ', graphicsInfo);

const gpuInfo = graphicsInfo.controllers.map((gpu, index) => ({
[`gpu_${index}`]: {
vendor: gpu.vendor,
Expand All @@ -125,7 +119,6 @@ if (!gotTheLock) {

// Combine all GPU info into a single object
const allGpuInfo = Object.assign({}, ...gpuInfo);
log.info('GPU Info: ', allGpuInfo);
// Set Sentry context with all GPU information
Sentry.setContext('gpus', allGpuInfo);
})
Expand Down Expand Up @@ -155,7 +148,7 @@ if (!gotTheLock) {
log.error('ERROR: Main window not found!');
return;
}
startWebSocketServer();

mainWindow.on('close', () => {
mainWindow = null;
app.quit();
Expand Down Expand Up @@ -228,10 +221,6 @@ if (!gotTheLock) {
}
}
);

ipcMain.handle(IPC_CHANNELS.GET_COMFYUI_URL, () => {
return `http://${host}:${port}`;
});
});
}

Expand All @@ -240,7 +229,7 @@ function loadComfyIntoMainWindow() {
log.error('Trying to load ComfyUI into main window but it is not ready yet.');
return;
}
mainWindow.webContents.send(IPC_CHANNELS.COMFYUI_READY, port);
mainWindow.loadURL(`http://${host}:${port}`);
}

async function loadRendererIntoMainWindow(): Promise<void> {
Expand All @@ -252,7 +241,7 @@ async function loadRendererIntoMainWindow(): Promise<void> {
log.info('Loading Vite Dev Server');
await mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
log.info('Opened Vite Dev Server');
//mainWindow.webContents.openDevTools();
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(path.join(__dirname, `../renderer/index.html`));
}
Expand Down Expand Up @@ -895,48 +884,6 @@ function getDefaultUserResourcesPath(): string {
return process.platform === 'win32' ? path.join(app.getPath('home'), 'comfyui-electron') : app.getPath('userData');
}

/**
* For log watching.
*/
function startWebSocketServer() {
wss = new WebSocketServer({ port: 7999 });

wss.on('connection', (ws) => {
const logPath = path.join(app.getPath('logs'), 'comfyui.log');

// Send the initial content
const initialStream = createReadStream(logPath, { encoding: 'utf-8' });
initialStream.on('data', (chunk) => {
ws.send(chunk);
});

let lastSize = 0;
const watcher = watchFile(logPath, { interval: 1000 }, (curr, prev) => {
if (curr.size > lastSize) {
const stream = createReadStream(logPath, {
start: lastSize,
encoding: 'utf-8',
});
stream.on('data', (chunk) => {
ws.send(chunk);
});
lastSize = curr.size;
}
});

ws.on('close', () => {
watcher.unref();
});
});
}

function closeWebSocketServer() {
if (wss) {
wss.close();
wss = null;
}
}

/**
* Rotate old log files by adding a timestamp to the end of the file.
* @param logDir The directory to rotate the logs in.
Expand Down
12 changes: 0 additions & 12 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ export interface ElectronAPI {
onLogMessage: (callback: (message: string) => void) => void;
onFirstTimeSetupComplete: (callback: () => void) => void;
onDefaultInstallLocation: (callback: (location: string) => void) => void;
onComfyUIReady: (callback: (port: number) => void) => void;
sendReady: () => void;
restartApp: (customMessage?: string, delay?: number) => void;
onOpenDevTools: (callback: () => void) => void;
isPackaged: () => Promise<boolean>;
openDialog: (options: Electron.OpenDialogOptions) => Promise<string[] | undefined>;
getComfyUIUrl: () => Promise<string>;
getPreloadScript: () => Promise<string>;
/**
* Open the logs folder in the system's default file explorer.
*/
Expand Down Expand Up @@ -63,9 +60,6 @@ const electronAPI: ElectronAPI = {
callback(value);
});
},
onComfyUIReady: (callback: (port: number) => void) => {
ipcRenderer.on(IPC_CHANNELS.COMFYUI_READY, (_event, port: number) => callback(port));
},
sendReady: () => {
console.log('Sending ready event to main process');
ipcRenderer.send(IPC_CHANNELS.RENDERER_READY);
Expand All @@ -86,12 +80,6 @@ const electronAPI: ElectronAPI = {
selectSetupDirectory: (directory: string) => {
ipcRenderer.send(IPC_CHANNELS.SELECTED_DIRECTORY, directory);
},
getComfyUIUrl: (): Promise<string> => {
return ipcRenderer.invoke(IPC_CHANNELS.GET_COMFYUI_URL);
},
getPreloadScript: (): Promise<string> => {
return ipcRenderer.invoke(IPC_CHANNELS.GET_PRELOAD_SCRIPT);
},
openDialog: (options: Electron.OpenDialogOptions) => {
return ipcRenderer.invoke(IPC_CHANNELS.OPEN_DIALOG, options);
},
Expand Down
1 change: 1 addition & 0 deletions src/pythonEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class PythonEnvironment {
}

async isInstalled(): Promise<boolean> {
log.info(`Checking if Python is installed at ${this.pythonInterpreterPath} and ${this.pythonRecordPath}`);
return (await pathAccessible(this.pythonInterpreterPath)) && (await pathAccessible(this.pythonRecordPath));
}

Expand Down
54 changes: 0 additions & 54 deletions src/renderer/components/ComfyUIContainer.tsx

This file was deleted.

20 changes: 1 addition & 19 deletions src/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import log from 'electron-log/renderer';
import FirstTimeSetup from './screens/FirstTimeSetup';
import { ElectronAPI } from 'src/preload';
import { ELECTRON_BRIDGE_API } from 'src/constants';
import ComfyUIContainer from './components/ComfyUIContainer';

export interface ProgressUpdate {
status: string;
Expand All @@ -31,9 +30,6 @@ const Home: React.FC = () => {
const [status, setStatus] = useState('Starting...');
const [logs, setLogs] = useState<string[]>([]);
const [defaultInstallLocation, setDefaultInstallLocation] = useState<string>('');
const [comfyReady, setComfyReady] = useState(false);
const [comfyPort, setComfyPort] = useState<number | null>(null);
const [preloadScript, setPreloadScript] = useState<string>('');

const updateProgress = useCallback(({ status: newStatus }: ProgressUpdate) => {
log.info(`Setting new status: ${newStatus}`);
Expand All @@ -60,16 +56,6 @@ const Home: React.FC = () => {
log.info('First time setup complete');
setShowSetup(false);
});

electronAPI.onComfyUIReady((port: number) => {
log.info('ComfyUI ready');
setComfyPort(port);
setComfyReady(true);
});

electronAPI.getPreloadScript().then((script) => {
setPreloadScript(script);
});
}, []);

useEffect(() => {
Expand All @@ -81,7 +67,7 @@ const Home: React.FC = () => {
log.info(`Received log message: ${message}`);
addLogMessage(message);
});
}, [updateProgress, addLogMessage, setComfyPort]);
}, [updateProgress, addLogMessage]);

useEffect(() => {
const electronAPI: ElectronAPI = (window as any)[ELECTRON_BRIDGE_API];
Expand All @@ -103,10 +89,6 @@ const Home: React.FC = () => {
);
}

if (comfyReady && comfyPort) {
return <ComfyUIContainer comfyPort={comfyPort} preloadScript={preloadScript} />;
}

return (
<div style={bodyStyle}>
<ProgressOverlay status={status} logs={logs} />
Expand Down

0 comments on commit 6836432

Please sign in to comment.