168 lines
4.7 KiB
C#
168 lines
4.7 KiB
C#
using Sarand.Net.Mobile.Maui.App;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sarand.Net.Mobile.Maui.Controls
|
|
{
|
|
//=======================================================================
|
|
public class SamEntry : Entry
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public enum TextCaseType
|
|
{
|
|
Normal,
|
|
Upper,
|
|
Lower,
|
|
Title
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private bool trimTextOnCompletion = true;
|
|
private TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
|
|
private TextCaseType textCase = TextCaseType.Normal;
|
|
private bool numericOnly = false;
|
|
private byte decimals = 0;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SamEntry()
|
|
{
|
|
DefaultFontSize = FontSize;
|
|
TextChanged += SamEntry_TextChanged;
|
|
Completed += SamEntry_Completed;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public bool TrimText
|
|
{
|
|
get { return trimTextOnCompletion; }
|
|
set
|
|
{
|
|
if (trimTextOnCompletion == value) return;
|
|
trimTextOnCompletion = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Whether to format text as it is entered.
|
|
/// <para>NB This can lead to problems with the cursor position and typing doesn't give expected results.</para>
|
|
/// <para>Default: False.</para>
|
|
/// </summary>
|
|
public bool FormatTextOnChange { get; set; } = false;
|
|
public double DefaultFontSize { get; }
|
|
public TextCaseType TextCase
|
|
{
|
|
get { return textCase; }
|
|
set
|
|
{
|
|
if (textCase == value) return;
|
|
textCase = value;
|
|
FormatText();
|
|
}
|
|
}
|
|
public bool NumericOnly
|
|
{
|
|
get { return numericOnly; }
|
|
set
|
|
{
|
|
if (numericOnly == value) return;
|
|
numericOnly = value;
|
|
if (numericOnly)
|
|
HorizontalTextAlignment = TextAlignment.End;
|
|
Keyboard = Keyboard.Numeric;
|
|
FormatText();
|
|
}
|
|
}
|
|
public byte Decimals
|
|
{
|
|
get { return decimals; }
|
|
set
|
|
{
|
|
if (numericOnly && decimals == value) return;
|
|
numericOnly = true;
|
|
HorizontalTextAlignment = TextAlignment.End;
|
|
decimals = value;
|
|
Keyboard = Keyboard.Numeric;
|
|
FormatText();
|
|
}
|
|
}
|
|
|
|
// Native behaviour modified by SamHandler.
|
|
public bool NativeNoBorder { get; set; } = Sam.Visuals.Form.Controls.NativeNoBorder;
|
|
public bool NativeNoUnderline { get; set; } = Sam.Visuals.Form.Controls.NativeNoUnderline;
|
|
public bool NativeBackgroundClear { get; set; } = Sam.Visuals.Form.Controls.NativeBackgroundClear;
|
|
|
|
//-------------------------------------------------------------------
|
|
// F o r m a t T e x t
|
|
//-------------------------------------------------------------------
|
|
private void FormatText(bool trimText = false)
|
|
{
|
|
String fn = GetType().Name + ".FormatText";
|
|
String originalText = Text;
|
|
String formattedText = originalText;
|
|
if (StringUtil.IsEmpty(formattedText)) return;
|
|
if (trimText)
|
|
formattedText = StringUtil.TrimAll(formattedText);
|
|
|
|
if (numericOnly)
|
|
{
|
|
if (decimals > 0)
|
|
{
|
|
decimal value = StringUtil.ToDecimal(formattedText);
|
|
if (value != 0.0m)
|
|
formattedText = value.ToString("N" + decimals);
|
|
else if (!formattedText.Equals("0"))
|
|
formattedText = "";
|
|
}
|
|
else
|
|
{
|
|
long value = StringUtil.ToLong(formattedText);
|
|
if (value != 0)
|
|
formattedText = value.ToString();
|
|
else if (!formattedText.Equals("0"))
|
|
formattedText = "";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (textCase == TextCaseType.Upper)
|
|
formattedText = textInfo.ToUpper(formattedText);
|
|
else if (textCase == TextCaseType.Lower)
|
|
formattedText = textInfo.ToLower(formattedText);
|
|
else if (textCase == TextCaseType.Title)
|
|
formattedText =
|
|
StringUtil.ToTitleCase(formattedText, textInfo);
|
|
}
|
|
|
|
if (!originalText.Equals(formattedText))
|
|
{
|
|
SaLogger.Debug(fn + " Text: " + originalText + ". Formatted: " + formattedText);
|
|
Text = formattedText;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// C o n t r o l E v e n t s
|
|
//-------------------------------------------------------------------
|
|
private void SamEntry_TextChanged(object? sender, TextChangedEventArgs e)
|
|
{
|
|
if (FormatTextOnChange) FormatText();
|
|
}
|
|
private void SamEntry_Completed(object? sender, EventArgs e)
|
|
{
|
|
FormatText(trimTextOnCompletion);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|