Files
cursor-lang/CursorLang.Settings/Views/Converters.cs
T
alex 55ac8e6556 lightweight variant (#1)
Reviewed-on: #1
Co-authored-by: Aleksandr Neychev <alexnejchev73@gmail.com>
2026-08-12 13:37:30 +00:00

64 lines
2.6 KiB
C#

using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using DrawingColor = System.Drawing.Color;
namespace CursorLang.Settings.Views;
/// <summary>
/// Shows an element when the value matches one of those listed in the parameter,
/// separated by commas. Needed so that the anchor point settings and the screen
/// settings are not shown at the same time.
/// </summary>
public sealed class EnumToVisibilityConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
string? current = value?.ToString();
bool matches = parameter?.ToString()?
.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Any(expected => expected == current) ?? false;
return matches ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
Binding.DoNothing;
}
/// <summary>
/// A colour into a "#RRGGBB" notation: the alpha is not shown, because
/// transparency is a separate setting.
/// </summary>
/// <remarks>
/// The settings hold their colours as <see cref="System.Drawing.Color"/> — the agent
/// reads the same file and must not be made to load WindowsBase for a colour. Turning
/// them into something WPF can paint with is the job of this file and of
/// <see cref="ColorToBrushConverter"/>, and of nothing else.
/// </remarks>
public sealed class ColorToHexConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is DrawingColor color ? $"#{color.R:X2}{color.G:X2}{color.B:X2}" : string.Empty;
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
Binding.DoNothing;
}
/// <summary>
/// A colour into a brush — for the palette swatches in the list.
/// </summary>
public sealed class ColorToBrushConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is DrawingColor color
? new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B))
: Brushes.Transparent;
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
value is SolidColorBrush brush
? DrawingColor.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B)
: Binding.DoNothing;
}