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

Fix TopMost handling for Popups on Windows #17841

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions samples/ControlCatalog/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
<TabItem Header="Pointers">
<pages:PointersPage />
</TabItem>
<TabItem Header="Popups">
<pages:PopupsPage />
</TabItem>
<TabItem Header="ProgressBar">
<pages:ProgressBarPage />
</TabItem>
Expand Down
18 changes: 18 additions & 0 deletions samples/ControlCatalog/Pages/PopupsPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<UserControl x:Class="ControlCatalog.Pages.PopupsPage"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<StackPanel Orientation="Vertical" Spacing="10">

<TextBlock Text="'Light Dismiss' Popup" />
<Button Content="Show Popup" Click="ButtonLightDismiss_OnClick" />

<TextBlock Text="Popup that stays open" />
<Button Content="Show Popup" Click="ButtonPopupStaysOpen_OnClick" />

<TextBlock Text="TopMost popup that stays open" />
<Button Content="Show Popup" Click="ButtonTopMostPopupStaysOpen" />
Comment on lines +7 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These testing buttons can be moved to the IntegrationTestApp. I can try to add integration test on top of them later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use some guidance on this one. Not sure what I should do 😳


</StackPanel>

</UserControl>
111 changes: 111 additions & 0 deletions samples/ControlCatalog/Pages/PopupsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Primitives.PopupPositioning;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media;

namespace ControlCatalog.Pages;

public class PopupsPage : UserControl
{
private Popup? _popup;
private Popup? _topMostPopup;
public PopupsPage()
{
InitializeComponent();

}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

private void ButtonLightDismiss_OnClick(object sender, RoutedEventArgs e)
{
new Popup
{
Placement = PlacementMode.Bottom,
PlacementTarget = sender as Button,
Child = new Panel { Margin = new Thickness(10) ,Children = { new TextBlock { Text = "Popup Content" } }},
IsLightDismissEnabled = true,
IsOpen = true
};
}

private void ButtonPopupStaysOpen_OnClick(object sender, RoutedEventArgs e)
{
if (_popup is not null)
return;

var closeButton = new Button { Content = "Click to Close" };
closeButton.Click += (o, args) =>
{
if (_popup is null)
return;

LogicalChildren.Remove(_popup);
_popup.IsOpen = false;
_popup = null;
};
_popup = new Popup
{
Placement = PlacementMode.Bottom,
PlacementTarget = sender as Button,
Child = new StackPanel
{
Spacing = 10,
Margin = new Thickness(10),
Orientation = Orientation.Vertical,
Children =
{
new TextBox
{
Width = 100,
Text = "Some text",
},
new TextBox
{
Width = 100,
Text = "Some other text",
},
closeButton
}
},
IsLightDismissEnabled = false,
};

LogicalChildren.Add(_popup);
_popup.IsOpen = true;
}

private void ButtonTopMostPopupStaysOpen(object sender, RoutedEventArgs e)
{
if (_topMostPopup is not null)
return;

var closeButton = new Button { Content = "Click to Close" };
closeButton.Click += (o, args) =>
{
if (_topMostPopup is null)
return;
_topMostPopup.IsOpen = false;
_topMostPopup = null;
};
_topMostPopup = new Popup
{
Placement = PlacementMode.Bottom,
PlacementTarget = sender as Button,
Child = new Panel
{
Margin = new Thickness(10), Children = { closeButton }
},
IsLightDismissEnabled = false,
Topmost = true,
IsOpen = true
};
}
}
5 changes: 5 additions & 0 deletions src/Avalonia.Controls/Primitives/PopupRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public PopupRoot(TopLevel parent, IPopupImpl impl, IAvaloniaDependencyResolver?
{
ParentTopLevel = parent;
impl.SetWindowManagerAddShadowHint(WindowManagerAddShadowHint);
impl.SetTopmost(Topmost);
}

/// <summary>
Expand Down Expand Up @@ -225,6 +226,10 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
{
PlatformImpl?.SetWindowManagerAddShadowHint(change.GetNewValue<bool>());
}
else if (change.Property == TopmostProperty)
{
PlatformImpl?.SetTopmost(Topmost);
}
}
}
}
7 changes: 5 additions & 2 deletions src/Windows/Avalonia.Win32/PopupImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ protected override IntPtr CreateWindowOverride(ushort atom)
UnmanagedMethods.WindowStyles.WS_CLIPCHILDREN;

UnmanagedMethods.WindowStyles exStyle =
UnmanagedMethods.WindowStyles.WS_EX_TOOLWINDOW |
UnmanagedMethods.WindowStyles.WS_EX_TOPMOST;
UnmanagedMethods.WindowStyles.WS_EX_TOOLWINDOW;

var result = UnmanagedMethods.CreateWindowEx(
(int)exStyle,
Expand Down Expand Up @@ -84,6 +83,10 @@ protected override IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr l
_maxAutoSize = null;
goto default;
case UnmanagedMethods.WindowsMessage.WM_MOUSEACTIVATE:
if (_parent?.Handle is not null)
{
UnmanagedMethods.SetFocus(_parent.Handle.Handle);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxkatz6 found an issue with focus management. I'm not sure if this is the correct way to handle it but if I don't set the focus to the parent when the popup is clicked, I'm not able to type in the popup if there's a textbox, for example.

}
return (IntPtr)UnmanagedMethods.MouseActivate.MA_NOACTIVATE;
default:
return base.WndProc(hWnd, msg, wParam, lParam);
Expand Down
Loading