Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/Controls/Foldable/src/TwoPaneView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Maui.Devices;
using Microsoft.Maui.Foldable;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Converters;

namespace Microsoft.Maui.Controls.Foldable
{
Expand Down Expand Up @@ -152,7 +153,7 @@ public double MinWideModeWidth
/// <summary>
/// Gets the calculated width (in wide mode) or height (in tall mode) of pane 1, or sets the GridLength value of pane 1.
/// </summary>
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
public GridLength Pane1Length
{
get { return (GridLength)GetValue(Pane1LengthProperty); }
Expand All @@ -162,7 +163,7 @@ public GridLength Pane1Length
/// <summary>
/// Gets the calculated width (in wide mode) or height (in tall mode) of pane 2, or sets the GridLength value of pane 2.
/// </summary>
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
public GridLength Pane2Length
{
get { return (GridLength)GetValue(Pane2LengthProperty); }
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Build.Tasks/XamlCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public ICollection<string> GetResourceNamesInUse(VariableDefinition variableDefi
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexAlignSelfTypeConverter")), typeof(EnumTypeConverter<Layouts.FlexAlignSelf>) },
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexWrapTypeConverter")), typeof(EnumTypeConverter<Layouts.FlexWrap>) },
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexBasisTypeConverter")), typeof(FlexBasisTypeConverter) },
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "GridLengthTypeConverter")), typeof(Microsoft.Maui.Controls.XamlC.GridLengthTypeConverter) },

};

// State used by SetPropertiesVisitor
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/ColumnDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ColumnDefinition(GridLength width)
=> SetValue(WidthProperty, width);

