feat: add separator and duplicate support, try to vertical center textbox.

This commit is contained in:
rabbitism
2023-06-29 03:10:23 +08:00
parent c9eb87cd37
commit c878e45047
4 changed files with 64 additions and 11 deletions

View File

@@ -65,6 +65,24 @@ public class TagInput: TemplatedControl
set => SetValue(ItemTemplateProperty, value);
}
public static readonly StyledProperty<string> SeparatorProperty = AvaloniaProperty.Register<TagInput, string>(
nameof(Separator));
public string Separator
{
get => GetValue(SeparatorProperty);
set => SetValue(SeparatorProperty, value);
}
public static readonly StyledProperty<bool> AllowDuplicatesProperty = AvaloniaProperty.Register<TagInput, bool>(
nameof(AllowDuplicates), defaultValue: true);
public bool AllowDuplicates
{
get => GetValue(AllowDuplicatesProperty);
set => SetValue(AllowDuplicatesProperty, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
@@ -82,8 +100,26 @@ public class TagInput: TemplatedControl
{
if (_textBox.Text?.Length > 0)
{
Items.Insert(Items.Count - 1, _textBox.Text);
Tags.Insert(Items.Count - 2, _textBox.Text ?? string.Empty);
string[] values;
if (!string.IsNullOrEmpty(Separator))
{
values = _textBox.Text.Split(new string[] { Separator },
StringSplitOptions.RemoveEmptyEntries);
}
else
{
values = new[] { _textBox.Text };
}
if (!AllowDuplicates)
{
values = values.Distinct().Except(Tags).ToArray();
}
for(int i = 0; i < values.Length; i++)
{
Items.Insert(Items.Count - 1, values[i]);
Tags.Insert(Items.Count - 2, values[i]);
}
_textBox.Text = "";
}
}
@@ -91,6 +127,10 @@ public class TagInput: TemplatedControl
{
if (_textBox.Text?.Length == 0)
{
if (Tags.Count == 0)
{
return;
}
Items.RemoveAt(Items.Count - 2);
Tags.RemoveAt(Items.Count - 1);
}

View File

@@ -71,7 +71,7 @@ public class TagInputPanel: Panel
// Width is enough to place next child
if (MathUtilities.GreaterThan(deltaX, child.DesiredSize.Width))
{
child.Arrange(new Rect(currentLineX, totalHeight, child.DesiredSize.Width, child.DesiredSize.Height));
child.Arrange(new Rect(currentLineX, totalHeight, child.DesiredSize.Width, Math.Max(child.DesiredSize.Height, currentLineHeight)));
currentLineX += child.DesiredSize.Width;
currentLineHeight = Math.Max(currentLineHeight, child.DesiredSize.Height);
}
@@ -99,7 +99,9 @@ public class TagInputPanel: Panel
}
else
{
last.Arrange(new Rect(currentLineX, totalHeight,lastDeltaX, last.DesiredSize.Height));
currentLineHeight = children.Count == 1 ? finalSize.Height : currentLineHeight;
last.Arrange(new Rect(currentLineX, totalHeight, lastDeltaX,
Math.Max(currentLineHeight, last.DesiredSize.Height)));
currentLineHeight = Math.Max(currentLineHeight, last.DesiredSize.Height);
totalHeight += currentLineHeight;
}