feat: upgrade version. implement more keys.

This commit is contained in:
rabbitism
2024-03-10 12:33:26 +08:00
parent dd31df874a
commit 7e7c020b57
4 changed files with 183 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AvaloniaVersion>11.0.9</AvaloniaVersion> <AvaloniaVersion>11.0.10</AvaloniaVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -1,15 +1,36 @@
<UserControl xmlns="https://github.com/avaloniaui" <UserControl
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="Ursa.Demo.Pages.NumPadDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns="https://github.com/avaloniaui"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="Ursa.Demo.Pages.NumPadDemo"> xmlns:u="https://irihi.tech/ursa"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<StackPanel HorizontalAlignment="Left"> <StackPanel HorizontalAlignment="Left">
<TextBox u:NumPad.Attach="True" Width="100" Watermark="Invoke NumPad"></TextBox> <TextBox
<TextBox u:NumPad.Attach="True" Width="100" Watermark="Invoke NumPad"></TextBox> HorizontalAlignment="Stretch"
<TextBox u:NumPad.Attach="True" Width="100" Watermark="Invoke NumPad"></TextBox> u:NumPad.Attach="True"
<u:IPv4Box u:NumPad.Attach="True" Width="200" ></u:IPv4Box> Watermark="Invoke NumPad" />
<u:NumericIntUpDown u:NumPad.Attach="True" Width="100" Watermark="Invoke NumPad"></u:NumericIntUpDown> <TextBox
HorizontalAlignment="Stretch"
u:NumPad.Attach="True"
Watermark="Invoke NumPad" />
<TextBox
HorizontalAlignment="Stretch"
u:NumPad.Attach="True"
Watermark="Invoke NumPad" />
<u:IPv4Box Width="200" u:NumPad.Attach="True" />
<u:NumericIntUpDown
HorizontalAlignment="Stretch"
u:NumPad.Attach="True"
Watermark="Invoke NumPad" />
<Border Theme="{DynamicResource CardBorder}">
<StackPanel>
<TextBox Name="text" Width="200" />
<u:NumPad Target="{Binding #text}" />
</StackPanel>
</Border>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -3,34 +3,37 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here --> <!-- Add Resources Here -->
<Design.PreviewWith>
<u:NumPad></u:NumPad>
</Design.PreviewWith>
<ControlTheme x:Key="{x:Type u:NumPad}" TargetType="{x:Type u:NumPad}"> <ControlTheme x:Key="{x:Type u:NumPad}" TargetType="{x:Type u:NumPad}">
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:NumPad"> <ControlTemplate TargetType="u:NumPad">
<Border> <Border>
<Border.Styles> <Border.Styles>
<Style Selector="RepeatButton">
<Setter Property="Command" Value="{Binding $parent[u:NumPad].ProcessClick}" />
<Setter Property="CommandParameter" Value="{Binding $self.Content}" />
<Setter Property="Width" Value="48" />
<Setter Property="Height" Value="48" />
</Style>
<Style Selector="u|NumPadButton"> <Style Selector="u|NumPadButton">
<Setter Property="NumMode" Value="{Binding $parent[u:NumPad].NumMode}" /> <Setter Property="NumMode" Value="{Binding $parent[u:NumPad].NumMode}" />
<Setter Property="Command" Value="{Binding $parent[u:NumPad].ProcessClick}" /> <Setter Property="Command" Value="{Binding $parent[u:NumPad].ProcessClick}" />
<Setter Property="Focusable" Value="False"></Setter> <Setter Property="Focusable" Value="False" />
<Setter Property="CommandParameter" Value="{Binding $self}" /> <Setter Property="CommandParameter" Value="{Binding $self}" />
<Setter Property="Width" Value="54" /> <Setter Property="Width" Value="54" />
<Setter Property="Height" Value="54" /> <Setter Property="Height" Value="54" />
<Setter Property="UseLayoutRounding" Value="False"></Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
<Setter Property="VerticalAlignment" Value="Stretch"></Setter>
<Setter Property="Margin" Value="1" />
</Style> </Style>
</Border.Styles> </Border.Styles>
<Grid ColumnDefinitions="*,*,*,*" RowDefinitions="*,*,*,*,*"> <Grid ColumnDefinitions="*,*,*,*" RowDefinitions="*,*,*,*,*" HorizontalAlignment="Left" VerticalAlignment="Top">
<ToggleButton <ToggleButton
Grid.Row="0" Grid.Row="0"
Grid.Column="0" Grid.Column="0"
Width="54" MinWidth="54"
Focusable="False" MinHeight="54"
Height="54"
Padding="0" Padding="0"
Focusable="False"
FontWeight="Regular"
Margin="1"
IsChecked="{TemplateBinding NumMode, IsChecked="{TemplateBinding NumMode,
Mode=TwoWay}"> Mode=TwoWay}">
<TextBlock> <TextBlock>
@@ -44,14 +47,122 @@
Grid.Column="1" Grid.Column="1"
FunctionContent="/" FunctionContent="/"
NumContent="/" NumContent="/"
NumKey="OemQuestion" /> NumKey="Divide" />
<u:NumPadButton
Grid.Row="0"
Grid.Column="2"
FunctionContent="*"
NumContent="*"
NumKey="Multiply" />
<u:NumPadButton
Grid.Row="0"
Grid.Column="3"
FunctionContent="-"
NumContent="-"
NumKey="Subtract" />
<u:NumPadButton <u:NumPadButton
Grid.Row="1" Grid.Row="1"
Grid.Column="1" Grid.Column="0"
FunctionContent="Home" FunctionContent="Home"
FunctionKey="Home" FunctionKey="Home"
NumContent="7" NumContent="7"
NumKey="NumPad7" /> NumKey="NumPad7" />
<u:NumPadButton
Grid.Row="1"
Grid.Column="1"
FunctionContent="Up"
FunctionKey="Up"
NumContent="8"
NumKey="NumPad8" />
<u:NumPadButton
Grid.Row="1"
Grid.Column="2"
FunctionKey="PageUp"
NumContent="9"
NumKey="NumPad9">
<u:NumPadButton.FunctionContent>
<TextBlock><Run Text="Page"/><LineBreak/><Run Text="Up"/></TextBlock>
</u:NumPadButton.FunctionContent>
</u:NumPadButton>
<u:NumPadButton
Grid.Row="1"
Grid.RowSpan="2"
Grid.Column="3"
Height="{x:Static x:Double.NaN}"
VerticalAlignment="Stretch"
FunctionContent="+"
NumContent="+"
NumKey="Add" />
<u:NumPadButton
Grid.Row="2"
Grid.Column="0"
FunctionContent="Left"
FunctionKey="Left"
NumContent="4"
NumKey="NumPad4" />
<u:NumPadButton
Grid.Row="2"
Grid.Column="1"
FunctionContent=" "
FunctionKey="None"
NumContent="5"
NumKey="NumPad5" />
<u:NumPadButton
Grid.Row="2"
Grid.Column="2"
FunctionContent="Right"
FunctionKey="Right"
NumContent="6"
NumKey="NumPad6" />
<u:NumPadButton
Grid.Row="3"
Grid.Column="0"
FunctionContent="End"
FunctionKey="End"
NumContent="1"
NumKey="NumPad1" />
<u:NumPadButton
Grid.Row="3"
Grid.Column="1"
FunctionContent="Down"
FunctionKey="Down"
NumContent="2"
NumKey="NumPad2" />
<u:NumPadButton
Grid.Row="3"
Grid.Column="2"
FunctionKey="PageDown"
NumContent="3"
NumKey="NumPad3">
<u:NumPadButton.FunctionContent>
<TextBlock><Run Text="Page"/><LineBreak/><Run Text="Down"/></TextBlock>
</u:NumPadButton.FunctionContent>
</u:NumPadButton>
<u:NumPadButton
Grid.Row="3"
Grid.RowSpan="2"
Grid.Column="3"
Height="{x:Static x:Double.NaN}"
VerticalAlignment="Stretch"
FunctionContent="Enter"
FunctionKey="Enter"
NumContent="Enter" />
<u:NumPadButton
Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="2"
FunctionContent="Insert"
FunctionKey="Insert"
NumContent="0"
Width="{x:Static x:Double.NaN}"
NumKey="NumPad0" />
<u:NumPadButton
Grid.Row="4"
Grid.Column="2"
FunctionContent="Delete"
FunctionKey="Delete"
NumContent="."
NumKey="Decimal"/>
</Grid> </Grid>
</Border> </Border>
</ControlTemplate> </ControlTemplate>
@@ -62,10 +173,13 @@
<Setter Property="Focusable" Value="False" /> <Setter Property="Focusable" Value="False" />
<Setter Property="Cursor" Value="Hand" /> <Setter Property="Cursor" Value="Hand" />
<Setter Property="Background" Value="{DynamicResource ButtonDefaultBackground}" /> <Setter Property="Background" Value="{DynamicResource ButtonDefaultBackground}" />
<Setter Property="CornerRadius" Value="3"/> <Setter Property="CornerRadius" Value="3" />
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:NumPadButton"> <ControlTemplate TargetType="u:NumPadButton">
<Border Name="PART_Background" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}"> <Border
Name="PART_Background"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
<Panel> <Panel>
<ContentPresenter <ContentPresenter
Name="PART_ContentPresenter" Name="PART_ContentPresenter"
@@ -84,10 +198,10 @@
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Style Selector="^:pointerover /template/ Border#PART_Background"> <Style Selector="^:pointerover /template/ Border#PART_Background">
<Setter Property="Background" Value="{DynamicResource ButtonDefaultPointeroverBackground}"></Setter> <Setter Property="Background" Value="{DynamicResource ButtonDefaultPointeroverBackground}" />
</Style> </Style>
<Style Selector="^:pressed /template/ Border#PART_Background"> <Style Selector="^:pressed /template/ Border#PART_Background">
<Setter Property="Background" Value="{DynamicResource ButtonDefaultPressedBackground}"></Setter> <Setter Property="Background" Value="{DynamicResource ButtonDefaultPressedBackground}" />
</Style> </Style>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -76,14 +76,30 @@ public class NumPad: TemplatedControl
[Key.NumPad7] = "7", [Key.NumPad7] = "7",
[Key.NumPad8] = "8", [Key.NumPad8] = "8",
[Key.NumPad9] = "9", [Key.NumPad9] = "9",
[Key.OemPlus] = "+", [Key.Add] = "+",
[Key.OemMinus] = "-", [Key.Subtract] = "-",
[Key.Multiply] = "*",
[Key.Divide] = "/",
[Key.Decimal] = ".",
}; };
public void ProcessClick(object o) public void ProcessClick(object o)
{ {
if (Target?.IsFocused != true)
{
return;
}
if (o is NumPadButton b) if (o is NumPadButton b)
{ {
if (b.NumKey is null)
{
Target?.RaiseEvent(new KeyEventArgs()
{
Source = this,
RoutedEvent = KeyDownEvent,
Key = b.FunctionKey ?? Key.None,
});
}
if (b is { NumMode: true, NumKey: not null }) if (b is { NumMode: true, NumKey: not null })
{ {
Target?.RaiseEvent(new TextInputEventArgs() Target?.RaiseEvent(new TextInputEventArgs()