diff options
Diffstat (limited to 'windows/WinUI/ToolbarItem.xaml.cs')
-rw-r--r-- | windows/WinUI/ToolbarItem.xaml.cs | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/windows/WinUI/ToolbarItem.xaml.cs b/windows/WinUI/ToolbarItem.xaml.cs new file mode 100644 index 00000000..b7ac793f --- /dev/null +++ b/windows/WinUI/ToolbarItem.xaml.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; +using System.Text.RegularExpressions; +using System.Timers; +using System.Windows.Threading; +using System.IO; +using System.Diagnostics; + +namespace WinUI +{ + /// <summary> + /// Interaction logic for ToolbarItem.xaml + /// </summary> + public partial class ToolbarItem : Window, INotifyPropertyChanged + { + private APIHandler handler = APIHandler.Instance; + + private NetworkListView netListView = null; + private JoinNetworkView joinNetView = null; + + private NetworkMonitor mon = NetworkMonitor.Instance; + + private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>(); + + public ObservableCollection<MenuItem> NetworkCollection + { + get { return _networkCollection; } + set { _networkCollection = value; } + } + + public ToolbarItem() + { + InitializeComponent(); + + mon.SubscribeNetworkUpdates(updateNetworks); + mon.SubscribeStatusUpdates(updateStatus); + } + + ~ToolbarItem() + { + mon.UnsubscribeNetworkUpdates(updateNetworks); + mon.UnsubscribeStatusUpdates(updateStatus); + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + private void updateNetworks(List<ZeroTierNetwork> networks) + { + if (networks != null) + { + this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + NetworkCollection.Clear(); + foreach (ZeroTierNetwork n in networks) + { + MenuItem item = new MenuItem(); + item.Header = n.Title; + item.DataContext = n; + item.IsChecked = n.IsConnected; + item.Click += ToolbarItem_NetworkClicked; + + NetworkCollection.Add(item); + } + })); + } + } + + private void updateStatus(ZeroTierStatus status) + { + if (status != null) + { + Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + nodeIdMenuItem.Header = "Node ID: " + status.Address; + nodeIdMenuItem.IsEnabled = true; + })); + } + } + + private void ToolbarItem_TrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e) + { + Console.WriteLine("TrayContextMenuOpen"); + } + + private void ToolbarItem_PreviewTrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e) + { + Console.WriteLine("PreviewTrayContextMenuOpen"); + } + + private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e) + { + + } + + private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e) + { + if (netListView == null) + { + netListView = new WinUI.NetworkListView(); + netListView.Closed += ShowNetworksClosed; + netListView.Show(); + } + } + + private void ShowNetworksClosed(object sender, System.EventArgs e) + { + netListView = null; + } + + private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e) + { + if (joinNetView == null) + { + joinNetView = new JoinNetworkView(); + joinNetView.Closed += JoinNetworkClosed; + joinNetView.Show(); + } + } + + private void JoinNetworkClosed(object sender, System.EventArgs e) + { + joinNetView = null; + } + + private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e) + { + if(sender.GetType() == typeof(MenuItem)) + { + MenuItem item = e.Source as MenuItem; + if (item.DataContext != null) + { + ZeroTierNetwork network = item.DataContext as ZeroTierNetwork; + if (item.IsChecked) + { + APIHandler.Instance.LeaveNetwork(network.NetworkId); + } + else + { + APIHandler.Instance.JoinNetwork(network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault); + } + } + } + } + } +} |