PopupWindowAction locks up when invoked on minimized window
Steps to reproduce: In InteractivityQuickstart sample, in InteractionRequestViewModel.cs, add namespace System.Threading.Tasks and modify RaiseNotification method the following way:
private async void RaiseNotification()
{
await Task.Delay(5000);
this.NotificationRequest.Raise(
new Notification { Content = "Notification Message", Title = "Notification" },
n => { InteractionResultMessage = "The user was notified."; });
}
Then run the sample. After pressing Raise Default Notification button, the PopupWindowAction dialog pops up after 5 seconds delay. If the program window is minimized after pressing the button in less than 5 seconds, the program locks up.
This seems to happen because of the logic that is invoked when setting CenterOverAssociatedObject="True". When setting this property, the PopupWindowAction hooks into the Window's SizeChanged event. When the window is minimized the position of the AssociatedObject is {-31997,-31922}, which is obviously way off screen. So when the position of the popup is calculated, it is being positioned way off screen in order to be centered around the hidden window. So, technically the reason it appears to be locked is because it's a modal window that you can't activate because it is way off your monitor.
I'm not sure how I will fix this. Just because the position of the parent window is less than 0 doesn't mean it is off screen. It could mean that it is simply on a different monitor, and depending on the DPI of the monitor, the negative values can be very large.
In the meantime you should use the WindowStartupLocation property instead. Since, this scenario doesn't require the popup to be centered around a single element in a portion of the view.
One can look into Window.WindowState to determine if the window is in normal state and if not, use Window.RestoreBounds for the "true" position of the window.
Well, the AssociateObject isn't always a Window, so we might have to look for the AssociatedObject's Window and then check the state. I'll play around with the logic and see what I can come up with.
In my implementation of PopupWindowAction (created for Prism 4.1 when it did not contain such an implementation) I used the following code:
sizeHandler =
(o, e) =>
{
wrapperWindow.SizeChanged -= sizeHandler;
FrameworkElement invoker = this.AssociatedObject;
Window parent = Window.GetWindow(invoker);
bool normal = parent.WindowState == WindowState.Normal;
double positionX = normal ? parent.Left : parent.RestoreBounds.Left;
double positionY = normal ? parent.Top : parent.RestoreBounds.Top;
wrapperWindow.Top = positionY + ((invoker.ActualHeight - wrapperWindow.ActualHeight) / 2);
wrapperWindow.Left = positionX + ((invoker.ActualWidth - wrapperWindow.ActualWidth) / 2);
};
But this seems not quite equivalent to current implementation in Prism - it seems there is a difference in parent window bounds (outer bounds vs client area bounds) and also this does not contain latest additions for multiple monitors.
Well, I am thinking that if the parent windows is minimized, then the popup's WindowStartupLocation should be CenterScreen by default.
So maybe something as simple as this would fix it:
var parentWindow = wrapperWindow.Owner;
if (parentWindow != null && parentWindow.WindowState == WindowState.Minimized)
{
wrapperWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
return;
}
What do you think?
I am thinking of the following scenario: the user initiated some long-running task, and when the task is completed, the program notifies the user with a dialog window. Meanwhile, the user minimized the main program window. Possible scenarios are:
- The popup is displayed on desktop, leaving main window minimized (and for this case CenterScreen would be OK).
- Main window is activated and brought to normal state. In this case we would rather center the dialog in the window.
- Main window is left minimized and the dialog is centered in the window (what we have now).
Activation of the main window when it is needed is easily achieved with a trigger in a view model and an appropriate behavior in the view, but this is out of scope for the current PopupWindowAction implementation.
I think given the scope of the PopupWindowAction, my proposed fix will work :)
I agree it is a reasonable workaround. It works well.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.