Merge pull request #156 from irihitech/146-ipv4

Improve IPV4 input interaction with Numpad
This commit is contained in:
Dong Bin
2024-03-16 17:09:11 +08:00
committed by GitHub
2 changed files with 28 additions and 32 deletions

View File

@@ -4,7 +4,7 @@
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here --> <!-- Add Resources Here -->
<Design.PreviewWith> <Design.PreviewWith>
<u:NumPad></u:NumPad> <u:NumPad />
</Design.PreviewWith> </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">
@@ -18,22 +18,26 @@
<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="UseLayoutRounding" Value="False" />
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter> <Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch"></Setter> <Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Margin" Value="1" /> <Setter Property="Margin" Value="1" />
</Style> </Style>
</Border.Styles> </Border.Styles>
<Grid ColumnDefinitions="*,*,*,*" RowDefinitions="*,*,*,*,*" HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid
HorizontalAlignment="Left"
VerticalAlignment="Top"
ColumnDefinitions="*,*,*,*"
RowDefinitions="*,*,*,*,*">
<ToggleButton <ToggleButton
Grid.Row="0" Grid.Row="0"
Grid.Column="0" Grid.Column="0"
MinWidth="54" MinWidth="54"
MinHeight="54" MinHeight="54"
Margin="1"
Padding="0" Padding="0"
Focusable="False" Focusable="False"
FontWeight="Regular" FontWeight="Regular"
Margin="1"
IsChecked="{TemplateBinding NumMode, IsChecked="{TemplateBinding NumMode,
Mode=TwoWay}"> Mode=TwoWay}">
<TextBlock> <TextBlock>
@@ -84,7 +88,7 @@
NumContent="9" NumContent="9"
NumKey="NumPad9"> NumKey="NumPad9">
<u:NumPadButton.FunctionContent> <u:NumPadButton.FunctionContent>
<TextBlock><Run Text="Page"/><LineBreak/><Run Text="Up"/></TextBlock> <TextBlock><Run Text="Page" /><LineBreak /><Run Text="Up" /></TextBlock>
</u:NumPadButton.FunctionContent> </u:NumPadButton.FunctionContent>
</u:NumPadButton> </u:NumPadButton>
<u:NumPadButton <u:NumPadButton
@@ -94,8 +98,8 @@
Height="{x:Static x:Double.NaN}" Height="{x:Static x:Double.NaN}"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
FunctionContent="+" FunctionContent="+"
NumContent="+"
FunctionKey="Add" FunctionKey="Add"
NumContent="+"
NumKey="Add" /> NumKey="Add" />
<u:NumPadButton <u:NumPadButton
Grid.Row="2" Grid.Row="2"
@@ -139,7 +143,7 @@
NumContent="3" NumContent="3"
NumKey="NumPad3"> NumKey="NumPad3">
<u:NumPadButton.FunctionContent> <u:NumPadButton.FunctionContent>
<TextBlock><Run Text="Page"/><LineBreak/><Run Text="Down"/></TextBlock> <TextBlock><Run Text="Page" /><LineBreak /><Run Text="Down" /></TextBlock>
</u:NumPadButton.FunctionContent> </u:NumPadButton.FunctionContent>
</u:NumPadButton> </u:NumPadButton>
<u:NumPadButton <u:NumPadButton
@@ -150,15 +154,16 @@
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
FunctionContent="Enter" FunctionContent="Enter"
FunctionKey="Enter" FunctionKey="Enter"
NumContent="Enter" /> NumContent="Enter"
NumKey="Enter" />
<u:NumPadButton <u:NumPadButton
Grid.Row="4" Grid.Row="4"
Grid.Column="0" Grid.Column="0"
Grid.ColumnSpan="2" Grid.ColumnSpan="2"
Width="{x:Static x:Double.NaN}"
FunctionContent="Insert" FunctionContent="Insert"
FunctionKey="Insert" FunctionKey="Insert"
NumContent="0" NumContent="0"
Width="{x:Static x:Double.NaN}"
NumKey="NumPad0" /> NumKey="NumPad0" />
<u:NumPadButton <u:NumPadButton
Grid.Row="4" Grid.Row="4"
@@ -166,7 +171,7 @@
FunctionContent="Delete" FunctionContent="Delete"
FunctionKey="Delete" FunctionKey="Delete"
NumContent="." NumContent="."
NumKey="Decimal"/> NumKey="Decimal" />
</Grid> </Grid>
</Border> </Border>
</ControlTemplate> </ControlTemplate>

View File

@@ -136,7 +136,7 @@ public class IPv4Box: TemplatedControl
if (_currentActivePresenter is null) return; if (_currentActivePresenter is null) return;
var keymap = TopLevel.GetTopLevel(this)?.PlatformSettings?.HotkeyConfiguration; var keymap = TopLevel.GetTopLevel(this)?.PlatformSettings?.HotkeyConfiguration;
bool Match(List<KeyGesture> gestures) => gestures.Any(g => g.Matches(e)); bool Match(List<KeyGesture> gestures) => gestures.Any(g => g.Matches(e));
if (e.Key == Key.Enter) if (e.Key is Key.Enter or Key.Return)
{ {
ParseBytes(ShowLeadingZero); ParseBytes(ShowLeadingZero);
SetIPAddressInternal(); SetIPAddressInternal();
@@ -170,25 +170,6 @@ public class IPv4Box: TemplatedControl
_currentActivePresenter?.ShowCaret(); _currentActivePresenter?.ShowCaret();
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
{
if (string.IsNullOrEmpty(_currentActivePresenter.Text))
{
base.OnKeyDown(e);
return;
}
_currentActivePresenter?.HideCaret();
_currentActivePresenter.ClearSelection();
if (Equals(_currentActivePresenter, _fourthText))
{
base.OnKeyDown(e);
return;
}
MoveToNextPresenter(_currentActivePresenter, false);
_currentActivePresenter?.ShowCaret();
_currentActivePresenter.MoveCaretToStart();
e.Handled = true;
}
else if (e.Key == Key.Back) else if (e.Key == Key.Back)
{ {
DeleteImplementation(_currentActivePresenter); DeleteImplementation(_currentActivePresenter);
@@ -212,6 +193,16 @@ public class IPv4Box: TemplatedControl
if (e.Handled) return; if (e.Handled) return;
string? s = e.Text; string? s = e.Text;
if (string.IsNullOrEmpty(s)) return; if (string.IsNullOrEmpty(s)) return;
if (s == ".")
{
_currentActivePresenter?.HideCaret();
_currentActivePresenter.ClearSelection();
MoveToNextPresenter(_currentActivePresenter, false);
_currentActivePresenter?.ShowCaret();
_currentActivePresenter.MoveCaretToStart();
e.Handled = false;
return;
}
if (!char.IsNumber(s![0])) return; if (!char.IsNumber(s![0])) return;
if (_currentActivePresenter != null) if (_currentActivePresenter != null)
{ {