Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add setting to login dialog in order to change the focus from the username field to the password field when pressing enter #4529

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/MahApps.Metro/Controls/Dialogs/LoginDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ public bool RememberCheckBoxChecked
set => this.SetValue(RememberCheckBoxCheckedProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="ChangeFocusOnUsernameFieldWithEnterProperty"/> dependency property.</summary>
public static readonly DependencyProperty ChangeFocusOnUsernameFieldWithEnterProperty
= DependencyProperty.Register(nameof(ChangeFocusOnUsernameFieldWithEnter),
typeof(bool),
typeof(LoginDialog),
new PropertyMetadata(default(bool)));

public bool ChangeFocusOnUsernameFieldWithEnter
{
get => (bool) this.GetValue(ChangeFocusOnUsernameFieldWithEnterProperty);
set => this.SetValue(ChangeFocusOnUsernameFieldWithEnterProperty, value);
}

#endregion DependencyProperties

#region Constructor
Expand Down Expand Up @@ -257,6 +270,7 @@ internal LoginDialog(MetroWindow? parentWindow, LoginDialogSettings? settings)
this.SetCurrentValue(RememberCheckBoxVisibilityProperty, loginDialogSettings.RememberCheckBoxVisibility);
this.SetCurrentValue(RememberCheckBoxTextProperty, loginDialogSettings.RememberCheckBoxText);
this.SetCurrentValue(RememberCheckBoxCheckedProperty, loginDialogSettings.RememberCheckBoxChecked);
this.SetCurrentValue(ChangeFocusOnUsernameFieldWithEnterProperty, loginDialogSettings.ChangeFocusOnUsernameFieldWithEnter);
}
}

Expand Down Expand Up @@ -299,16 +313,23 @@ private void OnKeyDownHandler(object sender, KeyEventArgs e)
}
else if (e.Key == Key.Enter)
{
this.CleanUpHandlers();

this.tcs.TrySetResult(!ReferenceEquals(sender, this.PART_NegativeButton)
? new LoginDialogData
{
Username = this.Username,
SecurePassword = this.PART_PasswordBox?.SecurePassword,
ShouldRemember = this.RememberCheckBoxChecked
}
: null!);
if (PART_TextBox != null && PART_TextBox.IsFocused && ChangeFocusOnUsernameFieldWithEnter)
{
PART_PasswordBox?.Focus();
}
else
{
this.CleanUpHandlers();

this.tcs.TrySetResult(!ReferenceEquals(sender, this.PART_NegativeButton)
? new LoginDialogData
{
Username = this.Username,
SecurePassword = this.PART_PasswordBox?.SecurePassword,
ShouldRemember = this.RememberCheckBoxChecked
}
: null!);
}

e.Handled = true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/MahApps.Metro/Controls/Dialogs/LoginDialogSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ public LoginDialogSettings(MetroDialogSettings? source)
public string RememberCheckBoxText { get; set; } = DefaultRememberCheckBoxText;

public bool RememberCheckBoxChecked { get; set; } = false;

public bool ChangeFocusOnUsernameFieldWithEnter { get; set; }
}
}