Files
DP44/Common/DTS.Common/Controls/CommonStatusRibbon.xaml.cs
2026-04-17 14:55:32 -04:00

322 lines
12 KiB
C#

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
using DTS.Common.Events;
using Prism.Events;
using Prism.Ioc;
using Unity;
namespace DTS.Common.Controls
{
/// <summary>
/// Interaction logic for CommonStatusRibbon.xaml
/// this is a helper class for the status ribbon + status text shown on pages that have status feedback
/// </summary>
public partial class CommonStatusRibbon : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, string propertyName = null)
{
if (Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private IUnityContainer _unityContainer { get; }
private IEventAggregator _eventAggregator { get; }
public CommonStatusRibbon()
{
InitializeComponent();
if (_unityContainer == null) { _unityContainer = ContainerLocator.Container.Resolve<IUnityContainer>(); }
if (_eventAggregator == null)
{
_eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
}
_eventAggregator.GetEvent<ProgressBarEvent>().Subscribe(OnProgressBarEvent, ThreadOption.PublisherThread);
}
private void OnProgressBarEvent(ProgressBarEventArg arg)
{
if (arg.ProgressBarName != ProgressBarName) { return; }
if (null != arg.ProgressBarText)
{
SetAggregateStatusText(arg.ProgressBarText);
}
if (!double.IsNaN(arg.ProgressBarPercentage))
{
SetProgressValue(Convert.ToInt32(arg.ProgressBarPercentage));
}
SetProgressBarVisibility(arg.ProgressBarVisibility);
SetAggregateStatusColor(arg.ProgressBarColor);
}
#region ProgressBarValue
public static readonly DependencyProperty SetProgressBarValueProperty =
DependencyProperty.Register("ProgressBarValue", typeof(int), typeof(CommonStatusRibbon),
new PropertyMetadata(0, OnProgressValueChanged));
private static void OnProgressValueChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var sr = d as CommonStatusRibbon;
sr?.OnProgressBarValueChanged(e);
}
private void OnProgressBarValueChanged(DependencyPropertyChangedEventArgs e)
{
SetProgressValue(Convert.ToInt32(e.NewValue));
}
private delegate void SetProgressValueDelegate(int value);
private int _progressBarValue;
/// <summary>
/// sets the progress bar progress. Not thread friendly, call SetProgressValue if coming from a background thread
/// </summary>
public int ProgressBarValue
{
get => _progressBarValue;
set => SetProperty(ref _progressBarValue, value, "ProgressBarValue");
}
/// <summary>
/// sets the progress bar progress
/// thread friendly
/// </summary>
/// <param name="value">progress from 0-100. values less than 0 or greater than 100 are normalized to 0 or 100</param>
public void SetProgressValue(int value)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new SetProgressValueDelegate(SetProgressValue), value);
return;
}
if (value < 0) { value = 0; }
else if (value > 100) { value = 100; }
ProgressBarValue = value;
}
#endregion
#region ProgressBarVisibility
public static readonly DependencyProperty SetProgressBarVisibilityProperty =
DependencyProperty.Register("ProgressBarVisibility", typeof(Visibility), typeof(CommonStatusRibbon),
new PropertyMetadata(Visibility.Collapsed, OnProgressVisibilityChanged));
private static void OnProgressVisibilityChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var sr = d as CommonStatusRibbon;
sr?.OnProgressBarVisibilityChanged(e);
}
private void OnProgressBarVisibilityChanged(DependencyPropertyChangedEventArgs e)
{
SetProgressBarVisibility((Visibility)e.NewValue);
}
/// <summary>
/// controls the progress bar visibility.
/// SetProgressBarVisibility is thread friendly
/// </summary>
private Visibility _progressBarVisibility = Visibility.Hidden;
public Visibility ProgressBarVisibility
{
get => _progressBarVisibility;
set => SetProperty(ref _progressBarVisibility, value, "ProgressBarVisibility");
}
private delegate void SetProgressBarVisibilityDelegate(Visibility v);
/// <summary>
/// sets the progress bar visibility
/// thread friendly
/// </summary>
/// <param name="v"></param>
public void SetProgressBarVisibility(Visibility v)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new SetProgressBarVisibilityDelegate(SetProgressBarVisibility), v);
}
else
{
ProgressBarVisibility = v;
}
}
#endregion
#region ProgressBarName
public static readonly DependencyProperty ProgressBarNameProperty =
DependencyProperty.Register("ProgressBarName", typeof(string), typeof(CommonStatusRibbon),
new PropertyMetadata("OverallStatus", OnProgressBarNameChanged));
private static void OnProgressBarNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sr = d as CommonStatusRibbon;
sr?.OnProgressBarNameChanged(e);
}
private void OnProgressBarNameChanged(DependencyPropertyChangedEventArgs e)
{
SetProgressBarName(Convert.ToString(e.NewValue));
}
public void SetProgressBarName(string s)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new Action(() => SetProgressBarName(s)));
}
else
{
ProgressBarName = s;
}
}
private string _progressBarName = "OverallStatus";
public string ProgressBarName
{
get => _progressBarName;
set => SetProperty(ref _progressBarName, value, "ProgressBarName");
}
#endregion ProgressBarName
#region AggregateStatusText
public static readonly DependencyProperty SetAggregateStatusTextProperty =
DependencyProperty.Register("AggregateStatusText", typeof(string), typeof(CommonStatusRibbon),
new PropertyMetadata("", OnAggregateStatusTextChanged));
private static void OnAggregateStatusTextChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var sr = d as CommonStatusRibbon;
sr?.OnAggregateStatusTextChanged(e);
}
private void OnAggregateStatusTextChanged(DependencyPropertyChangedEventArgs e)
{
SetAggregateStatusText(Convert.ToString(e.NewValue));
}
/// <summary>
/// retrieves the status text
/// to set status text use SetStatusTextNoTranslate or the property Status
/// </summary>
private string _aggregateStatusText = "---";
public string AggregateStatusText
{
get => _aggregateStatusText;
set => SetProperty(ref _aggregateStatusText, value, "AggregateStatusText");
}
public void SetAggregateStatusText(string s)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new Action(() => SetAggregateStatusText(s)));
}
else
{
AggregateStatusText = s;
}
}
#endregion
#region AggregateStatusColor
public static readonly DependencyProperty SetAggregateStatusColorProperty =
DependencyProperty.Register("AggregateStatusColor", typeof(Color), typeof(CommonStatusRibbon),
new PropertyMetadata(Colors.AliceBlue, OnAggregateStatusColorChanged));
private static void OnAggregateStatusColorChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var sr = d as CommonStatusRibbon;
sr?.OnAggregateStatusColorChanged(e);
}
private void OnAggregateStatusColorChanged(DependencyPropertyChangedEventArgs e)
{
SetAggregateStatusColor((Color)e.NewValue);
}
private delegate void SetStatusColorDelegate(Color c);
/// <summary>
/// sets the color of the ribbon background
/// thread friendly
/// </summary>
/// <param name="c"></param>
public void SetAggregateStatusColor(Color c)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new SetStatusColorDelegate(SetAggregateStatusColor), c);
return;
}
AggregateStatusColor = c;
}
private Color _aggregateStatusColor = Colors.AliceBlue;
/// <summary>
/// determines the color of the status background
/// not thread friendly
/// SetAggregateStatusColor is thread friendly
/// </summary>
public Color AggregateStatusColor
{
get => _aggregateStatusColor;
set
{
SetAlert(false);
SetProperty(ref _aggregateStatusColor, value, "AggregateStatusColor");
}
}
#endregion
private Visibility _alertVisibility = Visibility.Hidden;
public Visibility AlertVisibility
{
get => _alertVisibility;
set => SetProperty(ref _alertVisibility, value, "AlertVisibility");
}
private SolidColorBrush _textColor = new SolidColorBrush(Colors.Black);
public SolidColorBrush TextColor
{
get => _textColor;
set
{
SetProperty(ref _textColor, value, "TextColor");
OnPropertyChanged("TextColor");
}
}
private bool _isAlertSet;
public delegate void SetAlertDelegate(bool set);
public void SetAlert(bool set)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.BeginInvoke(new SetAlertDelegate(SetAlert), set);
return;
}
if (set == _isAlertSet) { return; }
_isAlertSet = set;
if (set)
{
//turn on alert
TextColor = BrushesAndColors.Brush_ArmSystemForeground;
AlertVisibility = Visibility.Visible;
}
else
{
//turn off alert
TextColor = new SolidColorBrush(Colors.Black);
AlertVisibility = Visibility.Hidden;
}
}
}
}