MainWindow.xaml.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace WinUI
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. APIHandler handler = new APIHandler();
  24. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  25. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. updateStatus();
  30. updateNetworks();
  31. DataObject.AddPastingHandler(joinNetworkID, OnPaste);
  32. }
  33. private void updateStatus()
  34. {
  35. var status = handler.GetStatus();
  36. this.networkId.Content = status.Address;
  37. this.versionString.Content = status.Version;
  38. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  39. }
  40. private void updateNetworks()
  41. {
  42. var networks = handler.GetNetworks();
  43. networksPage.setNetworks(networks);
  44. }
  45. private void joinButton_Click(object sender, RoutedEventArgs e)
  46. {
  47. if (joinNetworkID.Text.Length < 16)
  48. {
  49. MessageBox.Show("Invalid Network ID");
  50. }
  51. else
  52. {
  53. handler.JoinNetwork(joinNetworkID.Text);
  54. }
  55. }
  56. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  57. {
  58. e.Handled = !charRegex.IsMatch(e.Text);
  59. }
  60. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  61. {
  62. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  63. if (!isText) return;
  64. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  65. if (!wholeStringRegex.IsMatch(text))
  66. {
  67. e.CancelCommand();
  68. }
  69. }
  70. }
  71. }