using System.Windows;
namespace Chronos.App.Editor;
///
/// Whether the user is using a control (it holds the keyboard focus or the mouse capture: a slider dragged or focused),
/// for a binding to say so (, bound one way to the source): the Camera step's Straighten
/// shows the preview's grid while it is used (owner review 1, R6). starts the tracking.
///
public static class SliderActivity
{
public static readonly DependencyProperty TracksProperty = DependencyProperty.RegisterAttached(
"Tracks", typeof(bool), typeof(SliderActivity), new PropertyMetadata(false, OnTracksChanged));
public static readonly DependencyProperty IsActiveProperty = DependencyProperty.RegisterAttached(
"IsActive", typeof(bool), typeof(SliderActivity), new FrameworkPropertyMetadata(false));
public static bool GetTracks(DependencyObject d) => (bool)d.GetValue(TracksProperty);
public static void SetTracks(DependencyObject d, bool value) => d.SetValue(TracksProperty, value);
public static bool GetIsActive(DependencyObject d) => (bool)d.GetValue(IsActiveProperty);
public static void SetIsActive(DependencyObject d, bool value) => d.SetValue(IsActiveProperty, value);
private static void OnTracksChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not FrameworkElement element) return;
element.IsKeyboardFocusWithinChanged -= OnChanged;
element.IsMouseCaptureWithinChanged -= OnChanged;
element.Unloaded -= OnUnloaded;
if (e.NewValue is not true) return;
element.IsKeyboardFocusWithinChanged += OnChanged;
element.IsMouseCaptureWithinChanged += OnChanged;
element.Unloaded += OnUnloaded;
Update(element);
}
private static void OnChanged(object sender, DependencyPropertyChangedEventArgs e) => Update((FrameworkElement)sender);
/// A control taken off screen (another step) is no longer being used.
private static void OnUnloaded(object sender, RoutedEventArgs e) => ((FrameworkElement)sender).SetCurrentValue(IsActiveProperty, false);
/// Set through , so a binding on the property stays and receives it.
private static void Update(FrameworkElement element)
{
var active = element.IsKeyboardFocusWithin || element.IsMouseCaptureWithin;
if (GetIsActive(element) != active) element.SetCurrentValue(IsActiveProperty, active);
}
}