-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.axaml.cs
More file actions
510 lines (445 loc) · 17.4 KB
/
Copy pathMainWindow.axaml.cs
File metadata and controls
510 lines (445 loc) · 17.4 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using IpWidget.Services;
namespace IpWidget;
public partial class MainWindow : Window
{
private readonly IpService _service = new();
private readonly GeoService _geo = new();
private readonly FlagService _flags = new();
private readonly Settings _settings = Settings.Load();
private readonly Dictionary<string, SourceRow> _rows = new();
private CancellationTokenSource? _inflight;
private DispatcherTimer? _autoTimer;
private bool _busy;
private bool _applying;
private string? _lastCountryCode;
/// <summary>Set by App; performs a real application shutdown (window lives in tray).</summary>
public Action? RequestQuit { get; set; }
// full view
private TextBlock _ipText = null!, _statusText = null!, _checkLabel = null!;
private ItemsControl _sourcesList = null!;
private Button _checkBtn = null!;
private PathIcon _pinIcon = null!;
private StackPanel _geoPanel = null!;
private Image _flagImg = null!;
private TextBlock _geoText = null!;
private Border _vpnChip = null!;
private PathIcon _vpnIcon = null!;
private TextBlock _vpnText = null!;
// hud view
private Grid _hudView = null!;
private Border _fullCard = null!;
private Border _settingsOverlay = null!;
private Image _hudFlag = null!;
private TextBlock _hudIp = null!, _hudCountry = null!;
private StackPanel _hudControls = null!;
// settings controls
private RadioButton _closeTrayRadio = null!, _closeQuitRadio = null!;
private CheckBox _topmostChk = null!, _flagChk = null!, _compactChk = null!;
private ToggleButton _autoBtn = null!;
private static readonly IBrush Muted = new SolidColorBrush(Color.Parse("#B9C7EC"));
private static readonly IBrush Accent = new SolidColorBrush(Color.Parse("#4FD1FF"));
public MainWindow()
{
InitializeComponent();
_ipText = this.FindControl<TextBlock>("IpText")!;
_statusText = this.FindControl<TextBlock>("StatusText")!;
_checkLabel = this.FindControl<TextBlock>("CheckLabel")!;
_sourcesList = this.FindControl<ItemsControl>("SourcesList")!;
_checkBtn = this.FindControl<Button>("CheckBtn")!;
_pinIcon = this.FindControl<PathIcon>("PinIcon")!;
_geoPanel = this.FindControl<StackPanel>("GeoPanel")!;
_flagImg = this.FindControl<Image>("FlagImg")!;
_geoText = this.FindControl<TextBlock>("GeoText")!;
_vpnChip = this.FindControl<Border>("VpnChip")!;
_vpnIcon = this.FindControl<PathIcon>("VpnIcon")!;
_vpnText = this.FindControl<TextBlock>("VpnText")!;
_hudView = this.FindControl<Grid>("HudView")!;
_fullCard = this.FindControl<Border>("FullCard")!;
_settingsOverlay = this.FindControl<Border>("SettingsOverlay")!;
_hudFlag = this.FindControl<Image>("HudFlag")!;
_hudIp = this.FindControl<TextBlock>("HudIp")!;
_hudCountry = this.FindControl<TextBlock>("HudCountry")!;
_hudControls = this.FindControl<StackPanel>("HudControls")!;
_closeTrayRadio = this.FindControl<RadioButton>("CloseTrayRadio")!;
_closeQuitRadio = this.FindControl<RadioButton>("CloseQuitRadio")!;
_topmostChk = this.FindControl<CheckBox>("TopmostChk")!;
_flagChk = this.FindControl<CheckBox>("FlagChk")!;
_compactChk = this.FindControl<CheckBox>("CompactChk")!;
// ---- full view wiring ----
// drag the whole card / settings overlay; buttons consume their own press so they won't drag
_fullCard.PointerPressed += OnDrag;
_settingsOverlay.PointerPressed += OnDrag;
this.FindControl<Button>("SettingsBtn")!.Click += (_, _) => ShowSettings(true);
this.FindControl<Button>("CompactBtn")!.Click += (_, _) => SetCompact(true);
this.FindControl<Button>("PinBtn")!.Click += OnPin;
this.FindControl<Button>("MinBtn")!.Click += (_, _) => Hide();
this.FindControl<Button>("CloseBtn")!.Click += (_, _) => OnCloseRequested();
_checkBtn.Click += async (_, _) => await CheckAsync();
this.FindControl<Button>("CopyBtn")!.Click += OnCopy;
_autoBtn = this.FindControl<ToggleButton>("AutoBtn")!;
_autoBtn.IsCheckedChanged += OnAutoToggled;
// ---- hud view wiring ----
_hudView.PointerPressed += OnDrag;
_hudView.PointerEntered += (_, _) => _hudControls.Opacity = 1;
_hudView.PointerExited += (_, _) => _hudControls.Opacity = 0;
this.FindControl<Button>("HudRefreshBtn")!.Click += async (_, _) => await CheckAsync();
this.FindControl<Button>("HudExpandBtn")!.Click += (_, _) => SetCompact(false);
this.FindControl<Button>("HudCloseBtn")!.Click += (_, _) => OnCloseRequested();
// ---- settings wiring ----
this.FindControl<Button>("SettingsBackBtn")!.Click += (_, _) => ShowSettings(false);
_closeTrayRadio.IsCheckedChanged += OnSettingChanged;
_closeQuitRadio.IsCheckedChanged += OnSettingChanged;
_topmostChk.IsCheckedChanged += OnSettingChanged;
_flagChk.IsCheckedChanged += OnSettingChanged;
_compactChk.IsCheckedChanged += OnCompactCheckChanged;
ApplySettingsToUi();
BuildRows();
Opened += OnOpened;
}
private void InitializeComponent() => AvaloniaXamlLoader.Load(this);
private async void OnOpened(object? sender, EventArgs e)
{
if (_settings.X is { } x && _settings.Y is { } y)
Position = new PixelPoint(x, y);
Topmost = _settings.Topmost;
_pinIcon.Foreground = Topmost ? Accent : Muted;
SetCompact(_settings.Compact, save: false);
_autoBtn.IsChecked = _settings.AutoRefresh; // restores timer via OnAutoToggled
await CheckAsync();
}
// ---------- tray hooks ----------
public void ShowFromTray()
{
Show();
WindowState = WindowState.Normal;
Activate();
}
public void TriggerCheck() => _ = CheckAsync();
// ---------- mode / settings ----------
private void SetCompact(bool compact, bool save = true)
{
_settings.Compact = compact;
_settingsOverlay.IsVisible = false;
_fullCard.IsVisible = !compact;
_hudView.IsVisible = compact;
if (compact)
{
TransparencyLevelHint = new[] { WindowTransparencyLevel.Transparent };
Width = 250;
Height = 128;
}
else
{
TransparencyLevelHint = new[]
{
WindowTransparencyLevel.AcrylicBlur,
WindowTransparencyLevel.Blur,
WindowTransparencyLevel.Transparent,
};
Width = 340;
Height = 452;
}
if (_compactChk.IsChecked != compact) _compactChk.IsChecked = compact;
if (save) Save();
}
private void ShowSettings(bool show)
{
if (show) ApplySettingsToUi();
_settingsOverlay.IsVisible = show;
}
private void ApplySettingsToUi()
{
_applying = true;
_closeTrayRadio.IsChecked = _settings.CloseToTray;
_closeQuitRadio.IsChecked = !_settings.CloseToTray;
_topmostChk.IsChecked = _settings.Topmost;
_flagChk.IsChecked = _settings.ShowFlag;
_compactChk.IsChecked = _settings.Compact;
_applying = false;
}
private void OnSettingChanged(object? sender, RoutedEventArgs e)
{
if (_applying) return;
_settings.CloseToTray = _closeTrayRadio.IsChecked == true;
_settings.Topmost = _topmostChk.IsChecked == true;
_settings.ShowFlag = _flagChk.IsChecked == true;
Topmost = _settings.Topmost;
_pinIcon.Foreground = Topmost ? Accent : Muted;
if (!_settings.ShowFlag)
{
_flagImg.IsVisible = false;
_hudFlag.IsVisible = false;
}
else if (_lastCountryCode is not null)
{
_ = LoadFlagAsync(_lastCountryCode);
}
Save();
}
private void OnCompactCheckChanged(object? sender, RoutedEventArgs e)
{
if (_applying) return;
if (_compactChk.IsChecked is { } want && want != _settings.Compact)
SetCompact(want);
}
private void OnCloseRequested()
{
if (_settings.CloseToTray) Hide();
else (RequestQuit ?? Close)();
}
private void Save()
{
var p = Position;
_settings.X = p.X;
_settings.Y = p.Y;
_settings.Save();
}
// ---------- checking ----------
private void BuildRows()
{
var panel = new List<Control>();
foreach (var r in _service.CreatePending())
{
var row = new SourceRow(r.Name);
_rows[r.Name] = row;
panel.Add(row.Root);
}
_sourcesList.ItemsSource = panel;
}
private async Task CheckAsync()
{
if (_busy) return;
_busy = true;
_checkBtn.IsEnabled = false;
_checkLabel.Text = "Проверяю…";
_statusText.Text = "опрашиваю источники…";
_geoPanel.IsVisible = false;
_vpnChip.IsVisible = false;
_inflight?.Cancel();
_inflight = new CancellationTokenSource();
var ct = _inflight.Token;
var results = _service.CreatePending();
foreach (var r in results)
_rows[r.Name].SetPending();
try
{
var consensus = await _service.CheckAllAsync(results, r =>
Dispatcher.UIThread.Post(() => _rows[r.Name].Update(r)), ct);
if (ct.IsCancellationRequested) return;
int ok = 0;
foreach (var r in results)
if (r.State == SourceState.Ok) ok++;
if (consensus is not null)
{
_ipText.Text = consensus;
_hudIp.Text = consensus;
_statusText.Text = $"подтверждено {ok}/{results.Count} источниками";
await LoadGeoAsync(consensus, ct);
}
else
{
_ipText.Text = "—";
_hudIp.Text = "нет сети";
_statusText.Text = "не удалось определить IP (нет сети?)";
}
}
finally
{
_busy = false;
_checkBtn.IsEnabled = true;
_checkLabel.Text = "Проверить";
}
}
private async Task LoadGeoAsync(string ip, CancellationToken ct)
{
var info = await _geo.LookupAsync(ip, ct);
if (ct.IsCancellationRequested || info is null) return;
_geoText.Text = string.IsNullOrWhiteSpace(info.Location)
? info.Provider
: $"{info.Location} · {info.Provider}";
_geoPanel.IsVisible = true;
_hudCountry.Text = info.Location;
_lastCountryCode = info.CountryCode;
if (_settings.ShowFlag && info.CountryCode is not null)
await LoadFlagAsync(info.CountryCode, ct);
if (info.IsHosting)
{
_vpnChip.Background = new SolidColorBrush(Color.Parse("#33FFB020"));
_vpnChip.BorderThickness = new Thickness(1);
_vpnChip.BorderBrush = new SolidColorBrush(Color.Parse("#66FFB020"));
_vpnIcon.Data = (Geometry)this.FindResource("IconShieldAlert")!;
_vpnIcon.Foreground = new SolidColorBrush(Color.Parse("#FFC85A"));
_vpnText.Foreground = new SolidColorBrush(Color.Parse("#FFD98A"));
_vpnText.Text = "Похоже на VPN / хостинг";
}
else
{
_vpnChip.Background = new SolidColorBrush(Color.Parse("#2633E38B"));
_vpnChip.BorderThickness = new Thickness(1);
_vpnChip.BorderBrush = new SolidColorBrush(Color.Parse("#5533E38B"));
_vpnIcon.Data = (Geometry)this.FindResource("IconShieldCheck")!;
_vpnIcon.Foreground = new SolidColorBrush(Color.Parse("#3BE38B"));
_vpnText.Foreground = new SolidColorBrush(Color.Parse("#8CF0BE"));
_vpnText.Text = "Резидентский IP";
}
_vpnChip.IsVisible = true;
}
private async Task LoadFlagAsync(string cc, CancellationToken ct = default)
{
Bitmap? bmp = await _flags.GetAsync(cc, ct);
if (ct.IsCancellationRequested) return;
if (bmp is null)
{
_flagImg.IsVisible = false;
_hudFlag.IsVisible = false;
return;
}
_flagImg.Source = bmp;
_flagImg.IsVisible = true;
_hudFlag.Source = bmp;
_hudFlag.IsVisible = _settings.ShowFlag;
}
// ---------- misc handlers ----------
private void OnDrag(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
BeginMoveDrag(e);
}
private void OnPin(object? sender, RoutedEventArgs e)
{
Topmost = !Topmost;
_settings.Topmost = Topmost;
_pinIcon.Foreground = Topmost ? Accent : Muted;
Save();
}
private async void OnCopy(object? sender, RoutedEventArgs e)
{
var ip = _ipText.Text;
if (string.IsNullOrWhiteSpace(ip) || ip == "—") return;
if (Clipboard is { } clip)
{
await clip.SetTextAsync(ip);
_statusText.Text = $"скопировано: {ip}";
}
}
private void OnAutoToggled(object? sender, RoutedEventArgs e)
{
var on = _autoBtn.IsChecked == true;
if (on)
{
_autoTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(30) };
_autoTimer.Tick += async (_, _) => await CheckAsync();
_autoTimer.Start();
}
else
{
_autoTimer?.Stop();
_autoTimer = null;
}
if (!_applying && _settings.AutoRefresh != on)
{
_settings.AutoRefresh = on;
Save();
}
}
protected override void OnClosed(EventArgs e)
{
Save();
_inflight?.Cancel();
_autoTimer?.Stop();
_service.Dispose();
_geo.Dispose();
_flags.Dispose();
base.OnClosed(e);
}
}
/// <summary>One row per source: status dot + name + ip + latency badge.</summary>
internal sealed class SourceRow
{
public Border Root { get; }
private readonly Ellipse _dot;
private readonly TextBlock _ip;
private readonly TextBlock _latency;
private static readonly IBrush Pending = new SolidColorBrush(Color.Parse("#5A6488"));
private static readonly IBrush Ok = new SolidColorBrush(Color.Parse("#3BE38B"));
private static readonly IBrush Fail = new SolidColorBrush(Color.Parse("#FF6B8B"));
private static readonly IBrush IpMuted = new SolidColorBrush(Color.Parse("#8FA0C8"));
private static readonly IBrush IpOk = new SolidColorBrush(Color.Parse("#EAF2FF"));
private static readonly IBrush IpFail = new SolidColorBrush(Color.Parse("#FF9DB3"));
public SourceRow(string name)
{
_dot = new Ellipse { Width = 9, Height = 9, Fill = Pending, VerticalAlignment = VerticalAlignment.Center };
_ip = new TextBlock { Text = "…", Foreground = IpMuted, FontSize = 12, VerticalAlignment = VerticalAlignment.Center, TextTrimming = TextTrimming.CharacterEllipsis };
_latency = new TextBlock { Text = "", Foreground = new SolidColorBrush(Color.Parse("#6E7AA5")), FontSize = 11, VerticalAlignment = VerticalAlignment.Center };
var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("Auto,Auto,*,Auto") };
grid.Children.Add(_dot);
Grid.SetColumn(_dot, 0);
var nameBlock = new TextBlock
{
Text = name,
Foreground = new SolidColorBrush(Color.Parse("#CFE3FF")),
FontSize = 12,
FontWeight = FontWeight.SemiBold,
Margin = new Thickness(10, 0, 0, 0),
VerticalAlignment = VerticalAlignment.Center,
Width = 78,
};
grid.Children.Add(nameBlock);
Grid.SetColumn(nameBlock, 1);
_ip.Margin = new Thickness(6, 0, 0, 0);
grid.Children.Add(_ip);
Grid.SetColumn(_ip, 2);
grid.Children.Add(_latency);
Grid.SetColumn(_latency, 3);
Root = new Border
{
Background = new SolidColorBrush(Color.Parse("#12FFFFFF")),
CornerRadius = new CornerRadius(10),
Padding = new Thickness(12, 9),
Child = grid,
};
}
public void SetPending()
{
_dot.Fill = Pending;
_ip.Text = "…";
_ip.Foreground = IpMuted;
_latency.Text = "";
}
public void Update(IpSourceResult r)
{
switch (r.State)
{
case SourceState.Ok:
_dot.Fill = Ok;
_ip.Text = r.Ip;
_ip.Foreground = IpOk;
_latency.Text = $"{r.ElapsedMs} ms";
break;
case SourceState.Failed:
_dot.Fill = Fail;
_ip.Text = r.Error ?? "ошибка";
_ip.Foreground = IpFail;
_latency.Text = r.ElapsedMs > 0 ? $"{r.ElapsedMs} ms" : "";
break;
default:
SetPending();
break;
}
}
}