Files
Chronos/src/Titano.Ui/Controls/AutoSlider.cs
T
Claude 3f8464aabc fix(ui): toasts render; RTL-aware icons, charts and media; keyboard and screen-reader support in the kit
- ToastHost gets its template (nothing drew before); toasts are announced, pause while hovered or focused,
  Esc dismisses the focused one, the action closes it, FocusNewest brings the keyboard to them.
- Windows take the UI language's direction when created and follow every switch.
- Icons mirror only when the glyph is directional (IconData.IsDirectional, Bidi.IsDirectional override);
  the brand mark never mirrors; icons centre in stretched slots (ComboBox chevron).
- Card photos, scrub position, preview surface and shot data, mono templates, paths are left-to-right islands;
  the preview tool row follows the layout direction.
- Chart words (categories, legends, summaries) read in the UI direction and legends start on its side.
- Automation peers: charts as named images, filmstrip as a slider, curve editor value, preview image,
  split button, AutoSlider group; localized names for caption, reset, remove, dismiss and drop-down buttons.
- Focus visible on list and table rows and the preview; curve editor fully operable by keyboard.
- Rail item collapsed layout fixed; job bar trims its text only once the bar is at its minimum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-03 17:22:42 +00:00

172 lines
9.6 KiB
C#

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;
/// <summary>
/// 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 <see cref="Header"/> holding the named slider, the Auto pill and the reset button.
/// </summary>
[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); }
/// <summary>Formatted current value (read by the template).</summary>
public string DisplayValue { get => (string)GetValue(DisplayValueProperty); private set => SetValue(DisplayValueProperty, value); }
/// <summary>"Auto · 41" pill text (Common.AutoValue).</summary>
public string AutoText { get => (string)GetValue(AutoTextProperty); private set => SetValue(AutoTextProperty, value); }
/// <summary>Tooltip of the slider track: what moving it does.</summary>
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;
}
}
/// <summary>Returns the control to the advisor's value.</summary>
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<double> 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));
}
}
/// <summary>A named group around the slider, the Auto pill and the reset button.</summary>
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;
}
}