91 lines
2.9 KiB
C#
91 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.IO;
|
|
|
|
namespace WGneu {
|
|
public static class Utils {
|
|
public static void SetInputChanged(Control input) {
|
|
input.BorderBrush = Brushes.Orange;
|
|
}
|
|
|
|
public static void SetInputInvalid(Control input) {
|
|
input.BorderBrush = Brushes.Red;
|
|
}
|
|
|
|
public static void ClearInputState(Control input) {
|
|
input.ClearValue(Control.BorderBrushProperty);
|
|
}
|
|
|
|
public static IEnumerable<T> FindVisualChilds<T>(DependencyObject depObj) where T : DependencyObject {
|
|
if (depObj == null)
|
|
yield return (T)Enumerable.Empty<T>();
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {
|
|
DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
|
|
if (ithChild == null)
|
|
continue;
|
|
if (ithChild is T t)
|
|
yield return t;
|
|
foreach (T childOfChild in FindVisualChilds<T>(ithChild))
|
|
yield return childOfChild;
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<T> FindVisualChilds<T>(DependencyObject depObj, IEnumerable<DependencyObject> exempt) where T : DependencyObject {
|
|
return FindVisualChilds<T>(depObj).Where(c => !exempt.Contains(c));
|
|
}
|
|
|
|
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 sealed class TemporaryFile : IDisposable {
|
|
private int Usages = 0;
|
|
public string FilePath { get; private set; }
|
|
|
|
public TemporaryFile() : this("") {}
|
|
|
|
public TemporaryFile(string ext) : this(Path.GetTempPath(), ext) {}
|
|
|
|
public TemporaryFile(string dir, string ext) {
|
|
FilePath = Path.Combine(dir, Path.GetRandomFileName() + ext);
|
|
Usages++;
|
|
Create();
|
|
}
|
|
|
|
~TemporaryFile() {
|
|
Delete();
|
|
}
|
|
|
|
public void Dispose() {
|
|
if (--Usages == 0) {
|
|
Delete();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|
|
|
|
public TemporaryFile NewReference() {
|
|
Usages++;
|
|
return this;
|
|
}
|
|
|
|
private void Create() {
|
|
using (File.Create(FilePath)) {};
|
|
}
|
|
|
|
private void Delete() {
|
|
if (FilePath == null) return;
|
|
File.Delete(FilePath);
|
|
FilePath = null;
|
|
}
|
|
}
|
|
}
|
|
}
|