123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- using InABox.Core;
- using InABox.Wpf;
- using Image = System.Windows.Controls.Image;
- using System.Runtime.InteropServices;
- using System.Windows.Interop;
- namespace InABox.WPF;
- public class FuncTemplateSelector : DataTemplateSelector
- {
- public Func<object, DependencyObject, FrameworkElement?> TemplateFunc { get; set; }
- public FuncTemplateSelector(Func<object, DependencyObject, FrameworkElement?> templateFunc)
- {
- TemplateFunc = templateFunc;
- }
- public override DataTemplate SelectTemplate(object item, DependencyObject container)
- {
- return TemplateGenerator.CreateDataTemplate(() =>
- {
- return TemplateFunc(item, container);
- });
- }
- }
- public static class WPFUtils
- {
-
- public static void MoveToCenter(this Window window)
- {
- if (!GetCursorPos(out POINT cursorPoint))
- return;
- IntPtr mainWindow = MonitorFromPoint(new POINT() { x = 1, y = 1 }, MONITOR_DEFAULTTO.MONITOR_DEFAULTTONULL);
- IntPtr monitorHandle = MonitorFromPoint(cursorPoint, MONITOR_DEFAULTTO.MONITOR_DEFAULTTONULL);
- MONITORINFO monitorInfo = new() { cbSize = (uint)Marshal.SizeOf<MONITORINFO>() };
- if (!GetMonitorInfo(monitorHandle, ref monitorInfo))
- return;
- IntPtr windowHandle = new WindowInteropHelper(window).EnsureHandle();
- if (!GetWindowPlacement(windowHandle, out WINDOWPLACEMENT windowPlacement))
- return;
- int left = monitorInfo.rcWork.left + Math.Max(0, (int)((monitorInfo.rcWork.Width - windowPlacement.rcNormalPosition.Width) / 2D));
- int top = monitorInfo.rcWork.top + Math.Max(0, (int)((monitorInfo.rcWork.Height - windowPlacement.rcNormalPosition.Height) / 2D));
- windowPlacement.rcNormalPosition = new RECT(left, top, windowPlacement.rcNormalPosition.Width, windowPlacement.rcNormalPosition.Height);
- SetWindowPlacement(windowHandle, ref windowPlacement);
- }
- [DllImport("User32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool GetCursorPos(out POINT lpPoint);
- [DllImport("User32.dll")]
- private static extern IntPtr MonitorFromPoint(POINT pt, MONITOR_DEFAULTTO dwFlags);
- private enum MONITOR_DEFAULTTO : uint
- {
- MONITOR_DEFAULTTONULL = 0x00000000,
- MONITOR_DEFAULTTOPRIMARY = 0x00000001,
- MONITOR_DEFAULTTONEAREST = 0x00000002,
- }
- [DllImport("User32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
- [StructLayout(LayoutKind.Sequential)]
- private struct MONITORINFO
- {
- public uint cbSize;
- public RECT rcMonitor;
- public RECT rcWork;
- public uint dwFlags;
- }
- [DllImport("User32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
- [DllImport("User32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
- [StructLayout(LayoutKind.Sequential)]
- private struct WINDOWPLACEMENT
- {
- public uint length;
- public uint flags;
- public uint showCmd;
- public POINT ptMinPosition;
- public POINT ptMaxPosition;
- public RECT rcNormalPosition;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct POINT
- {
- public int x;
- public int y;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct RECT
- {
- public int left;
- public int top;
- public int right;
- public int bottom;
- public int Width => right - left;
- public int Height => bottom - top;
- public RECT(int x, int y, int width, int height)
- {
- left = x;
- top = y;
- right = x + width;
- bottom = y + height;
- }
- }
- #region Setters/Triggers etc
-
- public static Style AddSetter(this Style style, DependencyProperty property, object value)
- {
- style.Setters.Add(new Setter(property, value));
- return style;
- }
- public static DataTrigger AddSetter(this DataTrigger trigger, DependencyProperty property, object value)
- {
- trigger.Setters.Add(new Setter(property, value));
- return trigger;
- }
- public static DataTrigger AddDataTrigger(this Style style)
- {
- var trigger = new DataTrigger();
- style.Triggers.Add(trigger);
- return trigger;
- }
- public static DataTrigger AddDataTrigger(this Style style, Binding binding, object value)
- {
- var trigger = new DataTrigger() { Binding = binding, Value = value };
- style.Triggers.Add(trigger);
- return trigger;
- }
-
- #endregion
-
- public static SolidColorBrush ToBrush(this System.Windows.Media.Color color, double opacity = 1.0)
- {
- return new SolidColorBrush(color) { Opacity = opacity };
- }
-
- #region Multi-Binding
- public static MultiBinding CreateMultiBinding(IMultiValueConverter? converter = null, string? format = null, object? parameter = null)
- {
- return new MultiBinding
- {
- Converter = converter,
- ConverterParameter = parameter,
- StringFormat = format
- };
- }
- public static MultiBinding AddBinding(this MultiBinding multi, BindingBase binding)
- {
- multi.Bindings.Add(binding);
- return multi;
- }
- #endregion
- #region Binding
- public static Binding CreateBinding<T, TProperty>(
- T source,
- Expression<Func<T, TProperty>> expression,
- IValueConverter? converter = null,
- string? format = null)
- {
- return new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
- {
- Source = source,
- Converter = converter,
- StringFormat = format
- };
- }
- public static Binding CreateBinding<T, TProperty>(
- Expression<Func<T, TProperty>> expression,
- IValueConverter? converter = null,
- BindingMode mode = BindingMode.Default,
- string? format = null,
- RelativeSource? relativeSource = null)
- {
- return new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
- {
- Converter = converter,
- StringFormat = format,
- Mode = mode,
- RelativeSource = relativeSource
- };
- }
- public static DataTrigger Bind<T, TProperty, TValue>(
- this DataTrigger trigger,
- T source,
- Expression<Func<T, TProperty>> expression,
- TValue value,
- IValueConverter<TProperty, TValue>? converter,
- string? format = null)
- {
- trigger.Binding = CreateBinding(source, expression, converter, format);
- trigger.Value = value;
- return trigger;
- }
- public static DataTrigger Bind<T, TProperty, TValue>(
- this DataTrigger trigger,
- Expression<Func<T, TProperty>> expression,
- TValue value,
- IValueConverter<TProperty, TValue>? converter,
- BindingMode mode = BindingMode.Default,
- string? format = null,
- RelativeSource? relativeSource = null)
- {
- trigger.Binding = CreateBinding(expression, converter, mode, format, relativeSource: relativeSource);
- trigger.Value = value;
- return trigger;
- }
- public static DataTrigger Bind<T, TProperty>(
- this DataTrigger trigger,
- T source,
- Expression<Func<T, TProperty>> expression,
- TProperty value,
- IValueConverter? converter = null,
- string? format = null)
- {
- trigger.Binding = CreateBinding(source, expression, converter, format);
- trigger.Value = value;
- return trigger;
- }
- public static DataTrigger Bind<T, TProperty>(
- this DataTrigger trigger,
- Expression<Func<T, TProperty>> expression,
- TProperty value,
- IValueConverter? converter = null,
- BindingMode mode = BindingMode.Default,
- string? format = null,
- RelativeSource? relativeSource = null)
- {
- trigger.Binding = CreateBinding(expression, converter, mode, format, relativeSource: relativeSource);
- trigger.Value = value;
- return trigger;
- }
- public static void Bind<T, TProperty>(
- this FrameworkElement element,
- DependencyProperty property,
- T source,
- Expression<Func<T, TProperty>> expression,
- IValueConverter? converter = null,
- string? format = null)
- {
- element.SetBinding(
- property,
- CreateBinding(source, expression, converter, format)
- );
- }
- public static void Bind<T, TProperty>(
- this FrameworkElement element,
- DependencyProperty property,
- Expression<Func<T, TProperty>> expression,
- IValueConverter? converter = null,
- BindingMode mode = BindingMode.Default,
- string? format = null,
- RelativeSource? relativeSource = null)
- {
- element.SetBinding(
- property,
- CreateBinding(expression, converter, mode, format, relativeSource: relativeSource)
- );
- }
- public static FrameworkElementFactory Bind<T, TProperty>(
- this FrameworkElementFactory element,
- DependencyProperty property,
- T source,
- Expression<Func<T, TProperty>> expression,
- IValueConverter? converter = null,
- string? format = null)
- {
- element.SetBinding(
- property,
- CreateBinding(source, expression, converter, format)
- );
- return element;
- }
- public static FrameworkElementFactory Bind<T, TProperty>(
- this FrameworkElementFactory element,
- DependencyProperty property,
- Expression<Func<T, TProperty>> expression,
- IValueConverter? converter = null,
- BindingMode mode = BindingMode.Default,
- string? format = null,
- RelativeSource? relativeSource = null)
- {
- element.SetBinding(
- property,
- CreateBinding(expression, converter, mode, format, relativeSource: relativeSource ?? RelativeSource.TemplatedParent)
- );
- return element;
- }
- #endregion
- #region Converters
- public static IMultiValueConverter WrapConverter(this IValueConverter converter, Func<object?[], object?> convert, Func<object?, object?[]?>? convertBack = null)
- {
- return new MultiFuncConverter(convert, convertBack, converter);
- }
- #endregion
- public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
- where T : DependencyObject
- {
- DependencyObject? parent = dependencyObject;
- do
- {
- parent = LogicalTreeHelper.GetParent(parent);
- } while(parent != null && parent is not T);
- return parent as T;
- }
- public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj, bool recursive = true)
- {
- if (depObj != null)
- for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
- {
- var child = VisualTreeHelper.GetChild(depObj, i);
- if (child is null)
- continue;
- if (child is T t)
- yield return t;
- if (recursive)
- {
- foreach (var childOfChild in FindVisualChildren<T>(child))
- yield return childOfChild;
- }
- }
- }
- public static T? FindChild<T>(this DependencyObject parent, string childName)
- where T : FrameworkElement
- {
- return parent.FindVisualChildren<T>().FirstOrDefault(x => x.Name == childName);
- }
- #region Grid Children
- public static int GetRow(this Grid grid, DependencyObject dependencyObject)
- {
- while (true)
- {
- var parent = LogicalTreeHelper.GetParent(dependencyObject);
- if (parent == null)
- return -1;
- if (parent == grid)
- return Grid.GetRow(dependencyObject as UIElement);
- dependencyObject = parent;
- }
- }
- public static int GetRowSpan(this Grid grid, DependencyObject dependencyObject)
- {
- while (true)
- {
- var parent = LogicalTreeHelper.GetParent(dependencyObject);
- if (parent == null)
- return -1;
- if (parent == grid)
- return Grid.GetRowSpan(dependencyObject as UIElement);
- dependencyObject = parent;
- }
- }
-
- public static int GetColumn(this Grid grid, DependencyObject dependencyObject)
- {
- while (true)
- {
- var parent = LogicalTreeHelper.GetParent(dependencyObject);
- if (parent == null)
- return -1;
- if (parent == grid)
- return Grid.GetColumn(dependencyObject as UIElement);
- dependencyObject = parent;
- }
- }
- public static int GetColumnSpan(this Grid grid, DependencyObject dependencyObject)
- {
- while (true)
- {
- var parent = LogicalTreeHelper.GetParent(dependencyObject);
- if (parent == null)
- return -1;
- if (parent == grid)
- return Grid.GetColumnSpan(dependencyObject as UIElement);
- dependencyObject = parent;
- }
- }
-
- public static void SetGridPosition(this FrameworkElement element, int row, int column, int rowspan = 1, int colspan = 1)
- {
- element.SetValue(Grid.ColumnProperty, column);
- element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
- element.SetValue(Grid.RowProperty, row);
- element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
- }
- public static Grid AddChild(this Grid grid, FrameworkElement element, int row, int column, int rowSpan = 1, int colSpan = 1)
- {
- element.SetGridPosition(row, column, rowSpan, colSpan);
- grid.Children.Add(element);
- return grid;
- }
- #endregion
- #region Grid Columns + Rows
- public static ColumnDefinition AddColumn(this Grid grid, GridUnitType type, double value = 1)
- {
- var colDef = new ColumnDefinition { Width = new GridLength(value, type) };
- grid.ColumnDefinitions.Add(colDef);
- return colDef;
- }
- public static ColumnDefinition AddColumn(this Grid grid, double value)
- {
- var colDef = new ColumnDefinition { Width = new GridLength(value) };
- grid.ColumnDefinitions.Add(colDef);
- return colDef;
- }
- public static RowDefinition AddRow(this Grid grid, GridUnitType type, double value = 1)
- {
- var rowDef = new RowDefinition { Height = new GridLength(value, type) };
- grid.RowDefinitions.Add(rowDef);
- return rowDef;
- }
- public static RowDefinition AddRow(this Grid grid, double value)
- {
- var rowDef = new RowDefinition { Height = new GridLength(value) };
- grid.RowDefinitions.Add(rowDef);
- return rowDef;
- }
- #endregion
- #region Menu Utils
- private static void ItemsControlInsert(ItemsControl menu, FrameworkElement item, int index)
- {
- if (index != -1)
- {
- menu.Items.Insert(index, item);
- }
- else
- {
- menu.Items.Add(item);
- }
- }
- private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, bool enabled, int index = -1)
- {
- var item = new MenuItem { Header = caption, IsEnabled = enabled };
- if (image != null)
- item.Icon = new Image() { Source = enabled ? image.AsBitmapImage(24, 24) : image.AsGrayScale().AsBitmapImage(24, 24) };
- ItemsControlInsert(menu, item, index);
- return item;
- }
- private static MenuItem DoAddMenuItem(ItemsControl menu, string caption, Bitmap? image, Action? click, bool enabled, int index = -1)
- {
- var item = DoAddMenuItem(menu, caption, image, enabled, index);
- if (click != null)
- {
- item.Click += (o, e) =>
- {
- click();
- };
- }
- return item;
- }
- private static MenuItem DoAddMenuItem<T>(ItemsControl menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled, int index = -1)
- {
- var item = DoAddMenuItem(menu, caption, image, enabled, index);
- item.Tag = tag;
- item.Click += (o, e) =>
- {
- click((T)(o as MenuItem)!.Tag);
- };
- return item;
- }
- public delegate void CheckToggleAction(bool isChecked);
- public delegate void CheckToggleAction<T>(T tag, bool isChecked);
- private static MenuItem DoAddCheckItem(ItemsControl menu, string caption, CheckToggleAction click, bool isChecked, bool enabled, int index = -1)
- {
- var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
- item.Click += (o, e) =>
- {
- click(item.IsChecked);
- };
- ItemsControlInsert(menu, item, index);
- return item;
- }
- private static MenuItem DoAddCheckItem<T>(ItemsControl menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked, bool enabled, int index = -1)
- {
- var item = new MenuItem { Header = caption, IsEnabled = enabled, IsCheckable = true, IsChecked = isChecked };
- item.Tag = tag;
- item.Click += (o, e) =>
- {
- click((T)(o as MenuItem)!.Tag, item.IsChecked);
- };
- ItemsControlInsert(menu, item, index);
- return item;
- }
- private static Separator DoAddSeparator(ItemsControl menu, int index)
- {
- var separator = new Separator();
- ItemsControlInsert(menu, separator, index);
- return separator;
- }
- private static Separator? DoAddSeparatorIfNeeded(ItemsControl menu, int index)
- {
- if (menu.Items.Count == 0) return null;
- var lastIndex = index != -1 ? index - 1 : menu.Items.Count - 1;
- if (lastIndex < 0 || lastIndex >= menu.Items.Count) return null;
- var last = menu.Items[lastIndex];
- if (last is Separator) return null;
- var separator = new Separator();
- ItemsControlInsert(menu, separator, index);
- return separator;
- }
- private static void DoRemoveUnnecessarySeparators(ItemsControl menu)
- {
- while(menu.Items.Count > 0 && menu.Items[0] is Separator)
- {
- menu.Items.RemoveAt(0);
- }
- while(menu.Items.Count > 0 && menu.Items[^1] is Separator)
- {
- menu.Items.RemoveAt(menu.Items.Count - 1);
- }
- }
- public static Separator AddSeparator(this ContextMenu menu, int index = -1) => DoAddSeparator(menu, index);
- public static Separator AddSeparator(this MenuItem menu, int index = -1) => DoAddSeparator(menu, index);
- public static Separator? AddSeparatorIfNeeded(this ContextMenu menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
- public static Separator? AddSeparatorIfNeeded(this MenuItem menu, int index = -1) => DoAddSeparatorIfNeeded(menu, index);
- public static void RemoveUnnecessarySeparators(this ContextMenu menu) => DoRemoveUnnecessarySeparators(menu);
- public static void RemoveUnnecessarySeparators(this MenuItem menu) => DoRemoveUnnecessarySeparators(menu);
- public static MenuItem AddItem(this ContextMenu menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
- => DoAddMenuItem(menu, caption, image, click, enabled, index);
- public static MenuItem AddItem(this MenuItem menu, string caption, Bitmap? image, Action? click, bool enabled = true, int index = -1)
- => DoAddMenuItem(menu, caption, image, click, enabled, index);
- public static MenuItem AddItem<T>(this ContextMenu menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
- => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
- public static MenuItem AddItem<T>(this MenuItem menu, string caption, Bitmap? image, T tag, Action<T> click, bool enabled = true, int index = -1)
- => DoAddMenuItem(menu, caption, image, tag, click, enabled, index);
- public static MenuItem AddCheckItem(this ContextMenu menu, string caption, CheckToggleAction click, bool isChecked = false, bool enabled = true, int index = -1)
- => DoAddCheckItem(menu, caption, click, isChecked, enabled, index);
- public static MenuItem AddCheckItem<T>(this ContextMenu menu, string caption, T tag, CheckToggleAction<T> click, bool isChecked = false, bool enabled = true, int index = -1)
- => DoAddCheckItem(menu, caption, tag, click, isChecked, enabled, index);
- #endregion
- }
|