Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: Add Check For Updates button #466

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const IPC_CHANNELS = {
RENDERER_READY: 'renderer-ready',
RESTART_APP: 'restart-app',
REINSTALL: 'reinstall',
CHECK_FOR_UPDATES: 'check-for-updates',
LOG_MESSAGE: 'log-message',
OPEN_DIALOG: 'open-dialog',
DOWNLOAD_PROGRESS: 'download-progress',
Expand Down Expand Up @@ -31,6 +32,7 @@ export const IPC_CHANNELS = {
SHOW_DIRECTORY_PICKER: 'show-directory-picker',
INSTALL_COMFYUI: 'install-comfyui',
SHOW_CONTEXT_MENU: 'show-context-menu',
GET_OS_PLATFORM: 'get-os-platform',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the OS_PLATFORM in electron API?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per specification the menu item should show only if the application is ran on Win32 platform so I need to way to determine that in frontend.

Copy link
Contributor

@webfiltered webfiltered Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a duplicate here - one sec.

Edit: See commit here - you can just call process.platform. No need for IPC, it just needs to be node.js not web app.

b11b185

@huchenlei My req. was hiding e.g. Apple MPS as an option on Windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have the getOS API added in a separate PR?

} as const;

export enum ProgressStatus {
Expand Down
29 changes: 27 additions & 2 deletions src/main-process/appWindow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { BrowserWindow, screen, app, shell, ipcMain, Tray, Menu, dialog, MenuItem, type Point } from 'electron';
import {
BrowserWindow,
screen,
app,
shell,
ipcMain,
Tray,
Menu,
dialog,
MenuItem,
type Point,
ipcRenderer,
} from 'electron';
import path from 'node:path';
import Store from 'electron-store';
import { AppWindowSettings } from '../store';
Expand All @@ -7,12 +19,13 @@ import { IPC_CHANNELS, ProgressStatus, ServerArgs } from '../constants';
import { getAppResourcesPath } from '../install/resourcePaths';
import { DesktopConfig } from '../store/desktopConfig';
import type { ElectronContextMenuOptions } from '../preload';
import { EventEmitter } from 'node:stream';

/**
* Creates a single application window that displays the renderer and encapsulates all the logic for sending messages to the renderer.
* Closes the application when the window is closed.
*/
export class AppWindow {
export class AppWindow extends EventEmitter {
private window: BrowserWindow;
/** Volatile store containing window config - saves window state between launches. */
private store: Store<AppWindowSettings>;
Expand All @@ -24,6 +37,7 @@ export class AppWindow {
private editMenu?: Menu;

public constructor() {
super();
const installed = DesktopConfig.store.get('installState') === 'installed';
const primaryDisplay = screen.getPrimaryDisplay();
const { width, height } = installed ? primaryDisplay.workAreaSize : { width: 1024, height: 768 };
Expand Down Expand Up @@ -258,6 +272,17 @@ export class AppWindow {
}

const contextMenu = Menu.buildFromTemplate([
...(process.platform === 'darwin'
? [
{
label: 'Check for Updates',
click: () => {
this.emit(IPC_CHANNELS.CHECK_FOR_UPDATES);
},
},
]
: []),

{
label: 'Show Comfy Window',
click: () => {
Expand Down
23 changes: 21 additions & 2 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { app, dialog, ipcMain, type Point } from 'electron';
import log from 'electron-log/main';
import * as Sentry from '@sentry/electron/main';
import { graphics } from 'systeminformation';
import { graphics, osInfo } from 'systeminformation';
import todesktop from '@todesktop/runtime';
import { IPC_CHANNELS, ProgressStatus, ServerArgs } from '../constants';
import { ComfySettings } from '../config/comfySettings';
import { AppWindow } from './appWindow';
import { ComfyServer } from './comfyServer';
import { ComfyServerConfig } from '../config/comfyServerConfig';
import fs from 'fs';
import os from 'os';
import { InstallOptions, type ElectronContextMenuOptions } from '../preload';
import path from 'path';
import { getModelsDirectory, validateHardware } from '../utils';
Expand All @@ -28,7 +29,9 @@ export class ComfyDesktopApp {
public basePath: string,
public comfySettings: ComfySettings,
public appWindow: AppWindow
) {}
) {
appWindow.addListener(IPC_CHANNELS.CHECK_FOR_UPDATES, this.checkForUpdates);
}

get pythonInstallPath() {
return app.isPackaged ? this.basePath : path.join(app.getAppPath(), 'assets');
Expand Down Expand Up @@ -91,6 +94,20 @@ export class ComfyDesktopApp {
}
}

async checkForUpdates(): Promise<void> {
log.info('Checking for updates ...');

try {
const result = await todesktop.autoUpdater?.checkForUpdates();
if (result?.updateInfo) {
log.info('Update found:', result.updateInfo.version);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split this function into following 2 functions:

  • checkForUpdates: Check for update and returns the update info
  • updateToLatest: Actually restart and do the update

todesktop.autoUpdater?.restartAndInstall();
}
} catch (e) {
log.error('Update check failed:', e);
}
}

registerIPCHandlers(): void {
ipcMain.on(IPC_CHANNELS.SHOW_CONTEXT_MENU, (_event, options?: ElectronContextMenuOptions) => {
this.appWindow.showSystemContextMenu(options);
Expand Down Expand Up @@ -120,6 +137,8 @@ export class ComfyDesktopApp {
log.info('Reinstalling...');
this.reinstall();
});
ipcMain.handle(IPC_CHANNELS.CHECK_FOR_UPDATES, () => this.checkForUpdates());
ipcMain.handle(IPC_CHANNELS.GET_OS_PLATFORM, () => Promise.resolve(os.platform()));
ipcMain.handle(IPC_CHANNELS.SEND_ERROR_TO_SENTRY, async (_event, { error, extras }): Promise<string | null> => {
try {
return Sentry.captureMessage(error, {
Expand Down
6 changes: 6 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ const electronAPI = {
reinstall: () => {
return ipcRenderer.invoke(IPC_CHANNELS.REINSTALL);
},
checkForUpdates: () => {
return ipcRenderer.invoke(IPC_CHANNELS.CHECK_FOR_UPDATES);
},
getOsPlatform: (): Promise<string> => {
return ipcRenderer.invoke(IPC_CHANNELS.GET_OS_PLATFORM);
},
openDialog: (options: Electron.OpenDialogOptions) => {
return ipcRenderer.invoke(IPC_CHANNELS.OPEN_DIALOG, options);
},
Expand Down
Loading