electron / update-electron-app

🌲 A drop-in module that adds autoUpdating capabilities to Electron apps
MIT License
716 stars 131 forks source link

feat: add custom dialog option #126

Open ArthurLobopro opened 8 months ago

ArthurLobopro commented 8 months ago

Hello, how you merged the PR #105 I rewrite the PR #97 to avoid merge conflicts. I am unable to test the feature on windows or macos so if someone can test it I will be grateful :)

ArthurLobopro commented 8 months ago

I think I added all the missing semicolons, did I forget someone?

ArthurLobopro commented 8 months ago

@erickzhao I think the most people that want to change something on the dialog only wants to change the update message to something different (Like the Issue #116 ) or in another language (this is my case).

Somehow, a way to do my own custom dialog can be interesting I think. How would you implements that?

dsanders11 commented 8 months ago

I think the possibility to localize the text is a reasonable feature, and should probably be supported without requiring users to re-implement the dialog all together.

For customizing the dialog more deeply, though, we could look at providing a hook, say IUpdateElectronAppOptions .onNotifyUser which takes a function. Then we could export a makeUserNotifier(options: IDialogMessages), which has this code moved to it and returns a function.

https://github.com/electron/update-electron-app/blob/06373904910b96aebd72f31434f81d0184693906/src/index.ts#L186-L202

Then to do localization the user would do:

updateElectronApp({
  ...,
  onNotifyUser: makeUserNotifier({ 
    restartButtonText: 'Restart!!'
  })
})

And if they wanted to more deeply customize the notify they have a hook to do whatever they'd like:

updateElectronApp({
  ...,
  onNotifyUser: (event, releaseNotes, releaseName, releaseDate, updateURL) => {
    if (releaseName === 'AwesomeRelease') {
      // Do fireworks
    }
  }
})
ArthurLobopro commented 8 months ago

@dsanders11 great idea! @erickzhao what do you think about that?

ArthurLobopro commented 8 months ago

I realize if we provide the hook we will lose the logger context, because the logger is declared inside the updateElectronApp function.

https://github.com/electron/update-electron-app/blob/06373904910b96aebd72f31434f81d0184693906/src/index.ts#L154-L156

To solve it I think we can add a listener just to log it:

 autoUpdater.on(
      'update-downloaded',
      (event, releaseNotes, releaseName, releaseDate, updateURL) => {
        log('update-downloaded', [event, releaseNotes, releaseName, releaseDate, updateURL]);
      },
    );

 autoUpdater.on(
  'update-downloaded',
  (event, releaseNotes, releaseName, releaseDate, updateURL) => { 
      const { title, restartButtonText, laterButtonText, detail } = opts.dialog;

      const dialogOpts = {
        type: 'info',
        buttons: [restartButtonText, laterButtonText],
        title,
        message: process.platform === 'win32' ? releaseNotes : releaseName,
        detail,
     };

    dialog.showMessageBox(dialogOpts).then(({ response }) => {
        if (response === 0) autoUpdater.quitAndInstall();
    });
  },);
ArthurLobopro commented 8 months ago

I found another thing.

When we create the dialog, we pick the dialog API from electron in the options

https://github.com/electron/update-electron-app/blob/06373904910b96aebd72f31434f81d0184693906/src/index.ts#L124

https://github.com/electron/update-electron-app/blob/06373904910b96aebd72f31434f81d0184693906/src/index.ts#L132

But to create a separated function to create the notifier, maybe we will need to import directly from electron.

dsanders11 commented 8 months ago

When we create the dialog, we pick the dialog API from electron in the options

It looks like this is just an undocumented option for testing purposes (I'm not familiar with this code base, but that's my take from a quick read), so it could similarly be an undocumented option on IDialogMessages, or we could tweak the signature of the proposed onNotifyUser to receive an object and it could be an undocumented value there:

updateElectronApp({
  ...,
  onNotifyUser: (info) => {
    const { event, releaseNotes, releaseName, releaseDate, updateURL } = info;
    // info.electron is an undocumented value for tests
    if (releaseName === 'AwesomeRelease') {
      // Do fireworks
    }
  }
})
ArthurLobopro commented 8 months ago

I don't know why this error happens. The Electron namespace is already referenced here:

https://github.com/electron/update-electron-app/blob/1c9e970c0785f25b9d6528eae0dcd35204c8c08d/src/index.ts#L207

ArthurLobopro commented 8 months ago

Can someone test it on Windows or Macos? I am using Linux on my PC.