using System.Windows; using System.Windows.Automation.Peers; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using Titano.Core.Localization; namespace Titano.Ui.Controls; /// /// Advisor-aware slider (BRIEF §10.4, DESIGN §4): in Auto the track is accent with an "Auto · value" pill whose /// tooltip carries the advisor's reason; touching the slider switches to manual (sun track, value shown, reset /// button returns to Auto). When not applicable the value reads "n/a" with the reason in the tooltip. Screen readers /// see a group named after holding the named slider, the Auto pill and the reset button. /// [TemplatePart(Name = PartSlider, Type = typeof(Slider))] public class AutoSlider : Control { public const string PartSlider = "PART_Slider"; public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(nameof(Header), typeof(string), typeof(AutoSlider)); public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value), typeof(double), typeof(AutoSlider), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged)); public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(nameof(Minimum), typeof(double), typeof(AutoSlider), new PropertyMetadata(0.0)); public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(nameof(Maximum), typeof(double), typeof(AutoSlider), new PropertyMetadata(1.0)); public static readonly DependencyProperty SmallChangeProperty = DependencyProperty.Register(nameof(SmallChange), typeof(double), typeof(AutoSlider), new PropertyMetadata(0.01)); public static readonly DependencyProperty IsSnapToTickEnabledProperty = DependencyProperty.Register(nameof(IsSnapToTickEnabled), typeof(bool), typeof(AutoSlider), new PropertyMetadata(false)); public static readonly DependencyProperty TickFrequencyProperty = DependencyProperty.Register(nameof(TickFrequency), typeof(double), typeof(AutoSlider), new PropertyMetadata(1.0)); public static readonly DependencyProperty IsAutoProperty = DependencyProperty.Register(nameof(IsAuto), typeof(bool), typeof(AutoSlider), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); public static readonly DependencyProperty AutoValueProperty = DependencyProperty.Register(nameof(AutoValue), typeof(double), typeof(AutoSlider), new PropertyMetadata(0.0, OnAutoValueChanged)); public static readonly DependencyProperty AutoReasonProperty = DependencyProperty.Register(nameof(AutoReason), typeof(string), typeof(AutoSlider)); public static readonly DependencyProperty IsApplicableProperty = DependencyProperty.Register(nameof(IsApplicable), typeof(bool), typeof(AutoSlider), new PropertyMetadata(true)); public static readonly DependencyProperty NotApplicableReasonProperty = DependencyProperty.Register(nameof(NotApplicableReason), typeof(string), typeof(AutoSlider)); public static readonly DependencyProperty ValueFormatProperty = DependencyProperty.Register(nameof(ValueFormat), typeof(string), typeof(AutoSlider), new PropertyMetadata("0.##", OnFormatChanged)); public static readonly DependencyProperty DisplayValueProperty = DependencyProperty.Register(nameof(DisplayValue), typeof(string), typeof(AutoSlider), new PropertyMetadata(string.Empty)); public static readonly DependencyProperty AutoTextProperty = DependencyProperty.Register(nameof(AutoText), typeof(string), typeof(AutoSlider), new PropertyMetadata(string.Empty)); public static readonly DependencyProperty SliderToolTipProperty = DependencyProperty.Register(nameof(SliderToolTip), typeof(object), typeof(AutoSlider)); public static readonly RoutedCommand ResetCommand = new(nameof(ResetCommand), typeof(AutoSlider)); private Slider? _slider; private bool _syncing; static AutoSlider() { DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoSlider), new FrameworkPropertyMetadata(typeof(AutoSlider))); } public AutoSlider() { CommandBindings.Add(new CommandBinding(ResetCommand, (_, _) => ResetToAuto())); // The pill and the value follow a language switch; the static event is held only while the slider is shown. Loaded += (_, _) => { Loc.CultureChanged -= OnCultureChanged; Loc.CultureChanged += OnCultureChanged; UpdateTexts(); }; Unloaded += (_, _) => Loc.CultureChanged -= OnCultureChanged; UpdateTexts(); } public string? Header { get => (string?)GetValue(HeaderProperty); set => SetValue(HeaderProperty, value); } public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } public double Minimum { get => (double)GetValue(MinimumProperty); set => SetValue(MinimumProperty, value); } public double Maximum { get => (double)GetValue(MaximumProperty); set => SetValue(MaximumProperty, value); } public double SmallChange { get => (double)GetValue(SmallChangeProperty); set => SetValue(SmallChangeProperty, value); } public bool IsSnapToTickEnabled { get => (bool)GetValue(IsSnapToTickEnabledProperty); set => SetValue(IsSnapToTickEnabledProperty, value); } public double TickFrequency { get => (double)GetValue(TickFrequencyProperty); set => SetValue(TickFrequencyProperty, value); } public bool IsAuto { get => (bool)GetValue(IsAutoProperty); set => SetValue(IsAutoProperty, value); } public double AutoValue { get => (double)GetValue(AutoValueProperty); set => SetValue(AutoValueProperty, value); } public string? AutoReason { get => (string?)GetValue(AutoReasonProperty); set => SetValue(AutoReasonProperty, value); } public bool IsApplicable { get => (bool)GetValue(IsApplicableProperty); set => SetValue(IsApplicableProperty, value); } public string? NotApplicableReason { get => (string?)GetValue(NotApplicableReasonProperty); set => SetValue(NotApplicableReasonProperty, value); } public string ValueFormat { get => (string)GetValue(ValueFormatProperty); set => SetValue(ValueFormatProperty, value); } /// Formatted current value (read by the template). public string DisplayValue { get => (string)GetValue(DisplayValueProperty); private set => SetValue(DisplayValueProperty, value); } /// "Auto · 41" pill text (Common.AutoValue). public string AutoText { get => (string)GetValue(AutoTextProperty); private set => SetValue(AutoTextProperty, value); } /// Tooltip of the slider track: what moving it does. public object? SliderToolTip { get => GetValue(SliderToolTipProperty); set => SetValue(SliderToolTipProperty, value); } public override void OnApplyTemplate() { if (_slider is not null) { _slider.ValueChanged -= OnSliderValueChanged; _slider.PreviewMouseDown -= OnSliderTouched; _slider.PreviewKeyDown -= OnSliderTouched; } base.OnApplyTemplate(); _slider = GetTemplateChild(PartSlider) as Slider; if (_slider is not null) { _slider.Value = IsAuto ? AutoValue : Value; _slider.ValueChanged += OnSliderValueChanged; _slider.PreviewMouseDown += OnSliderTouched; _slider.PreviewKeyDown += OnSliderTouched; } } /// Returns the control to the advisor's value. public void ResetToAuto() { IsAuto = true; Value = AutoValue; UpdateTexts(); } private void OnSliderTouched(object sender, RoutedEventArgs e) { if (e is KeyEventArgs k && k.Key is not (Key.Left or Key.Right or Key.Up or Key.Down or Key.PageUp or Key.PageDown or Key.Home or Key.End)) return; IsAuto = false; } private void OnSliderValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (_syncing) return; Value = e.NewValue; } private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var s = (AutoSlider)d; if (s._slider is not null && !s._syncing) { s._syncing = true; s._slider.Value = (double)e.NewValue; s._syncing = false; } s.UpdateTexts(); } private static void OnAutoValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var s = (AutoSlider)d; if (s.IsAuto) s.Value = (double)e.NewValue; s.UpdateTexts(); } protected override AutomationPeer OnCreateAutomationPeer() => new AutoSliderAutomationPeer(this); private static void OnFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((AutoSlider)d).UpdateTexts(); private void OnCultureChanged(object? sender, EventArgs e) => UpdateTexts(); private void UpdateTexts() { var culture = Loc.Culture; DisplayValue = Value.ToString(ValueFormat, culture); AutoText = Loc.L("Common.AutoValue", AutoValue.ToString(ValueFormat, culture)); } } /// A named group around the slider, the Auto pill and the reset button. public sealed class AutoSliderAutomationPeer(AutoSlider owner) : FrameworkElementAutomationPeer(owner) { protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Group; protected override string GetClassNameCore() => nameof(AutoSlider); protected override string GetNameCore() { var name = base.GetNameCore(); return string.IsNullOrEmpty(name) ? ((AutoSlider)Owner).Header ?? string.Empty : name; } }