forked from NetSparkleUpdater/NetSparkle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateAvailableWindow.axaml.cs
More file actions
194 lines (176 loc) · 6.78 KB
/
UpdateAvailableWindow.axaml.cs
File metadata and controls
194 lines (176 loc) · 6.78 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using NetSparkleUpdater.Enums;
using NetSparkleUpdater.Events;
using NetSparkleUpdater.Interfaces;
using NetSparkleUpdater.UI.Avalonia.Controls;
using NetSparkleUpdater.UI.Avalonia.Interfaces;
using NetSparkleUpdater.UI.Avalonia.ViewModels;
using System.Linq;
using TheArtOfDev.HtmlRenderer.Avalonia;
namespace NetSparkleUpdater.UI.Avalonia
{
/// <summary>
/// Interaction logic for UpdateAvailableWindow.xaml.
///
/// Window that shows the list of available updates to the user
/// </summary>
public partial class UpdateAvailableWindow : BaseWindow, IUpdateAvailable, IReleaseNotesDisplayer, IUserRespondedToUpdateCheck
{
private UpdateAvailableWindowViewModel? _dataContext;
private RowDefinition? _releaseNotesRow;
private ScrollViewer? _htmlLabelContainer;
private HtmlLabel? _htmlLabel;
private bool _wasResponseSent = false;
/// <summary>
/// Initialize the available update window with no initial date context
/// (and thus no initial information on downloadable releases to show
/// to the user)
/// </summary>
public UpdateAvailableWindow() : base(true)
{
this.InitializeComponent();
InitViews();
}
/// <summary>
/// Initialize the available update window with the given view model,
/// which contains the information on the updates that are available to the
/// end user
/// </summary>
/// <param name="viewModel">View model with info on the updates that are available
/// to the user</param>
/// <param name="iconBitmap">Bitmap to use for the app's logo/graphic</param>
public UpdateAvailableWindow(UpdateAvailableWindowViewModel viewModel, Bitmap? iconBitmap) : base(true)
{
this.InitializeComponent();
InitViews();
var imageControl = this.FindControl<Image>("AppIcon");
if (imageControl != null)
{
imageControl.Source = iconBitmap;
}
#if DEBUG
Application.Current?.AttachDeveloperTools();
#endif
DataContext = _dataContext = viewModel;
_dataContext.ReleaseNotesDisplayer = this;
_dataContext.UserRespondedHandler = this;
Closing += UpdateAvailableWindow_Closing;
}
private void InitViews()
{
var grid = this.FindControl<Grid>("MainGrid");
_releaseNotesRow = grid?.RowDefinitions[2];
_htmlLabel = this.FindControl<HtmlLabel>("ChangeNotesHTMLLabel");
_htmlLabelContainer = this.FindControl<ScrollViewer>("ChangeNotesScrollViewer");
//_htmlLabel.SetValue(HtmlLabel.AutoSizeHeightOnlyProperty, true); // throws on 0.10.0 for some reason?
}
/// <summary>
/// Change the main grid's background color. Use new SolidColorBrush(Colors.Transparent) or null to clear.
/// </summary>
/// <param name="solidColorBrush"></param>
public void ChangeMainGridBackgroundColor(IBrush solidColorBrush)
{
var grid = this.FindControl<Grid>("MainGrid");
if (grid != null)
{
grid.Background = solidColorBrush;
}
}
private void UpdateAvailableWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
{
UserRespondedToUpdateCheck(UpdateAvailableResult.None); // just in case
Closing -= UpdateAvailableWindow_Closing;
}
UpdateAvailableResult IUpdateAvailable.Result => _dataContext?.UserResponse ?? UpdateAvailableResult.None;
AppCastItem IUpdateAvailable.CurrentItem => CurrentItem;
/// <summary>
/// The item that the user is being asked about updating to
/// </summary>
public AppCastItem CurrentItem
{
// I don't really like the creating of a new app cast item, but we'll revisit
// some of this when we refactor the UI stuff, so it's OK for now. Not really used
// anywhere in our lib, anyway.
get { return _dataContext?.Updates?.FirstOrDefault() ?? new AppCastItem(); }
}
/// <summary>
/// An event that informs its listeners how the user responded to the
/// software update request
/// </summary>
public event UserRespondedToUpdate? UserResponded;
void IUpdateAvailable.BringToFront()
{
BringToFront();
}
void IUpdateAvailable.Close()
{
UserRespondedToUpdateCheck(UpdateAvailableResult.None); // just in case
Closing -= UpdateAvailableWindow_Closing;
CloseWindow();
}
void IUpdateAvailable.HideReleaseNotes()
{
if (_dataContext != null)
{
_dataContext.AreReleaseNotesVisible = false;
}
if (_releaseNotesRow != null)
{
_releaseNotesRow.Height = new GridLength(0);
}
if (_htmlLabelContainer != null)
{
_htmlLabelContainer.IsVisible = false;
}
Height = 225;
}
void IUpdateAvailable.HideRemindMeLaterButton()
{
if (_dataContext != null)
{
_dataContext.IsRemindMeLaterVisible = false;
}
}
void IUpdateAvailable.HideSkipButton()
{
if (_dataContext != null)
{
_dataContext.IsSkipVisible = false;
}
}
void IUpdateAvailable.Show()
{
ShowWindow();
}
/// <summary>
/// The user responded to the update check with a given response
/// </summary>
/// <param name="response">How the user responded to the update (e.g. install the update, remind me later)</param>
public void UserRespondedToUpdateCheck(UpdateAvailableResult response)
{
if (!_wasResponseSent)
{
_wasResponseSent = true;
UserResponded?.Invoke(this, new UpdateResponseEventArgs(_dataContext?.UserResponse ?? UpdateAvailableResult.None, CurrentItem));
}
}
/// <summary>
/// Show the HTML release notes to the user via the UI dispatcher
/// </summary>
/// <param name="htmlNotes">The HTML notes to show to the end user</param>
public void ShowReleaseNotes(string htmlNotes)
{
if (_htmlLabel != null)
{
Dispatcher.UIThread.InvokeAsync(() =>
{
_htmlLabel.Text = htmlNotes;
});
}
}
}
}