diff options
author | Adam Ierymenko <adam.ierymenko@zerotier.com> | 2018-04-25 06:39:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-25 06:39:02 -0700 |
commit | 42ec780a6f6eedef4d8b1d8218bd72fc6ed75cc0 (patch) | |
tree | 7bf86c4d92d6a0f77eced79bfc33313c62c7b6dd /windows/WinUI/JoinNetworkView.xaml.cs | |
parent | 18c9dc8a0649c866eff9f299f20fa5b19c502e52 (diff) | |
parent | 4608880fb06700822d01e9e5d6729fcdeb82b64b (diff) | |
download | infinitytier-42ec780a6f6eedef4d8b1d8218bd72fc6ed75cc0.tar.gz infinitytier-42ec780a6f6eedef4d8b1d8218bd72fc6ed75cc0.zip |
Merge branch 'dev' into netbsd-support
Diffstat (limited to 'windows/WinUI/JoinNetworkView.xaml.cs')
-rw-r--r-- | windows/WinUI/JoinNetworkView.xaml.cs | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/windows/WinUI/JoinNetworkView.xaml.cs b/windows/WinUI/JoinNetworkView.xaml.cs new file mode 100644 index 00000000..0b4ac324 --- /dev/null +++ b/windows/WinUI/JoinNetworkView.xaml.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +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; + +namespace WinUI +{ + /// <summary> + /// Interaction logic for JoinNetworkView.xaml + /// </summary> + public partial class JoinNetworkView : Window + { + Regex charRegex = new Regex("[0-9a-fxA-FX]"); + Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$"); + + public JoinNetworkView() + { + InitializeComponent(); + + DataObject.AddPastingHandler(joinNetworkBox, onPaste); + DataObject.AddCopyingHandler(joinNetworkBox, onCopyCut); + } + + private void joinNetworkBox_OnTextEntered(object sender, TextCompositionEventArgs e) + { + e.Handled = !charRegex.IsMatch(e.Text); + + if ( (joinNetworkBox.Text.Length + e.Text.Length) == 16) + { + joinButton.IsEnabled = true; + } + else + { + joinButton.IsEnabled = false; + } + } + + private void joinNetworkBox_OnKeyDown(object sender, KeyEventArgs e) + { + if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) + { + if (e.Key == Key.X && joinNetworkBox.IsSelectionActive) + { + // handle ctrl-x removing characters + joinButton.IsEnabled = false; + } + } + else if (e.Key == Key.Delete || e.Key == Key.Back) + { + if ((joinNetworkBox.Text.Length - 1) == 16) + { + joinButton.IsEnabled = true; + } + else + { + joinButton.IsEnabled = false; + } + } + else + { + if ((joinNetworkBox.Text.Length + 1) > 16) + { + e.Handled = true; + } + } + } + + private void onPaste(object sender, DataObjectPastingEventArgs e) + { + var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true); + if (!isText) + { + joinButton.IsEnabled = false; + return; + } + + var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string; + + if (!wholeStringRegex.IsMatch(text)) + { + e.Handled = true; + e.CancelCommand(); + } + + if (text.Length == 16 || (joinNetworkBox.Text.Length + text.Length) == 16) + { + joinButton.IsEnabled = true; + } + else if (text.Length > 16 || (joinNetworkBox.Text.Length + text.Length) > 16) + { + e.Handled = true; + e.CancelCommand(); + } + else + { + joinButton.IsEnabled = false; + } + } + + private void onCopyCut(object sender, DataObjectCopyingEventArgs e) + { + + } + + private void joinButton_Click(object sender, RoutedEventArgs e) + { + bool allowDefault = allowDefaultCheckbox.IsChecked.Value; + bool allowGlobal = allowGlobalCheckbox.IsChecked.Value; + bool allowManaged = allowManagedCheckbox.IsChecked.Value; + + APIHandler.Instance.JoinNetwork(this.Dispatcher, joinNetworkBox.Text, allowManaged, allowGlobal, allowDefault); + + Close(); + } + } +} |