ToolbarItem.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using System.Text.RegularExpressions;
  18. using System.Timers;
  19. using System.Windows.Threading;
  20. using System.IO;
  21. using System.Diagnostics;
  22. namespace WinUI
  23. {
  24. /// <summary>
  25. /// Interaction logic for ToolbarItem.xaml
  26. /// </summary>
  27. public partial class ToolbarItem : Window, INotifyPropertyChanged
  28. {
  29. private APIHandler handler = APIHandler.Instance;
  30. private NetworkListView netListView = null;
  31. private JoinNetworkView joinNetView = null;
  32. private AboutView aboutView = null;
  33. private PreferencesView prefsView = null;
  34. private NetworkMonitor mon = NetworkMonitor.Instance;
  35. private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>();
  36. public ObservableCollection<MenuItem> NetworkCollection
  37. {
  38. get { return _networkCollection; }
  39. set { _networkCollection = value; }
  40. }
  41. public ToolbarItem()
  42. {
  43. InitializeComponent();
  44. mon.SubscribeNetworkUpdates(updateNetworks);
  45. mon.SubscribeStatusUpdates(updateStatus);
  46. }
  47. ~ToolbarItem()
  48. {
  49. mon.UnsubscribeNetworkUpdates(updateNetworks);
  50. mon.UnsubscribeStatusUpdates(updateStatus);
  51. }
  52. public event PropertyChangedEventHandler PropertyChanged;
  53. protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
  54. {
  55. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  56. }
  57. private void updateNetworks(List<ZeroTierNetwork> networks)
  58. {
  59. if (networks != null)
  60. {
  61. this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  62. {
  63. NetworkCollection.Clear();
  64. foreach (ZeroTierNetwork n in networks)
  65. {
  66. MenuItem item = new MenuItem();
  67. item.Header = n.Title;
  68. item.DataContext = n;
  69. item.IsChecked = n.IsConnected;
  70. item.Click += ToolbarItem_NetworkClicked;
  71. NetworkCollection.Add(item);
  72. }
  73. }));
  74. }
  75. }
  76. private void updateStatus(ZeroTierStatus status)
  77. {
  78. if (status != null)
  79. {
  80. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  81. {
  82. nodeIdMenuItem.Header = "Node ID: " + status.Address;
  83. nodeIdMenuItem.IsEnabled = true;
  84. }));
  85. }
  86. }
  87. private void ToolbarItem_TrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e)
  88. {
  89. Console.WriteLine("TrayContextMenuOpen");
  90. }
  91. private void ToolbarItem_PreviewTrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e)
  92. {
  93. Console.WriteLine("PreviewTrayContextMenuOpen");
  94. }
  95. private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e)
  96. {
  97. // TODO: Copy Node ID to clipboard
  98. }
  99. private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e)
  100. {
  101. if (netListView == null)
  102. {
  103. netListView = new WinUI.NetworkListView();
  104. netListView.Closed += ShowNetworksClosed;
  105. netListView.Show();
  106. }
  107. }
  108. private void ShowNetworksClosed(object sender, System.EventArgs e)
  109. {
  110. netListView = null;
  111. }
  112. private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
  113. {
  114. if (joinNetView == null)
  115. {
  116. joinNetView = new JoinNetworkView();
  117. joinNetView.Closed += JoinNetworkClosed;
  118. joinNetView.Show();
  119. }
  120. }
  121. private void JoinNetworkClosed(object sender, System.EventArgs e)
  122. {
  123. joinNetView = null;
  124. }
  125. private void ToolbarItem_AboutClicked(object sender, System.EventArgs e)
  126. {
  127. if (aboutView == null)
  128. {
  129. aboutView = new AboutView();
  130. aboutView.Closed += AboutClosed;
  131. aboutView.Show();
  132. }
  133. }
  134. private void AboutClosed(object sender, System.EventArgs e)
  135. {
  136. aboutView = null;
  137. }
  138. private void ToolbarItem_PreferencesClicked(object sender, System.EventArgs e)
  139. {
  140. if (prefsView == null)
  141. {
  142. prefsView = new PreferencesView();
  143. prefsView.Closed += PreferencesClosed;
  144. prefsView.Show();
  145. }
  146. }
  147. private void PreferencesClosed(object sender, System.EventArgs e)
  148. {
  149. prefsView = null;
  150. }
  151. private void ToolbarItem_QuitClicked(object sender, System.EventArgs e)
  152. {
  153. this.Close();
  154. Application.Current.Shutdown();
  155. Environment.Exit(0);
  156. }
  157. private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e)
  158. {
  159. if(sender.GetType() == typeof(MenuItem))
  160. {
  161. MenuItem item = e.Source as MenuItem;
  162. if (item.DataContext != null)
  163. {
  164. ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
  165. if (item.IsChecked)
  166. {
  167. APIHandler.Instance.LeaveNetwork(network.NetworkId);
  168. }
  169. else
  170. {
  171. APIHandler.Instance.JoinNetwork(network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }