electronnsiselectron-buildernsdialogs

How can I enable the cancel button in an electron-built application?


I'm working with an electron application that is built via electron-builder. When I generate the installer and begin installing with it, I see that the cancel button is disabled.

I've looked around at some electron builder documentation and done a few google searches but I seem to be coming up blank here.

edit: Discovered that I can use build/installer.nsh to actually modify UI elements, now I'm just wondering, how do I gain access to the button to enable / disable it, the examples I've seen use an .ini file to store options or something similar, but I'm getting recommendations to use nsDialogs.

Is nsDialogs something that's already readily accessible to me or do I need to import something into my installer.nsh file to use nsDialogs?

And by that token, how would I access the cancel button in nsDialogs?

Is there a configurable value that enables me to... enable that cancel button so users can choose to cancel during installation?

Thanks!


Solution

  • NSIS installers do not support canceling the installation when you enter the InstFiles page (or any page after it). This is by design because of how the scripting language works.

    If you don't mind hacks you can call the uninstaller when the user clicks cancel during the install phase:

    Var Cancel
    !include LogicLib.nsh
    !include WinMessages.nsh
    
    Function .onUserAbort
        StrCmp $Cancel "" +3
            IntOp $Cancel $Cancel | 1
            Abort
    FunctionEnd
    
    !macro FakeWork c
    !if "${c}" < 10
    Sleep 333
    DetailPrint .
    !define /redef /math c "${c}" + 1
    !insertmacro ${__MACRO__} "${c}"
    !endif
    !macroend
    
    Section Uninstall
    !insertmacro FakeWork 0
    Delete "$InstDir\Uninst.exe"
    RMDir "$InstDir"
    SectionEnd
    
    Function CheckCancel
    ${If} $Cancel = 1
        IntOp $Cancel $Cancel + 1
        GetDlgItem $0 $hwndParent 2
        EnableWindow $0 0
        DetailPrint "Canceling..."
        SetDetailsPrint none
        ExecWait '"$InstDir\Uninst.exe" /S _?=$InstDir'
        Delete "$InstDir\Uninst.exe"
        RMDir "$InstDir"
        SetDetailsPrint both
        Quit
    ${EndIf}
    FunctionEnd
    
    
    Section
    SetOutPath $InstDir
    WriteUninstaller "$InstDir\Uninst.exe"
    StrCpy $Cancel 0 ; Allow special cancel mode
    GetDlgItem $0 $hwndParent 2
    EnableWindow $0 1 ; Enable cancel button
    
    !insertmacro FakeWork 0 ; Replace these with File or other instructions
    Call CheckCancel
    !insertmacro FakeWork 0
    Call CheckCancel
    !insertmacro FakeWork 0
    Call CheckCancel
    
    StrCpy $Cancel "" ; We are done, ignore special cancel mode
    SectionEnd
    

    (I have no idea how to integrate this with electron-builder, sorry)

    If you want a proper roll-back installer, try Inno Setup or WiX (MSI).