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 ChangeFocusWithEnterOnUsernameField to LoginDialogSettings #4527

Closed
Closed
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="ChangeFocusWithEnterOnUsernameField"/> dependency property.</summary>
public static readonly DependencyProperty ChangeFocusWithEnterOnUsernameFieldProperty
= DependencyProperty.Register(nameof(ChangeFocusWithEnterOnUsernameField),
typeof(bool),
typeof(LoginDialog),
new PropertyMetadata(default(bool)));

public bool ChangeFocusWithEnterOnUsernameField
{
get => (bool) this.GetValue(ChangeFocusWithEnterOnUsernameFieldProperty);
set => this.SetValue(ChangeFocusWithEnterOnUsernameFieldProperty, 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(ChangeFocusWithEnterOnUsernameFieldProperty, loginDialogSettings.ChangeFocusWithEnterOnUsernameField);
}
}

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 && ChangeFocusWithEnterOnUsernameField)
{
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 ChangeFocusWithEnterOnUsernameField { get; set; } = false;
}
}
Loading