Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flyout focus enhancement #1805

Merged
merged 2 commits into from
Feb 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions MahApps.Metro/Controls/Flyout.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand All @@ -15,6 +16,7 @@ namespace MahApps.Metro.Controls
/// </summary>
[TemplatePart(Name = "PART_BackButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_Header", Type = typeof(ContentPresenter))]
[TemplatePart(Name = "PART_Content", Type = typeof(ContentPresenter))]
public class Flyout : ContentControl
{
/// <summary>
Expand Down Expand Up @@ -57,6 +59,8 @@ public event RoutedEventHandler ClosingFinished
public static readonly DependencyProperty CloseButtonVisibilityProperty = DependencyProperty.Register("CloseButtonVisibility", typeof(Visibility), typeof(Flyout), new FrameworkPropertyMetadata(Visibility.Visible));
public static readonly DependencyProperty TitleVisibilityProperty = DependencyProperty.Register("TitleVisibility", typeof(Visibility), typeof(Flyout), new FrameworkPropertyMetadata(Visibility.Visible));
public static readonly DependencyProperty AreAnimationsEnabledProperty = DependencyProperty.Register("AreAnimationsEnabled", typeof(bool), typeof(Flyout), new PropertyMetadata(true));
public static readonly DependencyProperty FocusedElementProperty = DependencyProperty.Register("FocusedElement", typeof(FrameworkElement), typeof(Flyout), new UIPropertyMetadata(null));
public static readonly DependencyProperty AllowFocusElementProperty = DependencyProperty.Register("AllowFocusElement", typeof(bool), typeof(Flyout), new PropertyMetadata(true));

internal PropertyChangeNotifier IsOpenPropertyChangeNotifier { get; set; }
internal PropertyChangeNotifier ThemePropertyChangeNotifier { get; set; }
Expand Down Expand Up @@ -185,6 +189,24 @@ public FlyoutTheme Theme
set { SetValue(ThemeProperty, value); }
}

/// <summary>
/// Gets or sets the focused element.
/// </summary>
public FrameworkElement FocusedElement
{
get { return (FrameworkElement)this.GetValue(FocusedElementProperty); }
set { this.SetValue(FocusedElementProperty, value); }
}

/// <summary>
/// Gets or sets a value indicating whether the flyout should try focus an element.
/// </summary>
public bool AllowFocusElement
{
get { return (bool)this.GetValue(AllowFocusElementProperty); }
set { this.SetValue(AllowFocusElementProperty, value); }
}

public Flyout()
{
this.Loaded += (sender, args) => UpdateFlyoutTheme();
Expand Down Expand Up @@ -315,9 +337,12 @@ private static void IsOpenedChanged(DependencyObject dependencyObject, Dependenc
}
flyout.Visibility = Visibility.Visible;
flyout.ApplyAnimation(flyout.Position, flyout.AnimateOpacity);
flyout.TryFocusElement();
}
else
{
// focus the Flyout itself to avoid nasty FocusVisual painting (it's visible until the Flyout is closed)
flyout.Focus();
if (flyout.hideStoryboard != null)
{
flyout.hideStoryboard.Completed += flyout.HideStoryboard_Completed;
Expand All @@ -334,9 +359,12 @@ private static void IsOpenedChanged(DependencyObject dependencyObject, Dependenc
if ((bool)e.NewValue)
{
flyout.Visibility = Visibility.Visible;
flyout.TryFocusElement();
}
else
{
// focus the Flyout itself to avoid nasty FocusVisual painting (it's visible until the Flyout is closed)
flyout.Focus();
flyout.Hide();
}
VisualStateManager.GoToState(flyout, (bool)e.NewValue == false ? "HideDirect" : "ShowDirect", true);
Expand Down Expand Up @@ -364,6 +392,27 @@ private void Hide()
this.RaiseEvent(new RoutedEventArgs(ClosingFinishedEvent));
}

private void TryFocusElement()
{
if (this.AllowFocusElement)
{
// first focus itself
this.Focus();

if (this.FocusedElement != null)
{
this.FocusedElement.Focus();
}
else if (this.PART_Content == null || !this.PART_Content.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)))
{
if (this.PART_Header != null)
{
this.PART_Header.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
}
}
}
}

private static void ThemeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var flyout = (Flyout)dependencyObject;
Expand Down Expand Up @@ -409,6 +458,8 @@ static Flyout()
SplineDoubleKeyFrame showFrame;
SplineDoubleKeyFrame showFrameY;
SplineDoubleKeyFrame fadeOutFrame;
ContentPresenter PART_Header;
ContentPresenter PART_Content;

public override void OnApplyTemplate()
{
Expand All @@ -418,6 +469,9 @@ public override void OnApplyTemplate()
if (root == null)
return;

PART_Header = (ContentPresenter)GetTemplateChild("PART_Header");
PART_Content = (ContentPresenter)GetTemplateChild("PART_Content");

hideStoryboard = (Storyboard)GetTemplateChild("HideStoryboard");
hideFrame = (SplineDoubleKeyFrame)GetTemplateChild("hideFrame");
hideFrameY = (SplineDoubleKeyFrame)GetTemplateChild("hideFrameY");
Expand Down
7 changes: 6 additions & 1 deletion MahApps.Metro/Themes/Flyout.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<DockPanel>
<DockPanel FocusVisualStyle="{x:Null}"
Focusable="False">
<ContentPresenter x:Name="PART_Header"
DockPanel.Dock="Top"
ContentSource="Header"
Expand Down Expand Up @@ -255,8 +256,12 @@
<Style TargetType="{x:Type Controls:Flyout}">
<Setter Property="Visibility"
Value="Hidden" />
<Setter Property="KeyboardNavigation.ControlTabNavigation"
Value="Cycle" />
<Setter Property="KeyboardNavigation.TabNavigation"
Value="Cycle" />
<Setter Property="KeyboardNavigation.DirectionalNavigation"
Value="Cycle" />
<Setter Property="VerticalAlignment"
Value="Stretch" />
<Setter Property="VerticalContentAlignment"
Expand Down
3 changes: 2 additions & 1 deletion samples/MetroDemo/ExampleWindows/FlyoutDemo.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
Margin="100 0 0 0"
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Controls:MetroWindow}, Path=ActualWidth}"
Header="Settings"
Position="Right">
Position="Right"
FocusedElement="{Binding ElementName=firstTB}">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand Down
1 change: 0 additions & 1 deletion samples/MetroDemo/ExampleWindows/FlyoutDemo.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public FlyoutDemo()
{
this.DataContext = new MainWindowViewModel();
this.InitializeComponent();
settingsFlyout.IsOpenChanged += (sender, args) => firstTB.Focus();
this.Closing += (s, e) =>
{
if (_hideOnClose)
Expand Down