Skip to content

Commit c623514

Browse files
authored
Merge pull request #4398 from IvenBach/Issue4278_Mousewheel_doesnt_scroll_in_AC_settings
Allow mousewheel scrolling while inside DataGrid of Autocompletion settings window
2 parents 6f1b36a + b829fda commit c623514

File tree

6 files changed

+129
-31
lines changed

6 files changed

+129
-31
lines changed

Rubberduck.Core/Rubberduck.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@
382382
<Compile Include="AutoComplete\AutoCompleteClosingParenthese.cs" />
383383
<Compile Include="AutoComplete\AutoCompleteClosingString.cs" />
384384
<Compile Include="AutoComplete\AutoCompleteService.cs" />
385+
<Compile Include="UI\Settings\ScrollViewerCorrector.cs" />
385386
<Compile Include="UI\Settings\Converters\EmptyLineHandlingToTextConverter.cs" />
386387
<Compile Include="UI\Settings\Converters\EmptyLineHandlingValueTextToEnumValueConverter.cs" />
387388
<Compile Include="UI\Settings\Converters\EnumToBooleanConverter.cs" />

Rubberduck.Core/UI/Settings/AutoCompleteSettings.xaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@
4444
</MultiTrigger>
4545
</Style.Triggers>
4646
</Style>
47-
47+
<Style TargetType="ScrollViewer">
48+
<Style.Setters>
49+
<Setter Property="settings:ScrollViewerCorrector.FixScrolling" Value="True" />
50+
</Style.Setters>
51+
</Style>
4852
</UserControl.Resources>
4953
<Grid>
5054
<Grid.RowDefinitions>
5155
<RowDefinition Height="*" />
5256
<RowDefinition Height="32" />
5357
</Grid.RowDefinitions>
54-
<ScrollViewer Grid.Row="0">
58+
<ScrollViewer x:Name="ScrollViewer"
59+
Grid.Row="0">
5560
<StackPanel Margin="5,5,5,0" ScrollViewer.VerticalScrollBarVisibility="Auto">
5661
<Label Background="DarkGray"
5762
HorizontalContentAlignment="Stretch"
@@ -93,8 +98,7 @@
9398
<CheckBox IsEnabled="{Binding IsEnabled}" Margin="10,10" IsChecked="{Binding EnableSmartConcat}" HorizontalAlignment="Center" Content="{Resx ResxName=Rubberduck.Resources.Settings.AutoCompletesPage, Key=EnableSmartConcat}" />
9499
</WrapPanel>
95100
<Border BorderBrush="DarkGray" BorderThickness="1" CornerRadius="2">
96-
<DataGrid Name="AutoCompleteSettingsGrid"
97-
ItemsSource="{Binding Settings}"
101+
<DataGrid ItemsSource="{Binding Settings}"
98102
AutoGenerateColumns="False"
99103
CanUserReorderColumns="False"
100104
IsReadOnly="False"

Rubberduck.Core/UI/Settings/GeneralSettings.xaml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@
6969
</MultiTrigger>
7070
</Style.Triggers>
7171
</Style>
72+
<Style TargetType="ScrollViewer">
73+
<Style.Setters>
74+
<Setter Property="settings:ScrollViewerCorrector.FixScrolling" Value="True" />
75+
</Style.Setters>
76+
</Style>
7277
</UserControl.Resources>
7378
<Grid>
74-
<ScrollViewer>
79+
<ScrollViewer x:Name="ScrollViewer">
7580
<Grid>
7681
<StackPanel Grid.Row="0" Margin="5,5,5,0">
7782
<Label Background="DarkGray"
@@ -151,22 +156,22 @@
151156
</DockPanel>
152157

153158
<DataGrid ItemsSource="{Binding Hotkeys}"
154-
HorizontalAlignment="Stretch"
155-
AutoGenerateColumns="False"
156-
CanUserDeleteRows="False"
157-
CanUserReorderColumns="False"
158-
CanUserResizeColumns="True"
159-
IsReadOnly="False"
160-
SelectionMode="Single"
161-
CanUserAddRows="False"
162-
HorizontalGridLinesBrush="Transparent"
163-
VerticalGridLinesBrush="Transparent"
164-
HeadersVisibility="Column"
165-
VerticalScrollBarVisibility="Auto"
166-
HorizontalScrollBarVisibility="Hidden"
167-
ItemContainerStyle="{StaticResource PrettifyRow}"
168-
ColumnHeaderHeight="22"
169-
BorderThickness="0">
159+
HorizontalAlignment="Stretch"
160+
AutoGenerateColumns="False"
161+
CanUserDeleteRows="False"
162+
CanUserReorderColumns="False"
163+
CanUserResizeColumns="True"
164+
IsReadOnly="False"
165+
SelectionMode="Single"
166+
CanUserAddRows="False"
167+
HorizontalGridLinesBrush="Transparent"
168+
VerticalGridLinesBrush="Transparent"
169+
HeadersVisibility="Column"
170+
VerticalScrollBarVisibility="Auto"
171+
HorizontalScrollBarVisibility="Hidden"
172+
ItemContainerStyle="{StaticResource PrettifyRow}"
173+
ColumnHeaderHeight="22"
174+
BorderThickness="0">
170175
<DataGrid.CellStyle>
171176
<Style TargetType="{x:Type DataGridCell}">
172177
<Setter Property="BorderThickness" Value="0" />

