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; n.IsConnected = true;
} }
else
{
n.IsConnected = false;
}
} }
_nwCb(_knownNetworks); _nwCb(_knownNetworks);

View file

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

View file

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -23,18 +25,17 @@ namespace WinUI
/// <summary> /// <summary>
/// Interaction logic for ToolbarItem.xaml /// Interaction logic for ToolbarItem.xaml
/// </summary> /// </summary>
public partial class ToolbarItem : Window public partial class ToolbarItem : Window, INotifyPropertyChanged
{ {
private APIHandler handler = APIHandler.Instance; private APIHandler handler = APIHandler.Instance;
private NetworkListView netListView = null; private NetworkListView netListView = null;
private List<ZeroTierNetwork> networkList = null;
private NetworkMonitor mon = NetworkMonitor.Instance; 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; } get { return _networkCollection; }
set { _networkCollection = value; } set { _networkCollection = value; }
@ -54,27 +55,30 @@ namespace WinUI
mon.UnsubscribeStatusUpdates(updateStatus); 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) private void updateNetworks(List<ZeroTierNetwork> networks)
{ {
if (networks != null) if (networks != null)
{ {
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{ {
NetworkCollection.Clear();
foreach (ZeroTierNetwork n in networks) 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(item);
{
_networkCollection.Add(n);
}
else
{
_networkCollection[index] = n;
}
} }
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;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,8 +11,27 @@ using Newtonsoft.Json;
namespace WinUI namespace WinUI
{ {
[Serializable] [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) protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx)
{ {
try try
@ -37,6 +58,8 @@ namespace WinUI
IsConnected = false; IsConnected = false;
} }
public event PropertyChangedEventHandler PropertyChanged;
public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx) public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx)
{ {
info.AddValue("nwid", NetworkId); info.AddValue("nwid", NetworkId);
@ -58,59 +81,351 @@ namespace WinUI
info.AddValue("allowDefault", AllowDefault); 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")] [JsonProperty("nwid")]
public string NetworkId { get; set; } public string NetworkId {
get
{
return networkId;
}
set
{
networkId = value;
NotifyPropertyChanged();
}
}
[JsonProperty("mac")] [JsonProperty("mac")]
public string MacAddress { get; set; } public string MacAddress
{
get
{
return macAddress;
}
set
{
macAddress = value;
NotifyPropertyChanged();
}
}
[JsonProperty("name")] [JsonProperty("name")]
public string NetworkName { get; set; } public string NetworkName
{
get
{
return networkName;
}
set
{
networkName = value;
NotifyPropertyChanged();
}
}
[JsonProperty("status")] [JsonProperty("status")]
public string NetworkStatus { get; set; } public string NetworkStatus
{
get
{
return networkStatus;
}
set
{
networkStatus = value;
NotifyPropertyChanged();
}
}
[JsonProperty("type")] [JsonProperty("type")]
public string NetworkType { get; set; } public string NetworkType
{
get
{
return networkType;
}
set
{
networkType = value;
NotifyPropertyChanged();
}
}
[JsonProperty("mtu")] [JsonProperty("mtu")]
public int MTU { get; set; } public int MTU
{
get
{
return mtu;
}
set
{
mtu = value;
NotifyPropertyChanged();
}
}
[JsonProperty("dhcp")] [JsonProperty("dhcp")]
public bool DHCP { get; set; } public bool DHCP
{
get
{
return dhcp;
}
set
{
dhcp = value;
NotifyPropertyChanged();
}
}
[JsonProperty("bridge")] [JsonProperty("bridge")]
public bool Bridge { get; set ; } public bool Bridge
{
get
{
return bridge;
}
set
{
bridge = value;
NotifyPropertyChanged();
}
}
[JsonProperty("broadcastEnabled")] [JsonProperty("broadcastEnabled")]
public bool BroadcastEnabled { get ; set; } public bool BroadcastEnabled
{
get
{
return broadcastEnabled;
}
set
{
broadcastEnabled = value;
NotifyPropertyChanged();
}
}
[JsonProperty("portError")] [JsonProperty("portError")]
public int PortError { get; set; } public int PortError
{
get
{
return portError;
}
set
{
portError = value;
NotifyPropertyChanged();
}
}
[JsonProperty("netconfRevision")] [JsonProperty("netconfRevision")]
public int NetconfRevision { get; set; } public int NetconfRevision
{
get
{
return netconfRevision;
}
set
{
netconfRevision = value;
NotifyPropertyChanged();
}
}
[JsonProperty("assignedAddresses")] [JsonProperty("assignedAddresses")]
public string[] AssignedAddresses { get; set; } public string[] AssignedAddresses
{
get
{
return assignedAddresses;
}
set
{
assignedAddresses = value;
NotifyPropertyChanged();
}
}
[JsonProperty("routes")] [JsonProperty("routes")]
public NetworkRoute[] Routes { get; set; } public NetworkRoute[] Routes
{
get
{
return routes;
}
set
{
routes = value;
NotifyPropertyChanged();
}
}
[JsonProperty("portDeviceName")] [JsonProperty("portDeviceName")]
public string DeviceName { get; set; } public string DeviceName
{
get
{
return deviceName;
}
set
{
deviceName = value;
NotifyPropertyChanged();
}
}
[JsonProperty("allowManaged")] [JsonProperty("allowManaged")]
public bool AllowManaged { get; set; } public bool AllowManaged
{
get
{
return allowManaged;
}
set
{
allowManaged = value;
NotifyPropertyChanged();
}
}
[JsonProperty("allowGlobal")] [JsonProperty("allowGlobal")]
public bool AllowGlobal { get; set; } public bool AllowGlobal
{
get
{
return allowGlobal;
}
set
{
allowGlobal = value;
NotifyPropertyChanged();
}
}
[JsonProperty("allowDefault")] [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 public String Title
{ {