Repository navigation
Expand file tree
/
Copy pathStatusWindow.xaml.cs
More file actions
286 lines (219 loc) · 8.16 KB
/
Copy pathStatusWindow.xaml.cs
File metadata and controls
286 lines (219 loc) · 8.16 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using NETworkManager.Settings;
using NETworkManager.Utilities;
using NETworkManager.Views;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
namespace NETworkManager;
public partial class StatusWindow : INotifyPropertyChanged
{
#region Constructor
public StatusWindow(MainWindow mainWindow)
{
InitializeComponent();
DataContext = this;
Title = $"NETworkManager {AssemblyManager.Current.Version} - {Localization.Resources.Strings.NetworkStatus}";
_mainWindow = mainWindow;
_dispatcherTimerClose.Interval = TimeSpan.FromMilliseconds(33);
_dispatcherTimerClose.Tick += DispatcherTimerTime_Tick;
_networkConnectionView = new NetworkConnectionWidgetView();
ContentControlNetworkConnection.Content = _networkConnectionView;
// With SizeToContent="Height" the height is only known after layout; re-anchor to the
// bottom-right whenever it changes so the window does not drift to the top of the screen.
SizeChanged += (_, _) => UpdatePosition();
}
#endregion
#region PropertyChangedEventHandler
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region Variables
private readonly DispatcherTimer _dispatcherTimerClose = new(DispatcherPriority.Render);
private readonly Stopwatch _stopwatch = new();
/// <summary>
/// Incremented whenever the close timer is started or stopped. The expired timer only hides the window if it is
/// still the same run (the user may have opened the window again while the timer waited before hiding it).
/// </summary>
private int _closeTimerRun;
private readonly MainWindow _mainWindow;
private readonly NetworkConnectionWidgetView _networkConnectionView;
public bool ShowTime
{
get;
set
{
if (value == field)
return;
field = value;
OnPropertyChanged();
}
}
public double TimeMax
{
get;
private set
{
if (value == field)
return;
field = value;
OnPropertyChanged();
}
}
public double Time
{
get;
set
{
// Same guard as NotificationWindow.TimeRemaining: ignore sub-0.001 changes so the
// countdown updates at the same cadence in both windows.
if (Math.Abs(value - field) < 0.001)
return;
field = value;
OnPropertyChanged();
}
}
#endregion
#region ICommands & Actions
public ICommand ShowMainWindowCommand => new RelayCommand(_ => ShowMainWindowAction());
private void ShowMainWindowAction()
{
HideWindow();
if (_mainWindow.ShowWindowCommand.CanExecute(null))
_mainWindow.ShowWindowCommand.Execute(null);
}
public ICommand CloseCommand => new RelayCommand(_ => CloseAction());
private void CloseAction()
{
HideWindow();
}
#endregion
#region Methods
private void Check()
{
_networkConnectionView.Check();
}
/// <summary>
/// Show the window on the screen.
/// </summary>
/// <param name="enableCloseTimer">Automatically close the window after a certain time.</param>
public void ShowWindow(bool enableCloseTimer = false)
{
// A network change while the user has the window open (no countdown): keep it open, only check again
if (enableCloseTimer && IsVisible && !ShowTime)
{
Check();
return;
}
// Position bottom-right on the primary screen. With SizeToContent="Height" the final
// height is only known after layout, so this is refined again in SizeChanged.
UpdatePosition();
// Opened on a network change: don't take the focus from the application the user is working in (the window
// closes itself, see the close timer). Clicking into it stops the close timer (Activated).
ShowActivated = !enableCloseTimer;
// Close the window after a certain time. Before Show(), so the countdown is part of the first layout (the
// window doesn't grow and move right after it was shown).
if (enableCloseTimer)
SetupCloseTimer();
// Show the window
Show();
// Check the network connection
Check();
if (enableCloseTimer)
return;
// Opened by the user, cancel a running close timer (e.g. if the window was opened on a network change)
StopCloseTimer();
// Focus the window
Activate();
}
/// <summary>
/// Anchors the window to the bottom-right corner of the primary screen. Uses
/// <see cref="FrameworkElement.ActualHeight"/> (not Height, which is NaN while
/// SizeToContent is active) so the window stays bottom-anchored as its height changes.
/// </summary>
private void UpdatePosition()
{
// ToDo: User setting...
if (Screen.PrimaryScreen == null)
return;
var scaleFactor = System.Windows.Media.VisualTreeHelper.GetDpi(this).DpiScaleX;
var workingArea = Screen.PrimaryScreen.WorkingArea;
// Not wider than the working area (e.g. 1280 x 720 with 150 % scaling is 853 px wide), otherwise the window
// would start outside the screen on the left
MaxWidth = Math.Max(0, workingArea.Width / scaleFactor - 20);
Left = Math.Max(workingArea.Left / scaleFactor + 10,
workingArea.Right / scaleFactor - Math.Min(Width, MaxWidth) - 10);
Top = workingArea.Bottom / scaleFactor - ActualHeight - 10;
}
private void SetupCloseTimer()
{
_closeTimerRun++;
TimeMax = SettingsManager.Current.Status_WindowCloseTime;
Time = TimeMax;
_stopwatch.Restart();
ShowTime = true;
_dispatcherTimerClose.Start();
}
/// <summary>
/// Stops the close timer (e.g. the window is opened by the user or hidden). A pending close of the expired timer
/// is canceled, see <see cref="_closeTimerRun" />.
/// </summary>
private void StopCloseTimer()
{
_closeTimerRun++;
_dispatcherTimerClose.Stop();
_stopwatch.Stop();
ShowTime = false;
}
/// <summary>
/// Hides the window and stops the close timer, so it doesn't keep running in the background.
/// </summary>
private void HideWindow()
{
StopCloseTimer();
Hide();
}
#endregion
#region Events
private void MetroWindow_Activated(object sender, EventArgs e)
{
// The user clicked into the window that was opened on a network change: keep it open (like opened by the user).
// ShowTime: the countdown is running or has just ended (the window is hidden after a short delay).
if (ShowTime)
StopCloseTimer();
}
private void MetroWindow_Deactivated(object sender, EventArgs e)
{
HideWindow();
}
private void MetroWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
HideWindow();
}
private async void DispatcherTimerTime_Tick(object sender, EventArgs e)
{
// Use a local for the close decision — the Time setter now ignores sub-0.001 changes, so
// reading the property back could stay stuck at a tiny positive value and never hide.
var remaining = Math.Max(0.0, TimeMax - _stopwatch.Elapsed.TotalSeconds);
Time = remaining;
if (remaining > 0)
return;
_dispatcherTimerClose.Stop();
_stopwatch.Stop();
var run = _closeTimerRun;
await Task.Delay(250);
// Opened again (by the user or another network change) in the meantime
if (run != _closeTimerRun)
return;
HideWindow();
}
#endregion
}