Skip to content

Commit

Permalink
Generator: fix IsBindEvent handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Jan 3, 2025
1 parent 3350370 commit 50810a7
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ static Popup()
/// </summary>
[Parameter] public MMP.LayoutAlignment? VerticalOptions { get; set; }
/// <summary>
/// Property that represents the Window that's showing the Popup.
/// </summary>
[Parameter] public MC.Window Window { get; set; }
/// <summary>
/// Gets or sets the <see cref="T:Microsoft.Maui.Controls.View" /> content to render in the Popup.
/// </summary>
[Parameter] public RenderFragment ChildContent { get; set; }
/// <summary>
/// Property that represents the Window that's showing the Popup.
/// </summary>
[Parameter] public RenderFragment Window { get; set; }
[Parameter] public EventCallback<CM.Core.PopupClosedEventArgs> OnClosed { get; set; }
[Parameter] public EventCallback<CM.Core.PopupOpenedEventArgs> OnOpened { get; set; }

Expand Down Expand Up @@ -137,16 +137,12 @@ protected override void HandleParameter(string name, object value)
NativeControl.VerticalOptions = VerticalOptions ?? (MMP.LayoutAlignment)CMV.Popup.VerticalOptionsProperty.DefaultValue;
}
break;
case nameof(Window):
if (!Equals(Window, value))
{
Window = (MC.Window)value;
NativeControl.Window = Window;
}
break;
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;
case nameof(Window):
Window = (RenderFragment)value;
break;
case nameof(OnClosed):
if (!Equals(OnClosed, value))
{
Expand Down Expand Up @@ -178,6 +174,7 @@ protected override void RenderAdditionalElementContent(RenderTreeBuilder builder
{
base.RenderAdditionalElementContent(builder, ref sequence);
RenderTreeBuilderHelper.AddContentProperty<CMV.Popup>(builder, sequence++, ChildContent, (x, value) => x.Content = (MC.View)value);
RenderTreeBuilderHelper.AddContentProperty<CMV.Popup>(builder, sequence++, Window, (x, value) => x.Window = (MC.Window)value);
}

static partial void RegisterAdditionalHandlers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static MDNavigationBar()
[Parameter] public bool? HasLabel { get; set; }
[Parameter] public int? SelectedIndex { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public EventCallback<MC.SelectedItemChangedEventArgs> OnSelectedItemChanged { get; set; }

public new MCM.NavigationBar NativeControl => (MCM.NavigationBar)((BindableObject)this).NativeControl;

Expand All @@ -53,6 +54,16 @@ protected override void HandleParameter(string name, object value)
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;
case nameof(OnSelectedItemChanged):
if (!Equals(OnSelectedItemChanged, value))
{
void NativeControlSelectedItemChanged(object sender, MC.SelectedItemChangedEventArgs e) => InvokeEventCallback(OnSelectedItemChanged, e);

OnSelectedItemChanged = (EventCallback<MC.SelectedItemChangedEventArgs>)value;
NativeControl.SelectedItemChanged -= NativeControlSelectedItemChanged;
NativeControl.SelectedItemChanged += NativeControlSelectedItemChanged;
}
break;

default:
base.HandleParameter(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static MDNavigationDrawer()
[Parameter] public Color ToolBarBackGroundColour { get; set; }
[Parameter] public RenderFragment FooterItems { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public EventCallback<MC.SelectedItemChangedEventArgs> OnSelectedItemChanged { get; set; }

public new MCM.NavigationDrawer NativeControl => (MCM.NavigationDrawer)((BindableObject)this).NativeControl;

Expand Down Expand Up @@ -106,6 +107,16 @@ protected override void HandleParameter(string name, object value)
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;
case nameof(OnSelectedItemChanged):
if (!Equals(OnSelectedItemChanged, value))
{
void NativeControlSelectedItemChanged(object sender, MC.SelectedItemChangedEventArgs e) => InvokeEventCallback(OnSelectedItemChanged, e);

OnSelectedItemChanged = (EventCallback<MC.SelectedItemChangedEventArgs>)value;
NativeControl.SelectedItemChanged -= NativeControlSelectedItemChanged;
NativeControl.SelectedItemChanged += NativeControlSelectedItemChanged;
}
break;

default:
base.HandleParameter(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static MDSplitView()
[Parameter] public bool? IsPaneOpen { get; set; }
[Parameter] public RenderFragment Content { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public EventCallback<MCM.Core.ValueChangedEventArgs> OnContentChanged { get; set; }

public new MCM.SplitView NativeControl => (MCM.SplitView)((BindableObject)this).NativeControl;

Expand Down Expand Up @@ -57,6 +58,16 @@ protected override void HandleParameter(string name, object value)
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;
case nameof(OnContentChanged):
if (!Equals(OnContentChanged, value))
{
void NativeControlContentChanged(object sender, MCM.Core.ValueChangedEventArgs e) => InvokeEventCallback(OnContentChanged, e);

OnContentChanged = (EventCallback<MCM.Core.ValueChangedEventArgs>)value;
NativeControl.ContentChanged -= NativeControlContentChanged;
NativeControl.ContentChanged += NativeControlContentChanged;
}
break;

default:
base.HandleParameter(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static MDTabs()
[Parameter] public bool? HasLabel { get; set; }
[Parameter] public int? SelectedIndex { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public EventCallback<MC.SelectedItemChangedEventArgs> OnSelectedItemChanged { get; set; }

public new MCM.Tabs NativeControl => (MCM.Tabs)((BindableObject)this).NativeControl;

Expand Down Expand Up @@ -78,6 +79,16 @@ protected override void HandleParameter(string name, object value)
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;
case nameof(OnSelectedItemChanged):
if (!Equals(OnSelectedItemChanged, value))
{
void NativeControlSelectedItemChanged(object sender, MC.SelectedItemChangedEventArgs e) => InvokeEventCallback(OnSelectedItemChanged, e);

OnSelectedItemChanged = (EventCallback<MC.SelectedItemChangedEventArgs>)value;
NativeControl.SelectedItemChanged -= NativeControlSelectedItemChanged;
NativeControl.SelectedItemChanged += NativeControlSelectedItemChanged;
}
break;

default:
base.HandleParameter(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ internal static GeneratedPropertyInfo[] GetEventCallbackProperties(GeneratedType
.Where(e => e.DeclaredAccessibility == Accessibility.Public && e.IsBrowsable())
.Select(eventInfo =>
{
var isBindEvent = IsBindEvent(eventInfo, out var bindedProperty);

if (isBindEvent && IsRenderFragmentPropertySymbol(containingType, bindedProperty))
return null;
var isBindEvent = IsBindEvent(eventInfo, containingType, out var bindedProperty);

var eventCallbackName = isBindEvent ? $"{bindedProperty.Name}Changed" : GetEventCallbackName(eventInfo);

Expand Down Expand Up @@ -178,10 +175,11 @@ private static string GetEventCallbackName(IEventSymbol eventSymbol)
};
}

private static bool IsBindEvent(IEventSymbol eventSymbol, out IPropertySymbol property)
private static bool IsBindEvent(IEventSymbol eventSymbol, GeneratedTypeInfo containingType, out IPropertySymbol property)
{
var properties = eventSymbol.ContainingType.GetMembers().OfType<IPropertySymbol>()
.Where(p => IsPublicProperty(p) && HasPublicSetter(p));
var properties = eventSymbol.ContainingType.GetMembers()
.OfType<IPropertySymbol>()
.Where(p => IsValueProperty(p, containingType));

property = properties.FirstOrDefault(p =>
eventSymbol.Name == $"{p.Name}Changed" // e.g. Value - ValueChanged
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,8 @@ string GetConvertedProperty(ITypeSymbol propertyType, string propName)

internal static GeneratedPropertyInfo[] GetValueProperties(GeneratedTypeInfo generatedType)
{
var componentInfo = generatedType.Settings;

var props = GetMembers<IPropertySymbol>(componentInfo.TypeSymbol, generatedType.Settings.Include)
.Where(p => !componentInfo.Exclude.Contains(p.Name))
.Where(p => !componentInfo.ContentProperties.Contains(p.Name))
.Where(IsPublicProperty)
.Where(HasPublicSetter)
.Where(prop => IsExplicitlyAllowed(prop, generatedType) || !DisallowedComponentTypes.Contains(prop.Type.GetFullName()))
.Where(prop => prop.Type.GetFullName() == "Microsoft.Maui.Controls.Brush" || !IsRenderFragmentPropertySymbol(generatedType, prop))
var props = GetMembers<IPropertySymbol>(generatedType.Settings.TypeSymbol, generatedType.Settings.Include)
.Where(p => IsValueProperty(p, generatedType))
.OrderBy(prop => prop.Name, StringComparer.OrdinalIgnoreCase);

return props.Select(prop =>
Expand Down Expand Up @@ -186,6 +179,20 @@ internal static GeneratedPropertyInfo[] GetValueProperties(GeneratedTypeInfo gen
.ToArray();
}

private static bool IsValueProperty(IPropertySymbol prop, GeneratedTypeInfo generatedType)
{
if (generatedType.Settings.Exclude.Contains(prop.Name))
return false;

if (generatedType.Settings.ContentProperties.Contains(prop.Name))
return false;

return IsPublicProperty(prop)
&& HasPublicSetter(prop)
&& (IsExplicitlyAllowed(prop, generatedType) || !DisallowedComponentTypes.Contains(prop.Type.GetFullName()))
&& (prop.Type.GetFullName() == "Microsoft.Maui.Controls.Brush" || !IsRenderFragmentPropertySymbol(generatedType, prop));
}


private static string GetComponentPropertyTypeName(IPropertySymbol propertySymbol, GeneratedTypeInfo containingType, bool isRenderFragmentProperty = false, bool makeNullable = false)
{
Expand Down
12 changes: 12 additions & 0 deletions src/BlazorBindings.Maui/Elements/TabbedPage.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Maui.Graphics;
using System;
using System.Collections.Specialized;
using System.Threading.Tasks;

Expand Down Expand Up @@ -45,6 +46,7 @@ static TabbedPage()
[Parameter] public Color UnselectedTabColor { get; set; }
[Parameter] public RenderFragment BarBackground { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public EventCallback OnCurrentPageChanged { get; set; }
[Parameter] public EventCallback<NotifyCollectionChangedEventArgs> OnPagesChanged { get; set; }

public new MC.TabbedPage NativeControl => (MC.TabbedPage)((BindableObject)this).NativeControl;
Expand Down Expand Up @@ -89,6 +91,16 @@ protected override void HandleParameter(string name, object value)
case nameof(ChildContent):
ChildContent = (RenderFragment)value;
break;
case nameof(OnCurrentPageChanged):
if (!Equals(OnCurrentPageChanged, value))
{
void NativeControlCurrentPageChanged(object sender, EventArgs e) => InvokeEventCallback(OnCurrentPageChanged);

OnCurrentPageChanged = (EventCallback)value;
NativeControl.CurrentPageChanged -= NativeControlCurrentPageChanged;
NativeControl.CurrentPageChanged += NativeControlCurrentPageChanged;
}
break;
case nameof(OnPagesChanged):
if (!Equals(OnPagesChanged, value))
{
Expand Down

0 comments on commit 50810a7

Please sign in to comment.