using WPFluent.Input;
using System.Diagnostics;
using System.Windows.Controls;
// ReSharper disable once CheckNamespace
namespace WPFluent.Controls;
///
/// Extended with additional parameters like .
///
public class TextBox : System.Windows.Controls.TextBox
{
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty ClearButtonEnabledProperty = DependencyProperty.Register(
nameof(ClearButtonEnabled),
typeof(bool),
typeof(TextBox),
new PropertyMetadata(true));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty CurrentPlaceholderEnabledProperty = DependencyProperty.Register(
nameof(CurrentPlaceholderEnabled),
typeof(bool),
typeof(TextBox),
new PropertyMetadata(true));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty IconPlacementProperty = DependencyProperty.Register(
nameof(IconPlacement),
typeof(ElementPlacement),
typeof(TextBox),
new PropertyMetadata(ElementPlacement.Left));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
nameof(Icon),
typeof(IconElement),
typeof(TextBox),
new PropertyMetadata(null, null, IconElement.Coerce));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty IsTextSelectionEnabledProperty = DependencyProperty.Register(
nameof(IsTextSelectionEnabled),
typeof(bool),
typeof(TextBox),
new PropertyMetadata(false));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty PlaceholderEnabledProperty = DependencyProperty.Register(
nameof(PlaceholderEnabled),
typeof(bool),
typeof(TextBox),
new PropertyMetadata(true, OnPlaceholderEnabledChanged));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register(
nameof(PlaceholderText),
typeof(string),
typeof(TextBox),
new PropertyMetadata(string.Empty));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register(
nameof(ShowClearButton),
typeof(bool),
typeof(TextBox),
new PropertyMetadata(false));
///
/// Identifies the dependency property.
///
public static readonly DependencyProperty TemplateButtonCommandProperty = DependencyProperty.Register(
nameof(TemplateButtonCommand),
typeof(IRelayCommand),
typeof(TextBox),
new PropertyMetadata(null));
///
/// Initializes a new instance of the class.
///
public TextBox()
{
SetValue(TemplateButtonCommandProperty, new RelayCommand(OnTemplateButtonClick));
CurrentPlaceholderEnabled = PlaceholderEnabled;
}
private static void OnPlaceholderEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if(d is not TextBox control)
{
return;
}
control.OnPlaceholderEnabledChanged();
}
///
/// Hides the clear button by property.
///
protected void HideClearButton()
{
if(ClearButtonEnabled && !IsKeyboardFocusWithin && ShowClearButton)
{
SetCurrentValue(ShowClearButtonProperty, false);
}
}
///
/// Triggered when the user clicks the clear text button.
///
protected virtual void OnClearButtonClick()
{
if(Text.Length > 0)
{
SetCurrentValue(TextProperty, string.Empty);
}
}
///
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
CaretIndex = Text.Length;
RevealClearButton();
}
///
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
HideClearButton();
}
protected virtual void OnPlaceholderEnabledChanged() { SetPlaceholderTextVisibility(); }
///
/// Triggered by clicking a button in the control template.
///
protected virtual void OnTemplateButtonClick(string? parameter)
{
Debug.WriteLine($"INFO: {typeof(TextBox)} button clicked", "WPFluent");
OnClearButtonClick();
}
///
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
SetPlaceholderTextVisibility();
RevealClearButton();
}
///
/// Reveals the clear button by property.
///
protected void RevealClearButton()
{
if(ClearButtonEnabled && IsKeyboardFocusWithin)
{
SetCurrentValue(ShowClearButtonProperty, Text.Length > 0);
}
}
protected void SetPlaceholderTextVisibility()
{
if(PlaceholderEnabled)
{
if(CurrentPlaceholderEnabled && Text.Length > 0)
{
SetCurrentValue(CurrentPlaceholderEnabledProperty, false);
}
if(!CurrentPlaceholderEnabled && Text.Length < 1)
{
SetCurrentValue(CurrentPlaceholderEnabledProperty, true);
}
}
else if(CurrentPlaceholderEnabled)
{
SetCurrentValue(CurrentPlaceholderEnabledProperty, false);
}
}
///
/// Gets or sets a value indicating whether to enable the clear button.
///
public bool ClearButtonEnabled
{
get => (bool)GetValue(ClearButtonEnabledProperty);
set => SetValue(ClearButtonEnabledProperty, value);
}
///
/// Gets or sets a value indicating whether to display the placeholder text.
///
public bool CurrentPlaceholderEnabled
{
get => (bool)GetValue(CurrentPlaceholderEnabledProperty);
protected set => SetValue(CurrentPlaceholderEnabledProperty, value);
}
///
/// Gets or sets displayed .
///
public IconElement? Icon { get => (IconElement?)GetValue(IconProperty); set => SetValue(IconProperty, value); }
///
/// Gets or sets which side the icon should be placed on.
///
public ElementPlacement IconPlacement
{
get => (ElementPlacement)GetValue(IconPlacementProperty);
set => SetValue(IconPlacementProperty, value);
}
///
/// Gets or sets a value indicating whether text selection is enabled.
///
public bool IsTextSelectionEnabled
{
get => (bool)GetValue(IsTextSelectionEnabledProperty);
set => SetValue(IsTextSelectionEnabledProperty, value);
}
///
/// Gets or sets a value indicating whether to enable the placeholder text.
///
public bool PlaceholderEnabled
{
get => (bool)GetValue(PlaceholderEnabledProperty);
set => SetValue(PlaceholderEnabledProperty, value);
}
///
/// Gets or sets placeholder text.
///
public string PlaceholderText
{
get => (string)GetValue(PlaceholderTextProperty);
set => SetValue(PlaceholderTextProperty, value);
}
///
/// Gets or sets a value indicating whether to show the clear button when is focused.
///
public bool ShowClearButton
{
get => (bool)GetValue(ShowClearButtonProperty);
protected set => SetValue(ShowClearButtonProperty, value);
}
///
/// Gets the command triggered when clicking the button.
///
public IRelayCommand TemplateButtonCommand => (IRelayCommand)GetValue(TemplateButtonCommandProperty);
}