Skip to content

Commit

Permalink
Move focus visual properties to FocusVisualHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Oct 22, 2019
1 parent b5675ea commit af4f8d8
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 164 deletions.
2 changes: 1 addition & 1 deletion ModernWpf.Controls/HyperlinkButton/HyperlinkButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="ui:FrameworkElementHelper.FocusVisualMargin" Value="-3" />
<Setter Property="ui:FocusVisualHelper.FocusVisualMargin" Value="-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:HyperlinkButton">
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf.Controls/RadioButtons/RadioButtons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<Setter Property="MinWidth" Value="120" />
<Setter Property="AllowDrop" Value="False" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="ui:FrameworkElementHelper.FocusVisualMargin" Value="-7,-3,4,-3" />
<Setter Property="ui:FocusVisualHelper.FocusVisualMargin" Value="-7,-3,4,-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:RadioButtonsListViewItem">
Expand Down
4 changes: 2 additions & 2 deletions ModernWpf.Controls/ToggleSwitch/ToggleSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<!--<Setter Property="ManipulationMode" Value="System,TranslateX" />-->
<Setter Property="FocusVisualStyle" Value="{DynamicResource EmptyFocusVisual}" />
<Setter Property="ui:FrameworkElementHelper.FocusVisualMargin" Value="-7,-3,-7,-3" />
<Setter Property="ui:FocusVisualHelper.FocusVisualMargin" Value="-7,-3,-7,-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ToggleSwitch">
Expand Down Expand Up @@ -224,7 +224,7 @@
<Control
x:Name="FocusVisual"
Style="{DynamicResource CustomFocusVisual}"
Margin="{TemplateBinding ui:FrameworkElementHelper.FocusVisualMargin}" />
Margin="{TemplateBinding ui:FocusVisualHelper.FocusVisualMargin}" />
</Grid>
<ContentPresenter
x:Name="OffContentPresenter"
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf.SampleApp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<Setter Property="MinHeight" Value="{DynamicResource GridViewItemMinHeight}" />
<Setter Property="AllowDrop" Value="False" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="ui:FrameworkElementHelper.FocusVisualMargin" Value="-2" />
<Setter Property="ui:FocusVisualHelper.FocusVisualMargin" Value="-2" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
Expand Down
141 changes: 131 additions & 10 deletions ModernWpf/Controls/Primitives/FocusVisualHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,127 @@ namespace ModernWpf.Controls.Primitives
{
public static class FocusVisualHelper
{
#region FocusVisualPrimaryBrush

public static Brush GetFocusVisualPrimaryBrush(FrameworkElement element)
{
return (Brush)element.GetValue(FocusVisualPrimaryBrushProperty);
}

public static void SetFocusVisualPrimaryBrush(FrameworkElement element, Brush value)
{
element.SetValue(FocusVisualPrimaryBrushProperty, value);
}

public static readonly DependencyProperty FocusVisualPrimaryBrushProperty =
DependencyProperty.RegisterAttached(
"FocusVisualPrimaryBrush",
typeof(Brush),
typeof(FocusVisualHelper),
null);

#endregion

#region FocusVisualSecondaryBrush

public static Brush GetFocusVisualSecondaryBrush(FrameworkElement element)
{
return (Brush)element.GetValue(FocusVisualSecondaryBrushProperty);
}

public static void SetFocusVisualSecondaryBrush(FrameworkElement element, Brush value)
{
element.SetValue(FocusVisualSecondaryBrushProperty, value);
}

public static readonly DependencyProperty FocusVisualSecondaryBrushProperty =
DependencyProperty.RegisterAttached(
"FocusVisualSecondaryBrush",
typeof(Brush),
typeof(FocusVisualHelper),
null);

#endregion

#region FocusVisualPrimaryThickness

public static Thickness GetFocusVisualPrimaryThickness(FrameworkElement element)
{
return (Thickness)element.GetValue(FocusVisualPrimaryThicknessProperty);
}

public static void SetFocusVisualPrimaryThickness(FrameworkElement element, Thickness value)
{
element.SetValue(FocusVisualPrimaryThicknessProperty, value);
}

public static readonly DependencyProperty FocusVisualPrimaryThicknessProperty =
DependencyProperty.RegisterAttached(
"FocusVisualPrimaryThickness",
typeof(Thickness),
typeof(FocusVisualHelper),
new FrameworkPropertyMetadata(new Thickness(1)));

#endregion

#region FocusVisualSecondaryThickness

public static Thickness GetFocusVisualSecondaryThickness(FrameworkElement element)
{
return (Thickness)element.GetValue(FocusVisualSecondaryThicknessProperty);
}

public static void SetFocusVisualSecondaryThickness(FrameworkElement element, Thickness value)
{
element.SetValue(FocusVisualSecondaryThicknessProperty, value);
}

public static readonly DependencyProperty FocusVisualSecondaryThicknessProperty =
DependencyProperty.RegisterAttached(
"FocusVisualSecondaryThickness",
typeof(Thickness),
typeof(FocusVisualHelper),
new FrameworkPropertyMetadata(new Thickness(1)));

#endregion

#region FocusVisualMargin

/// <summary>
/// Gets the outer margin of the focus visual for a FrameworkElement.
/// </summary>
/// <param name="element">The element from which to read the property value.</param>
/// <returns>
/// Provides margin values for the focus visual. The default value is a default Thickness
/// with all properties (dimensions) equal to 0.
/// </returns>
public static Thickness GetFocusVisualMargin(FrameworkElement element)
{
return (Thickness)element.GetValue(FocusVisualMarginProperty);
}

/// <summary>
/// Sets the outer margin of the focus visual for a FrameworkElement.
/// </summary>
/// <param name="element">The element on which to set the attached property.</param>
/// <param name="value">The property value to set.</param>
public static void SetFocusVisualMargin(FrameworkElement element, Thickness value)
{
element.SetValue(FocusVisualMarginProperty, value);
}

/// <summary>
/// Identifies the FocusVisualMargin dependency property.
/// </summary>
public static readonly DependencyProperty FocusVisualMarginProperty =
DependencyProperty.RegisterAttached(
"FocusVisualMargin",
typeof(Thickness),
typeof(FocusVisualHelper),
null);

#endregion

#region IsSystemFocusVisual

public static bool GetIsSystemFocusVisual(Control control)
Expand All @@ -31,11 +152,11 @@ private static void OnIsSystemFocusVisualChanged(DependencyObject d, DependencyP
var control = (Control)d;
if ((bool)e.NewValue)
{
control.IsVisibleChanged += OnControlIsVisibleChanged;
control.IsVisibleChanged += OnFocusVisualIsVisibleChanged;
}
else
{
control.IsVisibleChanged -= OnControlIsVisibleChanged;
control.IsVisibleChanged -= OnFocusVisualIsVisibleChanged;
control.ClearValue(FrameworkElement.MarginProperty);
}
}
Expand Down Expand Up @@ -86,26 +207,26 @@ private static void SetFocusedElement(Control control, FrameworkElement value)

#endregion

private static void OnControlIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
private static void OnFocusVisualIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var control = (Control)sender;
var focusVisual = (Control)sender;
if ((bool)e.NewValue)
{
if ((VisualTreeHelper.GetParent(control) as Adorner)?.AdornedElement is FrameworkElement focusedElement)
if ((VisualTreeHelper.GetParent(focusVisual) as Adorner)?.AdornedElement is FrameworkElement focusedElement)
{
SetIsSystemFocusVisualVisible(focusedElement, true);
control.Margin = FrameworkElementHelper.GetFocusVisualMargin(focusedElement);
SetFocusedElement(control, focusedElement);
focusVisual.Margin = FocusVisualHelper.GetFocusVisualMargin(focusedElement);
SetFocusedElement(focusVisual, focusedElement);
}
}
else
{
FrameworkElement focusedElement = GetFocusedElement(control);
FrameworkElement focusedElement = GetFocusedElement(focusVisual);
if (focusedElement != null)
{
focusedElement.ClearValue(IsSystemFocusVisualVisiblePropertyKey);
control.ClearValue(FrameworkElement.MarginProperty);
control.ClearValue(FocusedElementProperty);
focusVisual.ClearValue(FrameworkElement.MarginProperty);
focusVisual.ClearValue(FocusedElementProperty);
}
}
}
Expand Down
129 changes: 0 additions & 129 deletions ModernWpf/Controls/Primitives/FrameworkElementHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion ModernWpf/Styles/Button.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="primitives:ControlHelper.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="primitives:FrameworkElementHelper.FocusVisualMargin" Value="-3" />
<Setter Property="primitives:FocusVisualHelper.FocusVisualMargin" Value="-3" />
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
Expand Down
6 changes: 3 additions & 3 deletions ModernWpf/Styles/Calendar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="primitives:FrameworkElementHelper.FocusVisualMargin" Value="-3" />
<Setter Property="primitives:FocusVisualHelper.FocusVisualMargin" Value="-3" />
<Setter Property="FontSize" Value="20" />
<Setter Property="Background" Value="{DynamicResource CalendarViewNavigationButtonBackground}" />
<Setter Property="Template">
Expand Down Expand Up @@ -203,7 +203,7 @@
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="primitives:FrameworkElementHelper.FocusVisualMargin" Value="-2" />
<Setter Property="primitives:FocusVisualHelper.FocusVisualMargin" Value="-2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CalendarDayButton}">
Expand Down Expand Up @@ -311,7 +311,7 @@
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="primitives:FrameworkElementHelper.FocusVisualMargin" Value="-2" />
<Setter Property="primitives:FocusVisualHelper.FocusVisualMargin" Value="-2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CalendarButton}">
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf/Styles/CheckBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<Setter Property="MinHeight" Value="32" />
<Setter Property="primitives:ControlHelper.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}" />
<Setter Property="primitives:FrameworkElementHelper.FocusVisualMargin" Value="-7,-3,-7,-3" />
<Setter Property="primitives:FocusVisualHelper.FocusVisualMargin" Value="-7,-3,-7,-3" />
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
Expand Down
Loading

0 comments on commit af4f8d8

Please sign in to comment.