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"> <TabItem Header="Banner">
<pages:BannerDemo /> <pages:BannerDemo />
</TabItem> </TabItem>
<TabItem Header="IPv4Box">
<pages:IPv4BoxDemo />
</TabItem>
</TabControl> </TabControl>
</Grid> </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> <ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="Badge.axaml" /> <ResourceInclude Source="Badge.axaml" />
<ResourceInclude Source="Banner.axaml" /> <ResourceInclude Source="Banner.axaml" />
<ResourceInclude Source="IPv4Box.axaml" />
</ResourceDictionary.MergedDictionaries> </ResourceDictionary.MergedDictionaries>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Net; using System.Net;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
@@ -5,27 +6,37 @@ using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters; using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity;
namespace Ursa.Controls; namespace Ursa.Controls;
[TemplatePart(PART_FirstTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_FirstTextBox, typeof(TextBox))]
[TemplatePart(PART_SecondTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_SecondTextBox, typeof(TextBox))]
[TemplatePart(PART_ThirdTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_ThirdTextBox, typeof(TextBox))]
[TemplatePart(PART_FourthTextPresenter, typeof(TextPresenter))] [TemplatePart(PART_FourthTextBox, typeof(TextBox))]
[TemplatePart(PART_PortNumericInput, typeof(NumericUpDown))] [TemplatePart(PART_PortTextBox, typeof(TextBox))]
public class IPv4Box: TemplatedControl public class IPv4Box: TemplatedControl
{ {
public const string PART_FirstTextPresenter = "PART_FirstTextPresenter"; public const string PART_FirstTextBox = "PART_FirstTextPresenter";
public const string PART_SecondTextPresenter = "PART_SecondTextPresenter"; public const string PART_SecondTextBox = "PART_SecondTextPresenter";
public const string PART_ThirdTextPresenter = "PART_ThirdTextPresenter"; public const string PART_ThirdTextBox = "PART_ThirdTextPresenter";
public const string PART_FourthTextPresenter = "PART_FourthTextPresenter"; public const string PART_FourthTextBox = "PART_FourthTextPresenter";
public const string PART_PortNumericInput = "PART_PortNumericInput"; public const string PART_PortTextBox = "PART_PortNumericInput";
private TextPresenter? _firstTextPresenter; private TextBox? _firstText;
private TextPresenter? _secondTextPresenter; private TextBox? _secondText;
private TextPresenter? _thirdTextPresenter; private TextBox? _thirdText;
private TextPresenter? _fourthTextPresenter; private TextBox? _fourthText;
private NumericUpDown? _portNumericInput; 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>( public static readonly StyledProperty<IPAddress> IPAddressProperty = AvaloniaProperty.Register<IPv4Box, IPAddress>(
nameof(IPAddress)); nameof(IPAddress));
@@ -34,7 +45,7 @@ public class IPv4Box: TemplatedControl
get => GetValue(IPAddressProperty); get => GetValue(IPAddressProperty);
set => SetValue(IPAddressProperty, value); set => SetValue(IPAddressProperty, value);
} }
public static readonly StyledProperty<int> PortProperty = AvaloniaProperty.Register<IPv4Box, int>( public static readonly StyledProperty<int> PortProperty = AvaloniaProperty.Register<IPv4Box, int>(
nameof(Port)); nameof(Port));
@@ -44,32 +55,50 @@ public class IPv4Box: TemplatedControl
set => SetValue(PortProperty, value); set => SetValue(PortProperty, value);
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
_firstTextPresenter = e.NameScope.Find<TextPresenter>(PART_FirstTextPresenter); _firstText = e.NameScope.Find<TextBox>(PART_FirstTextBox);
_secondTextPresenter = e.NameScope.Find<TextPresenter>(PART_SecondTextPresenter); _secondText = e.NameScope.Find<TextBox>(PART_SecondTextBox);
_thirdTextPresenter = e.NameScope.Find<TextPresenter>(PART_ThirdTextPresenter); _thirdText = e.NameScope.Find<TextBox>(PART_ThirdTextBox);
_fourthTextPresenter = e.NameScope.Find<TextPresenter>(PART_FourthTextPresenter); _fourthText = e.NameScope.Find<TextBox>(PART_FourthTextBox);
_portNumericInput = e.NameScope.Find<NumericUpDown>(PART_PortNumericInput); _portText = e.NameScope.Find<TextBox>(PART_PortTextBox);
var box = new TextBox();
_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) protected override void OnKeyDown(KeyEventArgs e)
{ {
if (e.Key == Key.Left) 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) 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) else if (e.Key == Key.Tab)
@@ -82,4 +111,13 @@ public class IPv4Box: TemplatedControl
} }
base.OnKeyDown(e); base.OnKeyDown(e);
} }
protected override void OnTextInput(TextInputEventArgs e)
{
if (_firstText != null)
{
_firstText.Text = e.Text;
}
base.OnTextInput(e);
}
} }