/
githubmirror
/
PowerToys
Обзор
Документация
Войти
/
githubmirror
/
PowerToys
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/modules/powerdisplay/PowerDisplay/Helpers/SliderExtensions.cs
132 строки
5 KB
Niels Laute
[PowerDisplay] Debounced + wheel-scrollable brightness/contrast/volume sliders (#47756)
11 май 2026, 10:15
Не верифицирован
11 май 2026, 10:15
0e766bc
Код
Авторство
О чём код?
// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Input; namespace PowerDisplay.Helpers { /// <summary> /// Provides attached dependency properties and methods for the <see cref="Slider"/> control. /// </summary> public static class SliderExtensions { /// <summary> /// Attached <see cref="DependencyProperty"/> that, when <c>true</c>, lets the /// <see cref="Slider"/> respond to mouse-wheel input by adjusting its /// <see cref="RangeBase.Value"/> by <see cref="MouseWheelChangeProperty"/> /// per notch. The wheel event is marked handled so an enclosing /// <see cref="ScrollViewer"/> does not also scroll. /// </summary> public static readonly DependencyProperty IsMouseWheelEnabledProperty = DependencyProperty.RegisterAttached( nameof(GetIsMouseWheelEnabled)[3..], typeof(bool), typeof(SliderExtensions), new PropertyMetadata(false, OnIsMouseWheelEnabledChanged)); /// <summary> /// Attached <see cref="DependencyProperty"/> for the value added to or subtracted /// from <see cref="RangeBase.Value"/> per mouse-wheel notch. Defaults to /// <see cref="double.NaN"/>, which means "use the slider's own /// <see cref="Slider.SmallChange"/>". /// </summary> public static readonly DependencyProperty MouseWheelChangeProperty = DependencyProperty.RegisterAttached( nameof(GetMouseWheelChange)[3..], typeof(double), typeof(SliderExtensions), new PropertyMetadata(double.NaN)); /// <summary> /// Gets the value of the <see cref="IsMouseWheelEnabledProperty"/> attached property. /// </summary> /// <param name="obj">The <see cref="Slider"/> to read the value from.</param> /// <returns><c>true</c> if mouse-wheel scrolling is enabled on the slider; otherwise <c>false</c>.</returns> public static bool GetIsMouseWheelEnabled(Slider obj) { return (bool)obj.GetValue(IsMouseWheelEnabledProperty); } /// <summary> /// Sets the value of the <see cref="IsMouseWheelEnabledProperty"/> attached property. /// </summary> /// <param name="obj">The <see cref="Slider"/> to set the value on.</param> /// <param name="value"><c>true</c> to enable mouse-wheel scrolling on the slider; otherwise <c>false</c>.</param> public static void SetIsMouseWheelEnabled(Slider obj, bool value) { obj.SetValue(IsMouseWheelEnabledProperty, value); } /// <summary> /// Gets the value of the <see cref="MouseWheelChangeProperty"/> attached property. /// </summary> /// <param name="obj">The <see cref="Slider"/> to read the value from.</param> /// <returns>The per-notch delta, or <see cref="double.NaN"/> to inherit from <see cref="Slider.SmallChange"/>.</returns> public static double GetMouseWheelChange(Slider obj) { return (double)obj.GetValue(MouseWheelChangeProperty); } /// <summary> /// Sets the value of the <see cref="MouseWheelChangeProperty"/> attached property. /// </summary> /// <param name="obj">The <see cref="Slider"/> to set the value on.</param> /// <param name="value">The per-notch delta to apply to <see cref="RangeBase.Value"/>.</param> public static void SetMouseWheelChange(Slider obj, double value) { obj.SetValue(MouseWheelChangeProperty, value); } private static void OnIsMouseWheelEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not Slider slider) { return; } slider.PointerWheelChanged -= OnPointerWheelChanged; if (e.NewValue is true) { slider.PointerWheelChanged += OnPointerWheelChanged; } } private static void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) { if (sender is not Slider slider) { return; } int delta = e.GetCurrentPoint(slider).Properties.MouseWheelDelta; if (delta == 0) { return; } double step = GetMouseWheelChange(slider); if (double.IsNaN(step) || step <= 0) { step = slider.SmallChange; } double notches = delta / 120.0; double newValue = Math.Clamp(slider.Value + (notches * step), slider.Minimum, slider.Maximum); if (newValue != slider.Value) { slider.Value = newValue; } // Claim the gesture so an enclosing ScrollViewer does not also scroll. e.Handled = true; } } }