Skip to content

Conversation

@PatrickAgeev
Copy link
Contributor

GridLength Converter

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new UniversalGridLengthConverter class that provides comprehensive conversion between various types (strings, numbers, arrays) and MAUI's GridLength type, supporting multiple formats including pixel values, star notation, and auto sizing.

Changes:

  • Added a new IValueConverter implementation with bidirectional conversion support for GridLength
  • Supports multiple input formats: strings (with "px", "*", "Auto"), numeric types, and object arrays
  • Includes culture-aware parsing for international number formats

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2 to +6
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.
Comment on lines +22 to +31
// Строковые форматы
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),

// Из массива [value, type]
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.
Comment on lines +46 to +63
// Возвращаем значение по умолчанию в зависимости от типа
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
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.
Comment on lines +78 to +97
/*


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
};
} */

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.

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.

// Пробуем заменить запятую на точку
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.
};
}


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.
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.

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.
{
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant