ToolbarItem.xaml.cs 9.4 KB

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