code_review: PR #714

* remove `string.ToLower` warning
* override `OnLoaded` method directly
* clean namespace using

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2024-11-19 09:27:31 +08:00
parent ea1d966d27
commit d07a664166
No known key found for this signature in database
3 changed files with 25 additions and 26 deletions

View file

@ -6,20 +6,20 @@ namespace SourceGit.Models
{ {
public static readonly ResetMode[] Supported = public static readonly ResetMode[] Supported =
[ [
new ResetMode("Soft", "Keep all changes. Stage differences", "--soft", 'S', Brushes.Green), new ResetMode("Soft", "Keep all changes. Stage differences", "--soft", "S", Brushes.Green),
new ResetMode("Mixed", "Keep all changes. Unstage differences", "--mixed", 'M',Brushes.Orange), new ResetMode("Mixed", "Keep all changes. Unstage differences", "--mixed", "M", Brushes.Orange),
new ResetMode("Merge", "Reset while keeping unmerged changes", "--merge", 'G',Brushes.Purple), new ResetMode("Merge", "Reset while keeping unmerged changes", "--merge", "G", Brushes.Purple),
new ResetMode("Keep", "Reset while keeping local modifications", "--keep", 'K',Brushes.Purple), new ResetMode("Keep", "Reset while keeping local modifications", "--keep", "K", Brushes.Purple),
new ResetMode("Hard", "Discard all changes", "--hard", 'H',Brushes.Red), new ResetMode("Hard", "Discard all changes", "--hard", "H", Brushes.Red),
]; ];
public string Name { get; set; } public string Name { get; set; }
public string Desc { get; set; } public string Desc { get; set; }
public string Arg { get; set; } public string Arg { get; set; }
public char Key { get; set; } public string Key { get; set; }
public IBrush Color { get; set; } public IBrush Color { get; set; }
public ResetMode(string n, string d, string a, char k, IBrush b) public ResetMode(string n, string d, string a, string k, IBrush b)
{ {
Name = n; Name = n;
Desc = d; Desc = d;

View file

@ -7,7 +7,6 @@
xmlns:c="using:SourceGit.Converters" xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.Reset" x:Class="SourceGit.Views.Reset"
Loaded="Control_OnLoaded"
x:DataType="vm:Reset"> x:DataType="vm:Reset">
<StackPanel Orientation="Vertical" Margin="8,0"> <StackPanel Orientation="Vertical" Margin="8,0">
<TextBlock FontSize="18" <TextBlock FontSize="18"
@ -37,12 +36,13 @@
HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0" Margin="0,0,8,0"
Text="{DynamicResource Text.Reset.Mode}"/> Text="{DynamicResource Text.Reset.Mode}"/>
<ComboBox x:Name="ResetMode" Grid.Row="2" Grid.Column="1" <ComboBox Grid.Row="2" Grid.Column="1"
x:Name="ResetMode"
Height="28" Padding="8,0" Height="28" Padding="8,0"
VerticalAlignment="Center" HorizontalAlignment="Stretch" VerticalAlignment="Center" HorizontalAlignment="Stretch"
KeyDown="InputElement_OnKeyDown"
ItemsSource="{Binding Source={x:Static m:ResetMode.Supported}}" ItemsSource="{Binding Source={x:Static m:ResetMode.Supported}}"
SelectedItem="{Binding SelectedMode, Mode=TwoWay}"> SelectedItem="{Binding SelectedMode, Mode=TwoWay}"
KeyDown="OnResetModeKeyDown">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>
<DataTemplate DataType="m:ResetMode"> <DataTemplate DataType="m:ResetMode">
<Grid ColumnDefinitions="16,60,*"> <Grid ColumnDefinitions="16,60,*">

View file

@ -1,8 +1,6 @@
using System.Linq;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using SourceGit.Models;
namespace SourceGit.Views namespace SourceGit.Views
{ {
@ -13,27 +11,28 @@ namespace SourceGit.Views
InitializeComponent(); InitializeComponent();
} }
private void InputElement_OnKeyDown(object sender, KeyEventArgs e) protected override void OnLoaded(RoutedEventArgs e)
{ {
var key = e.Key.ToString().ToLower(); base.OnLoaded(e);
foreach (var item in ResetMode.ItemsSource)
ResetMode.Focus();
}
private void OnResetModeKeyDown(object sender, KeyEventArgs e)
{
if (sender is ComboBox comboBox)
{ {
if (item.GetType() == typeof(ResetMode)) var key = e.Key.ToString();
for (int i = 0; i < Models.ResetMode.Supported.Length; i++)
{ {
var resetMode = (ResetMode)item; if (key.Equals(Models.ResetMode.Supported[i].Key, System.StringComparison.OrdinalIgnoreCase))
if (resetMode.Key.ToString().ToLower() == key)
{ {
ResetMode.SelectedValue = resetMode; comboBox.SelectedIndex = i;
e.Handled = true;
return; return;
} }
} }
} }
} }
private void Control_OnLoaded(object sender, RoutedEventArgs e)
{
ResetMode.Focus();
}
} }
} }