Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions src/UraniumUI.Material/Converters/UniversalGridLengthConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Comment on lines +2 to +6
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several unused imports should be removed: System.Collections.Generic, System.Linq, System.Text, and System.Threading.Tasks are not used anywhere in this file. Only System, System.Globalization are actually needed.

Suggested change
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

Copilot uses AI. Check for mistakes.

namespace UraniumUI.Material.Converters;


Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an unnecessary blank line after the namespace declaration. Looking at other converters in the codebase (e.g., StringIsNotNullOrEmptyConverter.cs:3-4), there should be no blank line between the namespace and class declaration.

Copilot uses AI. Check for mistakes.
public class UniversalGridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

return value switch
{
null => GridLength.Auto,
GridLength gl => gl,

// Строковые форматы
string str => ParseAnyGridLengthFormat(str, culture),

// Числовые типы
double d => new GridLength(d),
int i => new GridLength(i),
float f => new GridLength(f),
decimal dec => new GridLength((double)dec),
Comment on lines +26 to +29
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue with negative values. The converter accepts negative values for GridLength (e.g., new GridLength(-5)), but GridLength typically expects non-negative values. Consider adding validation to reject negative values or document that negative values are intentionally allowed.

Copilot uses AI. Check for mistakes.

// Из массива [value, type]
Comment on lines +22 to +31
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are written in a non-English language (Russian). All code comments should be in English to maintain consistency with the rest of the codebase and ensure all contributors can understand them.

Copilot uses AI. Check for mistakes.
object[] arr when arr.Length == 2 && arr[0] is IConvertible val && arr[1] is GridUnitType type =>
new GridLength(System.Convert.ToDouble(val, culture), type),

_ => GridLength.Auto
};
}


Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an extra blank line between methods. Looking at other converters in the codebase, there should be only one blank line between methods.

Copilot uses AI. Check for mistakes.
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

if (value is not GridLength gridLength)
{
// Возвращаем значение по умолчанию в зависимости от типа
if (targetType == typeof(string))
return "Auto";
if (targetType == typeof(double))
return -1.0;
if (targetType == typeof(GridLength))
return GridLength.Auto;

return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
}

// Преобразуем GridLength в нужный тип
if (targetType == typeof(string))
return GridLengthToString(gridLength, culture);

if (targetType == typeof(double) || targetType == typeof(float) || targetType == typeof(int))
{
// Для числовых типов возвращаем значение только если это Absolute
Comment on lines +46 to +63
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are written in a non-English language (Russian). All code comments should be in English to maintain consistency with the rest of the codebase.

Copilot uses AI. Check for mistakes.
if (gridLength.GridUnitType == GridUnitType.Absolute)
{
return targetType == typeof(int) ? (int)gridLength.Value : gridLength.Value;
}
return targetType == typeof(int) ? -1 : -1.0;
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent return value for non-Absolute GridLength types. When converting back to numeric types, the method returns -1 for non-Absolute types, but GridLength.Star or GridLength.Auto could represent valid layouts. Consider documenting this behavior or throwing a NotSupportedException to make it clear that only Absolute values can be converted back to numeric types.

Suggested change
return targetType == typeof(int) ? -1 : -1.0;
throw new NotSupportedException("Only GridLength values with GridUnitType.Absolute can be converted to numeric types.");

Copilot uses AI. Check for mistakes.
}

if (targetType == typeof(GridLength))
return gridLength;

return gridLength.Value; // По умолчанию double
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ConvertBack method returns gridLength.Value as a fallback at the end, but this may not match the expected targetType. If targetType is a specific type that wasn't handled by the previous conditions, returning a double (gridLength.Value) may cause type mismatch issues. Consider making this more explicit or handling all expected target types.

Suggested change
return gridLength.Value; // По умолчанию double
// Если тип не поддерживается явно, возвращаем значение по умолчанию для targetType
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;