/// <summary>Gets or sets the width of the column.</summary>
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
public GridLength Width
{
get { return (GridLength)GetValue(WidthProperty); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destina
var definitions = new List<ColumnDefinition>(count);
foreach (var range in unsplit.Split(','))
{
var length = GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
definitions.Add(new ColumnDefinition(length));
}
#else
Expand All @@ -42,7 +42,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destina
var definitions = new List<ColumnDefinition>(count);
foreach (var lengthStr in lengths)
{
var length = GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
definitions.Add(new ColumnDefinition(length));
}
#endif
Expand All @@ -62,15 +62,15 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destina
if (count == 0)
return string.Empty;
if (count == 1)
return GridLengthTypeConverter.ConvertToString(definitions[0].Width);
return Converters.GridLengthTypeConverter.ConvertToString(definitions[0].Width);

// for multiple items
var pool = ArrayPool<string>.Shared;
var rentedArray = pool.Rent(definitions.Count);
for (var i = 0; i < definitions.Count; i++)
{
var definition = definitions[i];
rentedArray[i] = GridLengthTypeConverter.ConvertToString(definition.Width);
rentedArray[i] = Converters.GridLengthTypeConverter.ConvertToString(definition.Width);
}
var result = string.Join(", ", rentedArray, 0, definitions.Count);
pool.Return(rentedArray);
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/GridLengthTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Microsoft.Maui.Controls
{
[ProvideCompiled("Microsoft.Maui.Controls.XamlC.GridLengthTypeConverter")]
[Obsolete("Microsoft.Maui.Controls.GridLengthTypeConverter is obsolete. Use Microsoft.Maui.Converters.GridLengthConverter instead.")]
public class GridLengthTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/RowDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public RowDefinition(GridLength height)
}

/// <include file="../../docs/Microsoft.Maui.Controls/RowDefinition.xml" path="//Member[@MemberName='Height']/Docs/*" />
[System.ComponentModel.TypeConverter(typeof(GridLengthTypeConverter))]
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
public GridLength Height
{
get { return (GridLength)GetValue(HeightProperty); }
Expand Down
8 changes: 4 additions & 4 deletions src/Controls/src/Core/RowDefinitionCollectionTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destina
var definitions = new List<RowDefinition>(count);
foreach (var range in unsplit.Split(','))
{
var length = GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
definitions.Add(new RowDefinition(length));
}
#else
Expand All @@ -42,7 +42,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destina
var definitions = new List<RowDefinition>(count);
foreach (var lengthStr in lengths)
{
var length = GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
definitions.Add(new RowDefinition(length));
}
#endif
Expand All @@ -61,15 +61,15 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destina
if (count == 0)
return string.Empty;
if (count == 1)
return GridLengthTypeConverter.ConvertToString(definitions[0].Height);
return Converters.GridLengthTypeConverter.ConvertToString(definitions[0].Height);

// for multiple items
var pool = ArrayPool<string>.Shared;
var rentedArray = pool.Rent(definitions.Count);
for (var i = 0; i < definitions.Count; i++)
{
var definition = definitions[i];
rentedArray[i] = GridLengthTypeConverter.ConvertToString(definition.Height);
rentedArray[i] = Converters.GridLengthTypeConverter.ConvertToString(definition.Height);
}
var result = string.Join(", ", rentedArray, 0, definitions.Count);
pool.Return(rentedArray);
Expand Down
3 changes: 2 additions & 1 deletion src/Controls/src/SourceGen/NodeSGExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private static string ConvertEnum(string value, BaseNode node, ITypeSymbol toTyp
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Graphics.Converters.RectTypeConverter")!, (CreateLazyConverter<RectConverter>(), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Graphics.Rect")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Graphics.Converters.ColorTypeConverter")!, (CreateLazyConverter<ColorConverter>(), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Graphics.Color")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Graphics.Converters.PointTypeConverter")!, (CreateLazyConverter<PointConverter>(), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Graphics.Point")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.ThicknessTypeConverter")!, (CreateLazyConverter<ThicknessConverter>(), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Thickness")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.CornerRadiusTypeConverter")!, (CreateLazyRegistryConverter("Microsoft.Maui.CornerRadius"), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.CornerRadius")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.EasingTypeConverter")!, (CreateLazyRegistryConverter("Microsoft.Maui.Easing"), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Easing")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.FlexJustifyTypeConverter")!, (ConvertEnum, context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Layouts.FlexJustify")!) },
Expand All @@ -60,6 +59,8 @@ private static string ConvertEnum(string value, BaseNode node, ITypeSymbol toTyp
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.FlexAlignSelfTypeConverter")!, (ConvertEnum, context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Layouts.FlexAlignSelf")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.FlexWrapTypeConverter")!, (ConvertEnum, context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Layouts.FlexWrap")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.FlexBasisTypeConverter")!, (CreateLazyRegistryConverter("Microsoft.Maui.Layouts.FlexBasis"), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Layouts.FlexBasis")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.GridLengthTypeConverter")!, (CreateLazyConverter<GridLengthConverter>(), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.GridLength")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Converters.ThicknessTypeConverter")!, (CreateLazyConverter<ThicknessConverter>(), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Thickness")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.ColumnDefinitionCollectionTypeConverter")!, (CreateLazyRegistryConverter("Microsoft.Maui.Controls.ColumnDefinitionCollection"), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.ColumnDefinitionCollection")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.FlowDirectionConverter")!, (CreateLazyRegistryConverter("Microsoft.Maui.FlowDirection"), context.Compilation.GetTypeByMetadataName("Microsoft.Maui.FlowDirection")!) },
{ context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.FontSizeConverter")!, (CreateLazyRegistryConverter("System.Double"), context.Compilation.GetTypeByMetadataName("System.Double")!) },
Expand Down
19 changes: 19 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui29334.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui29334"
Title="Maui29334">
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition>
<RowDefinition.Height>
<OnIdiom x:TypeArguments="GridLength" Default="50" Desktop="100"/>
</RowDefinition.Height>
</RowDefinition>
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Label Text="Hello, World!" />
</Grid>

</ContentPage>
38 changes: 38 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui29334.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Microsoft.Maui.Controls.Xaml.UnitTests;

using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

public partial class Maui29334 : ContentPage
{

public Maui29334() => InitializeComponent();

public Maui29334(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void OnIdiomGridLength([Values] bool useCompiledXaml)
{
var page = new Maui29334(useCompiledXaml);

}
}
}
82 changes: 82 additions & 0 deletions src/Core/src/Converters/GridLengthTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.ComponentModel;
using System.Globalization;

namespace Microsoft.Maui.Converters;

public sealed class GridLengthTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
=> sourceType == typeof(double) || sourceType == typeof(string);

public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
{
if (value is double d)
return (GridLength)d;

var strValue = value as string ?? value?.ToString();
if (strValue is null)
throw new FormatException($"Invalid GridLength format: {value}");

return ParseStringToGridLength(strValue);
}

#if NET6_0_OR_GREATER
internal static GridLength ParseStringToGridLength(ReadOnlySpan<char> value)
#else
internal static GridLength ParseStringToGridLength(string value)
#endif
{
value = value.Trim();

if (value.Length != 0)
{
if (value.Length == 4 && value.Equals("auto", StringComparison.OrdinalIgnoreCase))
return GridLength.Auto;

if (value.Length == 1 && value[0] == '*')
return GridLength.Star;

#if NET6_0_OR_GREATER
var lastChar = value[^1];
#else
var lastChar = value[value.Length - 1];
#endif
if (lastChar == '*')
{
#if NET6_0_OR_GREATER
var prefix = value[..^1];
#else
var prefix = value.Substring(0, value.Length - 1);
#endif

if (double.TryParse(prefix, NumberStyles.Number, CultureInfo.InvariantCulture, out var starLength))
return new GridLength(starLength, GridUnitType.Star);
}

if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var absoluteLength))
return new GridLength(absoluteLength);
}

