private void Deactivate()
 {
     if (IsMakingPdf)
     {
         _makePdfBackgroundWorker.CancelAsync();
     }
     _publishEpubApi?.AbortMakingEpub();
     // This allows various cleanup of controls which we won't use again, since we
     // always switch to this state when we reactivate the view.
     // In particular, it is part of the solution to BL-4901 that the HtmlPublishPanel,
     // if it is active, is removed (hence deactivated) and disposed.
     SetDisplayMode(PublishModel.DisplayModes.WaitForUserToChooseSomething);
     // This is only supposed to be active in one mode of PublishView.
     if (_htmlControl != null)
     {
         Controls.Remove(_htmlControl);
         _htmlControl.Dispose();
         _htmlControl = null;
     }
     Browser.SuppressJavaScriptErrors = false;
     Browser.ClearCache();             // of anything used in publish mode; may help free memory.
     PublishHelper.Cancel();
     PublishHelper.InPublishTab = false;
 }
        public void SetDisplayMode(PublishModel.DisplayModes displayMode)
        {
            // This is only supposed to be active in one mode of PublishView.
            Browser.SuppressJavaScriptErrors = false;
            Browser.ClearCache();             // try to free memory when switching
            // Abort any work we're doing to prepare a preview (at least stop it interfering with other navigation).
            PublishHelper.Cancel();

            if (displayMode != PublishModel.DisplayModes.Upload && _uploadControl != null)
            {
                Controls.Remove(_uploadControl);
                _uploadControl = null;
            }
            if ((displayMode != PublishModel.DisplayModes.Android || displayMode != PublishModel.DisplayModes.EPUB) &&
                _htmlControl != null && Controls.Contains(_htmlControl))
            {
                Controls.Remove(_htmlControl);

                // disposal of the browser is good but it hides a multitude of sins that we'd rather catch and fix during development. E.g. BL-4901
                if (!ApplicationUpdateSupport.IsDevOrAlpha)
                {
                    _htmlControl.Dispose();
                    _htmlControl = null;
                }
            }

            switch (displayMode)
            {
            case PublishModel.DisplayModes.WaitForUserToChooseSomething:
                _printButton.Enabled = _saveButton.Enabled = false;
                Cursor = Cursors.Default;
                _workingIndicator.Visible = false;
                _pdfViewer.Visible        = false;
                break;

            case PublishModel.DisplayModes.Working:
                _printButton.Enabled     = _saveButton.Enabled = false;
                _workingIndicator.Cursor = Cursors.WaitCursor;
                Cursor = Cursors.WaitCursor;
                _workingIndicator.Visible = true;
                _pdfViewer.Visible        = false;
                break;

            case PublishModel.DisplayModes.ShowPdf:
                Logger.WriteEvent("Entering Publish PDF Screen");
                if (RobustFile.Exists(_model.PdfFilePath))
                {
                    _pdfViewer.Visible        = true;
                    _workingIndicator.Visible = false;
                    Cursor = Cursors.Default;
                    _saveButton.Enabled  = true;
                    _printButton.Enabled = _pdfViewer.ShowPdf(_model.PdfFilePath);
                }
                break;

            case PublishModel.DisplayModes.Printing:
                _simpleAllPagesRadio.Enabled = false;
                _bookletCoverRadio.Enabled   = false;
                _bookletBodyRadio.Enabled    = false;
                _printButton.Enabled         = _saveButton.Enabled = false;
                _workingIndicator.Cursor     = Cursors.WaitCursor;
                Cursor = Cursors.WaitCursor;
                _workingIndicator.Visible = true;
                _pdfViewer.Visible        = true;
                break;

            case PublishModel.DisplayModes.ResumeAfterPrint:
                _simpleAllPagesRadio.Enabled = true;
                _pdfViewer.Visible           = true;
                _workingIndicator.Visible    = false;
                Cursor = Cursors.Default;
                _saveButton.Enabled  = true;
                _printButton.Enabled = true;
                _pdfViewer.Visible   = true;
                break;

            case PublishModel.DisplayModes.Upload:
            {
                Logger.WriteEvent("Entering Publish Upload Screen");
                _workingIndicator.Visible = false;                         // If we haven't finished creating the PDF, we will indicate that in the progress window.
                _saveButton.Enabled       = _printButton.Enabled = false;  // Can't print or save in this mode...wouldn't be obvious what would be saved.
                _pdfViewer.Visible        = false;
                Cursor = Cursors.Default;

                if (_uploadControl == null)
                {
                    SetupPublishControl();
                }

                break;
            }

            case PublishModel.DisplayModes.Android:
                _saveButton.Enabled = _printButton.Enabled = false;                         // Can't print or save in this mode...wouldn't be obvious what would be saved.
                BloomReaderFileMaker.ControlForInvoke = ParentForm;                         // something created on UI thread that won't go away
                ShowHtmlPanel(BloomFileLocator.GetBrowserFile(false, "publish", "ReaderPublish", "loader.html"));
                break;

            case PublishModel.DisplayModes.EPUB:
                _saveButton.Enabled = _printButton.Enabled = false;                         // Can't print or save in this mode...wouldn't be obvious what would be saved.
                // We rather mangled the Readium code in the process of cutting away its own navigation
                // and other controls. It produces all kinds of JavaScript errors, but it seems to do
                // what we want in our preview. So just suppress the toasts for all of them. This is unfortunate because
                // we'll lose them for all the other JS code in this pane. But I don't have a better solution.
                // We still get them in the output window, in case we really want to look for one.
                Browser.SuppressJavaScriptErrors = true;
                PublishEpubApi.ControlForInvoke  = ParentForm;                        // something created on UI thread that won't go away
                ShowHtmlPanel(BloomFileLocator.GetBrowserFile(false, "publish", "ePUBPublish", "loader.html"));
                break;
            }
            UpdateSaveButton();
        }