Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/ShadUI/AttachedProperties/NumericUpDownAssist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ private static void SetupInputFiltering(NumericUpDown numericUpDown, TextBox tex
{
TextBox = textBox,
TextInputHandler = OnTextInput,
KeyDownHandler = OnKeyDown
KeyDownHandler = OnKeyDown,
PointerWheelHandler = OnPointerWheelChanged
});

textBox.AddHandler(InputElement.TextInputEvent, OnTextInput, RoutingStrategies.Tunnel);
textBox.KeyDown += OnKeyDown;
textBox.AddHandler(InputElement.KeyDownEvent, OnKeyDown, RoutingStrategies.Tunnel);
numericUpDown.AddHandler(InputElement.PointerWheelChangedEvent, OnPointerWheelChanged,
RoutingStrategies.Bubble, true);
return;

void OnTextInput(object? sender, TextInputEventArgs e)
Expand Down Expand Up @@ -144,7 +147,25 @@ void OnTextInput(object? sender, TextInputEventArgs e)

void OnKeyDown(object? sender, KeyEventArgs e)
{
// Reserved for future key filtering if needed
if (e.Key is not (Key.Up or Key.Down)) return;

var spinner = numericUpDown.FindControl<Spinner>("PART_Spinner");
if (spinner is null || !numericUpDown.AllowSpin) return;

var direction = e.Key == Key.Up ? SpinDirection.Increase : SpinDirection.Decrease;
spinner.RaiseEvent(new SpinEventArgs(Spinner.SpinEvent, direction));
e.Handled = true;
}

void OnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
{
if (e.Delta.Y == 0 || !numericUpDown.AllowSpin || !numericUpDown.IsKeyboardFocusWithin) return;

var newValue = (numericUpDown.Value ?? 0) +
(e.Delta.Y > 0 ? numericUpDown.Increment : -numericUpDown.Increment);

numericUpDown.Value = Math.Clamp(newValue, numericUpDown.Minimum, numericUpDown.Maximum);
e.Handled = true;
}
}

Expand All @@ -160,9 +181,12 @@ private static void CleanupHandlers(NumericUpDown? numericUpDown)
handlers.TextBox.RemoveHandler(InputElement.TextInputEvent, handlers.TextInputHandler);

if (handlers.KeyDownHandler is not null)
handlers.TextBox.KeyDown -= handlers.KeyDownHandler;
handlers.TextBox.RemoveHandler(InputElement.KeyDownEvent, handlers.KeyDownHandler);
}

if (numericUpDown is not null && handlers.PointerWheelHandler is not null)
numericUpDown.RemoveHandler(InputElement.PointerWheelChangedEvent, handlers.PointerWheelHandler);

numericUpDown?.ClearValue(InputHandlersProperty);
}

Expand All @@ -171,6 +195,7 @@ private class InputHandlers
public TextBox? TextBox { get; set; }
public EventHandler<TextInputEventArgs>? TextInputHandler { get; set; }
public EventHandler<KeyEventArgs>? KeyDownHandler { get; set; }
public EventHandler<PointerWheelEventArgs>? PointerWheelHandler { get; set; }
}

private static readonly AttachedProperty<InputHandlers> InputHandlersProperty =
Expand Down
3 changes: 3 additions & 0 deletions src/ShadUI/Utilities/SmoothScrollController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ private void OnPointerWheelChanged(
var isShiftPressed = (e.KeyModifiers & KeyModifiers.Shift) != 0;
while (source is not null && source != _instance)
{
if (source is ButtonSpinner { AllowSpin: true, IsKeyboardFocusWithin: true })
return;

if (source is ScrollViewer inner && inner.IsVisible)
{
var innerHasHorizontal = inner.HorizontalScrollBarVisibility != ScrollBarVisibility.Disabled;
Expand Down