ToolbarItem.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 Point netListLocation = new Point(0, 0);
  31. private Point joinNetLocation = new Point(0, 0);
  32. private Point aboutViewLocation = new Point(0, 0);
  33. private Point prefsViewLocation = new Point(0, 0);
  34. private NetworkListView netListView = new NetworkListView();
  35. private JoinNetworkView joinNetView = null;
  36. private AboutView aboutView = null;
  37. private PreferencesView prefsView = null;
  38. private NetworkMonitor mon = NetworkMonitor.Instance;
  39. private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>();
  40. public ObservableCollection<MenuItem> NetworkCollection
  41. {
  42. get { return _networkCollection; }
  43. set { _networkCollection = value; }
  44. }
  45. private string nodeId;
  46. public ToolbarItem()
  47. {
  48. InitializeComponent();
  49. mon.SubscribeNetworkUpdates(updateNetworks);
  50. mon.SubscribeStatusUpdates(updateStatus);
  51. }
  52. ~ToolbarItem()
  53. {
  54. mon.UnsubscribeNetworkUpdates(updateNetworks);
  55. mon.UnsubscribeStatusUpdates(updateStatus);
  56. }
  57. public event PropertyChangedEventHandler PropertyChanged;
  58. protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
  59. {
  60. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  61. }
  62. private void updateNetworks(List<ZeroTierNetwork> networks)
  63. {
  64. if (networks != null)
  65. {
  66. this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  67. {
  68. NetworkCollection.Clear();
  69. foreach (ZeroTierNetwork n in networks)
  70. {
  71. MenuItem item = new MenuItem();
  72. item.Header = n.Title;
  73. item.DataContext = n;
  74. item.IsChecked = n.IsConnected;
  75. item.Click += ToolbarItem_NetworkClicked;
  76. NetworkCollection.Add(item);
  77. }
  78. }));
  79. }
  80. }
  81. private void updateStatus(ZeroTierStatus status)
  82. {
  83. if (status != null)
  84. {
  85. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  86. {
  87. nodeIdMenuItem.Header = "Node ID: " + status.Address;
  88. nodeIdMenuItem.IsEnabled = true;
  89. nodeId = status.Address;
  90. }));
  91. }
  92. }
  93. private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e)
  94. {
  95. Clipboard.SetText(nodeId);
  96. }
  97. private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e)
  98. {
  99. if (netListView == null)
  100. {
  101. netListView = new WinUI.NetworkListView();
  102. netListView.Closed += ShowNetworksClosed;
  103. }
  104. bool netListNeedsMoving = true;
  105. if (netListLocation.X > 0 && netListLocation.Y > 0)
  106. {
  107. netListView.Left = netListLocation.X;
  108. netListView.Top = netListLocation.Y;
  109. netListNeedsMoving = false;
  110. }
  111. netListView.Show();
  112. if (netListNeedsMoving)
  113. {
  114. setWindowPosition(netListView);
  115. netListLocation.X = netListView.Left;
  116. netListLocation.Y = netListView.Top;
  117. }
  118. }
  119. private void ShowNetworksClosed(object sender, System.EventArgs e)
  120. {
  121. netListView = null;
  122. }
  123. private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
  124. {
  125. if (joinNetView == null)
  126. {
  127. joinNetView = new JoinNetworkView();
  128. joinNetView.Closed += JoinNetworkClosed;
  129. bool needsMove = true;
  130. if (joinNetLocation.X > 0 && joinNetLocation.Y > 0)
  131. {
  132. joinNetView.Left = joinNetLocation.X;
  133. joinNetView.Top = joinNetLocation.Y;
  134. needsMove = false;
  135. }
  136. joinNetView.Show();
  137. if (needsMove)
  138. {
  139. setWindowPosition(joinNetView);
  140. joinNetLocation.X = joinNetView.Left;
  141. joinNetLocation.Y = joinNetView.Top;
  142. }
  143. }
  144. }
  145. private void JoinNetworkClosed(object sender, System.EventArgs e)
  146. {
  147. joinNetView = null;
  148. }
  149. private void ToolbarItem_AboutClicked(object sender, System.EventArgs e)
  150. {
  151. if (aboutView == null)
  152. {
  153. aboutView = new AboutView();
  154. aboutView.Closed += AboutClosed;
  155. bool needsMove = true;
  156. if (aboutViewLocation.X > 0 && aboutViewLocation.Y > 0)
  157. {
  158. aboutView.Left = aboutViewLocation.X;
  159. aboutView.Top = aboutViewLocation.Y;
  160. needsMove = false;
  161. }
  162. aboutView.Show();
  163. if (needsMove)
  164. {
  165. setWindowPosition(aboutView);
  166. aboutViewLocation.X = aboutView.Left;
  167. aboutViewLocation.Y = aboutView.Top;
  168. }
  169. }
  170. }
  171. private void AboutClosed(object sender, System.EventArgs e)
  172. {
  173. aboutView = null;
  174. }
  175. private void ToolbarItem_PreferencesClicked(object sender, System.EventArgs e)
  176. {
  177. if (prefsView == null)
  178. {
  179. prefsView = new PreferencesView();
  180. prefsView.Closed += PreferencesClosed;
  181. bool needsMove = true;
  182. if (prefsViewLocation.X > 0 && prefsViewLocation.Y > 0)
  183. {
  184. prefsView.Left = prefsViewLocation.X;
  185. prefsView.Top = prefsViewLocation.Y;
  186. needsMove = false;
  187. }
  188. prefsView.Show();
  189. if (needsMove)
  190. {
  191. setWindowPosition(prefsView);
  192. prefsViewLocation.X = prefsView.Left;
  193. prefsViewLocation.Y = prefsView.Top;
  194. }
  195. }
  196. }
  197. private void PreferencesClosed(object sender, System.EventArgs e)
  198. {
  199. prefsView = null;
  200. }
  201. private void ToolbarItem_QuitClicked(object sender, System.EventArgs e)
  202. {
  203. NetworkMonitor.Instance.StopMonitor();
  204. this.Close();
  205. Application.Current.Shutdown();
  206. }
  207. private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e)
  208. {
  209. if(sender.GetType() == typeof(MenuItem))
  210. {
  211. MenuItem item = e.Source as MenuItem;
  212. if (item.DataContext != null)
  213. {
  214. ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
  215. if (item.IsChecked)
  216. {
  217. APIHandler.Instance.LeaveNetwork(network.NetworkId);
  218. }
  219. else
  220. {
  221. APIHandler.Instance.JoinNetwork(network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
  222. }
  223. }
  224. }
  225. }
  226. private void setWindowPosition(Window w)
  227. {
  228. double width = w.ActualWidth;
  229. double height = w.ActualHeight;
  230. double screenHeight = SystemParameters.PrimaryScreenHeight;
  231. double screenWidth = SystemParameters.PrimaryScreenWidth;
  232. double top = screenHeight - height - 40;
  233. double left = screenWidth - width - 20;
  234. w.Top = top;
  235. w.Left = left;
  236. }
  237. }
  238. }