122 lines
4.4 KiB
C#
122 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Diagnostics;
|
|
using System.Windows.Controls.Primitives;
|
|
|
|
namespace Elwig.Helpers {
|
|
public static class Utils {
|
|
public static void SetInputChanged(Control input) {
|
|
var brush = Brushes.Orange;
|
|
if (input is ComboBox cb) {
|
|
var border = GetComboBoxBorder(cb);
|
|
if (border != null)
|
|
border.BorderBrush = brush;
|
|
} else {
|
|
input.BorderBrush = brush;
|
|
}
|
|
}
|
|
|
|
public static void SetInputInvalid(Control input) {
|
|
var brush = Brushes.Red;
|
|
if (input is ComboBox cb) {
|
|
var border = GetComboBoxBorder(cb);
|
|
if (border != null)
|
|
border.BorderBrush = brush;
|
|
} else {
|
|
input.BorderBrush = brush;
|
|
}
|
|
}
|
|
|
|
public static void ClearInputState(Control input) {
|
|
if (input is ComboBox cb) {
|
|
GetComboBoxBorder(cb)?.ClearValue(Border.BorderBrushProperty);
|
|
} else {
|
|
input.ClearValue(Control.BorderBrushProperty);
|
|
}
|
|
}
|
|
|
|
private static Border? GetComboBoxBorder(ComboBox cb) {
|
|
var toggleButton = cb.Template.FindName("toggleButton", cb) as ToggleButton;
|
|
return toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
|
|
}
|
|
|
|
public static IEnumerable<T> FindAllChildren<T>(DependencyObject depObj) where T : DependencyObject {
|
|
if (depObj == null)
|
|
yield return (T)Enumerable.Empty<T>();
|
|
foreach (var child in LogicalTreeHelper.GetChildren(depObj)) {
|
|
if (child == null) {
|
|
continue;
|
|
} else if (child is T t) {
|
|
yield return t;
|
|
} else if (child is DependencyObject childDepOpj) {
|
|
foreach (T childOfChild in FindAllChildren<T>(childDepOpj)) {
|
|
yield return childOfChild;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<T> FindAllChildren<T>(DependencyObject depObj, IEnumerable<DependencyObject> exempt) where T : DependencyObject {
|
|
return FindAllChildren<T>(depObj).Where(c => !exempt.Contains(c));
|
|
}
|
|
|
|
public static T? FindNextSibling<T>(Control me) where T : DependencyObject {
|
|
var found = false;
|
|
foreach (var child in LogicalTreeHelper.GetChildren(me.Parent)) {
|
|
if (found && child is T c) {
|
|
return c;
|
|
} else if (child == me) {
|
|
found = true;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static int Modulo(string a, int b) {
|
|
if (!a.All(char.IsDigit))
|
|
throw new ArgumentException("First argument has to be a decimal string");
|
|
return a.Select(ch => ch - '0').Aggregate((sum, n) => (sum * 10 + n) % b);
|
|
}
|
|
|
|
public static void RunBackground(string title, Func<Task> a) {
|
|
Task.Run(async () => {
|
|
try {
|
|
await a();
|
|
} catch (Exception e) {
|
|
MessageBox.Show(e.ToString(), title, MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void MailTo(string emailAddress) {
|
|
Process.Start(new ProcessStartInfo() {
|
|
FileName = $"mailto:{emailAddress}",
|
|
UseShellExecute = true,
|
|
});
|
|
}
|
|
|
|
public static double KmwToOe(double kmw) {
|
|
return Math.Round(kmw * (4.54 + 0.022 * kmw), 0);
|
|
}
|
|
|
|
public static double OeToKmw(double oe) {
|
|
return Math.Round((-4.54 + Math.Sqrt(4.54 * 4.54 - 4 * 0.022 * -oe)) / 2 * 0.022, 1);
|
|
}
|
|
|
|
public static decimal DecFromDb(long value, byte precision) {
|
|
bool neg = value < 0;
|
|
if (neg) value = -value;
|
|
return new decimal((int)(value & 0xFFFFFFFF), (int)((value >> 32) & 0x7FFFFFFF), 0, neg, precision);
|
|
}
|
|
|
|
public static long DecToDb(decimal value, byte precision) {
|
|
return (long)decimal.Round(value * precision, 0);
|
|
}
|
|
}
|
|
}
|