MainWindow.xaml.cs 3.5 KB

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