ToolbarItem.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. using System.Text.RegularExpressions;
  19. using System.Timers;
  20. using System.Windows.Threading;
  21. using System.IO;
  22. using System.Diagnostics;
  23. using Microsoft.Win32;
  24. namespace WinUI
  25. {
  26. /// <summary>
  27. /// Interaction logic for ToolbarItem.xaml
  28. /// </summary>
  29. public partial class ToolbarItem : Window, INotifyPropertyChanged
  30. {
  31. private APIHandler handler = APIHandler.Instance;
  32. private Point netListLocation = new Point(0, 0);
  33. private Point joinNetLocation = new Point(0, 0);
  34. private Point aboutViewLocation = new Point(0, 0);
  35. private Point prefsViewLocation = new Point(0, 0);
  36. private NetworkListView netListView = new NetworkListView();
  37. private JoinNetworkView joinNetView = null;
  38. private AboutView aboutView = null;
  39. private PreferencesView prefsView = null;
  40. private NetworkMonitor mon = NetworkMonitor.Instance;
  41. private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>();
  42. private static Boolean shouldShowOnboardProcess = true;
  43. #if DEBUG
  44. private static bool isFirstRun = true;
  45. #endif
  46. public ObservableCollection<MenuItem> NetworkCollection
  47. {
  48. get { return _networkCollection; }
  49. set { _networkCollection = value; }
  50. }
  51. private string nodeId;
  52. public ToolbarItem()
  53. {
  54. InitializeComponent();
  55. mon.SubscribeNetworkUpdates(updateNetworks);
  56. mon.SubscribeStatusUpdates(updateStatus);
  57. SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
  58. }
  59. ~ToolbarItem()
  60. {
  61. mon.UnsubscribeNetworkUpdates(updateNetworks);
  62. mon.UnsubscribeStatusUpdates(updateStatus);
  63. }
  64. public event PropertyChangedEventHandler PropertyChanged;
  65. protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
  66. {
  67. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  68. }
  69. private void updateNetworks(List<ZeroTierNetwork> networks)
  70. {
  71. if (networks != null)
  72. {
  73. if (networks.Count > 0)
  74. {
  75. #if DEBUG
  76. if (isFirstRun)
  77. {
  78. shouldShowOnboardProcess = true;
  79. isFirstRun = false;
  80. }
  81. else
  82. {
  83. shouldShowOnboardProcess = false;
  84. }
  85. #else
  86. shouldShowOnboardProcess = false;
  87. #endif
  88. }
  89. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  90. {
  91. NetworkCollection.Clear();
  92. foreach (ZeroTierNetwork n in networks)
  93. {
  94. MenuItem item = new MenuItem();
  95. item.Header = n.Title.Replace("_", "__");
  96. item.DataContext = n;
  97. item.IsChecked = n.IsConnected;
  98. item.Click += ToolbarItem_NetworkClicked;
  99. NetworkCollection.Add(item);
  100. }
  101. }));
  102. if (shouldShowOnboardProcess)
  103. {
  104. // TODO: Show onboarding process window (on main thread
  105. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  106. {
  107. PageSwitcher ps = new PageSwitcher();
  108. ps.Show();
  109. }));
  110. shouldShowOnboardProcess = false;
  111. }
  112. }
  113. }
  114. private void updateStatus(ZeroTierStatus status)
  115. {
  116. if (status != null)
  117. {
  118. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  119. {
  120. nodeIdMenuItem.Header = "Node ID: " + status.Address;
  121. nodeIdMenuItem.IsEnabled = true;
  122. nodeId = status.Address;
  123. }));
  124. }
  125. }
  126. private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e)
  127. {
  128. try
  129. {
  130. Clipboard.SetDataObject(nodeId);
  131. }
  132. catch (ArgumentNullException)
  133. {
  134. // tried to copy a null nodeId
  135. Console.WriteLine("ArgumentNullException");
  136. }
  137. catch (Exception ex)
  138. {
  139. Console.WriteLine(ex.ToString());
  140. }
  141. }
  142. private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e)
  143. {
  144. if (netListView == null)
  145. {
  146. netListView = new WinUI.NetworkListView();
  147. netListView.Closed += ShowNetworksClosed;
  148. }
  149. bool netListNeedsMoving = true;
  150. if (netListLocation.X > 0 && netListLocation.Y > 0)
  151. {
  152. netListView.Left = netListLocation.X;
  153. netListView.Top = netListLocation.Y;
  154. netListNeedsMoving = false;
  155. }
  156. netListView.Show();
  157. if (netListNeedsMoving)
  158. {
  159. setWindowPosition(netListView);
  160. netListLocation.X = netListView.Left;
  161. netListLocation.Y = netListView.Top;
  162. }
  163. netListView.Activate();
  164. }
  165. private void ShowNetworksClosed(object sender, System.EventArgs e)
  166. {
  167. netListView = null;
  168. }
  169. private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
  170. {
  171. if (joinNetView == null)
  172. {
  173. joinNetView = new JoinNetworkView();
  174. joinNetView.Closed += JoinNetworkClosed;
  175. bool needsMove = true;
  176. if (joinNetLocation.X > 0 && joinNetLocation.Y > 0)
  177. {
  178. joinNetView.Left = joinNetLocation.X;
  179. joinNetView.Top = joinNetLocation.Y;
  180. needsMove = false;
  181. }
  182. joinNetView.Show();
  183. if (needsMove)
  184. {
  185. setWindowPosition(joinNetView);
  186. joinNetLocation.X = joinNetView.Left;
  187. joinNetLocation.Y = joinNetView.Top;
  188. }
  189. }
  190. else
  191. {
  192. joinNetView.Activate();
  193. }
  194. }
  195. private void JoinNetworkClosed(object sender, System.EventArgs e)
  196. {
  197. joinNetView = null;
  198. }
  199. private void ToolbarItem_CentralClicked(object sender, System.EventArgs e)
  200. {
  201. Process.Start("https://my.zerotier.com");
  202. }
  203. private void ToolbarItem_AboutClicked(object sender, System.EventArgs e)
  204. {
  205. if (aboutView == null)
  206. {
  207. aboutView = new AboutView();
  208. aboutView.Closed += AboutClosed;
  209. bool needsMove = true;
  210. if (aboutViewLocation.X > 0 && aboutViewLocation.Y > 0)
  211. {
  212. aboutView.Left = aboutViewLocation.X;
  213. aboutView.Top = aboutViewLocation.Y;
  214. needsMove = false;
  215. }
  216. aboutView.Show();
  217. if (needsMove)
  218. {
  219. setWindowPosition(aboutView);
  220. aboutViewLocation.X = aboutView.Left;
  221. aboutViewLocation.Y = aboutView.Top;
  222. }
  223. }
  224. else
  225. {
  226. aboutView.Activate();
  227. }
  228. }
  229. private void AboutClosed(object sender, System.EventArgs e)
  230. {
  231. aboutView = null;
  232. }
  233. private void ToolbarItem_PreferencesClicked(object sender, System.EventArgs e)
  234. {
  235. if (prefsView == null)
  236. {
  237. prefsView = new PreferencesView();
  238. prefsView.Closed += PreferencesClosed;
  239. bool needsMove = true;
  240. if (prefsViewLocation.X > 0 && prefsViewLocation.Y > 0)
  241. {
  242. prefsView.Left = prefsViewLocation.X;
  243. prefsView.Top = prefsViewLocation.Y;
  244. needsMove = false;
  245. }
  246. prefsView.Show();
  247. if (needsMove)
  248. {
  249. setWindowPosition(prefsView);
  250. prefsViewLocation.X = prefsView.Left;
  251. prefsViewLocation.Y = prefsView.Top;
  252. }
  253. }
  254. else
  255. {
  256. prefsView.Activate();
  257. }
  258. }
  259. private void PreferencesClosed(object sender, System.EventArgs e)
  260. {
  261. prefsView = null;
  262. }
  263. private void ToolbarItem_QuitClicked(object sender, System.EventArgs e)
  264. {
  265. NetworkMonitor.Instance.StopMonitor();
  266. Close();
  267. Application.Current.Shutdown();
  268. }
  269. private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e)
  270. {
  271. if(sender.GetType() == typeof(MenuItem))
  272. {
  273. MenuItem item = e.Source as MenuItem;
  274. if (item.DataContext != null)
  275. {
  276. ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
  277. if (item.IsChecked)
  278. {
  279. APIHandler.Instance.LeaveNetwork(Dispatcher, network.NetworkId);
  280. }
  281. else
  282. {
  283. APIHandler.Instance.JoinNetwork(Dispatcher, network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
  284. }
  285. }
  286. }
  287. }
  288. private void setWindowPosition(Window w)
  289. {
  290. double width = w.ActualWidth;
  291. double height = w.ActualHeight;
  292. double screenHeight = SystemParameters.PrimaryScreenHeight;
  293. double screenWidth = SystemParameters.PrimaryScreenWidth;
  294. double top = screenHeight - height - 40;
  295. double left = screenWidth - width - 20;
  296. w.Top = top;
  297. w.Left = left;
  298. }
  299. private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
  300. {
  301. // reset cached locations to (0, 0) when display size changes
  302. netListLocation.X = 0;
  303. netListLocation.Y = 0;
  304. joinNetLocation.X = 0;
  305. joinNetLocation.Y = 0;
  306. aboutViewLocation.X = 0;
  307. aboutViewLocation.Y = 0;
  308. prefsViewLocation.X = 0;
  309. prefsViewLocation.Y = 0;
  310. }
  311. }
  312. }