ToolbarItem.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  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.Shapes;
  15. using System.Text.RegularExpressions;
  16. using System.Timers;
  17. using System.Windows.Threading;
  18. using System.IO;
  19. using System.Diagnostics;
  20. namespace WinUI
  21. {
  22. /// <summary>
  23. /// Interaction logic for ToolbarItem.xaml
  24. /// </summary>
  25. public partial class ToolbarItem : Window
  26. {
  27. private APIHandler handler = APIHandler.Instance;
  28. private NetworkListView netListView = null;
  29. private List<ZeroTierNetwork> networkList = null;
  30. private NetworkMonitor mon = NetworkMonitor.Instance;
  31. private ObservableCollection<ZeroTierNetwork> _networkCollection = new ObservableCollection<ZeroTierNetwork>();
  32. public ObservableCollection<ZeroTierNetwork> NetworkCollection
  33. {
  34. get { return _networkCollection; }
  35. set { _networkCollection = value; }
  36. }
  37. private Timer timer = null;
  38. public ToolbarItem()
  39. {
  40. InitializeComponent();
  41. mon.SubscribeNetworkUpdates(updateNetworks);
  42. mon.SubscribeStatusUpdates(updateStatus);
  43. }
  44. ~ToolbarItem()
  45. {
  46. mon.UnsubscribeNetworkUpdates(updateNetworks);
  47. mon.UnsubscribeStatusUpdates(updateStatus);
  48. }
  49. private void updateNetworks(List<ZeroTierNetwork> networks)
  50. {
  51. if (networks != null)
  52. {
  53. this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  54. {
  55. foreach (ZeroTierNetwork n in networks)
  56. {
  57. int index = _networkCollection.IndexOf(n);
  58. if (index == -1)
  59. {
  60. _networkCollection.Add(n);
  61. }
  62. else
  63. {
  64. _networkCollection[index] = n;
  65. }
  66. }
  67. this.networkList = networks;
  68. }));
  69. }
  70. }
  71. private void updateStatus(ZeroTierStatus status)
  72. {
  73. if (status != null)
  74. {
  75. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  76. {
  77. nodeIdMenuItem.Header = "Node ID: " + status.Address;
  78. nodeIdMenuItem.IsEnabled = true;
  79. }));
  80. }
  81. }
  82. private void ToolbarItem_TrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e)
  83. {
  84. Console.WriteLine("TrayContextMenuOpen");
  85. }
  86. private void ToolbarItem_PreviewTrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e)
  87. {
  88. Console.WriteLine("PreviewTrayContextMenuOpen");
  89. }
  90. private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e)
  91. {
  92. }
  93. private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e)
  94. {
  95. if (netListView == null)
  96. {
  97. netListView = new WinUI.NetworkListView();
  98. netListView.Closed += ShowNetworksClosed;
  99. netListView.Show();
  100. }
  101. }
  102. private void ShowNetworksClosed(object sender, System.EventArgs e)
  103. {
  104. netListView = null;
  105. }
  106. private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
  107. {
  108. }
  109. private void JoinNetworkClosed(object sender, System.EventArgs e)
  110. {
  111. }
  112. }
  113. }