Files
Wino-Mail/Wino.Core.UWP/Extensions/UtilExtensions.cs

108 lines
3.5 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Media;
2025-02-16 11:35:43 +01:00
namespace Wino.Extensions;
public static class UtilExtensions
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:35:43 +01:00
public static float ToFloat(this double value) => (float)value;
public static List<FrameworkElement> Children(this DependencyObject parent)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:35:43 +01:00
var list = new List<FrameworkElement>();
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:35:43 +01:00
var child = VisualTreeHelper.GetChild(parent, i);
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
if (child is FrameworkElement)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:35:43 +01:00
list.Add(child as FrameworkElement);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:35:43 +01:00
list.AddRange(Children(child));
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:35:43 +01:00
return list;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
public static T GetChildByName<T>(this DependencyObject parent, string name)
{
var childControls = Children(parent);
var controls = childControls.OfType<FrameworkElement>();
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
if (controls == null)
{
return default(T);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:35:43 +01:00
var control = controls
.Where(x => x.Name.Equals(name))
.Cast<T>()
.First();
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
return control;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
public static Visual Visual(this UIElement element) =>
ElementCompositionPreview.GetElementVisual(element);
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
public static void SetChildVisual(this UIElement element, Visual childVisual) =>
ElementCompositionPreview.SetElementChildVisual(element, childVisual);
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
public static Point RelativePosition(this UIElement element, UIElement other) =>
element.TransformToVisual(other).TransformPoint(new Point(0, 0));
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
public static bool IsFullyVisibile(this FrameworkElement element, FrameworkElement parent)
{
if (element == null || parent == null)
return false;
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
if (element.Visibility != Visibility.Visible)
return false;
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
var elementBounds = element.TransformToVisual(parent).TransformBounds(new Windows.Foundation.Rect(0, 0, element.ActualWidth, element.ActualHeight));
var containerBounds = new Windows.Foundation.Rect(0, 0, parent.ActualWidth, parent.ActualHeight);
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
var originalElementWidth = elementBounds.Width;
var originalElementHeight = elementBounds.Height;
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
elementBounds.Intersect(containerBounds);
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
var newElementWidth = elementBounds.Width;
var newElementHeight = elementBounds.Height;
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
return originalElementWidth.Equals(newElementWidth) && originalElementHeight.Equals(newElementHeight);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
public static void ScrollToElement(this ScrollViewer scrollViewer, FrameworkElement element,
bool isVerticalScrolling = true, bool smoothScrolling = true, float? zoomFactor = null, bool bringToTopOrLeft = true, bool addMargin = true)
{
if (!bringToTopOrLeft && element.IsFullyVisibile(scrollViewer))
return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
var contentArea = (FrameworkElement)scrollViewer.Content;
var position = element.RelativePosition(contentArea);
2024-04-18 01:44:37 +02:00
2025-02-16 11:35:43 +01:00
if (isVerticalScrolling)
{
// Accomodate for additional header.
scrollViewer.ChangeView(null, Math.Max(0, position.Y - (addMargin ? 48 : 0)), zoomFactor, !smoothScrolling);
}
else
{
scrollViewer.ChangeView(position.X, null, zoomFactor, !smoothScrolling);
}
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:35:43 +01:00
public static T[] GetValues<T>() where T : struct, Enum => Enum.GetValues<T>();
2024-04-18 01:44:37 +02:00
}