Files
Common-VS2019/SacsMobileMaui/Maui/Controls/SamEntry.cs
T

147 lines
3.4 KiB
C#

using Sarand.Net.Mobile.Maui.App;
using Sarand.Net.Mobile.Maui.Logic;
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, ISamNativeControlStyle, ISamControl
{
// --- 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;
}
// --- Component state ---
public void SetControlActivity(bool enabled)
{
if (IsEnabled != enabled) IsEnabled = enabled;
}
// --- 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 styling ---
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;
// --- Format text ---
private void FormatText(bool trimText = false)
{
String fn = GetType().Name + ".FormatText";
String originalText = Text;
SamEntryTextFormatOptions options = new SamEntryTextFormatOptions()
{
TextCase = textCase,
NumericOnly = numericOnly,
Decimals = decimals,
TrimText = trimText,
TextInfo = textInfo
};
String formattedText = SamEntryTextFormatter.Format(originalText, options);
if (!originalText.Equals(formattedText))
{
SaLogger.Debug(fn + " Text: " + originalText + ". Formatted: " + formattedText);
Text = formattedText;
}
}
// --- Control events ---
private void SamEntry_TextChanged(object? sender, TextChangedEventArgs e)
{
if (FormatTextOnChange) FormatText();
}
private void SamEntry_Completed(object? sender, EventArgs e)
{
FormatText(trimTextOnCompletion);
}
// --- The End ---
}
}