|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Avalonia; |
| 5 | +using Avalonia.Controls; |
| 6 | +using Avalonia.Data; |
| 7 | +using Avalonia.Input; |
| 8 | +using Avalonia.Input.Platform; |
| 9 | +using Avalonia.Interactivity; |
| 10 | +using Avalonia.Markup.Xaml; |
| 11 | +using Microsoft.Git.CredentialManager.UI.Controls; |
| 12 | + |
| 13 | +namespace GitHub.UI.Controls |
| 14 | +{ |
| 15 | + public class SixDigitInput : UserControl, IFocusable |
| 16 | + { |
| 17 | + public static readonly DirectProperty<SixDigitInput, string> TextProperty = |
| 18 | + AvaloniaProperty.RegisterDirect<SixDigitInput, string>( |
| 19 | + nameof(Text), |
| 20 | + o => o.Text, |
| 21 | + (o, v) => o.Text = v, |
| 22 | + defaultBindingMode: BindingMode.TwoWay); |
| 23 | + |
| 24 | + private PlatformHotkeyConfiguration _keyMap; |
| 25 | + private IClipboard _clipboard; |
| 26 | + private bool _ignoreTextBoxUpdate; |
| 27 | + private TextBox[] _textBoxes; |
| 28 | + private string _text; |
| 29 | + |
| 30 | + public SixDigitInput() |
| 31 | + { |
| 32 | + InitializeComponent(); |
| 33 | + } |
| 34 | + |
| 35 | + private void InitializeComponent() |
| 36 | + { |
| 37 | + AvaloniaXamlLoader.Load(this); |
| 38 | + |
| 39 | + _keyMap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>(); |
| 40 | + _clipboard = AvaloniaLocator.Current.GetService<IClipboard>(); |
| 41 | + _textBoxes = new[] |
| 42 | + { |
| 43 | + this.FindControl<TextBox>("one"), |
| 44 | + this.FindControl<TextBox>("two"), |
| 45 | + this.FindControl<TextBox>("three"), |
| 46 | + this.FindControl<TextBox>("four"), |
| 47 | + this.FindControl<TextBox>("five"), |
| 48 | + this.FindControl<TextBox>("six"), |
| 49 | + }; |
| 50 | + |
| 51 | + foreach (TextBox textBox in _textBoxes) |
| 52 | + { |
| 53 | + SetUpTextBox(textBox); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public string Text |
| 58 | + { |
| 59 | + get => _text; |
| 60 | + set |
| 61 | + { |
| 62 | + SetAndRaise(TextProperty, ref _text, value); |
| 63 | + if (!_ignoreTextBoxUpdate) SetTextBoxes(value); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void SetTextBoxes(string text) |
| 68 | + { |
| 69 | + if (string.IsNullOrWhiteSpace(text)) |
| 70 | + { |
| 71 | + foreach (TextBox textBox in _textBoxes) |
| 72 | + { |
| 73 | + textBox.Text = string.Empty; |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + IEnumerable<char> digits = text.Where(char.IsDigit); |
| 79 | + string digitsStr = string.Join(string.Empty, digits).PadRight(6); |
| 80 | + for (int i = 0; i < digitsStr.Length; i++) |
| 81 | + { |
| 82 | + _textBoxes[i].Text = digitsStr.Substring(i, 1); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void SetFocus() |
| 88 | + { |
| 89 | + KeyboardDevice.Instance.SetFocusedElement(_textBoxes[0], NavigationMethod.Tab, KeyModifiers.None); |
| 90 | + } |
| 91 | + |
| 92 | + private void SetUpTextBox(TextBox textBox) |
| 93 | + { |
| 94 | + textBox.AddHandler(KeyDownEvent, OnPreviewKeyDown, RoutingStrategies.Tunnel); |
| 95 | + |
| 96 | + void OnPreviewKeyDown(object sender, KeyEventArgs e) |
| 97 | + { |
| 98 | + // Handle paste |
| 99 | + if (_keyMap.Paste.Any(x => x.Matches(e))) |
| 100 | + { |
| 101 | + OnPaste(); |
| 102 | + e.Handled = true; |
| 103 | + } |
| 104 | + // Handle keyboard navigation |
| 105 | + else if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Back) |
| 106 | + { |
| 107 | + e.Handled = e.Key == Key.Right ? MoveNext() : MovePrevious(); |
| 108 | + if (e.Key == Key.Back) |
| 109 | + { |
| 110 | + textBox.Text = string.Empty; |
| 111 | + } |
| 112 | + } |
| 113 | + // Only allow 0-9, Tab, Escape, and Delete |
| 114 | + else if (e.Key != Key.D0 && |
| 115 | + e.Key != Key.D1 && |
| 116 | + e.Key != Key.D2 && |
| 117 | + e.Key != Key.D3 && |
| 118 | + e.Key != Key.D4 && |
| 119 | + e.Key != Key.D5 && |
| 120 | + e.Key != Key.D6 && |
| 121 | + e.Key != Key.D7 && |
| 122 | + e.Key != Key.D8 && |
| 123 | + e.Key != Key.D9 && |
| 124 | + e.Key != Key.NumPad0 && |
| 125 | + e.Key != Key.NumPad1 && |
| 126 | + e.Key != Key.NumPad2 && |
| 127 | + e.Key != Key.NumPad3 && |
| 128 | + e.Key != Key.NumPad4 && |
| 129 | + e.Key != Key.NumPad5 && |
| 130 | + e.Key != Key.NumPad6 && |
| 131 | + e.Key != Key.NumPad7 && |
| 132 | + e.Key != Key.NumPad8 && |
| 133 | + e.Key != Key.NumPad9 && |
| 134 | + e.Key != Key.Tab && |
| 135 | + e.Key != Key.Escape && |
| 136 | + e.Key != Key.Delete) |
| 137 | + { |
| 138 | + e.Handled = true; |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + textBox.PropertyChanged += (s, e) => |
| 143 | + { |
| 144 | + if (e.Property.Name == nameof(TextBox.Text)) |
| 145 | + { |
| 146 | + try |
| 147 | + { |
| 148 | + _ignoreTextBoxUpdate = true; |
| 149 | + Text = string.Join(string.Empty, _textBoxes.Select(x => x.Text)); |
| 150 | + } |
| 151 | + finally |
| 152 | + { |
| 153 | + _ignoreTextBoxUpdate = false; |
| 154 | + } |
| 155 | + |
| 156 | + if (e.NewValue is string value && value.Length > 0) |
| 157 | + { |
| 158 | + MoveNext(); |
| 159 | + } |
| 160 | + } |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + private void OnPaste() |
| 165 | + { |
| 166 | + string text = _clipboard.GetTextAsync().GetAwaiter().GetResult(); |
| 167 | + Text = text; |
| 168 | + } |
| 169 | + |
| 170 | + private bool MoveNext() => MoveFocus(true); |
| 171 | + |
| 172 | + private bool MovePrevious() => MoveFocus(false); |
| 173 | + |
| 174 | + private bool MoveFocus(bool next) |
| 175 | + { |
| 176 | + // Get currently focused text box |
| 177 | + if (FocusManager.Instance.Current is TextBox textBox) |
| 178 | + { |
| 179 | + int textBoxIndex = Array.IndexOf(_textBoxes, textBox); |
| 180 | + if (textBoxIndex > -1) |
| 181 | + { |
| 182 | + int nextIndex = next |
| 183 | + ? Math.Min(_textBoxes.Length - 1, textBoxIndex + 1) |
| 184 | + : Math.Max(0, textBoxIndex - 1); |
| 185 | + |
| 186 | + KeyboardDevice.Instance.SetFocusedElement(_textBoxes[nextIndex], NavigationMethod.Tab, KeyModifiers.None); |
| 187 | + return true; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return false; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments