Skip to content

Commit d18d4d4

Browse files
Add TabAssist.TabScrollOffset to give control of the offset
Default value (40, although spec says 52) is set in style to allow easy override at the call site.
1 parent c27cc43 commit d18d4d4

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/MaterialDesignThemes.Wpf/Behaviors/Internal/TabControlHeaderScrollBehavior.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ private static void OnScrollableContentChanged(DependencyObject d, DependencyPro
6565
behavior.AddPaddingToScrollableContentIfWiderThanViewPort();
6666
}
6767

68-
internal const double ScrollOffset = 40; // MD spec says 52 DP, but that seems a little excessive in practice
69-
internal static readonly Thickness ScrollableContentPadding = new(ScrollOffset, 0, ScrollOffset, 0);
70-
internal static readonly Thickness NoScrollableContentPadding = new(0);
71-
7268
private double? _desiredScrollStart;
7369
private bool _isAnimatingScroll;
7470

@@ -103,11 +99,12 @@ private void AddPaddingToScrollableContentIfWiderThanViewPort()
10399

104100
if (ScrollableContent.ActualWidth > TabControl.ActualWidth)
105101
{
106-
ScrollableContent.Margin = ScrollableContentPadding;
102+
double offset = TabAssist.GetTabScrollOffset(TabControl);
103+
ScrollableContent.Margin = new(offset, 0, offset, 0);
107104
}
108105
else
109106
{
110-
ScrollableContent.Margin = NoScrollableContentPadding;
107+
ScrollableContent.Margin = new();
111108
}
112109
}
113110

src/MaterialDesignThemes.Wpf/Internal/BringIntoViewHijackingStackPanel.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ public TabScrollDirection TabScrollDirection
1515
DependencyProperty.Register(nameof(TabScrollDirection), typeof(TabScrollDirection),
1616
typeof(BringIntoViewHijackingStackPanel), new PropertyMetadata(TabScrollDirection.Unknown));
1717

18+
public double TabScrollOffset
19+
{
20+
get => (double)GetValue(TabScrollOffsetProperty);
21+
set => SetValue(TabScrollOffsetProperty, value);
22+
}
23+
24+
public static readonly DependencyProperty TabScrollOffsetProperty =
25+
DependencyProperty.Register(nameof(TabScrollOffset),
26+
typeof(double), typeof(BringIntoViewHijackingStackPanel), new PropertyMetadata(0d));
27+
1828
public BringIntoViewHijackingStackPanel()
1929
=> AddHandler(FrameworkElement.RequestBringIntoViewEvent, new RoutedEventHandler(OnRequestBringIntoView), false);
2030

@@ -26,8 +36,8 @@ private void OnRequestBringIntoView(object sender, RoutedEventArgs e)
2636

2737
// TODO: Consider making the "TabScrollDirection" a destructive read (i.e. reset the value once it is read) to avoid leaving a Backward/Forward value that may be misinterpreted at a later stage.
2838
double offset = TabScrollDirection switch {
29-
TabScrollDirection.Backward => -TabControlHeaderScrollBehavior.ScrollOffset,
30-
TabScrollDirection.Forward => TabControlHeaderScrollBehavior.ScrollOffset,
39+
TabScrollDirection.Backward => -TabScrollOffset,
40+
TabScrollDirection.Forward => TabScrollOffset,
3141
_ => 0
3242
};
3343
var point = child.TranslatePoint(new Point(), this);

src/MaterialDesignThemes.Wpf/TabAssist.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@ public static void SetAnimateTabScrolling(DependencyObject obj, bool value)
7878

7979
public static readonly DependencyProperty AnimateTabScrollingProperty =
8080
DependencyProperty.RegisterAttached("AnimateTabScrolling", typeof(bool), typeof(TabAssist), new PropertyMetadata(false));
81+
82+
public static double GetTabScrollOffset(DependencyObject obj)
83+
=> (double)obj.GetValue(TabScrollOffsetProperty);
84+
85+
public static void SetTabScrollOffset(DependencyObject obj, double value)
86+
=> obj.SetValue(TabScrollOffsetProperty, value);
87+
88+
public static readonly DependencyProperty TabScrollOffsetProperty =
89+
DependencyProperty.RegisterAttached("TabScrollOffset", typeof(double),
90+
typeof(TabAssist), new PropertyMetadata(0d));
8191
}

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TabControl.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
<b:Interaction.Behaviors>
4343
<behaviorsInternal:TabControlHeaderScrollBehavior TabControl="{Binding RelativeSource={RelativeSource TemplatedParent}}" ScrollableContent="{Binding ElementName=ScrollableContent}" />
4444
</b:Interaction.Behaviors>
45-
<internal:BringIntoViewHijackingStackPanel x:Name="ScrollableContent" TabScrollDirection="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(behaviorsInternal:TabControlHeaderScrollBehavior.TabScrollDirection)}">
45+
<internal:BringIntoViewHijackingStackPanel x:Name="ScrollableContent"
46+
TabScrollDirection="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(behaviorsInternal:TabControlHeaderScrollBehavior.TabScrollDirection)}"
47+
TabScrollOffset="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:TabAssist.TabScrollOffset)}">
4648
<UniformGrid x:Name="CenteredHeaderPanel"
4749
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
4850
Margin="{Binding Path=(wpf:TabAssist.HeaderPanelMargin), RelativeSource={RelativeSource TemplatedParent}}"
@@ -234,6 +236,8 @@
234236
<Setter Property="wpf:RippleAssist.Feedback" Value="{DynamicResource MaterialDesign.Brush.Button.Ripple}" />
235237
<Setter Property="wpf:TabAssist.HasUniformTabWidth" Value="False" />
236238
<Setter Property="wpf:TabAssist.AnimateTabScrolling" Value="True" />
239+
<!-- MD spec says 52 DP, but that seems a little excessive in practice -->
240+
<Setter Property="wpf:TabAssist.TabScrollOffset" Value="40" />
237241

238242
<Style.Triggers>
239243
<Trigger Property="wpf:TabAssist.HeaderBehavior" Value="Wrapping">

0 commit comments

Comments
 (0)