Katteker/Katteker.Gui/UpdateWindow.cs
Holger Boerchers d383f0ef1c Initial commit
2018-03-21 20:07:40 +01:00

146 lines
4.6 KiB
C#

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Katteker.Gui.Properties;
namespace Katteker.Gui
{
public sealed partial class UpdateWindow : Form
{
private const int HTCAPTION = 0x2;
private const int WM_NCLBUTTONDOWN = 0xA1;
/// <summary>
/// The Update Window
/// </summary>
public UpdateWindow()
{
Font = SystemFonts.MessageBoxFont;
Application.EnableVisualStyles();
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
InitializeComponent();
}
private string Changelog => UpdateManager.ChangelogFilename;
/// <summary>
/// Last release entry.
/// </summary>
public ReleaseEntry ReleaseEntry { get; set; }
/// <summary>
/// Update manager
/// </summary>
public UpdateManager UpdateManager { get; set; }
private string PublishPath => UpdateManager.UrlOrPath;
/// <inheritdoc />
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
components?.Dispose();
}
base.Dispose(disposing);
}
[DllImport("User32.dll")]
private static extern bool ReleaseCapture();
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
private void CloseBtn_Click(object sender, EventArgs e)
{
Close();
}
private void MaximizeBtn_Click(object sender, EventArgs e)
{
WindowState = WindowState != FormWindowState.Maximized ? FormWindowState.Maximized : FormWindowState.Normal;
}
private void MinimizeBtn_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
private void Titlebar_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
private async void UpdateWindow_Load(object sender, EventArgs e)
{
var changelog = await ChangelogHelper.LoadChangelogAsync(Changelog, PublishPath).ConfigureAwait(false);
Invoke(new Action(() => changelogBrowser.DocumentText = changelog));
if (ReleaseEntry == null)
{
Invoke(new Action(() => { WriteTitle(Resources.No_update_available); }));
}
else
{
var latest = ReleaseEntry.Version;
Invoke(new Action(() =>
{
WriteTitle(Resources.You_can_update_to_Version + latest);
updBtn.Visible = true;
}));
}
}
private async void UpdBtn_Click(object sender, EventArgs e)
{
Invoke(new Action(() => progressBar1.Visible = true));
try
{
var progress = new Progress<int>(x => Invoke(new Action(() => { progressBar1.Value = x; })));
await UpdateManager.UpdateAppAsync(progress).ConfigureAwait(false);
WriteTitle(Resources.You_re_up_to_date);
}
catch (Exception ex)
{
WriteTitle(ex.Message);
}
finally
{
Invoke(new Action(() =>
{
progressBar1.Visible = false;
updBtn.Visible = false;
var messResult = MessageBox.Show(
Resources.Installed_new_Version,
Resources.New_Version,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (messResult == DialogResult.Yes)
{
DialogResult = DialogResult.OK;
Close();
}
}));
}
}
private void WriteTitle(string text)
{
Invoke(new Action(() =>
{
Text = text;
titlebar.Text = text;
}));
}
}
}