can finally join/leave networks by clicking on them in the context menu

This commit is contained in:
Grant Limberg 2016-11-17 14:13:05 -08:00
parent b4bacd50a1
commit 5447c01e1f
4 changed files with 408 additions and 52 deletions

View File

@ -101,6 +101,10 @@ namespace WinUI
{
n.IsConnected = true;
}
else
{
n.IsConnected = false;
}
}
_nwCb(_knownNetworks);

View File

@ -12,7 +12,7 @@
<Window.Resources>
<CollectionViewSource Source="{Binding ElementName=Toolbar, Path=NetworkCollection}" x:Key="KnownNetworks">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="NetworkId" Direction="Ascending"/>
<scm:SortDescription PropertyName="Header" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
@ -25,16 +25,32 @@
PreviewTrayContextMenuOpen="ToolbarItem_PreviewTrayContextMenuOpen">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Node ID: abeb9f9bc5"
Click="ToolbarItem_NodeIDClicked"
x:Name="nodeIdMenuItem"/>
<Separator/>
<MenuItem Header="Join Network..."
Click="ToolbarItem_JoinNetworkClicked"/>
<MenuItem Header="Show Networks..."
Click="ToolbarItem_ShowNetworksClicked"/>
<Separator/>
<MenuItem Header="Networks">
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem Header="Node ID: abeb9f9bc5"
Click="ToolbarItem_NodeIDClicked"
x:Name="nodeIdMenuItem"/>
<Separator/>
<MenuItem Header="Join Network..."
Click="ToolbarItem_JoinNetworkClicked"/>
<MenuItem Header="Show Networks..."
Click="ToolbarItem_ShowNetworksClicked"/>
<Separator/>
<CollectionContainer Collection="{Binding Source={StaticResource KnownNetworks}}">
</CollectionContainer>
<Separator/>
<MenuItem Header="About..."/>
<MenuItem Header="Preferences..."/>
<Separator/>
<MenuItem Header="Quit"/>
</CompositeCollection>
</ContextMenu.ItemsSource>
<!--<MenuItem Header="Networks">
<MenuItem.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource KnownNetworks}}"/>
@ -43,16 +59,13 @@
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header" Value="{Binding Title}"/>
<Setter Property="MenuItem.IsCheckable" Value="True"/>
--><!-- <Setter Property="MenuItem.IsCheckable" Value="True"/> --><!--
<Setter Property="MenuItem.IsChecked" Value="{Binding IsConnected}"/>
<EventSetter Event="MenuItem.Click" Handler="ToolbarItem_NetworkClicked"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
<Separator/>
<MenuItem Header="About..."/>
<MenuItem Header="Preferences..."/>
<Separator/>
<MenuItem Header="Quit"/>
</MenuItem>-->
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>

View File

