MainWindow.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. updatePeers();
  35. DataObject.AddPastingHandler(joinNetworkID, OnPaste);
  36. timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer);
  37. timer.Interval = 2000;
  38. timer.Enabled = true;
  39. }
  40. private void updateStatus()
  41. {
  42. var status = handler.GetStatus();
  43. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  44. {
  45. this.networkId.Content = status.Address;
  46. }));
  47. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  48. {
  49. this.versionString.Content = status.Version;
  50. }));
  51. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  52. {
  53. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  54. }));
  55. }
  56. private void updateNetworks()
  57. {
  58. var networks = handler.GetNetworks();
  59. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  60. {
  61. networksPage.setNetworks(networks);
  62. }));
  63. }
  64. private void updatePeers()
  65. {
  66. var peers = handler.GetPeers();
  67. peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  68. {
  69. peersPage.SetPeers(peers);
  70. }));
  71. }
  72. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  73. {
  74. updateStatus();
  75. updateNetworks();
  76. updatePeers();
  77. }
  78. private void joinButton_Click(object sender, RoutedEventArgs e)
  79. {
  80. if (joinNetworkID.Text.Length < 16)
  81. {
  82. MessageBox.Show("Invalid Network ID");
  83. }
  84. else
  85. {
  86. handler.JoinNetwork(joinNetworkID.Text);
  87. }
  88. }
  89. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  90. {
  91. e.Handled = !charRegex.IsMatch(e.Text);
  92. }
  93. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  94. {
  95. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  96. if (!isText) return;
  97. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  98. if (!wholeStringRegex.IsMatch(text))
  99. {
  100. e.CancelCommand();
  101. }
  102. }
  103. }
  104. }