NetworkListView.xaml.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Timers;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using System.Windows.Threading;
  20. using System.ComponentModel;
  21. namespace WinUI
  22. {
  23. /// <summary>
  24. /// Interaction logic for MainWindow.xaml
  25. /// </summary>
  26. public partial class NetworkListView : Window
  27. {
  28. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  29. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  30. public NetworkListView()
  31. {
  32. InitializeComponent();
  33. Closed += onClosed;
  34. NetworkMonitor.Instance.SubscribeNetworkUpdates(updateNetworks);
  35. }
  36. ~NetworkListView()
  37. {
  38. }
  39. protected override void OnClosing(CancelEventArgs e)
  40. {
  41. e.Cancel = true;
  42. Hide();
  43. }
  44. private void onClosed(object sender, System.EventArgs e)
  45. {
  46. NetworkMonitor.Instance.UnsubscribeNetworkUpdates(updateNetworks);
  47. }
  48. private void updateNetworks(List<ZeroTierNetwork> networks)
  49. {
  50. if (networks != null)
  51. {
  52. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  53. {
  54. networksPage.setNetworks(networks);
  55. }));
  56. }
  57. }
  58. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  59. {
  60. e.Handled = !charRegex.IsMatch(e.Text);
  61. }
  62. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  63. {
  64. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  65. if (!isText) return;
  66. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  67. if (!wholeStringRegex.IsMatch(text))
  68. {
  69. e.CancelCommand();
  70. }
  71. }
  72. }
  73. }