@ -1,7 +1,9 @@
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;
@ -23,18 +25,17 @@ namespace WinUI
/// <summary>
/// Interaction logic for ToolbarItem.xaml
/// </summary>
public partial class ToolbarItem : Window
public partial class ToolbarItem : Window, INotifyPropertyChanged
{
private APIHandler handler = APIHandler.Instance;
private NetworkListView netListView = null;
private List<ZeroTierNetwork> networkList = null;
private NetworkMonitor mon = NetworkMonitor.Instance;
private ObservableCollection<ZeroTierNetwork> _networkCollection = new ObservableCollection<ZeroTierNetwork>();
private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>();
public ObservableCollection<ZeroTierNetwork> NetworkCollection
public ObservableCollection<MenuItem> NetworkCollection
{
get { return _networkCollection; }
set { _networkCollection = value; }
@ -54,27 +55,30 @@ namespace WinUI
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)
{
int index = _networkCollection.IndexOf(n);
MenuItem item = new MenuItem();
item.Header = n.Title;
item.DataContext = n;
item.IsChecked = n.IsConnected;
item.Click += ToolbarItem_NetworkClicked;
if (index == -1)
{
_networkCollection.Add(n);
}
else
{
_networkCollection[index] = n;
}
NetworkCollection.Add(item);
}
this.networkList = networks;
}));
}
}
@ -130,5 +134,25 @@ namespace WinUI
{
}
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);
}
}
}
}
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
@ -9,8 +11,27 @@ using Newtonsoft.Json;
namespace WinUI
{
[Serializable]
public class ZeroTierNetwork : ISerializable, IEquatable<ZeroTierNetwork>, IComparable<ZeroTierNetwork>
public class ZeroTierNetwork : ISerializable, IEquatable<ZeroTierNetwork>, IComparable<ZeroTierNetwork>, INotifyPropertyChanged
{
private string networkId;
private string macAddress;
private string networkName;
private string networkStatus;
private string networkType;
private Int32 mtu;
private bool dhcp;
private bool bridge;
private bool broadcastEnabled;
private Int32 portError;
private Int32 netconfRevision;
private string[] assignedAddresses;
private NetworkRoute[] routes;
private string deviceName;
private bool allowManaged;
private bool allowGlobal;
private bool allowDefault;
private bool isConnected;
protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx)
{
try
@ -37,6 +58,8 @@ namespace WinUI
IsConnected = false;
}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx)
{
info.AddValue("nwid", NetworkId);
@ -58,59 +81,351 @@ namespace WinUI
info.AddValue("allowDefault", AllowDefault);
}
public void UpdateNetwork(ZeroTierNetwork network)
{
if (network == null)
return;
if (!NetworkId.Equals(network.NetworkId))
{
NetworkId = network.NetworkId;
}
if (!MacAddress.Equals(network.MacAddress))
{
MacAddress = network.MacAddress;
}
if (!NetworkName.Equals(network.NetworkName))
{
NetworkName = network.NetworkName;
}
if (!NetworkStatus.Equals(network.NetworkStatus))
{
NetworkStatus = network.NetworkStatus;
}
if (!NetworkType.Equals(network.NetworkType))
{
NetworkType = network.NetworkType;
}
if (MTU != network.MTU)
{
MTU = network.MTU;
}
if (DHCP != network.DHCP)
{
DHCP = network.DHCP;
}
if (Bridge != network.Bridge)
{
Bridge = network.Bridge;
}
if (BroadcastEnabled != network.BroadcastEnabled)
{
BroadcastEnabled = network.BroadcastEnabled;
}
if (PortError != network.PortError)
{
PortError = network.PortError;
}
if (NetconfRevision != network.NetconfRevision)
{
NetconfRevision = network.NetconfRevision;
}
AssignedAddresses = network.AssignedAddresses;
Routes = network.Routes;
if (!DeviceName.Equals(network.DeviceName))
{
DeviceName = network.DeviceName;
}
if (AllowManaged != network.AllowManaged)
{
AllowManaged = network.AllowManaged;
}
if (AllowGlobal != network.AllowGlobal)
{
AllowGlobal = network.AllowGlobal;
}
if (AllowDefault != network.AllowDefault)
{
AllowDefault = network.AllowDefault;
}
if (IsConnected != network.IsConnected)
{
IsConnected = network.IsConnected;
}
}
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
[JsonProperty("nwid")]
public string NetworkId { get; set; }
public string NetworkId {
get
{
return networkId;
}
set
{
networkId = value;
NotifyPropertyChanged();
}
}
[JsonProperty("mac")]
public string MacAddress { get; set; }
public string MacAddress
{
get
{
return macAddress;
}
set
{
macAddress = value;
NotifyPropertyChanged();
}
}
[JsonProperty("name")]
public string NetworkName { get; set; }
public string NetworkName
{
get
{
return networkName;
}
set
{
networkName = value;
NotifyPropertyChanged();
}
}
[JsonProperty("status")]
public string NetworkStatus { get; set; }
public string NetworkStatus
{
get
{
return networkStatus;
}
set
{
networkStatus = value;
NotifyPropertyChanged();
}
}
[JsonProperty("type")]
public string NetworkType { get; set; }
public string NetworkType
{
get
{
return networkType;
}
set
{
networkType = value;
NotifyPropertyChanged();
}
}
[JsonProperty("mtu")]
public int MTU { get; set; }
public int MTU
{
get
{
return mtu;
}
set
{
mtu = value;
NotifyPropertyChanged();
}
}
[JsonProperty("dhcp")]
public bool DHCP { get; set; }
public bool DHCP
{
get
{
return dhcp;
}
set
{
dhcp = value;
NotifyPropertyChanged();
}
}
[JsonProperty("bridge")]
public bool Bridge { get; set ; }
public bool Bridge
{
get
{
return bridge;
}
set
{
bridge = value;
NotifyPropertyChanged();
}
}
[JsonProperty("broadcastEnabled")]
public bool BroadcastEnabled { get ; set; }
public bool BroadcastEnabled
{
get
{
return broadcastEnabled;
}
set
{
broadcastEnabled = value;
NotifyPropertyChanged();
}
}
[JsonProperty("portError")]
public int PortError { get; set; }
public int PortError
{
get
{
return portError;
}
set
{
portError = value;
NotifyPropertyChanged();
}
}
[JsonProperty("netconfRevision")]
public int NetconfRevision { get; set; }
public int NetconfRevision
{
get
{
return netconfRevision;
}
set
{
netconfRevision = value;
NotifyPropertyChanged();
}
}
[JsonProperty("assignedAddresses")]
public string[] AssignedAddresses { get; set; }
public string[] AssignedAddresses
{
get
{
return assignedAddresses;
}
set
{
assignedAddresses = value;
NotifyPropertyChanged();
}
}
[JsonProperty("routes")]
public NetworkRoute[] Routes { get; set; }
public NetworkRoute[] Routes
{
get
{
return routes;
}
set
{
routes = value;
NotifyPropertyChanged();
}
}
[JsonProperty("portDeviceName")]
public string DeviceName { get; set; }
public string DeviceName
{
get
{
return deviceName;
}
set
{
deviceName = value;
NotifyPropertyChanged();
}
}
[JsonProperty("allowManaged")]
public bool AllowManaged { get; set; }
public bool AllowManaged
{
get
{
return allowManaged;
}
set
{
allowManaged = value;
NotifyPropertyChanged();
}
}
[JsonProperty("allowGlobal")]
public bool AllowGlobal { get; set; }
public bool AllowGlobal
{
get
{
return allowGlobal;
}
set
{
allowGlobal = value;
NotifyPropertyChanged();
}
}
[JsonProperty("allowDefault")]
public bool AllowDefault { get; set; }
public bool AllowDefault
{
get
{
return allowDefault;
}
set
{
allowDefault = value;
NotifyPropertyChanged();
}
}
public bool IsConnected { get; set; } = false;
public bool IsConnected
{
get
{
return isConnected;
}
set
{
isConnected = value;
NotifyPropertyChanged();
}
}
public String Title
{