MainWindow.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Timers;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Windows.Threading;
  18. namespace WinUI
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. APIHandler handler = new APIHandler();
  26. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  27. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  28. Timer timer = new Timer();
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. updateStatus();
  33. updateNetworks();
  34. DataObject.AddPastingHandler(joinNetworkID, OnPaste);
  35. timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer);
  36. timer.Interval = 2000;
  37. timer.Enabled = true;
  38. }
  39. private void updateStatus()
  40. {
  41. var status = handler.GetStatus();
  42. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  43. {
  44. this.networkId.Content = status.Address;
  45. }));
  46. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  47. {
  48. this.versionString.Content = status.Version;
  49. }));
  50. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  51. {
  52. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  53. }));
  54. }
  55. private void updateNetworks()
  56. {
  57. var networks = handler.GetNetworks();
  58. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  59. {
  60. networksPage.setNetworks(networks);
  61. }));
  62. }
  63. private void updatePeers()
  64. {
  65. var peers = handler.GetPeers();
  66. peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  67. {
  68. peersPage.SetPeers(peers);
  69. }));
  70. }
  71. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  72. {
  73. updateStatus();
  74. updateNetworks();
  75. updatePeers();
  76. }
  77. private void joinButton_Click(object sender, RoutedEventArgs e)
  78. {
  79. if (joinNetworkID.Text.Length < 16)
  80. {
  81. MessageBox.Show("Invalid Network ID");
  82. }
  83. else
  84. {
  85. handler.JoinNetwork(joinNetworkID.Text);
  86. }
  87. }
  88. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  89. {
  90. e.Handled = !charRegex.IsMatch(e.Text);
  91. }
  92. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  93. {
  94. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  95. if (!isText) return;
  96. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  97. if (!wholeStringRegex.IsMatch(text))
  98. {
  99. e.CancelCommand();
  100. }
  101. }
  102. }
  103. }