Skip to content

Commit

Permalink
optimized tab switching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Jun 13, 2022
1 parent 41c661d commit 03a4777
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
8 changes: 2 additions & 6 deletions src/Shared/HandyControl_Shared/Controls/Ribbon/Ribbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace HandyControl.Controls
{
Expand Down Expand Up @@ -51,7 +52,7 @@ internal void ResetSelection()
InitializeSelection();
}

internal void NotifyMouseClickedOnTabHeader(RibbonTabHeader tabHeader)
internal void NotifyMouseClickedOnTabHeader(RibbonTabHeader tabHeader, MouseButtonEventArgs e)
{
if (_tabHeaderItemsControl == null)
{
Expand Down Expand Up @@ -110,11 +111,6 @@ protected override void OnSelectionChanged(SelectionChangedEventArgs e)
return;
}

if (e.RemovedItems.Count > 0 && ItemContainerGenerator.ContainerFromItem(e.RemovedItems[0]) is RibbonTab oldRibbonTab)
{
oldRibbonTab.IsSelected = false;
}

SelectedItem = e.AddedItems[0];
}

Expand Down
9 changes: 7 additions & 2 deletions src/Shared/HandyControl_Shared/Controls/Ribbon/RibbonTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ static RibbonTab()
VisibilityProperty.OverrideMetadata(typeof(RibbonTab), new PropertyMetadata(Visibility.Visible, OnVisibilityChanged));
}

public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
"IsSelected", typeof(bool), typeof(RibbonTab), new PropertyMetadata(ValueBoxes.FalseBox, OnIsSelectedChanged));
public static readonly DependencyProperty IsSelectedProperty =
Selector.IsSelectedProperty.AddOwner(typeof(RibbonTab),
new FrameworkPropertyMetadata(ValueBoxes.FalseBox,
FrameworkPropertyMetadataOptions.AffectsParentMeasure |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault |
FrameworkPropertyMetadataOptions.Journal,
OnIsSelectedChanged));

private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Expand Down
26 changes: 24 additions & 2 deletions src/Shared/HandyControl_Shared/Controls/Ribbon/RibbonTabHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,39 @@ public bool IsSelected
set => SetValue(IsSelectedProperty, value);
}

internal void PrepareRibbonTabHeader()
{
CoerceValue(IsSelectedProperty);
}

protected virtual void OnIsSelectedChanged(bool oldIsSelected, bool newIsSelected)
{

}

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
var ribbon = Ribbon;
if (ribbon != null)
{
ribbon.NotifyMouseClickedOnTabHeader(this, e);
e.Handled = true;
}

base.OnMouseLeftButtonDown(e);
}

protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus(e);

var ribbonTab = RibbonTab;
if (ribbonTab == null)
{
return;
}

Ribbon.NotifyMouseClickedOnTabHeader(this);
RibbonTab.IsSelected = true;
ribbonTab.IsSelected = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ public class RibbonTabHeaderItemsControl : ItemsControl
protected override DependencyObject GetContainerForItemOverride() => new RibbonTabHeader();

protected override bool IsItemItsOwnContainerOverride(object item) => item is RibbonTabHeader;

protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);

if (element is RibbonTabHeader ribbonTabHeader)
{
ribbonTabHeader.PrepareRibbonTabHeader();
}
}
}
}
5 changes: 4 additions & 1 deletion src/Shared/HandyControl_Shared/Themes/Styles/Ribbon.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Geometry x:Key="DialogBoxLauncherGeometry">M0,0 8,0 8,1 1,1 1,8 0,8 M 3.7,3 8,7.3 8,4 9,4 9,9 4,9 4,8 7.3,8 3,3.7</Geometry>

<Style TargetType="hc:RibbonGroup">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Height" Value="95"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Padding" Value="6"/>
Expand Down Expand Up @@ -73,6 +74,7 @@
</Style>

<Style TargetType="hc:RibbonTab">
<Setter Property="Focusable" Value="False"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="ItemsPanel">
<Setter.Value>
Expand All @@ -94,6 +96,7 @@
</Style>

<Style TargetType="hc:Ribbon">
<Setter Property="Focusable" Value="False"/>
<Setter Property="Background" Value="{DynamicResource RegionBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
Expand Down Expand Up @@ -123,7 +126,7 @@
<DropShadowEffect BlurRadius="8" ShadowDepth="2" Direction="-90" Color="{StaticResource EffectShadowColor}" Opacity=".1" RenderingBias="Performance"/>
</Border.Effect>
</Border>
<hc:RibbonTabHeaderItemsControl Grid.Row="0" Grid.Column="0" x:Name="PART_TabHeaderItemsControl" Margin="10,0,10,2">
<hc:RibbonTabHeaderItemsControl Focusable="False" Grid.Row="0" Grid.Column="0" x:Name="PART_TabHeaderItemsControl" Margin="10,0,10,2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<hc:SimpleStackPanel Orientation="Horizontal"/>
Expand Down

0 comments on commit 03a4777

Please sign in to comment.