2022-10-23 11:02:31 +13:00
|
|
|
|
// <copyright file="WPFExtensions.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
//
|
2023-05-20 06:43:19 +12:00
|
|
|
|
// Copyright (c) 2022-2023 Peter Kirmeier
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
|
|
|
|
{
|
2022-11-30 10:48:45 +13:00
|
|
|
|
using System;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Media;
|
2022-12-03 14:14:15 +13:00
|
|
|
|
using System.Windows.Threading;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal static class WPFExtensions
|
|
|
|
|
{
|
2022-12-03 14:14:15 +13:00
|
|
|
|
internal static void HandleInvoke(this DispatcherObject instance, Action action)
|
|
|
|
|
{
|
|
|
|
|
if (instance!.CheckAccess())
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
instance.Dispatcher.Invoke(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
internal static T? FindVisualChildOfType<T>(this DependencyObject depObj, int index = 0)
|
|
|
|
|
where T : DependencyObject
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
|
|
|
|
|
{
|
|
|
|
|
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
|
|
|
|
|
if (child != null)
|
|
|
|
|
{
|
2023-04-30 10:06:00 +12:00
|
|
|
|
if (child is T validChild)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
|
|
|
|
if (index-- == 0)
|
|
|
|
|
{
|
2023-04-30 10:06:00 +12:00
|
|
|
|
return validChild;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T? childItem = child.FindVisualChildOfType<T>(index);
|
|
|
|
|
if (childItem != null)
|
|
|
|
|
{
|
|
|
|
|
return childItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-03 07:34:24 +13:00
|
|
|
|
internal static Point GetRelativeChildPositionTo(this Visual parent, Visual? child)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2022-12-03 07:34:24 +13:00
|
|
|
|
return child == null ? new() : child.TransformToAncestor(parent).Transform(new ());
|
2022-11-14 07:58:54 +13:00
|
|
|
|
}
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
}
|