Browse Source

Fix #1484

pull/1489/head
ahopper 9 years ago
parent
commit
a0b1ad62b3
  1. 4
      samples/ControlCatalog/ControlCatalog.csproj
  2. 1
      samples/ControlCatalog/MainView.xaml
  3. 49
      samples/ControlCatalog/Pages/PopupPage.xaml
  4. 19
      samples/ControlCatalog/Pages/PopupPage.xaml.cs
  5. 47
      src/Avalonia.Controls/Primitives/Popup.cs

4
samples/ControlCatalog/ControlCatalog.csproj

@ -23,6 +23,8 @@
</PropertyGroup>
<ItemGroup>
<None Remove="Pages\ContextMenuPage.xaml" />
<None Remove="Pages\PopupPage.xaml" />
<None Remove="Pages\PopupPage.xaml.cs" />
</ItemGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
@ -84,6 +86,7 @@
<EmbeddedResource Include="Pages\NumericUpDownPage.xaml">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Pages\PopupPage.xaml" />
<EmbeddedResource Include="Pages\ProgressBarPage.xaml">
<SubType>Designer</SubType>
</EmbeddedResource>
@ -158,6 +161,7 @@
<Compile Include="Pages\MenuPage.xaml.cs">
<DependentUpon>MenuPage.xaml</DependentUpon>
</Compile>
<Compile Include="Pages\PopupPage.xaml.cs" />
<Compile Include="Pages\ProgressBarPage.xaml.cs">
<DependentUpon>ProgressBarPage.xaml</DependentUpon>
</Compile>

1
samples/ControlCatalog/MainView.xaml

@ -27,5 +27,6 @@
<TabItem Header="TextBox"><pages:TextBoxPage/></TabItem>
<TabItem Header="ToolTip"><pages:ToolTipPage/></TabItem>
<TabItem Header="TreeView"><pages:TreeViewPage/></TabItem>
<TabItem Header="Popup"><pages:PopupPage/></TabItem>
</TabControl>
</UserControl>

49
samples/ControlCatalog/Pages/PopupPage.xaml

@ -0,0 +1,49 @@
<UserControl xmlns="https://github.com/avaloniaui"
>
<StackPanel Orientation="Vertical" Gap="4">
<TextBlock Classes="h1">Popup</TextBlock>
<TextBlock Classes="h2">A Popup dialog</TextBlock>
<StackPanel Gap="4">
<ToggleButton IsChecked="{Binding #flyout.IsOpen, Mode=TwoWay}" Width="150" ClickMode="Press" >Flyout Auto close</ToggleButton>
<ToggleButton IsChecked="{Binding #flyout2.IsOpen, Mode=TwoWay}" Width="150" ClickMode="Press" >Flyout Toggle</ToggleButton>
<Popup Name="flyout" StaysOpen="False" Width="200" Height="100" >
<Border Background="LightGray" BorderBrush="Gray" BorderThickness="1" Padding="6" >
<StackPanel>
<DropDown>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item A</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item B</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item C</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item D</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item E</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item F</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item G</DropDownItem>
</DropDown>
<ToggleButton IsChecked="{Binding #nestedflyout.IsOpen, Mode=TwoWay}" Width="150" ClickMode="Press" >Nested Auto close</ToggleButton>
<Popup Name="nestedflyout" StaysOpen="False" Width="200" Height="200" >
<Border Background="LightGray" BorderBrush="Gray" BorderThickness="1" Padding="6" >
<StackPanel>
<TextBox>Nested Content</TextBox>
</StackPanel>
</Border>
</Popup>
</StackPanel>
</Border>
</Popup>
<Popup Name="flyout2" StaysOpen="True" Width="200" Height="100" >
<Border Background="LightGray" BorderBrush="Gray" BorderThickness="1" Padding="6" >
<StackPanel>
<DropDown>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item A</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item B</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item C</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item D</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item E</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item F</DropDownItem>
<DropDownItem ToolTip.Tip="Yet another nested popup">Item G</DropDownItem>
</DropDown>
</StackPanel>
</Border>
</Popup>
</StackPanel>
</StackPanel>
</UserControl>

19
samples/ControlCatalog/Pages/PopupPage.xaml.cs

@ -0,0 +1,19 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace ControlCatalog.Pages
{
public class PopupPage : UserControl
{
public PopupPage()
{
this.InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}

47
src/Avalonia.Controls/Primitives/Popup.cs

@ -215,7 +215,17 @@ namespace Avalonia.Controls.Primitives
{
var window = _topLevel as Window;
if (window != null)
window.Deactivated += WindowDeactivated;
{
window.Deactivated += WindowDeactivated;
}
else
{
var parentPopuproot = _topLevel as PopupRoot;
if(parentPopuproot != null && parentPopuproot.Parent!=null)
{
((Popup)(parentPopuproot.Parent)).Closed += ParentClosed;
}
}
_topLevel.AddHandler(PointerPressedEvent, PointerPressedOutside, RoutingStrategies.Tunnel);
_nonClientListener = InputManager.Instance.Process.Subscribe(ListenForNonClientClick);
}
@ -230,7 +240,7 @@ namespace Avalonia.Controls.Primitives
Opened?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Closes the popup.
/// </summary>
@ -244,6 +254,14 @@ namespace Avalonia.Controls.Primitives
var window = _topLevel as Window;
if (window != null)
window.Deactivated -= WindowDeactivated;
else
{
var parentPopuproot = _topLevel as PopupRoot;
if (parentPopuproot != null && parentPopuproot.Parent != null)
{
((Popup)parentPopuproot.Parent).Closed -= ParentClosed;
}
}
_nonClientListener?.Dispose();
_nonClientListener = null;
}
@ -381,16 +399,25 @@ namespace Avalonia.Controls.Primitives
{
if (!StaysOpen)
{
var root = ((IVisual)e.Source).GetVisualRoot();
if (root != this.PopupRoot)
{
if(!IsChildOrThis((IVisual)e.Source))
{
Close();
e.Handled = true;
}
}
}
private bool IsChildOrThis(IVisual child)
{
IVisual root = child.GetVisualRoot();
while (root is PopupRoot)
{
if (root == PopupRoot) return true;
root = ((PopupRoot)root).Parent.GetVisualRoot();
}
return false;
}
private void WindowDeactivated(object sender, EventArgs e)
{
if (!StaysOpen)
@ -398,5 +425,13 @@ namespace Avalonia.Controls.Primitives
Close();
}
}
private void ParentClosed(object sender, EventArgs e)
{
if (!StaysOpen)
{
Close();
}
}
}
}
Loading…
Cancel
Save