|
1 |
| -using Microsoft.UI.Xaml; |
2 |
| -using Microsoft.UI.Xaml.Controls; |
3 | 1 | using Microsoft.UI.Xaml.Input;
|
4 |
| -using Microsoft.UI.Xaml.Media; |
5 | 2 | using Windows.System;
|
6 | 3 |
|
7 | 4 | namespace ChatUI.Behaviors;
|
8 | 5 |
|
9 | 6 | public static class ReversedPointerWheel
|
10 | 7 | {
|
11 |
| - public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached( |
12 |
| - "IsEnabled", |
13 |
| - typeof(bool), |
14 |
| - typeof(ReversedPointerWheel), |
15 |
| - new PropertyMetadata(default(bool), OnIsEnabledChanged)); |
| 8 | + public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached( |
| 9 | + "IsEnabled", |
| 10 | + typeof(bool), |
| 11 | + typeof(ReversedPointerWheel), |
| 12 | + new PropertyMetadata(default(bool), OnIsEnabledChanged)); |
16 | 13 |
|
17 |
| - public static bool GetIsEnabled(FrameworkElement element) |
18 |
| - => (bool)element.GetValue(IsEnabledProperty); |
| 14 | + public static bool GetIsEnabled(FrameworkElement element) |
| 15 | + => (bool)element.GetValue(IsEnabledProperty); |
19 | 16 |
|
20 |
| - public static void SetIsEnabled(FrameworkElement element, bool value) |
21 |
| - => element.SetValue(IsEnabledProperty, value); |
| 17 | + public static void SetIsEnabled(FrameworkElement element, bool value) |
| 18 | + => element.SetValue(IsEnabledProperty, value); |
22 | 19 |
|
23 |
| - private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
24 |
| - { |
25 |
| - if (d is ScrollContentPresenter scp) |
26 |
| - { |
| 20 | + private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 21 | + { |
| 22 | + if (d is ScrollContentPresenter scp) |
| 23 | + { |
27 | 24 | #if HAS_UNO
|
28 |
| - // We do have native support for reversed PointerWheel in Uno. |
29 |
| - Uno.UI.Xaml.Controls.ScrollContentPresenter.SetIsPointerWheelReversed(scp, (bool)e.NewValue); |
| 25 | + // We do have native support for reversed PointerWheel in Uno. |
| 26 | + Uno.UI.Xaml.Controls.ScrollContentPresenter.SetIsPointerWheelReversed(scp, (bool)e.NewValue); |
30 | 27 | #else
|
31 |
| - scp.PointerWheelChanged -= OnWheelChanged; |
32 |
| - if (e.NewValue is true) |
33 |
| - { |
34 |
| - scp.PointerWheelChanged += OnWheelChanged; |
35 |
| - } |
| 28 | + scp.PointerWheelChanged -= OnWheelChanged; |
| 29 | + if (e.NewValue is true) |
| 30 | + { |
| 31 | + scp.PointerWheelChanged += OnWheelChanged; |
| 32 | + } |
36 | 33 | #endif
|
37 |
| - } |
38 |
| - else if (d is FrameworkElement sv) |
39 |
| - { |
40 |
| - sv.Loaded -= PropagateValueToScp; |
41 |
| - if (e.NewValue != DependencyProperty.UnsetValue) |
42 |
| - { |
43 |
| - sv.Loaded += PropagateValueToScp; |
44 |
| - PropagateValueToScp(sv, null); |
45 |
| - } |
| 34 | + } |
| 35 | + else if (d is FrameworkElement sv) |
| 36 | + { |
| 37 | + sv.Loaded -= PropagateValueToScp; |
| 38 | + if (e.NewValue != DependencyProperty.UnsetValue) |
| 39 | + { |
| 40 | + sv.Loaded += PropagateValueToScp; |
| 41 | + PropagateValueToScp(sv, null); |
| 42 | + } |
46 | 43 |
|
47 |
| - static void PropagateValueToScp(object sender, RoutedEventArgs? _) |
48 |
| - { |
49 |
| - if (sender is FrameworkElement sv && TryFindFirstChild(sv, 10, out ScrollContentPresenter scp)) |
50 |
| - { |
51 |
| - SetIsEnabled(scp, GetIsEnabled(sv)); |
52 |
| - } |
53 |
| - } |
54 |
| - } |
55 |
| - } |
| 44 | + static void PropagateValueToScp(object sender, RoutedEventArgs? _) |
| 45 | + { |
| 46 | + if (sender is FrameworkElement sv && TryFindFirstChild(sv, 10, out ScrollContentPresenter scp)) |
| 47 | + { |
| 48 | + SetIsEnabled(scp, GetIsEnabled(sv)); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
56 | 53 |
|
57 |
| - private static void OnWheelChanged(object sender, PointerRoutedEventArgs e) |
58 |
| - { |
59 |
| - if (sender is not ScrollContentPresenter { ScrollOwner: ScrollViewer sv } scp || !GetIsEnabled(scp)) |
60 |
| - { |
61 |
| - if (sender is FrameworkElement fe) |
62 |
| - { |
63 |
| - fe.PointerWheelChanged -= OnWheelChanged; |
64 |
| - } |
| 54 | + private static void OnWheelChanged(object sender, PointerRoutedEventArgs e) |
| 55 | + { |
| 56 | + if (sender is not ScrollContentPresenter { ScrollOwner: ScrollViewer sv } scp || !GetIsEnabled(scp)) |
| 57 | + { |
| 58 | + if (sender is FrameworkElement fe) |
| 59 | + { |
| 60 | + fe.PointerWheelChanged -= OnWheelChanged; |
| 61 | + } |
65 | 62 |
|
66 |
| - return; |
67 |
| - } |
| 63 | + return; |
| 64 | + } |
68 | 65 |
|
69 |
| - var properties = e.GetCurrentPoint(null).Properties; |
70 |
| - if (e.KeyModifiers == VirtualKeyModifiers.Control) |
71 |
| - { |
72 |
| - // Zoom, do nothing. |
73 |
| - } |
74 |
| - else if (!scp.CanVerticallyScroll || properties.IsHorizontalMouseWheel || e.KeyModifiers == VirtualKeyModifiers.Shift) |
75 |
| - { |
76 |
| - if (scp.CanHorizontallyScroll) |
77 |
| - { |
78 |
| - sv.ChangeView( |
79 |
| - horizontalOffset: sv.HorizontalOffset + properties.MouseWheelDelta, |
80 |
| - verticalOffset: null, |
81 |
| - zoomFactor: null, |
82 |
| - disableAnimation: false); |
| 66 | + var properties = e.GetCurrentPoint(null).Properties; |
| 67 | + if (e.KeyModifiers == VirtualKeyModifiers.Control) |
| 68 | + { |
| 69 | + // Zoom, do nothing. |
| 70 | + } |
| 71 | + else if (!scp.CanVerticallyScroll || properties.IsHorizontalMouseWheel || e.KeyModifiers == VirtualKeyModifiers.Shift) |
| 72 | + { |
| 73 | + if (scp.CanHorizontallyScroll) |
| 74 | + { |
| 75 | + sv.ChangeView( |
| 76 | + horizontalOffset: sv.HorizontalOffset + properties.MouseWheelDelta, |
| 77 | + verticalOffset: null, |
| 78 | + zoomFactor: null, |
| 79 | + disableAnimation: false); |
83 | 80 |
|
84 |
| - e.Handled = true; |
85 |
| - } |
86 |
| - } |
87 |
| - else |
88 |
| - { |
89 |
| - sv.ChangeView( |
90 |
| - horizontalOffset: null, |
91 |
| - verticalOffset: sv.VerticalOffset + properties.MouseWheelDelta, |
92 |
| - zoomFactor: null, |
93 |
| - disableAnimation: false); |
| 81 | + e.Handled = true; |
| 82 | + } |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + sv.ChangeView( |
| 87 | + horizontalOffset: null, |
| 88 | + verticalOffset: sv.VerticalOffset + properties.MouseWheelDelta, |
| 89 | + zoomFactor: null, |
| 90 | + disableAnimation: false); |
94 | 91 |
|
95 |
| - e.Handled = true; |
96 |
| - } |
97 |
| - } |
| 92 | + e.Handled = true; |
| 93 | + } |
| 94 | + } |
98 | 95 |
|
99 |
| - private static bool TryFindFirstChild<T>(DependencyObject element, uint limit, /*[NotNullWhen(true)]*/ out T result) |
100 |
| - { |
101 |
| - // Finds the first child of type T in the visual tree of element. |
102 |
| - // This is a workaround for the fact that ScrollViewer doesn't expose its ScrollContentPresenter. |
| 96 | + private static bool TryFindFirstChild<T>(DependencyObject element, uint limit, /*[NotNullWhen(true)]*/ out T result) |
| 97 | + { |
| 98 | + // Finds the first child of type T in the visual tree of element. |
| 99 | + // This is a workaround for the fact that ScrollViewer doesn't expose its ScrollContentPresenter. |
103 | 100 |
|
104 |
| - if (element is T t) |
105 |
| - { |
106 |
| - result = t; |
107 |
| - return true; |
108 |
| - } |
| 101 | + if (element is T t) |
| 102 | + { |
| 103 | + result = t; |
| 104 | + return true; |
| 105 | + } |
109 | 106 |
|
110 |
| - if (limit is not 0) |
111 |
| - { |
112 |
| - for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) |
113 |
| - { |
114 |
| - if (TryFindFirstChild(VisualTreeHelper.GetChild(element, i), limit - 1, out result)) |
115 |
| - { |
116 |
| - return true; |
117 |
| - } |
118 |
| - } |
119 |
| - } |
| 107 | + if (limit is not 0) |
| 108 | + { |
| 109 | + for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) |
| 110 | + { |
| 111 | + if (TryFindFirstChild(VisualTreeHelper.GetChild(element, i), limit - 1, out result)) |
| 112 | + { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
120 | 117 |
|
121 |
| - result = default; |
122 |
| - return false; |
123 |
| - } |
| 118 | + result = default; |
| 119 | + return false; |
| 120 | + } |
124 | 121 | }
|
0 commit comments