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

Close Flyouts with external click (makes #965 obsolete) #1000

Merged
merged 3 commits into from
Feb 4, 2014
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
20 changes: 15 additions & 5 deletions MahApps.Metro/Controls/Flyout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public class Flyout : ContentControl

public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(Flyout), new PropertyMetadata(default(string)));
public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(Position), typeof(Flyout), new PropertyMetadata(Position.Left, PositionChanged));
public static readonly DependencyProperty IsPinnableProperty = DependencyProperty.Register("IsPinnable", typeof(bool), typeof(Flyout), new PropertyMetadata(default(bool)));
public static readonly DependencyProperty IsPinnedProperty = DependencyProperty.Register("IsPinned", typeof(bool), typeof(Flyout), new PropertyMetadata(true));
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(Flyout), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsOpenedChanged));
public static readonly DependencyProperty IsModalProperty = DependencyProperty.Register("IsModal", typeof(bool), typeof(Flyout));
public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(Flyout));
public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.RegisterAttached("CloseCommand", typeof(ICommand), typeof(Flyout), new UIPropertyMetadata(null));
public static readonly DependencyProperty ThemeProperty = DependencyProperty.Register("Theme", typeof(FlyoutTheme), typeof(Flyout), new FrameworkPropertyMetadata(FlyoutTheme.Dark, ThemeChanged));
public static readonly DependencyProperty ExternalCloseButtonProperty = DependencyProperty.Register("ExternalCloseButton", typeof(MouseButton), typeof(Flyout), new PropertyMetadata(MouseButton.Left));

/// <summary>
/// Gets the actual theme (dark/light) of this flyout.
Expand Down Expand Up @@ -64,12 +65,21 @@ public bool IsOpen
}

/// <summary>
/// Gets/sets whether this flyout is pinnable.
/// Gets/sets whether this flyout stays open when the user clicks outside of it.
/// </summary>
public bool IsPinnable
public bool IsPinned
{
get { return (bool)GetValue(IsPinnableProperty); }
set { SetValue(IsPinnableProperty, value); }
get { return (bool)GetValue(IsPinnedProperty); }
set { SetValue(IsPinnedProperty, value); }
}

/// <summary>
/// Gets/sets the mouse button that closes the flyout on an external mouse click.
/// </summary>
public MouseButton ExternalCloseButton
{
get { return (MouseButton) GetValue(ExternalCloseButtonProperty); }
set { SetValue(ExternalCloseButtonProperty, value); }
}

/// <summary>
Expand Down
24 changes: 23 additions & 1 deletion MahApps.Metro/Controls/FlyoutsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MahApps.Metro.Controls
{
Expand All @@ -16,6 +17,27 @@ namespace MahApps.Metro.Controls
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(Flyout))]
public class FlyoutsControl : ItemsControl
{
public static readonly DependencyProperty OverrideExternalCloseButtonProperty = DependencyProperty.Register("OverrideExternalCloseButton", typeof(MouseButton?), typeof(FlyoutsControl), new PropertyMetadata(null));
public static readonly DependencyProperty OverrideIsPinnedProperty = DependencyProperty.Register("OverrideIsPinned", typeof(bool), typeof(FlyoutsControl), new PropertyMetadata(false));

/// <summary>
/// Gets/sets whether <see cref="MahApps.Metro.Controls.Flyout.ExternalCloseButton"/> is ignored and all flyouts behave as if it was set to the value of this property.
/// </summary>
public MouseButton? OverrideExternalCloseButton
{
get { return (MouseButton?) GetValue(OverrideExternalCloseButtonProperty); }
set { SetValue(OverrideExternalCloseButtonProperty, value); }
}

/// <summary>
/// Gets/sets whether <see cref="MahApps.Metro.Controls.Flyout.IsPinned"/> is ignored and all flyouts behave as if it was set false.
/// </summary>
public bool OverrideIsPinned
{
get { return (bool) GetValue(OverrideIsPinnedProperty); }
set { SetValue(OverrideIsPinnedProperty, value); }
}

static FlyoutsControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
Expand Down Expand Up @@ -139,4 +161,4 @@ private void ReorderZIndices(Flyout lastChanged)
}
}
}
}
}
28 changes: 27 additions & 1 deletion MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@ private void ThemeManagerOnIsThemeChanged(object sender, OnThemeChangedEventArgs
this.HandleWindowCommandsForFlyouts(flyouts);
}
}

private void FlyoutsPreviewKeyDown(object sender, MouseButtonEventArgs e)
{
if (Flyouts.OverrideExternalCloseButton == null)
{
foreach (Flyout flyout in Flyouts.Items)
{
if (flyout.ExternalCloseButton == e.ChangedButton && (flyout.IsPinned == false || Flyouts.OverrideIsPinned == true))
{
flyout.IsOpen = false;
}
}
}
else if (Flyouts.OverrideExternalCloseButton == e.ChangedButton)
{
foreach (Flyout flyout in Flyouts.Items)
{
if (flyout.IsPinned == false || Flyouts.OverrideIsPinned == true)
{
flyout.IsOpen = false;
}
}
}
}

static MetroWindow()
{
Expand All @@ -429,6 +453,8 @@ public override void OnApplyTemplate()
overlayBox = GetTemplateChild(PART_OverlayBox) as Grid;
metroDialogContainer = GetTemplateChild(PART_MetroDialogContainer) as Grid;
flyoutModal = GetTemplateChild(PART_FlyoutModal) as Rectangle;
flyoutModal.PreviewMouseDown += FlyoutsPreviewKeyDown;
this.PreviewMouseDown += FlyoutsPreviewKeyDown;

titleBar = GetTemplateChild(PART_TitleBar) as UIElement;

Expand Down Expand Up @@ -593,4 +619,4 @@ internal FlyoutStatusChangedRoutedEventArgs(RoutedEvent rEvent, object source):
public Flyout ChangedFlyout { get; internal set; }
}
}
}
}