Files
Shrlalgo.RvKits/WPFluent/Controls/PasswordBox/PasswordHelper.cs

104 lines
3.5 KiB
C#
Raw Normal View History

2025-07-11 09:20:23 +08:00
using System.Diagnostics;
namespace WPFluent.Controls;
public partial class PasswordBox
{
private class PasswordHelper
{
2025-07-11 09:20:23 +08:00
private string currentPassword;
private string currentText;
private string newPasswordValue;
private readonly PasswordBox passwordBox;
public PasswordHelper(PasswordBox passwordBox)
{
2025-07-11 09:20:23 +08:00
this.passwordBox = passwordBox;
currentText = string.Empty;
newPasswordValue = string.Empty;
currentPassword = string.Empty;
}
private bool IsDeleteOption()
{
2025-07-11 09:20:23 +08:00
Debug.Assert(currentText == passwordBox.Text, "_currentText == _passwordBox.Text");
Debug.Assert(currentPassword == passwordBox.Password, "_currentPassword == _passwordBox.Password");
2025-07-11 09:20:23 +08:00
return currentText.Length < currentPassword.Length;
}
private void UpdatePasswordWithInputCharacter(int insertIndex, string insertValue)
{
2025-07-11 09:20:23 +08:00
Debug.Assert(currentText == passwordBox.Text, "_currentText == _passwordBox.Text");
2025-07-11 09:20:23 +08:00
if(currentText.Length == newPasswordValue.Length)
{
// If it's a direct character replacement, remove the existing one before inserting the new one.
2025-07-11 09:20:23 +08:00
newPasswordValue = newPasswordValue.Remove(insertIndex, 1).Insert(insertIndex, insertValue);
}
else
{
2025-07-11 09:20:23 +08:00
newPasswordValue = newPasswordValue.Insert(insertIndex, insertValue);
}
}
public string GetNewPassword()
{
2025-07-11 09:20:23 +08:00
currentPassword = GetPassword();
newPasswordValue = currentPassword;
currentText = passwordBox.Text;
var selectionIndex = passwordBox.SelectionStart;
var passwordChar = passwordBox.PasswordChar;
var newCharacters = currentText.Replace(passwordChar.ToString(), string.Empty);
bool isDeleted = false;
if(IsDeleteOption())
{
2025-07-11 09:20:23 +08:00
newPasswordValue = currentPassword.Remove(selectionIndex, currentPassword.Length - currentText.Length);
isDeleted = true;
}
switch(newCharacters.Length)
{
case > 1:
{
2025-07-11 09:20:23 +08:00
var index = currentText.IndexOf(newCharacters[0]);
2025-07-11 09:20:23 +08:00
newPasswordValue =
index > newPasswordValue.Length - 1
? $"{newPasswordValue}{newCharacters}"
: newPasswordValue.Insert(index, newCharacters);
break;
}
case 1:
{
2025-07-11 09:20:23 +08:00
for(int i = 0; i < currentText.Length; i++)
{
2025-07-11 09:20:23 +08:00
if(currentText[i] == passwordChar)
{
continue;
}
2025-07-11 09:20:23 +08:00
UpdatePasswordWithInputCharacter(i, currentText[i].ToString());
break;
}
break;
}
case 0 when !isDeleted:
{
// The input is a PasswordChar, which is to be inserted at the designated position.
int insertIndex = selectionIndex - 1;
UpdatePasswordWithInputCharacter(insertIndex, passwordChar.ToString());
break;
}
}
2025-07-11 09:20:23 +08:00
return newPasswordValue;
}
2025-07-11 09:20:23 +08:00
public string GetPassword() => passwordBox.Password ?? string.Empty;
}
}