MainWindow.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }
  66. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  67. {
  68. updateStatus();
  69. updateNetworks();
  70. updatePeers();
  71. }
  72. private void joinButton_Click(object sender, RoutedEventArgs e)
  73. {
  74. if (joinNetworkID.Text.Length < 16)
  75. {
  76. MessageBox.Show("Invalid Network ID");
  77. }
  78. else
  79. {
  80. handler.JoinNetwork(joinNetworkID.Text);
  81. }
  82. }
  83. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  84. {
  85. e.Handled = !charRegex.IsMatch(e.Text);
  86. }
  87. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  88. {
  89. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  90. if (!isText) return;
  91. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  92. if (!wholeStringRegex.IsMatch(text))
  93. {
  94. e.CancelCommand();
  95. }
  96. }
  97. }
  98. }