Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attached behavior for scroll viewers to throttle touch pad scrolling #1592

Open
wants to merge 3 commits into
base: NetStandard
Choose a base branch
from
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
43 changes: 43 additions & 0 deletions FRBDK/Glue/Glue/Themes/Converters/ScrollThrottleBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace FlatRedBall.Glue.Themes.Converters;

public static class ScrollThrottleBehavior
{
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached(
"IsEnabled",
typeof(bool),
typeof(ScrollThrottleBehavior),
new PropertyMetadata(false, OnIsEnabledChanged));

public static bool GetIsEnabled(ScrollViewer obj) => (bool)obj.GetValue(IsEnabledProperty);
public static void SetIsEnabled(ScrollViewer obj, bool value) => obj.SetValue(IsEnabledProperty, value);

private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ScrollViewer scrollViewer
)
{
if ((bool)e.NewValue)
{
scrollViewer.PreviewMouseWheel += OnMouseWheel;
}
else
{
scrollViewer.PreviewMouseWheel -= OnMouseWheel;
}
}
}

private static void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
if (sender is ScrollViewer scrollViewer && System.Math.Abs(e.Delta) != 120)
{
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + ((double)e.Delta /120));
e.Handled = true;
}
}
}
4 changes: 3 additions & 1 deletion FRBDK/Glue/Glue/Themes/Frb.Styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
<Setter Property="TextOptions.TextRenderingMode" Value="Auto" />
</Style>

<Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource MaterialDesignScrollViewer}" />
<Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource MaterialDesignScrollViewer}">
<Setter Property="converters:ScrollThrottleBehavior.IsEnabled" Value="True" />
</Style>

<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource MaterialDesignScrollBar}">
<Style.Resources>
Expand Down