Copilot uses AI. Check for mistakes.
}


Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an extra blank line between methods. Looking at other converters in the codebase, there should be only one blank line between methods.

Copilot uses AI. Check for mistakes.
/*


public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

if (value is not GridLength gridLength)
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;

return targetType.Name switch
{
nameof(Double) or "Double" => gridLength.Value,
nameof(Single) or "Single" => (float)gridLength.Value,
nameof(Int32) or "Int32" => (int)gridLength.Value,
nameof(String) or "String" => GridLengthToString(gridLength, culture),
_ => gridLength.Value // По умолчанию double
};
} */

Comment on lines +78 to +97
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This large block of commented-out code should be removed. If this is an alternative implementation that might be needed in the future, it should either be removed entirely (version control preserves it) or documented with a clear explanation of why it's kept. Keeping dead code reduces maintainability.

Suggested change
/*
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;
if (value is not GridLength gridLength)
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
return targetType.Name switch
{
nameof(Double) or "Double" => gridLength.Value,
nameof(Single) or "Single" => (float)gridLength.Value,
nameof(Int32) or "Int32" => (int)gridLength.Value,
nameof(String) or "String" => GridLengthToString(gridLength, culture),
_ => gridLength.Value // По умолчанию double
};
} */

Copilot uses AI. Check for mistakes.
private GridLength ParseAnyGridLengthFormat(string value, CultureInfo culture)
{
value = value?.Trim() ?? string.Empty;

// Специальные значения
if (value.Equals("Auto", StringComparison.OrdinalIgnoreCase) ||
value.Equals("A", StringComparison.OrdinalIgnoreCase))
return GridLength.Auto;

// Проверяем все возможные форматы
if (TryParseGridLength(value, culture, out var result))
return result;

// Пробуем заменить запятую на точку
string normalized = value.Replace(',', '.');
if (TryParseGridLength(normalized, culture, out result))
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comma-to-dot replacement logic may not work correctly with cultures that use comma as a decimal separator. When you call TryParseGridLength with the normalized string, it still uses the culture parameter which may expect a comma. Consider using CultureInfo.InvariantCulture for the second parse attempt after normalizing to use a dot separator.

Suggested change
if (TryParseGridLength(normalized, culture, out result))
if (TryParseGridLength(normalized, CultureInfo.InvariantCulture, out result))

Copilot uses AI. Check for mistakes.
return result;

return GridLength.Auto;
}

private bool TryParseGridLength(string value, CultureInfo culture, out GridLength result)
{
result = GridLength.Auto;

if (string.IsNullOrWhiteSpace(value))
return false;

// Формат: "100" (пиксели)
if (double.TryParse(value, NumberStyles.Float, culture, out double pixels))
{
result = new GridLength(pixels);
return true;
}

// Формат: "100*" или "*" (звездочки)
if (value.EndsWith("*"))
{
string numberPart = value[..^1].Trim();

if (string.IsNullOrEmpty(numberPart))
{
result = GridLength.Star;
return true;
}

if (double.TryParse(numberPart, NumberStyles.Float, culture, out double stars))
{
result = new GridLength(stars, GridUnitType.Star);
return true;
}
}

// Формат: "100px"
if (value.EndsWith("px", StringComparison.OrdinalIgnoreCase))
{
string numberPart = value[..^2].Trim();
if (double.TryParse(numberPart, NumberStyles.Float, culture, out double px))
{
result = new GridLength(px);
return true;
}
}

return false;
}

private string GridLengthToString(GridLength gridLength, CultureInfo culture)
{
return gridLength.GridUnitType switch
{
GridUnitType.Absolute => $"{gridLength.Value.ToString(culture)}",
GridUnitType.Star => gridLength.Value == 1 ? "*" : $"{gridLength.Value.ToString(culture)}*",
GridUnitType.Auto => "Auto",
_ => gridLength.Value.ToString(culture)
};
}
}
Loading