HiraokaHyperTools.NetSparkle.N.UI.NetFramework.WinForms by kenjiuno, Deadpikle, Dirk Eisenberg

<PackageReference Include="HiraokaHyperTools.NetSparkle.N.UI.NetFramework.WinForms" Version="0.1.12" />

 UpdateAvailableWindow

using NetSparkle.Enums; using System; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Threading; using System.Windows.Forms; namespace NetSparkle.UI.NetFramework.WinForms { public class UpdateAvailableWindow { private readonly Sparkle _sparkle; private readonly AppCastItem[] _updates; private Timer _ensureDialogShownTimer; private CancellationToken _cancellationToken; private CancellationTokenSource _cancellationTokenSource; private ReleaseNotesGrabber _releaseNotesGrabber; private IContainer components; private PictureBox imgAppIcon; private Label lblHeader; private Label lblInfoText; private Label label3; private Button skipButton; private Button buttonRemind; private Button updateButton; private Panel panel1; private WebBrowser ReleaseNotesBrowser; private Label lblInstall; private Label lblDownload; private Label lblUnknownVersion; AppCastItem CurrentItem { get { if (_updates.Count() <= 0) return null; return _updates[0]; } } UpdateAvailableResult Result => UIFactory.ConvertDialogResultToUpdateAvailableResult(this.get_DialogResult()); public event EventHandler UserResponded; public UpdateAvailableWindow(Sparkle sparkle, AppCastItem[] items, Icon applicationIcon = null, bool isUpdateAlreadyDownloaded = false, string separatorTemplate = "", string headAddition = "") : this() { _sparkle = sparkle; _updates = items; _releaseNotesGrabber = new ReleaseNotesGrabber(separatorTemplate, headAddition, sparkle); InitializeComponent(); try { ReleaseNotesBrowser.set_AllowWebBrowserDrop(false); ReleaseNotesBrowser.set_AllowNavigation(false); } catch (Exception ex) { _sparkle.get_LogWriter().PrintMessage("Error in browser init: {0}", new object[1] { ex.Message }); } AppCastItem val = items.FirstOrDefault(); string newValue = isUpdateAlreadyDownloaded ? lblInstall.get_Text() : lblDownload.get_Text(); lblHeader.set_Text(lblHeader.get_Text().Replace("APP", (val != null) ? val.get_AppName() : "the application")); if (val != null) { string text = ""; try { text = Utilities.GetVersionString(new Version(val.get_AppVersionInstalled())); } catch { text = "?"; } lblInfoText.set_Text(lblInfoText.get_Text().Replace("APP", val.get_AppName()).Replace("OLDVERSION", text) .Replace("DOWNLOAD", newValue)); } else lblInfoText.set_Text(lblUnknownVersion.get_Text().Replace("DOWNLOAD", newValue)); bool flag = items.Any((AppCastItem x) => x.get_IsCriticalUpdate()); buttonRemind.set_Enabled(!flag); skipButton.set_Enabled(!flag); if (applicationIcon != null) { using (Icon icon = new Icon(applicationIcon, new Size(48, 48))) imgAppIcon.set_Image((Image)icon.ToBitmap()); this.set_Icon(applicationIcon); } _cancellationTokenSource = new CancellationTokenSource(); _cancellationToken = _cancellationTokenSource.Token; ReleaseNotesBrowser.set_DocumentText(_releaseNotesGrabber.GetLoadingText()); EnsureDialogShown(); LoadReleaseNotes(items); } private unsafe async void LoadReleaseNotes(AppCastItem[] items) { AppCastItem val = (from p in items orderby p.get_Version() descending select p).FirstOrDefault(); string releaseNotes = await _releaseNotesGrabber.DownloadAllReleaseNotesAsHTML(items, val, _cancellationToken); <>c__DisplayClass10_0 <>c__DisplayClass10_; ReleaseNotesBrowser.Invoke((Delegate)new MethodInvoker((object)<>c__DisplayClass10_, (IntPtr)(void*))); } private void UpdateAvailableWindow_FormClosing(object sender, FormClosingEventArgs e) { ((CancelEventArgs)e).Cancel = true; } void HideReleaseNotes() { RemoveReleaseNotesControls(); } void Show() { this.ShowDialog(); this.UserResponded?.Invoke(this, new EventArgs()); } void BringToFront() { this.BringToFront(); } void Close() { _cancellationTokenSource?.Cancel(); CloseForm(); } private unsafe void CloseForm() { if (this.get_InvokeRequired()) this.Invoke((Delegate)new MethodInvoker((object)this, (IntPtr)(void*))); else this.Close(); } public void RemoveReleaseNotesControls() { if ((int)label3.get_Parent() != 0) { Size size = this.get_Size(); int width = size.Width; size = this.get_Size(); Size size2 = new Size(width, size.Height - label3.get_Height() - panel1.get_Height()); label3.get_Parent().get_Controls().Remove(label3); ReleaseNotesBrowser.get_Parent().get_Controls().Remove(ReleaseNotesBrowser); panel1.get_Parent().get_Controls().Remove(panel1); this.set_Size(size2); } } private void OnSkipButtonClick(object sender, EventArgs e) { this.set_DialogResult(7); _cancellationTokenSource?.Cancel(); CloseForm(); } private void OnRemindClick(object sender, EventArgs e) { this.set_DialogResult(4); _cancellationTokenSource?.Cancel(); CloseForm(); } private void OnUpdateButtonClick(object sender, EventArgs e) { this.set_DialogResult(6); _cancellationTokenSource?.Cancel(); CloseForm(); } public void EnsureDialogShown() { _ensureDialogShownTimer = new Timer(); _ensureDialogShownTimer.add_Tick((EventHandler)EnsureDialogeShown_tick); _ensureDialogShownTimer.set_Interval(250); _ensureDialogShownTimer.Start(); } private void EnsureDialogeShown_tick(object sender, EventArgs e) { this.Activate(); this.set_TopMost(true); this.set_TopMost(false); this.Focus(); _ensureDialogShownTimer.set_Enabled(false); _ensureDialogShownTimer = null; } public void HideRemindMeLaterButton() { buttonRemind.set_Visible(false); } public void HideSkipButton() { skipButton.set_Visible(false); } protected override void Dispose(bool disposing) { if (disposing && components != null) components.Dispose(); this.Dispose(disposing); } private void InitializeComponent() { ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(UpdateAvailableWindow)); lblHeader = new Label(); lblInfoText = new Label(); label3 = new Label(); skipButton = new Button(); buttonRemind = new Button(); updateButton = new Button(); panel1 = new Panel(); ReleaseNotesBrowser = new WebBrowser(); imgAppIcon = new PictureBox(); lblInstall = new Label(); lblDownload = new Label(); lblUnknownVersion = new Label(); panel1.SuspendLayout(); ((ISupportInitialize)imgAppIcon).BeginInit(); this.SuspendLayout(); componentResourceManager.ApplyResources(lblHeader, "lblHeader"); lblHeader.set_Name("lblHeader"); componentResourceManager.ApplyResources(lblInfoText, "lblInfoText"); lblInfoText.set_Name("lblInfoText"); componentResourceManager.ApplyResources(label3, "label3"); label3.set_Name("label3"); componentResourceManager.ApplyResources(skipButton, "skipButton"); skipButton.set_Name("skipButton"); skipButton.set_UseVisualStyleBackColor(true); skipButton.add_Click((EventHandler)OnSkipButtonClick); componentResourceManager.ApplyResources(buttonRemind, "buttonRemind"); buttonRemind.set_Name("buttonRemind"); buttonRemind.set_UseVisualStyleBackColor(true); buttonRemind.add_Click((EventHandler)OnRemindClick); componentResourceManager.ApplyResources(updateButton, "updateButton"); updateButton.set_Name("updateButton"); updateButton.set_UseVisualStyleBackColor(true); updateButton.add_Click((EventHandler)OnUpdateButtonClick); componentResourceManager.ApplyResources(panel1, "panel1"); panel1.set_BorderStyle(2); panel1.get_Controls().Add(ReleaseNotesBrowser); panel1.set_Name("panel1"); componentResourceManager.ApplyResources(ReleaseNotesBrowser, "ReleaseNotesBrowser"); ReleaseNotesBrowser.set_IsWebBrowserContextMenuEnabled(false); ReleaseNotesBrowser.set_Name("ReleaseNotesBrowser"); componentResourceManager.ApplyResources(imgAppIcon, "imgAppIcon"); imgAppIcon.set_Name("imgAppIcon"); imgAppIcon.set_TabStop(false); componentResourceManager.ApplyResources(lblInstall, "lblInstall"); lblInstall.set_Name("lblInstall"); componentResourceManager.ApplyResources(lblDownload, "lblDownload"); lblDownload.set_Name("lblDownload"); componentResourceManager.ApplyResources(lblUnknownVersion, "lblUnknownVersion"); lblUnknownVersion.set_Name("lblUnknownVersion"); componentResourceManager.ApplyResources(this, "$this"); this.set_AutoScaleMode(1); this.get_Controls().Add(lblUnknownVersion); this.get_Controls().Add(panel1); this.get_Controls().Add(label3); this.get_Controls().Add(updateButton); this.get_Controls().Add(buttonRemind); this.get_Controls().Add(skipButton); this.get_Controls().Add(lblInfoText); this.get_Controls().Add(lblHeader); this.get_Controls().Add(imgAppIcon); this.get_Controls().Add(lblDownload); this.get_Controls().Add(lblInstall); this.set_MaximizeBox(false); this.set_Name("UpdateAvailableWindow"); this.set_SizeGripStyle(1); panel1.ResumeLayout(false); ((ISupportInitialize)imgAppIcon).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } } }