From 7cf3d2caa19da076ebd3539c8786ce62a53d35f9 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 8 Nov 2016 16:31:07 -0800 Subject: [PATCH] Network list window opens with click on the menu item --- windows/WinUI/APIHandler.cs | 14 ++++++++-- windows/WinUI/NetworkInfoView.xaml.cs | 12 ++++---- .../{MainWindow.xaml => NetworkList.xaml} | 2 +- ...MainWindow.xaml.cs => NetworkList.xaml.cs} | 23 ++++----------- windows/WinUI/NetworksPage.xaml.cs | 8 ------ windows/WinUI/ToolbarItem.xaml | 6 ++-- windows/WinUI/ToolbarItem.xaml.cs | 28 ++++++++++++++++++- windows/WinUI/WinUI.csproj | 6 ++-- 8 files changed, 57 insertions(+), 42 deletions(-) rename windows/WinUI/{MainWindow.xaml => NetworkList.xaml} (99%) rename windows/WinUI/{MainWindow.xaml.cs => NetworkList.xaml.cs} (89%) diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index f2fb1d21a..9cddd916a 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -19,7 +19,8 @@ namespace WinUI private string url = null; - private static APIHandler instance; + private static volatile APIHandler instance; + private static object syncRoot = new Object(); public static APIHandler Instance { @@ -27,7 +28,16 @@ namespace WinUI { if (instance == null) { - + lock (syncRoot) + { + if (instance == null) + { + if (!initHandler()) + { + return null; + } + } + } } return instance; diff --git a/windows/WinUI/NetworkInfoView.xaml.cs b/windows/WinUI/NetworkInfoView.xaml.cs index 3ecc31b84..c6c8726e5 100644 --- a/windows/WinUI/NetworkInfoView.xaml.cs +++ b/windows/WinUI/NetworkInfoView.xaml.cs @@ -20,14 +20,12 @@ namespace WinUI /// public partial class NetworkInfoView : UserControl { - private APIHandler handler; private ZeroTierNetwork network; - public NetworkInfoView(APIHandler handler, ZeroTierNetwork network) + public NetworkInfoView(ZeroTierNetwork network) { InitializeComponent(); - this.handler = handler; this.network = network; UpdateNetworkData(); @@ -78,13 +76,13 @@ namespace WinUI private void leaveButton_Click(object sender, RoutedEventArgs e) { - handler.LeaveNetwork(network.NetworkId); + APIHandler.Instance.LeaveNetwork(network.NetworkId); } private void AllowManaged_CheckStateChanged(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; - handler.JoinNetwork(network.NetworkId, + APIHandler.Instance.JoinNetwork(network.NetworkId, allowManaged.IsChecked ?? false, allowGlobal.IsChecked ?? false, allowDefault.IsChecked ?? false); @@ -93,7 +91,7 @@ namespace WinUI private void AllowGlobal_CheckStateChanged(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; - handler.JoinNetwork(network.NetworkId, + APIHandler.Instance.JoinNetwork(network.NetworkId, allowManaged.IsChecked ?? false, allowGlobal.IsChecked ?? false, allowDefault.IsChecked ?? false); @@ -102,7 +100,7 @@ namespace WinUI private void AllowDefault_CheckStateChanged(object sender, RoutedEventArgs e) { CheckBox cb = sender as CheckBox; - handler.JoinNetwork(network.NetworkId, + APIHandler.Instance.JoinNetwork(network.NetworkId, allowManaged.IsChecked ?? false, allowGlobal.IsChecked ?? false, allowDefault.IsChecked ?? false); diff --git a/windows/WinUI/MainWindow.xaml b/windows/WinUI/NetworkList.xaml similarity index 99% rename from windows/WinUI/MainWindow.xaml rename to windows/WinUI/NetworkList.xaml index 9d4a9fc19..6010a9908 100644 --- a/windows/WinUI/MainWindow.xaml +++ b/windows/WinUI/NetworkList.xaml @@ -4,7 +4,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WinUI" - mc:Ignorable="d" x:Class="WinUI.MainWindow" + mc:Ignorable="d" x:Class="WinUI.NetworkList" Title="ZeroTier One" Height="500" Width="500" Icon="ZeroTierIcon.ico"> diff --git a/windows/WinUI/MainWindow.xaml.cs b/windows/WinUI/NetworkList.xaml.cs similarity index 89% rename from windows/WinUI/MainWindow.xaml.cs rename to windows/WinUI/NetworkList.xaml.cs index a57ee4b9a..ae5e1c8eb 100644 --- a/windows/WinUI/MainWindow.xaml.cs +++ b/windows/WinUI/NetworkList.xaml.cs @@ -23,9 +23,8 @@ namespace WinUI /// /// Interaction logic for MainWindow.xaml /// - public partial class MainWindow : Window + public partial class NetworkList : Window { - APIHandler handler; Regex charRegex = new Regex("[0-9a-fxA-FX]"); Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$"); @@ -33,20 +32,9 @@ namespace WinUI bool connected = false; - public MainWindow() + public NetworkList() { InitializeComponent(); - } - - - public void SetAPIHandler(APIHandler handler) - { - timer.Stop(); - timer = new Timer(); - - this.handler = handler; - - networksPage.SetAPIHandler(handler); updateStatus(); @@ -64,11 +52,10 @@ namespace WinUI timer.Interval = 2000; timer.Enabled = true; } - private void updateStatus() { - var status = handler.GetStatus(); + var status = APIHandler.Instance.GetStatus(); if (status != null) { @@ -108,7 +95,7 @@ namespace WinUI private void updateNetworks() { - var networks = handler.GetNetworks(); + var networks = APIHandler.Instance.GetNetworks(); networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { @@ -141,7 +128,7 @@ namespace WinUI } else { - handler.JoinNetwork(joinNetworkID.Text); + APIHandler.Instance.JoinNetwork(joinNetworkID.Text); } } diff --git a/windows/WinUI/NetworksPage.xaml.cs b/windows/WinUI/NetworksPage.xaml.cs index 5a0dc19dc..e69613501 100644 --- a/windows/WinUI/NetworksPage.xaml.cs +++ b/windows/WinUI/NetworksPage.xaml.cs @@ -20,18 +20,11 @@ namespace WinUI /// public partial class NetworksPage : UserControl { - private APIHandler handler; - public NetworksPage() { InitializeComponent(); } - public void SetAPIHandler(APIHandler handler) - { - this.handler = handler; - } - public void setNetworks(List networks) { this.wrapPanel.Children.Clear(); @@ -44,7 +37,6 @@ namespace WinUI { this.wrapPanel.Children.Add( new NetworkInfoView( - handler, networks.ElementAt(i))); } } diff --git a/windows/WinUI/ToolbarItem.xaml b/windows/WinUI/ToolbarItem.xaml index 4a153c9a8..2a7af2290 100644 --- a/windows/WinUI/ToolbarItem.xaml +++ b/windows/WinUI/ToolbarItem.xaml @@ -15,10 +15,12 @@ PreviewTrayContextMenuOpen="ToolbarItem_PreviewTrayContextMenuOpen"> - + - + diff --git a/windows/WinUI/ToolbarItem.xaml.cs b/windows/WinUI/ToolbarItem.xaml.cs index 9ff7e8608..b3a1aafdf 100644 --- a/windows/WinUI/ToolbarItem.xaml.cs +++ b/windows/WinUI/ToolbarItem.xaml.cs @@ -24,7 +24,9 @@ namespace WinUI /// public partial class ToolbarItem : Window { - APIHandler handler = APIHandler.Instance; + private APIHandler handler = APIHandler.Instance; + + NetworkList netList = null; public ToolbarItem() { @@ -40,5 +42,29 @@ namespace WinUI { Console.WriteLine("PreviewTrayContextMenuOpen"); } + + private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e) + { + + } + + private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e) + { + if (netList == null) + { + netList = new WinUI.NetworkList(); + netList.Closed += ShowNetworksClosed; + } + + if (!netList.IsVisible) + { + netList.Show(); + } + } + + private void ShowNetworksClosed(object sender, System.EventArgs e) + { + netList = null; + } } } diff --git a/windows/WinUI/WinUI.csproj b/windows/WinUI/WinUI.csproj index 00c07f6ce..70e07ba37 100644 --- a/windows/WinUI/WinUI.csproj +++ b/windows/WinUI/WinUI.csproj @@ -115,7 +115,7 @@ - + MSBuild:Compile Designer @@ -124,8 +124,8 @@ App.xaml Code - - MainWindow.xaml + + NetworkList.xaml Code