private void ShowTrayPopup(Point cursorPosition)
{
if (IsDisposed) return;
//raise preview event no matter whether popup is currently set
//or not (enables client to set it on demand)
var args = RaisePreviewTrayPopupOpenEvent();
if (args.Handled) return;
if (TrayPopup != null)
{
//use absolute position, but place the popup centered above the icon
TrayPopupResolved.Placement = PlacementMode.AbsolutePoint;
TrayPopupResolved.HorizontalOffset = cursorPosition.X;
TrayPopupResolved.VerticalOffset = cursorPosition.Y;
//open popup
TrayPopupResolved.IsOpen = true;
IntPtr handle = IntPtr.Zero;
if (TrayPopupResolved.Child != null)
{
//try to get a handle on the popup itself (via its child)
HwndSource source = (HwndSource)PresentationSource.FromVisual(TrayPopupResolved.Child);
if (source != null) handle = source.Handle;
}
//if we don't have a handle for the popup, fall back to the message sink
if (handle == IntPtr.Zero) handle = messageSink.MessageWindowHandle;
//activate either popup or message sink to track deactivation.
//otherwise, the popup does not close if the user clicks somewhere else
WinApi.SetForegroundWindow(handle);
//raise attached event - item should never be null unless developers
//changed the CustomPopup directly...
if (TrayPopup != null) RaisePopupOpenedEvent(TrayPopup);
//bubble routed event
RaiseTrayPopupOpenEvent();
}
}