-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathBranchOrTagNameTextBox.cs
More file actions
66 lines (57 loc) · 1.77 KB
/
BranchOrTagNameTextBox.cs
File metadata and controls
66 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Text;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public class BranchOrTagNameTextBox : TextBox
{
protected override Type StyleKeyOverride => typeof(TextBox);
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
PastingFromClipboard += OnPastingFromClipboard;
}
protected override void OnUnloaded(RoutedEventArgs e)
{
PastingFromClipboard -= OnPastingFromClipboard;
base.OnUnloaded(e);
}
protected override void OnTextInput(TextInputEventArgs e)
{
if (string.IsNullOrEmpty(e.Text))
return;
var builder = new StringBuilder(e.Text.Length);
var chars = e.Text.ToCharArray();
foreach (var ch in chars)
{
if (char.IsWhiteSpace(ch))
builder.Append('-');
else
builder.Append(ch);
}
e.Text = builder.ToString();
base.OnTextInput(e);
}
private async void OnPastingFromClipboard(object sender, RoutedEventArgs e)
{
e.Handled = true;
try
{
var clipboard = TopLevel.GetTopLevel(this)?.Clipboard;
if (clipboard != null)
{
var text = await clipboard.TryGetTextAsync();
if (!string.IsNullOrEmpty(text))
OnTextInput(new TextInputEventArgs() { Text = text });
}
}
catch
{
// Ignore exceptions
}
}
}
}