Rubberduck.Core/UI/Settings/IndenterSettings.xaml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
4-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5-
xmlns:converters="clr-namespace:Rubberduck.UI.Settings.Converters"
6-
xmlns:controls="clr-namespace:Rubberduck.UI.Controls"
7-
xmlns:core="clr-namespace:System;assembly=mscorlib"
8-
xmlns:smartIndenter="clr-namespace:Rubberduck.SmartIndenter;assembly=Rubberduck.SmartIndenter"
9-
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:converters="clr-namespace:Rubberduck.UI.Settings.Converters"
6+
xmlns:controls="clr-namespace:Rubberduck.UI.Controls"
7+
xmlns:core="clr-namespace:System;assembly=mscorlib"
8+
xmlns:settings="clr-namespace:Rubberduck.UI.Settings"
9+
xmlns:smartIndenter="clr-namespace:Rubberduck.SmartIndenter;assembly=Rubberduck.SmartIndenter"
10+
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
1011
x:Class="Rubberduck.UI.Settings.IndenterSettings">
1112
<UserControl.Resources>
1213
<converters:EndOfLineCommentStyleToVisibilityConverter x:Key="EndOfLineCommentStyleToVisibility"/>
@@ -30,6 +31,11 @@
3031
<x:TypeExtension Type="{x:Type smartIndenter:EmptyLineHandling}"/>
3132
</ObjectDataProvider.MethodParameters>
3233
</ObjectDataProvider>
34+
<Style TargetType="ScrollViewer">
35+
<Style.Setters>
36+
<Setter Property="settings:ScrollViewerCorrector.FixScrolling" Value="True" />
37+
</Style.Setters>
38+
</Style>
3339
</UserControl.Resources>
3440
<ScrollViewer Margin="0,0,10,0">
3541
<StackPanel Margin="5">
@@ -360,7 +366,7 @@
360366
<Resx ResxName="Rubberduck.Resources.RubberduckUI" Key="IndenterSettings_CodeSampleHeader"/>
361367
</Label>
362368
</Expander.Header>
363-
<controls:BindableTextEditor
369+
<controls:BindableTextEditor
364370
Margin="10,10,10,10"
365371
FontFamily="Courier New "
366372
ShowLineNumbers="False"

Rubberduck.Core/UI/Settings/InspectionSettings.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,14 @@
255255
</DataTrigger>
256256
</Style.Triggers>
257257
</Style>
258+
<Style TargetType="ScrollViewer">
259+
<Style.Setters>
260+
<Setter Property="settings:ScrollViewerCorrector.FixScrolling" Value="True" />
261+
</Style.Setters>
262+
</Style>
258263
</UserControl.Resources>
259264
<Grid>
260-
<ScrollViewer>
265+
<ScrollViewer x:Name="ScrollViewer">
261266
<StackPanel Margin="5,5,5,0" ScrollViewer.VerticalScrollBarVisibility="Auto">
262267
<Label Background="DarkGray"
263268
HorizontalContentAlignment="Stretch"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Windows;
4+
using System.Windows.Controls;
5+
using System.Windows.Input;
6+
7+
namespace Rubberduck.UI.Settings
8+
{
9+
//adapted from https://serialseb.com/blog/2007/09/03/wpf-tips-6-preventing-scrollviewer-from/
10+
public class ScrollViewerCorrector
11+
{
12+
public static bool GetFixScrolling(DependencyObject obj)
13+
{
14+
return (bool)obj.GetValue(FixScrolling);
15+
}
16+
17+
public static void SetFixScrolling(DependencyObject obj, bool value)
18+
{
19+
obj.SetValue(FixScrolling, value);
20+
}
21+
22+
public static readonly DependencyProperty FixScrolling =
23+
DependencyProperty.RegisterAttached("FixScrolling", typeof(bool), typeof(ScrollViewerCorrector), new FrameworkPropertyMetadata(false, OnFixScrollingPropertyChanged));
24+
25+
public static void OnFixScrollingPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
26+
{
27+
if (!(sender is ScrollViewer viewer))
28+
{
29+
throw new ArgumentException("The dependency property can only be attached to a ScrollViewer", nameof(sender));
30+
}
31+
32+
if ((bool)e.NewValue == true)
33+
{
34+
viewer.PreviewMouseWheel += HandlePreviewMouseWheel;
35+
}
36+
else
37+
{
38+
viewer.PreviewMouseWheel -= HandlePreviewMouseWheel;
39+
}
40+
}
41+
42+
private static List<MouseWheelEventArgs> _avoidReentry = new List<MouseWheelEventArgs>();
43+
44+
private static void HandlePreviewMouseWheel(object sender, MouseWheelEventArgs e)
45+
{
46+
var scrollControl = sender as ScrollViewer;
47+
if (!e.Handled && sender != null && !_avoidReentry.Contains(e))
48+
{
49+
var previewEventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
50+
{
51+
RoutedEvent = UIElement.PreviewMouseWheelEvent,
52+
Source = sender
53+
};
54+
55+
_avoidReentry.Add(previewEventArg);
56+
if (e.OriginalSource is UIElement originalSource)
57+
{
58+
originalSource.RaiseEvent(previewEventArg);
59+
}
60+
_avoidReentry.Remove(previewEventArg);
61+
62+
bool isAlreadyAtTop = e.Delta > 0 && scrollControl.VerticalOffset == 0;
63+
bool isAlreadyAtBottom = (e.Delta <= 0 && scrollControl.VerticalOffset >= scrollControl.ExtentHeight - scrollControl.ViewportHeight);
64+
if (!previewEventArg.Handled && (isAlreadyAtTop || isAlreadyAtBottom))
65+
{
66+
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
67+
{
68+
RoutedEvent = UIElement.MouseWheelEvent,
69+
Source = sender
70+
};
71+
var parent = ((Control)sender).Parent as UIElement;
72+
parent.RaiseEvent(eventArg);
73+
}
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)