Re-add Update window

This commit is contained in:
Peter Kirmeier 2022-11-12 21:07:15 +01:00
parent 6db7226249
commit 55a84d40b0
9 changed files with 135 additions and 148 deletions

View file

@ -45,7 +45,7 @@ namespace SystemTrayMenu
if (Settings.Default.CheckForUpdates)
{
#if TODO // GITHUBUPDATE
#if TODO // WPF: Creating the dialog window must be called on STA thread
new Thread((obj) => GitHubUpdate.ActivateNewVersionFormOrCheckForUpdates(
showWhenUpToDate: false))
.Start();

View file

@ -6,22 +6,61 @@ namespace SystemTrayMenu.Helper.Updater
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Http;
using System.Reflection;
using System.Windows.Forms;
using System.Windows;
using SystemTrayMenu.UserInterface;
using SystemTrayMenu.Utilities;
public class GitHubUpdate
{
private static List<Dictionary<string, object>> releases;
private static Form newVersionForm;
private static UpdateWindow newVersionForm;
/// <summary>
/// Gets the latest release version name .
/// </summary>
public static string LatestVersionName
{
get
{
string result = "Unknown";
if (releases == null)
{
return result;
}
try
{
result = releases[0]["tag_name"].ToString() !.Replace("v", string.Empty); // 0 = latest
}
catch (Exception ex)
{
Log.Warn($"{nameof(LatestVersionName)} failed", ex);
}
return result;
}
}
/// <summary>
/// Opens the website on GitHub of the latest release version .
/// </summary>
public static void WebOpenLatestRelease() => Log.ProcessStart("https://github.com/Hofknecht/SystemTrayMenu/releases");
public static void ActivateNewVersionFormOrCheckForUpdates(bool showWhenUpToDate)
{
if (newVersionForm != null)
{
newVersionForm.HandleInvoke(newVersionForm.Activate);
if (newVersionForm!.CheckAccess())
{
newVersionForm.Dispatcher.Invoke(() => newVersionForm?.Activate());
}
else
{
newVersionForm.Activate();
}
}
else
{
@ -35,7 +74,7 @@ namespace SystemTrayMenu.Helper.Updater
HttpClient client = new();
// https://developer.github.com/v3/#user-agent-required
client.DefaultRequestHeaders.Add("User-Agent", "SystemTrayMenu/" + Application.ProductVersion.ToString());
client.DefaultRequestHeaders.Add("User-Agent", "SystemTrayMenu/" + Assembly.GetExecutingAssembly().GetName().Version.ToString());
// https://developer.github.com/v3/media/#request-specific-version
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3.text+json");
@ -61,9 +100,6 @@ namespace SystemTrayMenu.Helper.Updater
RemoveCurrentAndOlderVersions();
ShowNewVersionOrUpToDateDialog(showWhenUpToDate);
}
newVersionForm?.Dispose();
newVersionForm = null;
}
private static void RemoveCurrentAndOlderVersions()
@ -86,10 +122,10 @@ namespace SystemTrayMenu.Helper.Updater
{
if (releases.Count > 0)
{
if (NewVersionDialog() == DialogResult.Yes)
{
Log.ProcessStart("https://github.com/Hofknecht/SystemTrayMenu/releases");
}
newVersionForm = new();
newVersionForm.textBox.Text = GetChangelog();
newVersionForm.Closed += (_, _) => newVersionForm = null;
newVersionForm.ShowDialog();
}
else if (showWhenUpToDate)
{
@ -97,112 +133,6 @@ namespace SystemTrayMenu.Helper.Updater
}
}
/// <summary>
/// Creates a window to show changelog of new available versions.
/// </summary>
/// <param name="LatestVersionTitle">Name of latest release.</param>
/// <param name="Changelog">Pathnotes.</param>
/// <returns>OK = OK, Yes = Website, else = Cancel.</returns>
private static DialogResult NewVersionDialog()
{
const int ClientPad = 15;
newVersionForm = new()
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.FixedDialog,
Icon = Config.GetAppIcon(),
ShowInTaskbar = false,
};
newVersionForm.FormBorderStyle = FormBorderStyle.Sizable;
newVersionForm.MaximizeBox = true;
newVersionForm.MinimizeBox = false;
newVersionForm.ClientSize = new Size(600, 400);
newVersionForm.MinimumSize = newVersionForm.ClientSize;
newVersionForm.Text = Translator.GetText("New version available!");
Label label = new()
{
Size = new Size(newVersionForm.ClientSize.Width - ClientPad, 20),
Location = new Point(ClientPad, ClientPad),
Text = $"{Translator.GetText("Latest available version:")} {GetLatestVersionName()}",
};
newVersionForm.Controls.Add(label);
Button buttonOK = new()
{
DialogResult = DialogResult.OK,
Name = "buttonOK",
};
buttonOK.Location = new Point(
newVersionForm.ClientSize.Width - buttonOK.Size.Width - ClientPad,
newVersionForm.ClientSize.Height - buttonOK.Size.Height - ClientPad);
buttonOK.MinimumSize = new Size(75, 23);
buttonOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonOK.Text = Translator.GetText("OK");
buttonOK.AutoSizeMode = AutoSizeMode.GrowAndShrink;
buttonOK.AutoSize = true;
newVersionForm.Controls.Add(buttonOK);
Button buttonGoToDownloadPage = new()
{
DialogResult = DialogResult.Yes,
Name = "buttonGoToDownloadPage",
};
buttonGoToDownloadPage.Location = new Point(
newVersionForm.ClientSize.Width - buttonGoToDownloadPage.Size.Width - ClientPad - buttonOK.Size.Width - ClientPad,
newVersionForm.ClientSize.Height - buttonGoToDownloadPage.Size.Height - ClientPad);
buttonGoToDownloadPage.MinimumSize = new Size(75, 23);
buttonGoToDownloadPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonGoToDownloadPage.Text = Translator.GetText("Go to download page");
buttonGoToDownloadPage.AutoSizeMode = AutoSizeMode.GrowAndShrink;
buttonGoToDownloadPage.AutoSize = true;
newVersionForm.Controls.Add(buttonGoToDownloadPage);
TextBox textBox = new()
{
Location = new Point(ClientPad, label.Location.Y + label.Size.Height + 5),
};
textBox.Size = new Size(
newVersionForm.ClientSize.Width - (ClientPad * 2),
buttonOK.Location.Y - ClientPad - textBox.Location.Y);
textBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
textBox.Multiline = true;
textBox.Text = GetChangelog();
textBox.ReadOnly = true;
textBox.ScrollBars = ScrollBars.Both;
textBox.BackColor = Color.FromKnownColor(KnownColor.Window);
textBox.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
newVersionForm.Controls.Add(textBox);
newVersionForm.AcceptButton = buttonOK;
return newVersionForm.ShowDialog();
}
/// <summary>
/// Returns the latest release version name.
/// </summary>
/// <returns>Version name.</returns>
private static string GetLatestVersionName()
{
string result = "Unknown";
if (releases == null)
{
return result;
}
try
{
result = releases[0]["tag_name"].ToString().Replace("v", string.Empty);
}
catch (Exception ex)
{
Log.Warn($"{nameof(GetLatestVersionName)} failed", ex);
}
return result;
}
/// <summary>
/// Returns the change log from current version up to the latest release version.
/// </summary>

View file

@ -104,7 +104,6 @@
<NoWarn>1701;1702;WFAC010;MSB3061</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Helpers\Updater\GitHubUpdate.cs" />
<Compile Remove="UserInterface\CustomScrollbar\CustomScrollbar.cs" />
<Compile Remove="UserInterface\CustomScrollbar\ScrollbarControlDesigner.cs" />
<Compile Remove="UserInterface\HotkeyTextboxControl\HotkeyControl.cs" />
@ -137,7 +136,6 @@
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Helpers\Updater\GitHubUpdate.cs" />
<None Include="LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>

View file

@ -33,9 +33,7 @@ namespace SystemTrayMenu.Helper
AddItem(menu, "Frequently Asked Questions", Config.ShowHelpFAQ);
AddItem(menu, "Support SystemTrayMenu", Config.ShowSupportSystemTrayMenu);
AddItem(menu, "About SystemTrayMenu", About);
#if TODO // GITHUBUPDATE
AddItem(menu, "Check for updates", () => GitHubUpdate.ActivateNewVersionFormOrCheckForUpdates(showWhenUpToDate: true));
#endif
AddSeperator(menu);
AddItem(menu, "Restart", AppRestart.ByAppContextMenu);
AddItem(menu, "Exit app", () => Application.Current.Shutdown());

View file

@ -258,6 +258,7 @@
</ScrollViewer>
</TabItem>
<TabItem Header="{u:Translate 'Customize'}">
<!-- TODO: Adjust MaxHeight? -->
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" MaxHeight="600" VerticalAlignment="Top">
<StackPanel>
<GroupBox Header="{u:Translate 'Appearance'}">

View file

@ -1,23 +1,20 @@
// <copyright file="TaskbarLogo.xaml.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
//
// Copyright (c) 2022-2022 Peter Kirmeier
#nullable enable
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using SystemTrayMenu.DllImports;
/// <summary>
/// Logic of Taskbar window.

View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2022-2022 Peter Kirmeier -->
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="clr-namespace:SystemTrayMenu.Utilities"
xmlns:local="clr-namespace:SystemTrayMenu.UserInterface"
x:Class="SystemTrayMenu.UserInterface.UpdateWindow"
mc:Ignorable="d" Title="{u:Translate 'New version available!'}" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" MinWidth="600" MinHeight="400" WindowStyle="ToolWindow" ShowInTaskbar="False">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" x:Name="label" Content="{u:Translate 'Latest available version:'}" />
<!-- TODO: Adjust MaxHeight? -->
<TextBox Grid.Row="1" x:Name="textBox" MaxHeight="600" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" IsReadOnly="True"/>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="buttonGoToDownloadPage" Content="{u:Translate 'Go to download page'}" Margin="3" MinWidth="76" Click="ButtonGoToDownloadPage_Click"/>
<Button x:Name="buttonOk" Content="{u:Translate 'OK'}" Margin="3" MinWidth="76" IsDefault="True"/>
</StackPanel>
</Grid>
</Window>

View file

@ -0,0 +1,51 @@
// <copyright file="UpdateWindow.xaml.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
//
// Copyright (c) 2022-2022 Peter Kirmeier
#nullable enable
namespace SystemTrayMenu.UserInterface
{
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Media.Imaging;
using SystemTrayMenu.Helper.Updater;
/// <summary>
/// Logic of Update window.
/// </summary>
public partial class UpdateWindow : Window
{
public UpdateWindow()
{
InitializeComponent();
Assembly myassembly = Assembly.GetExecutingAssembly();
string myname = myassembly.GetName().Name ?? string.Empty;
using (Stream? imgstream = myassembly.GetManifestResourceStream(myname + ".Resources.SystemTrayMenu.png"))
{
if (imgstream != null)
{
BitmapImage imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = imgstream;
imageSource.EndInit();
Icon = imageSource;
}
}
label.Content = ((string)label.Content) + " " + GitHubUpdate.LatestVersionName;
}
private void ButtonGoToDownloadPage_Click(object sender, RoutedEventArgs e)
{
GitHubUpdate.WebOpenLatestRelease();
Close();
}
}
}

View file

@ -5,7 +5,6 @@
namespace SystemTrayMenu.Utilities
{
using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;
public static class FormsExtensions
@ -39,20 +38,5 @@ namespace SystemTrayMenu.Utilities
throw new NotImplementedException();
}
}
#if TODO
public static void HandleInvoke(this Control instance, Action action)
{
if (instance.InvokeRequired)
{
instance.Invoke(action);
}
else
{
action();
}
}
#endif
}
}