Skip to content

Add SliderExtensions (mouse-wheel attached properties)#851

Open
niels9001 wants to merge 1 commit intoCommunityToolkit:mainfrom
niels9001:niels9001/slider-mousewheel-extension
Open

Add SliderExtensions (mouse-wheel attached properties)#851
niels9001 wants to merge 1 commit intoCommunityToolkit:mainfrom
niels9001:niels9001/slider-mousewheel-extension

Conversation

@niels9001
Copy link
Copy Markdown
Collaborator

Summary

Adds a new SliderExtensions static class to the Extensions component, enabling mouse-wheel input on Slider controls via two attached properties:

Attached property Type Default Meaning
SliderExtensions.IsMouseWheelEnabled bool false When true, wheel events on the slider adjust Value.
SliderExtensions.MouseWheelChange double NaN (use slider's own SmallChange) Per-notch delta applied to Value.

Why

By default Slider ignores the mouse wheel. This is a small, common, opt-in convenience that's easy to add without subclassing — particularly useful when sliders are placed inside a ScrollViewer and you want wheel-over-slider to adjust the value rather than scroll the surrounding container (the handler marks the wheel event handled).

Behavior

  • Computes notches = MouseWheelDelta / 120 and applies notches * step, clamped to [Minimum, Maximum].
  • MouseWheelChange defaults to NaN, in which case the slider's own SmallChange is used as the per-notch delta — same source of truth used by arrow keys, no second knob unless the author wants one.
  • Sets e.Handled = true so an enclosing ScrollViewer doesn't also scroll.
  • Subscribes / unsubscribes PointerWheelChanged based on the IsMouseWheelEnabled value, so toggling it off cleanly removes the handler.

Origin

This was first written for the PowerToys PowerDisplay module, where wheel-scrollable brightness/contrast/volume sliders are very desirable. The implementation is intentionally dependency-free so it can live in the Toolkit and replace the local copy.

Files

  • components/Extensions/src/Slider/SliderExtensions.cs — the extension.
  • components/Extensions/samples/SliderExtensions/SliderWheelSample.xaml(.cs) — interactive sample with toggleable bool/numeric options.
  • components/Extensions/samples/SliderExtensions.md — docs page.

Validation

  • Built CommunityToolkit.WinUI.Extensions for net8.0-windows10.0.19041.0 and net9.0-windows10.0.19041.0 — clean.
  • Verified manually in the Extensions.Wasdk gallery head: wheel-over-slider adjusts the value, toggling IsMouseWheelEnabled off restores default behavior, MouseWheelChange numeric option changes the per-notch step.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Adds SliderExtensions.IsMouseWheelEnabled and SliderExtensions.MouseWheelChange attached properties to the Extensions component, allowing Slider controls to respond to mouse-wheel input. Includes a sample (SliderWheelSample) and docs page.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in SliderExtensions API to enable mouse-wheel interaction for WinUI Slider controls, plus a gallery sample and documentation to demonstrate usage within the Extensions component.

Changes:

  • Introduces SliderExtensions.IsMouseWheelEnabled and SliderExtensions.MouseWheelChange attached properties to adjust Slider.Value on wheel input.
  • Adds a new interactive sample page (SliderWheelSample) demonstrating the attached properties.
  • Adds a new docs page describing the new API and linking the sample.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
components/Extensions/src/Slider/SliderExtensions.cs New attached properties and wheel handler implementation for Slider.
components/Extensions/samples/SliderExtensions/SliderWheelSample.xaml.cs Sample page code-behind wiring the toolkit sample metadata.
components/Extensions/samples/SliderExtensions/SliderWheelSample.xaml Sample UI demonstrating wheel-over-slider behavior.
components/Extensions/samples/SliderExtensions.md Documentation for SliderExtensions and its attached properties.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +19 to +35
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="RangeBase.SmallChange"/>".
/// </summary>
public static readonly DependencyProperty MouseWheelChangeProperty = DependencyProperty.RegisterAttached(
nameof(GetMouseWheelChange)[3..],
typeof(double),
typeof(SliderExtensions),
new PropertyMetadata(double.NaN));
}

double step = GetMouseWheelChange(slider);
if (double.IsNaN(step) || step <= 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants