-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAboutBox.xaml.cs
More file actions
173 lines (143 loc) · 4.83 KB
/
AboutBox.xaml.cs
File metadata and controls
173 lines (143 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
namespace Menees.Windows.Presentation
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Menees.Diagnostics;
using Menees.Shell;
using IconImage = System.Drawing.Icon;
#endregion
/// <summary>
/// Interaction logic for AboutBox.xaml
/// </summary>
internal partial class AboutBox : ExtendedDialog
{
#region Private Data Members
private readonly Assembly callingAssembly;
private readonly string repository;
#endregion
#region Constructors
public AboutBox(Assembly callingAssembly, string? repository)
{
this.InitializeComponent();
this.callingAssembly = callingAssembly;
this.repository = repository ?? ApplicationInfo.ApplicationName.Replace(" ", string.Empty);
string applicationName = ApplicationInfo.ApplicationName;
this.Title = "About " + applicationName;
this.productName.Text = applicationName;
this.version.Text = ShellUtility.GetVersionInfo(callingAssembly);
this.copyright.Text = ShellUtility.GetCopyrightInfo(callingAssembly);
this.updateLink.Visibility = Visibility.Hidden;
if (callingAssembly != null)
{
Uri? productUrl = ReflectionUtility.GetProductUrl(callingAssembly);
if (productUrl != null)
{
this.webLink.Tag = productUrl;
this.webLink.Content = productUrl.GetComponents(UriComponents.Host | UriComponents.Path, UriFormat.Unescaped).TrimEnd('/');
}
}
}
#endregion
#region Public Methods
public void Execute(Window? owner)
{
// If there's no visible owner window, then this dialog should be centered on the screen.
if (owner == null || owner.Visibility != Visibility.Visible)
{
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.ShowInTaskbar = true;
}
// We can't do "this.icon.Source = owner.Icon;" because that always gets a small (e.g., 16x16) icon.
using (IconImage? appIcon = IconImage.ExtractAssociatedIcon(ApplicationInfo.ExecutableFile))
{
if (appIcon != null)
{
// From comment at: http://www.infosysblogs.com/microsoft/2007/04/wpf_assigning_icon_to_image_co.html
BitmapSource bitmap = Imaging.CreateBitmapSourceFromHIcon(appIcon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
this.icon.Source = bitmap;
}
}
this.Owner = owner;
this.ShowDialog();
}
#endregion
#region Private Methods
private static void FinishLink(object sender, RoutedEventArgs e)
{
// Hyperlink doesn't have a Visited propertly like it did in WinForms, so we'll just set the color.
// This loses the color change behavior on mouse hover, but I can live with that.
// (I thought about just closing the about box instead.)
if (sender is Control element)
{
element.Foreground = Brushes.Purple;
}
e.Handled = true;
}
#endregion
#region Private Event Handlers
private void EmailLink_Clicked(object sender, RoutedEventArgs e)
{
// UriBuilder always adds a '/' after the email address, which shows up in the opened
// mail message's To field. So we'll manually build a mailto-compatible Uri.
string link = string.Format("mailto:bill@menees.com?subject={0} {1}", this.productName.Text, this.version.Text);
if (WindowsUtility.ShellExecute(this, link))
{
FinishLink(sender, e);
}
e.Handled = true;
}
private void WebLink_Clicked(object sender, RoutedEventArgs e)
{
Uri uri = new("http://www.menees.com");
if (sender is ContentControl content && content.Tag is Uri productUri)
{
uri = productUri;
}
if (WindowsUtility.ShellExecute(this, uri.ToString()))
{
FinishLink(sender, e);
}
e.Handled = true;
}
private async void ExtendedDialog_LoadedAsync(object sender, RoutedEventArgs e)
{
string repository = this.repository;
Release? latest = await Task.Run(() => Release.FindGithubLatest(repository)).ConfigureAwait(true);
if (latest != null && this.IsLoaded)
{
Version? appVersion = ReflectionUtility.GetVersion(this.callingAssembly);
if (latest.Version > appVersion)
{
this.updateLink.Tag = latest;
this.updateLink.Content = $"★ Version {latest.Version} update available!";
this.updateLink.Visibility = Visibility.Visible;
}
}
}
private void UpdateLink_Clicked(object sender, RoutedEventArgs e)
{
if (this.updateLink.Tag is Release latest && WindowsUtility.ShellExecute(this, latest.HtmlUri.ToString()))
{
FinishLink(sender, e);
}
e.Handled = true;
}
#endregion
}
}