throw new FormatException($"Invalid GridLength format: {value.ToString()}");
}

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == typeof(string);

public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
{
if (destinationType == typeof(string) && value is GridLength length)
return ConvertToString(length);
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");

}

internal static string ConvertToString(GridLength length)
{
if (length.IsAuto)
return "auto";
if (length.IsStar)
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}*";
return length.Value.ToString(CultureInfo.InvariantCulture);
}
}
17 changes: 1 addition & 16 deletions src/Core/src/Primitives/GridLength.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Maui.Converters;

namespace Microsoft.Maui
{
Expand Down Expand Up @@ -119,21 +120,5 @@ public override string ToString()
/// <param name="right">The second GridLength to compare.</param>
/// <returns><see langword="true"/> if the two GridLengths differ; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(GridLength left, GridLength right) => !(left == right);

private sealed class GridLengthTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
=> sourceType == typeof(double);

public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
=> value switch
{
double d => (GridLength)d,
_ => throw new NotSupportedException(),
};

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => false;
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType) => throw new NotSupportedException();
}
}
}
6 changes: 6 additions & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable enable
Microsoft.Maui.Converters.GridLengthTypeConverter
Microsoft.Maui.Converters.GridLengthTypeConverter.GridLengthTypeConverter() -> void
Microsoft.Maui.Converters.SafeAreaEdgesTypeConverter
Microsoft.Maui.Converters.SafeAreaEdgesTypeConverter.SafeAreaEdgesTypeConverter() -> void
Microsoft.Maui.Graphics.MauiDrawable.SetEmptyBorderBrush() -> void
Expand Down Expand Up @@ -156,6 +158,10 @@ override Microsoft.Maui.Converters.FlexWrapTypeConverter.CanConvertFrom(System.C
override Microsoft.Maui.Converters.FlexWrapTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
override Microsoft.Maui.Converters.FlexWrapTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object! value) -> object?
override Microsoft.Maui.Converters.FlexWrapTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type! destinationType) -> object?
override Microsoft.Maui.Converters.GridLengthTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) -> bool
override Microsoft.Maui.Converters.GridLengthTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
override Microsoft.Maui.Converters.GridLengthTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value) -> object!
override Microsoft.Maui.Converters.GridLengthTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type? destinationType) -> object!
override Microsoft.Maui.Converters.SafeAreaEdgesTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type! sourceType) -> bool
override Microsoft.Maui.Converters.SafeAreaEdgesTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
override Microsoft.Maui.Converters.SafeAreaEdgesTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value) -> object!
Expand Down
Loading
Loading