From d07a664166e77965509fb4a7940209a509ad6914 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 19 Nov 2024 09:27:31 +0800 Subject: [PATCH] code_review: PR #714 * remove `string.ToLower` warning * override `OnLoaded` method directly * clean namespace using Signed-off-by: leo --- src/Models/ResetMode.cs | 14 +++++++------- src/Views/Reset.axaml | 8 ++++---- src/Views/Reset.axaml.cs | 29 ++++++++++++++--------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/Models/ResetMode.cs b/src/Models/ResetMode.cs index 312302a8..827ccaa9 100644 --- a/src/Models/ResetMode.cs +++ b/src/Models/ResetMode.cs @@ -6,20 +6,20 @@ namespace SourceGit.Models { public static readonly ResetMode[] Supported = [ - 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("Merge", "Reset while keeping unmerged changes", "--merge", 'G',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("Soft", "Keep all changes. Stage differences", "--soft", "S", Brushes.Green), + 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("Keep", "Reset while keeping local modifications", "--keep", "K", Brushes.Purple), + new ResetMode("Hard", "Discard all changes", "--hard", "H", Brushes.Red), ]; public string Name { get; set; } public string Desc { get; set; } public string Arg { get; set; } - public char Key { get; set; } + public string Key { 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; Desc = d; diff --git a/src/Views/Reset.axaml b/src/Views/Reset.axaml index 5c3025e9..b3a18e49 100644 --- a/src/Views/Reset.axaml +++ b/src/Views/Reset.axaml @@ -7,7 +7,6 @@ xmlns:c="using:SourceGit.Converters" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="SourceGit.Views.Reset" - Loaded="Control_OnLoaded" x:DataType="vm:Reset"> - + SelectedItem="{Binding SelectedMode, Mode=TwoWay}" + KeyDown="OnResetModeKeyDown"> diff --git a/src/Views/Reset.axaml.cs b/src/Views/Reset.axaml.cs index cefcf426..8c380538 100644 --- a/src/Views/Reset.axaml.cs +++ b/src/Views/Reset.axaml.cs @@ -1,8 +1,6 @@ -using System.Linq; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; -using SourceGit.Models; namespace SourceGit.Views { @@ -13,27 +11,28 @@ namespace SourceGit.Views InitializeComponent(); } - private void InputElement_OnKeyDown(object sender, KeyEventArgs e) + protected override void OnLoaded(RoutedEventArgs e) { - var key = e.Key.ToString().ToLower(); - foreach (var item in ResetMode.ItemsSource) + base.OnLoaded(e); + + 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 (resetMode.Key.ToString().ToLower() == key) + if (key.Equals(Models.ResetMode.Supported[i].Key, System.StringComparison.OrdinalIgnoreCase)) { - ResetMode.SelectedValue = resetMode; + comboBox.SelectedIndex = i; + e.Handled = true; return; } - } } } - - private void Control_OnLoaded(object sender, RoutedEventArgs e) - { - ResetMode.Focus(); - } } }