ToolbarItem.xaml.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. netListView.Activate();
  121. }
  122. private void ShowNetworksClosed(object sender, System.EventArgs e)
  123. {
  124. netListView = null;
  125. }
  126. private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
  127. {
  128. if (joinNetView == null)
  129. {
  130. joinNetView = new JoinNetworkView();
  131. joinNetView.Closed += JoinNetworkClosed;
  132. bool needsMove = true;
  133. if (joinNetLocation.X > 0 && joinNetLocation.Y > 0)
  134. {
  135. joinNetView.Left = joinNetLocation.X;
  136. joinNetView.Top = joinNetLocation.Y;
  137. needsMove = false;
  138. }
  139. joinNetView.Show();
  140. if (needsMove)
  141. {
  142. setWindowPosition(joinNetView);
  143. joinNetLocation.X = joinNetView.Left;
  144. joinNetLocation.Y = joinNetView.Top;
  145. }
  146. }
  147. else
  148. {
  149. joinNetView.Activate();
  150. }
  151. }
  152. private void JoinNetworkClosed(object sender, System.EventArgs e)
  153. {
  154. joinNetView = null;
  155. }
  156. private void ToolbarItem_AboutClicked(object sender, System.EventArgs e)
  157. {
  158. if (aboutView == null)
  159. {
  160. aboutView = new AboutView();
  161. aboutView.Closed += AboutClosed;
  162. bool needsMove = true;
  163. if (aboutViewLocation.X > 0 && aboutViewLocation.Y > 0)
  164. {
  165. aboutView.Left = aboutViewLocation.X;
  166. aboutView.Top = aboutViewLocation.Y;
  167. needsMove = false;
  168. }
  169. aboutView.Show();
  170. if (needsMove)
  171. {
  172. setWindowPosition(aboutView);
  173. aboutViewLocation.X = aboutView.Left;
  174. aboutViewLocation.Y = aboutView.Top;
  175. }
  176. }
  177. else
  178. {
  179. aboutView.Activate();
  180. }
  181. }
  182. private void AboutClosed(object sender, System.EventArgs e)
  183. {
  184. aboutView = null;
  185. }
  186. private void ToolbarItem_PreferencesClicked(object sender, System.EventArgs e)
  187. {
  188. if (prefsView == null)
  189. {
  190. prefsView = new PreferencesView();
  191. prefsView.Closed += PreferencesClosed;
  192. bool needsMove = true;
  193. if (prefsViewLocation.X > 0 && prefsViewLocation.Y > 0)
  194. {
  195. prefsView.Left = prefsViewLocation.X;
  196. prefsView.Top = prefsViewLocation.Y;
  197. needsMove = false;
  198. }
  199. prefsView.Show();
  200. if (needsMove)
  201. {
  202. setWindowPosition(prefsView);
  203. prefsViewLocation.X = prefsView.Left;
  204. prefsViewLocation.Y = prefsView.Top;
  205. }
  206. }
  207. else
  208. {
  209. prefsView.Activate();
  210. }
  211. }
  212. private void PreferencesClosed(object sender, System.EventArgs e)
  213. {
  214. prefsView = null;
  215. }
  216. private void ToolbarItem_QuitClicked(object sender, System.EventArgs e)
  217. {
  218. NetworkMonitor.Instance.StopMonitor();
  219. this.Close();
  220. Application.Current.Shutdown();
  221. }
  222. private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e)
  223. {
  224. if(sender.GetType() == typeof(MenuItem))
  225. {
  226. MenuItem item = e.Source as MenuItem;
  227. if (item.DataContext != null)
  228. {
  229. ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
  230. if (item.IsChecked)
  231. {
  232. APIHandler.Instance.LeaveNetwork(this.Dispatcher, network.NetworkId);
  233. }
  234. else
  235. {
  236. APIHandler.Instance.JoinNetwork(this.Dispatcher, network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
  237. }
  238. }
  239. }
  240. }
  241. private void setWindowPosition(Window w)
  242. {
  243. double width = w.ActualWidth;
  244. double height = w.ActualHeight;
  245. double screenHeight = SystemParameters.PrimaryScreenHeight;
  246. double screenWidth = SystemParameters.PrimaryScreenWidth;
  247. double top = screenHeight - height - 40;
  248. double left = screenWidth - width - 20;
  249. w.Top = top;
  250. w.Left = left;
  251. }
  252. private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
  253. {
  254. // reset cached locations to (0, 0) when display size changes
  255. netListLocation.X = 0;
  256. netListLocation.Y = 0;
  257. joinNetLocation.X = 0;
  258. joinNetLocation.Y = 0;
  259. aboutViewLocation.X = 0;
  260. aboutViewLocation.Y = 0;
  261. prefsViewLocation.X = 0;
  262. prefsViewLocation.Y = 0;
  263. }
  264. }
  265. }