commit: temporarily use TextBox

This commit is contained in:
rabbitism
2023-02-22 14:47:58 +08:00
parent e44fbd5fc5
commit f53076ae43
6 changed files with 158 additions and 26 deletions

View File

@@ -0,0 +1,15 @@
<UserControl
x:Class="Ursa.Demo.Pages.IPv4BoxDemo"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<StackPanel>
<TextBlock />
<u:IPv4Box Width="500" />
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,18 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Ursa.Demo.Pages;
public partial class IPv4BoxDemo : UserControl
{
public IPv4BoxDemo()
{
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}

View File

@@ -29,6 +29,9 @@
<TabItem Header="Banner">
<pages:BannerDemo />
</TabItem>
<TabItem Header="IPv4Box">
<pages:IPv4BoxDemo />
</TabItem>
</TabControl>
</Grid>

View File

@@ -0,0 +1,57 @@
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<ControlTheme x:Key="{x:Type u:IPv4Box}" TargetType="{x:Type u:IPv4Box}">
<Setter Property="u:IPv4Box.Template">
<ControlTemplate TargetType="u:IPv4Box">
<Border
BorderBrush="Black"
BorderThickness="1"
CornerRadius="3">
<Grid Width="{TemplateBinding Width}" ColumnDefinitions="*, Auto, *, Auto, *, Auto, *, Auto, * ">
<TextBox
Name="{x:Static u:IPv4Box.PART_FirstTextBox}"
Grid.Column="0"
BorderThickness="0" />
<TextPresenter
Grid.Column="1"
VerticalAlignment="Bottom"
Text="." />
<TextBox
Name="{x:Static u:IPv4Box.PART_SecondTextBox}"
Grid.Column="2"
BorderThickness="0" />
<TextPresenter
Grid.Column="3"
VerticalAlignment="Bottom"
Text="." />
<TextBox
Name="{x:Static u:IPv4Box.PART_ThirdTextBox}"
Grid.Column="4"
BorderThickness="0" />
<TextPresenter
Grid.Column="5"
VerticalAlignment="Bottom"
Text="." />
<TextBox
Name="{x:Static u:IPv4Box.PART_FourthTextBox}"
Grid.Column="6"
BorderThickness="0" />
<Rectangle
Grid.Column="7"
Width="1"
Margin="0,2"
VerticalAlignment="Stretch"
Fill="Black" />
<TextBox
Name="{x:Static u:IPv4Box.PART_PortTextBox}"
Grid.Column="8"
BorderThickness="0" />
</Grid>
</Border>
</ControlTemplate>
</Setter>
</ControlTheme>
</ResourceDictionary>

View File

@@ -3,5 +3,6 @@
<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="Badge.axaml" />
<ResourceInclude Source="Banner.axaml" />
<ResourceInclude Source="IPv4Box.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Net;
using Avalonia;
using Avalonia.Controls;
@@ -5,26 +6,36 @@ using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace Ursa.Controls;
[TemplatePart(PART_FirstTextPresenter, typeof(TextPresenter))]
[TemplatePart(PART_SecondTextPresenter, typeof(TextPresenter))]
[TemplatePart(PART_ThirdTextPresenter, typeof(TextPresenter))]
[TemplatePart(PART_FourthTextPresenter, typeof(TextPresenter))]
[TemplatePart(PART_PortNumericInput, typeof(NumericUpDown))]
[TemplatePart(PART_FirstTextBox, typeof(TextBox))]
[TemplatePart(PART_SecondTextBox, typeof(TextBox))]
[TemplatePart(PART_ThirdTextBox, typeof(TextBox))]
[TemplatePart(PART_FourthTextBox, typeof(TextBox))]
[TemplatePart(PART_PortTextBox, typeof(TextBox))]
public class IPv4Box: TemplatedControl
{
public const string PART_FirstTextPresenter = "PART_FirstTextPresenter";
public const string PART_SecondTextPresenter = "PART_SecondTextPresenter";
public const string PART_ThirdTextPresenter = "PART_ThirdTextPresenter";
public const string PART_FourthTextPresenter = "PART_FourthTextPresenter";
public const string PART_PortNumericInput = "PART_PortNumericInput";
private TextPresenter? _firstTextPresenter;
private TextPresenter? _secondTextPresenter;
private TextPresenter? _thirdTextPresenter;
private TextPresenter? _fourthTextPresenter;
private NumericUpDown? _portNumericInput;
public const string PART_FirstTextBox = "PART_FirstTextPresenter";
public const string PART_SecondTextBox = "PART_SecondTextPresenter";
public const string PART_ThirdTextBox = "PART_ThirdTextPresenter";
public const string PART_FourthTextBox = "PART_FourthTextPresenter";
public const string PART_PortTextBox = "PART_PortNumericInput";
private TextBox? _firstText;
private TextBox? _secondText;
private TextBox? _thirdText;
private TextBox? _fourthText;
private TextBox? _portText;
public static readonly StyledProperty<bool> ShowPortProperty = AvaloniaProperty.Register<IPv4Box, bool>(
nameof(ShowPort));
public bool ShowPort
{
get => GetValue(ShowPortProperty);
set => SetValue(ShowPortProperty, value);
}
public static readonly StyledProperty<IPAddress> IPAddressProperty = AvaloniaProperty.Register<IPv4Box, IPAddress>(
nameof(IPAddress));
@@ -45,31 +56,49 @@ public class IPv4Box: TemplatedControl
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_firstTextPresenter = e.NameScope.Find<TextPresenter>(PART_FirstTextPresenter);
_secondTextPresenter = e.NameScope.Find<TextPresenter>(PART_SecondTextPresenter);
_thirdTextPresenter = e.NameScope.Find<TextPresenter>(PART_ThirdTextPresenter);
_fourthTextPresenter = e.NameScope.Find<TextPresenter>(PART_FourthTextPresenter);
_portNumericInput = e.NameScope.Find<NumericUpDown>(PART_PortNumericInput);
var box = new TextBox();
_firstText = e.NameScope.Find<TextBox>(PART_FirstTextBox);
_secondText = e.NameScope.Find<TextBox>(PART_SecondTextBox);
_thirdText = e.NameScope.Find<TextBox>(PART_ThirdTextBox);
_fourthText = e.NameScope.Find<TextBox>(PART_FourthTextBox);
_portText = e.NameScope.Find<TextBox>(PART_PortTextBox);
_firstText.LostFocus += OnTextLostFocus;
}
private void OnTextKeyDown(object sender, KeyEventArgs args)
{
if (args.Key == Key.Right)
{
_secondText?.Focus();
}
}
private void OnTextLostFocus(object? sender, RoutedEventArgs args)
{
if (sender?.Equals(_firstText)??false)
{
Debug.WriteLine("FIRST");
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Left)
{
if ((_firstTextPresenter?.IsFocused??false) && _firstTextPresenter.SelectionStart == 0)
if ((_firstText?.IsFocused??false) && _firstText.SelectionStart == 0)
{
_secondTextPresenter?.Focus();
_secondText?.Focus();
}
}
else if (e.Key == Key.Right)
{
if ((_firstTextPresenter?.IsFocused ?? false) && _firstTextPresenter?.SelectionEnd == 2)
if ((_firstText?.IsFocused ?? false) && _firstText?.SelectionEnd == 2)
{
_firstTextPresenter.Focus();
_firstText.Focus();
}
}
else if (e.Key == Key.Tab)
@@ -82,4 +111,13 @@ public class IPv4Box: TemplatedControl
}
base.OnKeyDown(e);
}
protected override void OnTextInput(TextInputEventArgs e)
{
if (_firstText != null)
{
_firstText.Text = e.Text;
}
base.OnTextInput(e);
}
}