Files
Common-VS2019/SacsMobileMaui/Maui/Controls/Panels/SamControlPanel.cs
T

163 lines
5.9 KiB
C#

using Sarand.Net.Mobile.Maui.App;
using Sarand.Net.Mobile.Maui.Views;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Sarand.Net.Mobile.Maui.Controls.Views
{
//=======================================================================
public abstract class SamControlPanel : SamContentView
{
//-------------------------------------------------------------------
// Definitions.
//-------------------------------------------------------------------
// Data members.
// Components.
private HorizontalStackLayout controlLayout = new HorizontalStackLayout();
private SamContentView controlBorder = new SamContentView();
private SamContentView controlPanel = new SamContentView() { Padding = Sam.Visuals.Form.Controls.Padding };
// Styling.
private Thickness controlBorderThickness = new Thickness(Sam.Visuals.Form.Controls.BorderWidth);
private Color controlBorderColor = Sam.Visuals.Form.Controls.BorderColor;
private Color controlBackgroundColor = Sam.Visuals.Form.Controls.BackgroundColor;
//-------------------------------------------------------------------
// Construction.
protected SamControlPanel()
{
// Control layout: [Border[Panel]] [Suffix].
controlBorder.Content = controlPanel;
controlLayout.Add(controlBorder);
controlLayout.Add(LabelSuffix);
// Main layout.
Content = controlLayout;
SetControlActivity();
}
//-------------------------------------------------------------------
// Properties.
// Label.
public SamLabel Label { get; } = new SamLabel() { Margin = Sam.Visuals.Form.Label.Margin };
public String LabelText { get { return Label.Text; } set { if (Label.Text.Equals(value)) return; Label.Text = value; } }
// Control.
/// <summary>The control itself. NB May be composite</summary>
protected abstract View ControlView { get; }
/// <summary>Control used for read-only display.</summary>
protected SamLabel ReadOnlyLabel { get; } =
new SamLabel() { BackgroundColor = Colors.Transparent };
// Suffix.
protected SamLabel LabelSuffix { get; } = new SamLabel() { Margin = Sam.Visuals.Form.Label.Margin };
public String SuffixText { get { return LabelSuffix.Text; } set { if (LabelSuffix.Text.Equals(value)) return; LabelSuffix.Text = value; } }
public double SuffixPadding { get { return LabelSuffix.Padding.Left; } set { if (LabelSuffix.Padding.Left == value) return; LabelSuffix.Padding = new(value, 0, 0, 0); } }
// Styling.
public new Color BackgroundColor
{
get { return controlBackgroundColor; }
set { controlBackgroundColor = value; ConfigureComponents(); }
}
public Thickness BorderThickness
{
get { return controlBorderThickness; }
set { controlBorderThickness = value; ConfigureComponents(); }
}
public double BorderWidth
{
set { BorderThickness = new Thickness(value); }
}
public Color BorderColor
{
get { return controlBorderColor; }
set { controlBorderColor = value; }
}
public new LayoutOptions HorizontalOptions
{
get { return base.HorizontalOptions; }
set { base.HorizontalOptions = value; ControlView.HorizontalOptions = value; }
}
protected String ReadOnlyText
{
get { return ReadOnlyLabel.Text; }
set { if (ReadOnlyLabel.Text.Equals(value)) return; ReadOnlyLabel.Text = value; }
}
//-------------------------------------------------------------------
// C o n f i g u r e C o m p o n e n t s
//-------------------------------------------------------------------
/// <summary>Control configuration that can be changed dynamically.</summary>
public override void ConfigureComponents()
{
// Outer view acts as border.
controlBorder.BackgroundColor = controlBorderColor;
controlBorder.Padding = controlBorderThickness;
controlPanel.BackgroundColor = controlBackgroundColor;
}
//-------------------------------------------------------------------
// C o n t r o l A c t i v i t y
//-------------------------------------------------------------------
public override void SetControlActivity()
{
base.SetControlActivity();
}
public override void SetControlsReadOnly()
{
base.SetControlsReadOnly();
if (ReadOnly) controlPanel.Content = ReadOnlyLabel;
else controlPanel.Content = ControlView;
}
//-------------------------------------------------------------------
// C o n t r o l E v e n t s
//-------------------------------------------------------------------
public new event EventHandler<FocusEventArgs>? Focused;
protected void Control_Focused(object? sender, FocusEventArgs e)
{
if (Focused != null) Focused.Invoke(sender, e);
}
//-------------------------------------------------------------------
public new event EventHandler<FocusEventArgs>? Unfocused;
protected void Control_Unfocused(object? sender, FocusEventArgs e)
{
if (Unfocused != null) Unfocused.Invoke(sender, e);
}
//-------------------------------------------------------------------
// V i e w E v e n t s
//-------------------------------------------------------------------
protected override void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
base.OnPropertyChanged(propertyName);
//Logger.Debug(GetType().Name + ".OnPropertyChanged Name=" + propertyName);
if (propertyName.EqualsCI(PaddingProperty.PropertyName))
{
if (!controlPanel.Padding.Equals(Padding))
{
// Apply the padding to the embedded view and clear this view's padding.
controlPanel.Padding = Padding;
Padding = new Thickness();
}
}
else if (propertyName.EqualsCI(BackgroundColorProperty.PropertyName))
{
controlPanel.BackgroundColor = BackgroundColor;
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}