From b7089275f6ae6a799f34cf51b211c15434b52702 Mon Sep 17 00:00:00 2001 From: Silence Tai Date: Sun, 14 Jul 2019 16:36:47 +0800 Subject: [PATCH] Fix #2458 Unexpected delete server behavior (#2459) * Fix shadowsocks/shadowsocks-windows/#2458 * Servers List Box position adjustment is consistent with the v4.1.6 version --- shadowsocks-csharp/View/ConfigForm.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 963a5d4a..b99f19ca 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Runtime.CompilerServices; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; @@ -89,12 +88,6 @@ namespace Shadowsocks.View ApplyButton.Enabled = true; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void UpdateIndexToEnd() - { - _lastSelectedIndex = (ServersListBox.SelectedIndex = (_modifiedConfiguration.configs.Count - 1)); - } - private bool ValidateAndSaveSelectedServerDetails(bool isSave = false, bool isCopy = false) { try @@ -157,8 +150,11 @@ namespace Shadowsocks.View _modifiedConfiguration.configs.RemoveAt(_lastSelectedIndex); ServersListBox.SelectedIndexChanged -= ServersListBox_SelectedIndexChanged; + int lastIndex = ServersListBox.SelectedIndex; + LoadServerNameListToUI(_modifiedConfiguration); - UpdateIndexToEnd(); + + _lastSelectedIndex = (ServersListBox.SelectedIndex = lastIndex); ServersListBox.SelectedIndexChanged += ServersListBox_SelectedIndexChanged; return true; @@ -453,7 +449,7 @@ namespace Shadowsocks.View { Configuration.AddDefaultServerOrServer(_modifiedConfiguration); LoadServerNameListToUI(_modifiedConfiguration); - UpdateIndexToEnd(); + _lastSelectedIndex = (ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1); } } @@ -462,9 +458,9 @@ namespace Shadowsocks.View if (ValidateAndSaveSelectedServerDetails(isCopy: true)) { Server currServer = _modifiedConfiguration.configs[_lastSelectedIndex]; - Configuration.AddDefaultServerOrServer(_modifiedConfiguration, currServer); + Configuration.AddDefaultServerOrServer(_modifiedConfiguration, currServer, _lastSelectedIndex + 1); LoadServerNameListToUI(_modifiedConfiguration); - UpdateIndexToEnd(); + _lastSelectedIndex = (ServersListBox.SelectedIndex = (_lastSelectedIndex + 1)); } } @@ -478,7 +474,11 @@ namespace Shadowsocks.View } LoadServerNameListToUI(_modifiedConfiguration); - UpdateIndexToEnd(); + ServersListBox.SelectedIndexChanged -= ServersListBox_SelectedIndexChanged; + + _lastSelectedIndex = (ServersListBox.SelectedIndex = (_lastSelectedIndex >= _modifiedConfiguration.configs.Count ? (_modifiedConfiguration.configs.Count - 1) : _lastSelectedIndex)); + + ServersListBox.SelectedIndexChanged += ServersListBox_SelectedIndexChanged; LoadSelectedServerDetails(); UpdateButtons();