-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathUIFactory.cs
188 lines (164 loc) · 6.42 KB
/
UIFactory.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Drawing;
using System.Windows.Forms;
using NetSparkleUpdater.Interfaces;
using NetSparkleUpdater.Properties;
using NetSparkleUpdater.Enums;
using System.Threading;
using System.Collections.Generic;
namespace NetSparkleUpdater.UI.WinForms
{
/// <summary>
/// UI factory for WinForms .NET Framework interface
/// </summary>
public class UIFactory : IUIFactory
{
private Icon _applicationIcon = null;
/// <inheritdoc/>
public UIFactory()
{
HideReleaseNotes = false;
HideRemindMeLaterButton = false;
HideSkipButton = false;
}
/// <inheritdoc/>
public UIFactory(Icon applicationIcon)
{
_applicationIcon = applicationIcon;
HideReleaseNotes = false;
HideRemindMeLaterButton = false;
HideSkipButton = false;
}
/// <inheritdoc/>
public bool HideReleaseNotes { get; set; }
/// <inheritdoc/>
public bool HideSkipButton { get; set; }
/// <inheritdoc/>
public bool HideRemindMeLaterButton { get; set; }
/// <inheritdoc/>
public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater sparkle, List<AppCastItem> updates, bool isUpdateAlreadyDownloaded = false)
{
var window = new UpdateAvailableWindow(sparkle, updates, _applicationIcon, isUpdateAlreadyDownloaded);
if (HideReleaseNotes)
{
(window as IUpdateAvailable).HideReleaseNotes();
}
if (HideSkipButton)
{
(window as IUpdateAvailable).HideSkipButton();
}
if (HideRemindMeLaterButton)
{
(window as IUpdateAvailable).HideRemindMeLaterButton();
}
return window;
}
/// <inheritdoc/>
public virtual IDownloadProgress CreateProgressWindow(AppCastItem item)
{
return new DownloadProgressWindow(item, _applicationIcon);
}
/// <inheritdoc/>
public virtual ICheckingForUpdates ShowCheckingForUpdates()
{
return new CheckingForUpdatesWindow(_applicationIcon);
}
/// <inheritdoc/>
public virtual void Init()
{
// enable visual style to ensure that we have XP style or higher
// also in WPF applications
Application.EnableVisualStyles();
}
/// <inheritdoc/>
public virtual void ShowUnknownInstallerFormatMessage(string downloadFileName)
{
ShowMessage(Resources.DefaultUIFactory_MessageTitle,
string.Format(Resources.DefaultUIFactory_ShowUnknownInstallerFormatMessageText, downloadFileName));
}
/// <inheritdoc/>
public virtual void ShowVersionIsUpToDate()
{
ShowMessage(Resources.DefaultUIFactory_MessageTitle, Resources.DefaultUIFactory_ShowVersionIsUpToDateMessage);
}
/// <inheritdoc/>
public virtual void ShowVersionIsSkippedByUserRequest()
{
ShowMessage(Resources.DefaultUIFactory_MessageTitle, Resources.DefaultUIFactory_ShowVersionIsSkippedByUserRequestMessage);
}
/// <inheritdoc/>
public virtual void ShowCannotDownloadAppcast(string appcastUrl)
{
ShowMessage(Resources.DefaultUIFactory_ErrorTitle, Resources.DefaultUIFactory_ShowCannotDownloadAppcastMessage);
}
/// <inheritdoc/>
public virtual bool CanShowToastMessages()
{
return true;
}
/// <inheritdoc/>
public virtual void ShowToast(List<AppCastItem> updates, Action<List<AppCastItem>> clickHandler)
{
Thread thread = new Thread(() =>
{
var toast = new ToastNotifier(_applicationIcon)
{
ClickAction = clickHandler,
Updates = updates
};
toast.Show(Resources.DefaultUIFactory_ToastMessage, Resources.DefaultUIFactory_ToastCallToAction, 5);
Application.Run(toast);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
/// <inheritdoc/>
public virtual void ShowDownloadErrorMessage(string message, string appcastUrl)
{
ShowMessage(Resources.DefaultUIFactory_ErrorTitle, string.Format(Resources.DefaultUIFactory_ShowDownloadErrorMessage, message));
}
private void ShowMessage(string title, string message)
{
var messageWindow = new MessageNotificationWindow(title, message, _applicationIcon);
messageWindow.StartPosition = FormStartPosition.CenterScreen;
messageWindow.ShowDialog();
}
/// <inheritdoc/>
public void Shutdown()
{
Application.Exit();
}
#region --- Windows Forms Result Converters ---
/// <summary>
/// Method performs simple conversion of DialogResult to boolean.
/// This method is a convenience when upgrading legacy code.
/// </summary>
/// <param name="dialogResult">WinForms DialogResult instance</param>
/// <returns>Boolean based on dialog result</returns>
public static bool ConvertDialogResultToDownloadProgressResult(DialogResult dialogResult)
{
return (dialogResult != DialogResult.Abort) && (dialogResult != DialogResult.Cancel);
}
/// <summary>
/// Method performs simple conversion of DialogResult to UpdateAvailableResult.
/// This method is a convenience when upgrading legacy code.
/// </summary>
/// <param name="dialogResult">WinForms DialogResult instance</param>
/// <returns>Enumeration value based on dialog result</returns>
public static UpdateAvailableResult ConvertDialogResultToUpdateAvailableResult(DialogResult dialogResult)
{
switch (dialogResult)
{
case DialogResult.Yes:
return UpdateAvailableResult.InstallUpdate;
case DialogResult.No:
return UpdateAvailableResult.SkipUpdate;
case DialogResult.Retry:
case DialogResult.Cancel:
return UpdateAvailableResult.RemindMeLater;
}
return UpdateAvailableResult.None;
}
#endregion
}
}