forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusWindow.xaml.cs
More file actions
222 lines (168 loc) · 5.63 KB
/
Copy pathStatusWindow.xaml.cs
File metadata and controls
222 lines (168 loc) · 5.63 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
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();
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()
{
Hide();
if (_mainWindow.ShowWindowCommand.CanExecute(null))
_mainWindow.ShowWindowCommand.Execute(null);
}
public ICommand CloseCommand => new RelayCommand(_ => CloseAction());
private void CloseAction()
{
Hide();
}
#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)
{
// 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();
// Show the window
Show();
// Check the network connection
Check();
// Close the window after a certain time
if (enableCloseTimer)
{
SetupCloseTimer();
return;
}
// 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;
Left = Screen.PrimaryScreen.WorkingArea.Right / scaleFactor - Width - 10;
Top = Screen.PrimaryScreen.WorkingArea.Bottom / scaleFactor - ActualHeight - 10;
}
private void SetupCloseTimer()
{
TimeMax = SettingsManager.Current.Status_WindowCloseTime;
Time = TimeMax;
_stopwatch.Restart();
ShowTime = true;
_dispatcherTimerClose.Start();
}
#endregion
#region Events
private void MetroWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void MetroWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
Hide();
}
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();
await Task.Delay(250);
ShowTime = false;
Hide();
}
#endregion
}