diff --git a/src/app/App.tsx b/src/app/App.tsx index ac3dadd4..d1453b1b 100644 --- a/src/app/App.tsx +++ b/src/app/App.tsx @@ -33,6 +33,7 @@ import CitationModal from './CitationModal'; import ErrorBoundary from './components/ErrorBoundary'; import ErrorDialog from './components/ErrorDialog'; import ModifiedDialog from './components/ModifiedDialog'; +import UpdateDownloadDialog from './components/UpdateDownloadDialog'; const VerticalHeading = styled( Typography )( () => ( { writingMode: 'vertical-rl', @@ -522,6 +523,7 @@ const App = (): React.JSX.Element => { ) : null} + diff --git a/src/app/components/UpdateDownloadDialog.tsx b/src/app/components/UpdateDownloadDialog.tsx new file mode 100644 index 00000000..0618cc95 --- /dev/null +++ b/src/app/components/UpdateDownloadDialog.tsx @@ -0,0 +1,54 @@ +import React from 'react'; +import Dialog from '@mui/material/Dialog'; +import DialogTitle from '@mui/material/DialogTitle'; +import DialogContent from '@mui/material/DialogContent'; +import LinearProgress from '@mui/material/LinearProgress'; +import Typography from '@mui/material/Typography'; +import type { UpdateDownload } from '../../common/rendererBridge'; + +export interface UpdateDownloadDialogProps { + download: UpdateDownload; +} + +function dialogCopy( download: UpdateDownload ): { percent?: number; text: string; detail: string } { + switch ( download.status ) { + case 'connecting': + return { text: 'Starting…', detail: 'Connecting to the update server…' }; + case 'downloading': + case 'restarted': + if ( download.percent < 100 ) { + return { + percent: download.percent, + text: `${Math.round( download.percent )}%`, + detail: download.status === 'restarted' + ? 'Retrying with the full download…' + : 'RAxML-GUI will quit and install when the download finishes.', + }; + } + break; + default: + } + return { percent: 100, text: '100%', detail: 'Download complete. Preparing to install…' }; +} + +export default function UpdateDownloadDialog( { download }: UpdateDownloadDialogProps ): React.JSX.Element | null { + if ( download.status === 'idle' ) { + return null; + } + const { percent, text, detail } = dialogCopy( download ); + return ( + + Downloading update + + + {text} + {detail} + + + ); +} diff --git a/src/app/store/RunList.ts b/src/app/store/RunList.ts index f4878333..2b5998d7 100644 --- a/src/app/store/RunList.ts +++ b/src/app/store/RunList.ts @@ -1,5 +1,6 @@ import { observable, computed, action, makeObservable } from 'mobx'; import type { StateReport } from '../../main/getActiveState'; +import type { UpdateDownload } from '../../common/rendererBridge'; import Run from './Run'; import AppStore from './AppStore'; import { runEvents } from './StoreBase'; @@ -23,6 +24,8 @@ class RunList extends AppStore { deleteActive: action, onError: action, onUnhandledError: action, + updateDownload: observable, + onUpdateDownload: action, onBackboneConstraint: action, onMultifurcatingConstraint: action, } ); @@ -77,6 +80,12 @@ class RunList extends AppStore { this.error = error; }; + updateDownload: UpdateDownload = { status: 'idle' }; + + onUpdateDownload = ( download: UpdateDownload ): void => { + this.updateDownload = download; + }; + onBackboneConstraint = (): void => { this.activeRun.useBackboneConstraint = !this.activeRun.useBackboneConstraint; @@ -89,6 +98,7 @@ class RunList extends AppStore { listen = (): void => { this.subscribe( runEvents.onUnhandledError, this.onUnhandledError ); + this.subscribe( runEvents.onUpdateDownload, this.onUpdateDownload ); this.subscribe( runEvents.onAddRun, this.addRun ); this.subscribe( runEvents.onRemoveRun, this.deleteActive ); this.subscribe( runEvents.onToggleBackboneConstraint, this.onBackboneConstraint ); diff --git a/src/app/store/StoreBase.ts b/src/app/store/StoreBase.ts index f3c9496f..b0689e00 100644 --- a/src/app/store/StoreBase.ts +++ b/src/app/store/StoreBase.ts @@ -17,6 +17,7 @@ export const runEvents = { onToggleBackboneConstraint: 'onToggleBackboneConstraint', onToggleMultifurcatingConstraint: 'onToggleMultifurcatingConstraint', onUnhandledError: 'onUnhandledError', + onUpdateDownload: 'onUpdateDownload', } as const; export default class StoreBase { diff --git a/src/app/updateDownload.test.tsx b/src/app/updateDownload.test.tsx new file mode 100644 index 00000000..2a294af8 --- /dev/null +++ b/src/app/updateDownload.test.tsx @@ -0,0 +1,67 @@ +import { expect, it } from 'vitest'; +import { act } from 'react'; +import { createRoot } from 'react-dom/client'; +import Page from './index'; +import store from './store'; +import { installBridge } from '../test/setup'; +import type { UpdateDownloadPayload } from '../common/rendererBridge'; + +function title() { + return document.getElementById( 'update-download-title' )?.textContent; +} + +it( 'the page follows the update download in a dialog that cannot be dismissed', () => { + let hear: ( payload: UpdateDownloadPayload ) => void; + installBridge( { + onUpdateDownload( listener ) { + hear = listener; + return () => {}; + }, + } ); + store.listen(); + + const div = document.createElement( 'div' ); + document.body.appendChild( div ); + const root = createRoot( div ); + act( () => { + root.render( ); + } ); + expect( title() ).toBeUndefined(); + + const rows: [UpdateDownloadPayload, string, string][] = [ + [{ id: 'update', status: 'connecting' }, 'Starting…', 'Connecting to the update server…'], + [ + { id: 'update', status: 'downloading', percent: 42 }, + '42%', + 'RAxML-GUI will quit and install when the download finishes.', + ], + [{ id: 'update', status: 'restarted', percent: 10 }, '10%', 'Retrying with the full download…'], + [{ id: 'update', status: 'done' }, '100%', 'Download complete. Preparing to install…'], + ]; + for ( const [payload, percent, status] of rows ) { + act( () => { + hear( payload ); + } ); + expect( title() ).toBe( 'Downloading update' ); + const dialog = document.querySelector( '[role="dialog"]' ); + expect( dialog.textContent ).toContain( percent ); + expect( dialog.textContent ).toContain( status ); + expect( dialog.querySelector( 'button' ) ).toBeNull(); + } + + act( () => { + document.querySelector( '[role="dialog"]' ).dispatchEvent( new KeyboardEvent( 'keydown', { key: 'Escape', bubbles: true } ) ); + document.querySelector( '.MuiBackdrop-root' ).click(); + } ); + expect( title() ).toBe( 'Downloading update' ); + + act( () => { + hear( { id: 'update', status: 'idle' } ); + } ); + expect( title() ).toBeUndefined(); + + act( () => { + root.unmount(); + } ); + div.remove(); +} ); diff --git a/src/common/rendererBridge.ts b/src/common/rendererBridge.ts index 9a8233fd..51289b05 100644 --- a/src/common/rendererBridge.ts +++ b/src/common/rendererBridge.ts @@ -31,6 +31,8 @@ export type UpdateDownload = | { status: 'restarted'; percent: number } | { status: 'done' }; +export type UpdateDownloadPayload = UpdateDownload & { id: string }; + // An event payload the page does not read yet; the leaf that reads it tightens it. type Listener = ( payload: unknown ) => void; type Unsubscribe = () => void; @@ -76,6 +78,7 @@ export interface RendererBridge { onToggleBackboneConstraint: ( listener: Listener ) => Unsubscribe; onToggleMultifurcatingConstraint: ( listener: Listener ) => Unsubscribe; onUnhandledError: ( listener: Listener ) => Unsubscribe; + onUpdateDownload: ( listener: ( payload: UpdateDownloadPayload ) => void ) => Unsubscribe; getPathForFile: ( file: File ) => string; } diff --git a/src/constants/ipc.ts b/src/constants/ipc.ts index 1b595871..2c2fe55c 100644 --- a/src/constants/ipc.ts +++ b/src/constants/ipc.ts @@ -28,6 +28,7 @@ export const ALIGNMENT_MODEL_SELECTION_SUCCESS = export const ALIGNMENT_MODEL_SELECTION_FAILURE = 'ALIGNMENT_MODEL_SELECTION_FAILURE' as const; export const UNHANDLED_ERROR = 'UNHANDLED_ERROR' as const; +export const UPDATE_DOWNLOAD = 'UPDATE_DOWNLOAD' as const; export const TOGGLE_BACKBONE_CONSTRAINT = 'TOGGLE_BACKBONE_CONSTRAINT' as const; export const TOGGLE_MULTIFURCATING_CONSTRAINT = 'TOGGLE_MULTIFURCATING_CONSTRAINT' as const; diff --git a/src/main/updates.test.ts b/src/main/updates.test.ts index 49639be4..55839dd6 100644 --- a/src/main/updates.test.ts +++ b/src/main/updates.test.ts @@ -36,7 +36,10 @@ function updater( download: () => Promise = () => Promise.resolve( [] ) } function fakeWindow() { - return { setProgressBar: vi.fn<( value: number ) => void>() }; + return { + setProgressBar: vi.fn<( value: number ) => void>(), + webContents: { send: vi.fn<( channel: string, payload: { status: string } ) => void>() }, + }; } function progress( percent: number ) { @@ -60,6 +63,9 @@ async function startDownload( options: { win?.setProgressBar.mockImplementation( ( value ) => { calls.push( `bar ${value}` ); } ); + win?.webContents.send.mockImplementation( ( _channel, payload ) => { + calls.push( `send ${payload.status}` ); + } ); const choice = Promise.resolve( { response: 0 } ); const dialog = { showMessageBox: vi.fn<( options: unknown ) => Promise<{ response: number }>>( () => choice ), @@ -112,6 +118,21 @@ it( 'Yes downloads the update and shows its progress on the icon', async () => { expect( dialog.showErrorBox ).not.toHaveBeenCalled(); } ); +it( 'every state change is sent to the page', async () => { + const { updates, win } = await startDownload(); + updates.emit( 'download-progress', progress( 42 ) ); + updates.emit( 'download-progress', progress( 10 ) ); + updates.emit( 'update-downloaded' ); + updates.emit( 'error', new Error( 'network down' ) ); + expect( win?.webContents.send.mock.calls ).toEqual( [ + ['UPDATE_DOWNLOAD', { id: 'update', status: 'connecting' }], + ['UPDATE_DOWNLOAD', { id: 'update', status: 'downloading', percent: 42 }], + ['UPDATE_DOWNLOAD', { id: 'update', status: 'restarted', percent: 10 }], + ['UPDATE_DOWNLOAD', { id: 'update', status: 'done' }], + ['UPDATE_DOWNLOAD', { id: 'update', status: 'idle' }], + ] ); +} ); + it( 'the download-progress listener is registered before any download starts', () => { const updates = updater(); listenForUpdates( updates as unknown as AppUpdater, {} as Dialog, () => null ); @@ -159,7 +180,7 @@ it( 'an error mid-download resets the state, clears the bar, then shows the erro calls.length = 0; updates.emit( 'error', new Error( 'network down' ) ); expect( getUpdateDownload() ).toEqual( { status: 'idle' } ); - expect( calls ).toEqual( ['bar -1', 'error box'] ); + expect( calls ).toEqual( ['send idle', 'bar -1', 'error box'] ); expect( logs() ).toEqual( [ 'Update available.', 'Update download started.', diff --git a/src/main/updates.ts b/src/main/updates.ts index e9ec5e89..1b6355fa 100644 --- a/src/main/updates.ts +++ b/src/main/updates.ts @@ -2,6 +2,7 @@ import log from 'electron-log'; import type { BrowserWindow, Dialog } from 'electron'; import type { AppUpdater, ProgressInfo } from 'electron-updater'; import type { UpdateDownload } from '../common/rendererBridge'; +import { UPDATE_DOWNLOAD } from '../constants/ipc'; type WindowGetter = () => BrowserWindow | null; @@ -35,7 +36,9 @@ export function applyUpdateDownloadBar( win: BrowserWindow | null ): void { function setDownload( next: UpdateDownload ): void { download = next; - applyUpdateDownloadBar( getWindow() ); + const win = getWindow(); + win?.webContents.send( UPDATE_DOWNLOAD, { id: 'update', ...next } ); + applyUpdateDownloadBar( win ); } // checkForUpdates rejects as well as emitting 'error'; the listener already reports it, diff --git a/src/preload-proofs/onUpdateDownload.json b/src/preload-proofs/onUpdateDownload.json new file mode 100644 index 00000000..d6e84340 --- /dev/null +++ b/src/preload-proofs/onUpdateDownload.json @@ -0,0 +1,11 @@ +{ + "key": "updateDownload", + "method": "onUpdateDownload", + "channel": "UPDATE_DOWNLOAD", + "subscribe": true, + "result": { + "id": "update", + "status": "downloading", + "percent": 42 + } +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 4a7ea142..5ac0aee6 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -40,9 +40,10 @@ import { TOGGLE_MULTIFURCATING_CONSTRAINT, TREE_SELECT, UNHANDLED_ERROR, + UPDATE_DOWNLOAD, WRITE_FILE, } from '../constants/ipc'; -import type { RendererBridge, HostInfo } from '../common/rendererBridge'; +import type { RendererBridge, HostInfo, UpdateDownloadPayload } from '../common/rendererBridge'; import type { CheckOutputArgs } from '../main/checkOutput'; import type { StateReport } from '../main/getActiveState'; import type { StartRunPayload, RunFinishedPayload, RunErrorPayload } from '../main/startRun'; @@ -191,6 +192,9 @@ const rendererBridge = { onUnhandledError( listener: ( payload: unknown ) => void ) { return onEvent( UNHANDLED_ERROR, listener ); }, + onUpdateDownload( listener: ( payload: UpdateDownloadPayload ) => void ) { + return onEvent( UPDATE_DOWNLOAD, listener ); + }, getPathForFile( file: File ): string { return webUtils.getPathForFile( file ); },