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

Adding horizontal scroll support #2640

Merged
merged 1 commit into from
Apr 3, 2022
Merged
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
101 changes: 100 additions & 1 deletion MaterialDesignThemes.Wpf/ScrollViewerAssist.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;

namespace MaterialDesignThemes.Wpf
{
Expand Down Expand Up @@ -69,5 +71,102 @@ public static bool GetShowSeparators(DependencyObject element)
public static void SetIgnorePadding(DependencyObject element, bool value) => element.SetValue(IgnorePaddingProperty, value);
public static bool GetIgnorePadding(DependencyObject element) => (bool)element.GetValue(IgnorePaddingProperty);

private static readonly DependencyProperty HorizontalScrollHookProperty = DependencyProperty.RegisterAttached(
"HorizontalScrollHook", typeof(HwndSourceHook), typeof(ScrollViewerAssist), new PropertyMetadata(null));

public static readonly DependencyProperty SupportHorizontalScrollProperty = DependencyProperty.RegisterAttached(
"SupportHorizontalScroll", typeof(bool), typeof(ScrollViewerAssist), new PropertyMetadata(false, OnSupportHorizontalScrollChanged));

private static void OnSupportHorizontalScrollChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Based on: https://blog.walterlv.com/post/handle-horizontal-scrolling-of-touchpad-en.html
if (d is ScrollViewer scrollViewer)
{
if ((bool)e.NewValue)
{
OnLoaded(scrollViewer, sv =>
{
if (GetSupportHorizontalScroll(sv))
{
RegisterHook(sv);
}
});
}
else
{
OnLoaded(scrollViewer, sv =>
{
if (!GetSupportHorizontalScroll(sv))
{
RemoveHook(sv);
}
});
}
}

static void OnLoaded(ScrollViewer scrollViewer, Action<ScrollViewer> doOnLoaded)
{
if(scrollViewer.IsLoaded)
{
doOnLoaded(scrollViewer);
}
else
{
RoutedEventHandler? onLoaded = null;
onLoaded = (_, _) =>
{
scrollViewer.Loaded -= onLoaded;
doOnLoaded(scrollViewer);
};
scrollViewer.Loaded += onLoaded;
}
}

static void RemoveHook(ScrollViewer scrollViewer)
{
if (scrollViewer.GetValue(HorizontalScrollHookProperty) is HwndSourceHook hook &&
PresentationSource.FromVisual(scrollViewer) is HwndSource source)
{
source.RemoveHook(hook);
scrollViewer.SetValue(HorizontalScrollHookProperty, null);
}
}

static void RegisterHook(ScrollViewer scrollViewer)
{
RemoveHook(scrollViewer);
if (PresentationSource.FromVisual(scrollViewer) is HwndSource source)
{
HwndSourceHook hook = Hook;
scrollViewer.SetValue(HorizontalScrollHookProperty, hook);
source.AddHook(hook);
}

IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_MOUSEHWHEEL = 0x020E;
switch (msg)
{
case WM_MOUSEHWHEEL:
int tilt = (short)((wParam.ToInt32() >> 16) & 0xFFFF);
OnMouseTilt(tilt);
return (IntPtr)1;
}
return IntPtr.Zero;
}

void OnMouseTilt(int tilt)
{
scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + tilt);
}
}
}

public static void SetSupportHorizontalScroll(DependencyObject element, bool value)
=> element.SetValue(SupportHorizontalScrollProperty, value);

public static bool GetSupportHorizontalScroll(DependencyObject element)
=> (bool)element.GetValue(SupportHorizontalScrollProperty);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
Background="{TemplateBinding wpf:ColorZoneAssist.Background}"
Focusable="False">
<ScrollViewer
wpf:ScrollViewerAssist.SupportHorizontalScroll="True"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<StackPanel>
Expand Down