Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Behaviors #82

Merged
merged 4 commits into from
Feb 12, 2023
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
1 change: 1 addition & 0 deletions samples/ThirdPartyControlsSample/AppShell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<Shell>
<AlohaKitPage />
<CommunityToolkitPage />
<CommunityToolkitBehaviors />
<XCalendarPage />
</Shell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using BlazorBindings.Maui.Elements;
using CMB = CommunityToolkit.Maui.Behaviors;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements.CommunityToolkit.Behaviors
{
/// <summary>
/// The MaskedBehavior is a behavior that allows the user to define an input mask for data entry. Adding this behavior to an <see cref="T:Microsoft.Maui.Controls.InputView" /> (i.e. <see cref="T:Microsoft.Maui.Controls.Entry" />) control will force the user to only input values matching a given mask. Examples of its usage include input of a credit card number or a phone number.
/// </summary>
public partial class MaskedBehavior : BlazorBindings.Maui.Elements.Behavior
{
static MaskedBehavior()
{
RegisterAdditionalHandlers();
}

/// <summary>
/// The mask that the input value needs to match.
/// </summary>
[Parameter] public string Mask { get; set; }
/// <summary>
/// Gets or sets which character in the <see cref="P:CommunityToolkit.Maui.Behaviors.MaskedBehavior.Mask" /> property that will be visible and entered by a user. Defaults to 'X'. <br /> By default the 'X' character will be unmasked therefore a <see cref="P:CommunityToolkit.Maui.Behaviors.MaskedBehavior.Mask" /> of "XX XX XX" would display "12 34 56". If you wish to include 'X' in your <see cref="P:CommunityToolkit.Maui.Behaviors.MaskedBehavior.Mask" /> then you could set this <see cref="P:CommunityToolkit.Maui.Behaviors.MaskedBehavior.UnmaskedCharacter" /> to something else e.g. '0' and then use a <see cref="P:CommunityToolkit.Maui.Behaviors.MaskedBehavior.Mask" /> of "00X00X00" which would then display "12X34X56".
/// </summary>
[Parameter] public char? UnmaskedCharacter { get; set; }

public new CMB.MaskedBehavior NativeControl => (CMB.MaskedBehavior)((BindableObject)this).NativeControl;

protected override CMB.MaskedBehavior CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(Mask):
if (!Equals(Mask, value))
{
Mask = (string)value;
NativeControl.Mask = Mask;
}
break;
case nameof(UnmaskedCharacter):
if (!Equals(UnmaskedCharacter, value))
{
UnmaskedCharacter = (char?)value;
NativeControl.UnmaskedCharacter = UnmaskedCharacter ?? (char)CMB.MaskedBehavior.UnmaskedCharacterProperty.DefaultValue;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

static partial void RegisterAdditionalHandlers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using BlazorBindings.Maui.Elements;
using CMB = CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.Maui.Graphics;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements.CommunityToolkit.Behaviors
{
/// <summary>
/// <see cref="T:Microsoft.Maui.Controls.PlatformBehavior`2" /> that controls the Status bar color
/// </summary>
public partial class StatusBarBehavior : BlazorBindings.Maui.Elements.Behavior
{
static StatusBarBehavior()
{
RegisterAdditionalHandlers();
}

/// <summary>
/// Property that holds the value of the Status bar color.
/// </summary>
[Parameter] public Color StatusBarColor { get; set; }
/// <summary>
/// Property that holds the value of the Status bar color.
/// </summary>
[Parameter] public StatusBarStyle? StatusBarStyle { get; set; }

public new CMB.StatusBarBehavior NativeControl => (CMB.StatusBarBehavior)((BindableObject)this).NativeControl;

protected override CMB.StatusBarBehavior CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(StatusBarColor):
if (!Equals(StatusBarColor, value))
{
StatusBarColor = (Color)value;
NativeControl.StatusBarColor = StatusBarColor;
}
break;
case nameof(StatusBarStyle):
if (!Equals(StatusBarStyle, value))
{
StatusBarStyle = (StatusBarStyle?)value;
NativeControl.StatusBarStyle = StatusBarStyle ?? (StatusBarStyle)CMB.StatusBarBehavior.StatusBarStyleProperty.DefaultValue;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

static partial void RegisterAdditionalHandlers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using BlazorBindings.Maui.Elements.Input;
using Microsoft.AspNetCore.Components;

namespace BlazorBindings.Maui.Elements.CommunityToolkit.Behaviors
{
public partial class UserStoppedTypingBehavior
{
[Parameter] public EventCallback<string> Command { get; set; }

protected override bool HandleAdditionalParameter(string name, object value)
{
if (name == nameof(Command))
{
if (!Equals(Command, value))
{
Command = (EventCallback<string>)value;
NativeControl.Command = Command.HasDelegate
? new EventCallbackCommand(value => InvokeEventCallback(Command, (string)value))
: null;
}
return true;
}

return base.HandleAdditionalParameter(name, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using BlazorBindings.Maui.Elements;
using CMB = CommunityToolkit.Maui.Behaviors;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements.CommunityToolkit.Behaviors
{
/// <summary>
/// The <see cref="T:CommunityToolkit.Maui.Behaviors.UserStoppedTypingBehavior" /> is a behavior that allows the user to trigger an action when a user has stopped data input any <see cref="T:Microsoft.Maui.Controls.InputView" /> derivate like <see cref="T:Microsoft.Maui.Controls.Entry" /> or <see cref="T:Microsoft.Maui.Controls.SearchBar" />. Examples of its usage include triggering a search when a user has stopped entering their search query.
/// </summary>
public partial class UserStoppedTypingBehavior : BlazorBindings.Maui.Elements.Behavior
{
static UserStoppedTypingBehavior()
{
RegisterAdditionalHandlers();
}

/// <summary>
/// The minimum length of the input value required before <see cref="P:CommunityToolkit.Maui.Behaviors.UserStoppedTypingBehavior.Command" /> will be executed but only after <see cref="P:CommunityToolkit.Maui.Behaviors.UserStoppedTypingBehavior.StoppedTypingTimeThreshold" /> has passed.
/// </summary>
[Parameter] public int? MinimumLengthThreshold { get; set; }
/// <summary>
/// Indicates whether or not the keyboard should be dismissed automatically after the user stopped typing.
/// </summary>
[Parameter] public bool? ShouldDismissKeyboardAutomatically { get; set; }
/// <summary>
/// The time of inactivity in milliseconds after which <see cref="P:CommunityToolkit.Maui.Behaviors.UserStoppedTypingBehavior.Command" /> will be executed. If <see cref="P:CommunityToolkit.Maui.Behaviors.UserStoppedTypingBehavior.MinimumLengthThreshold" /> is also set, the condition there also needs to be met.
/// </summary>
[Parameter] public int? StoppedTypingTimeThreshold { get; set; }

public new CMB.UserStoppedTypingBehavior NativeControl => (CMB.UserStoppedTypingBehavior)((BindableObject)this).NativeControl;

protected override CMB.UserStoppedTypingBehavior CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(MinimumLengthThreshold):
if (!Equals(MinimumLengthThreshold, value))
{
MinimumLengthThreshold = (int?)value;
NativeControl.MinimumLengthThreshold = MinimumLengthThreshold ?? (int)CMB.UserStoppedTypingBehavior.MinimumLengthThresholdProperty.DefaultValue;
}
break;
case nameof(ShouldDismissKeyboardAutomatically):
if (!Equals(ShouldDismissKeyboardAutomatically, value))
{
ShouldDismissKeyboardAutomatically = (bool?)value;
NativeControl.ShouldDismissKeyboardAutomatically = ShouldDismissKeyboardAutomatically ?? (bool)CMB.UserStoppedTypingBehavior.ShouldDismissKeyboardAutomaticallyProperty.DefaultValue;
}
break;
case nameof(StoppedTypingTimeThreshold):
if (!Equals(StoppedTypingTimeThreshold, value))
{
StoppedTypingTimeThreshold = (int?)value;
NativeControl.StoppedTypingTimeThreshold = StoppedTypingTimeThreshold ?? (int)CMB.UserStoppedTypingBehavior.StoppedTypingTimeThresholdProperty.DefaultValue;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

static partial void RegisterAdditionalHandlers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@using BlazorBindings.Maui.Elements.CommunityToolkit.Behaviors

<ContentPage Title="CommunityToolkit.Behaviors">
<Behaviors>
<StatusBarBehavior StatusBarColor="statusBarColor" />
</Behaviors>

<ChildContent>

<VerticalStackLayout Margin="20" Spacing="8">

<Button Text="Change status bar color" OnClick="ChangeStatusBarColor" />

<Label>Entry with card number mask:</Label>
<Entry @bind-Text="cardNumberValue" Keyboard="Keyboard.Numeric">
<Behaviors>
<MaskedBehavior Mask="XXXX-XXXX-XXXX-XXXX" />
</Behaviors>
</Entry>
<Label>Entered value: @cardNumberValue</Label>

<Label>Entry with UserStoppedTypingBehavior</Label>
<Entry T="string">
<Behaviors>
<UserStoppedTypingBehavior Command="UserStoppedTyping"
MinimumLengthThreshold="2"
StoppedTypingTimeThreshold="1000" />
</Behaviors>
</Entry>
<Label>Search Query: @searchValue</Label>

</VerticalStackLayout>

</ChildContent>
</ContentPage>

@code {
Color statusBarColor;
string cardNumberValue;
string searchValue;

List<Color> colors = new() {
Colors.Red,
Colors.Green,
Colors.Blue,
null
};

void ChangeStatusBarColor()
{
var index = (colors.IndexOf(statusBarColor) + 1) % colors.Count;
statusBarColor = colors[index];
}

void UserStoppedTyping(string text)
{
searchValue = text;
}
}
6 changes: 5 additions & 1 deletion samples/ThirdPartyControlsSample/Properties/Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using BlazorBindings.Maui.ComponentGenerator;
using XCalendar.Maui.Views;

// AlohaKit
// AlohaKit.Controls
[assembly: GenerateComponent(typeof(Rating))]
[assembly: GenerateComponent(typeof(BusyIndicator))]
[assembly: GenerateComponent(typeof(Avatar))]
Expand All @@ -22,6 +22,10 @@
[assembly: GenerateComponent(typeof(CommunityToolkit.Maui.Views.DrawingView))]
[assembly: GenerateComponent(typeof(CommunityToolkit.Maui.Views.Popup), Exclude = new[] { nameof(CommunityToolkit.Maui.Views.Popup.Anchor) })]

[assembly: GenerateComponent(typeof(CommunityToolkit.Maui.Behaviors.MaskedBehavior))]
[assembly: GenerateComponent(typeof(CommunityToolkit.Maui.Behaviors.UserStoppedTypingBehavior))]
[assembly: GenerateComponent(typeof(CommunityToolkit.Maui.Behaviors.StatusBarBehavior))]

// XCalendar
[assembly: GenerateComponent(typeof(CalendarView),
GenericProperties = new[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,11 @@ public static string GetIdentifierName(string possibleIdentifier)

private string GetComponentGroup(INamedTypeSymbol typeToGenerate)
{
var assemblyName = typeToGenerate.ContainingAssembly.Name;
var nsName = typeToGenerate.ContainingNamespace.GetFullName();
var parts = nsName.Split('.')
.Except(new[] { "Maui", "Controls", "Views", "UI", "Microsoft" }, StringComparer.OrdinalIgnoreCase);

if (assemblyName == "Microsoft.Maui.Controls")
{
var nsName = typeToGenerate.ContainingNamespace.GetFullName();
return nsName == "Microsoft.Maui.Controls" ? "" : nsName.Replace("Microsoft.Maui.Controls.", "");
}
else
{
return assemblyName.Replace(".Maui", "").Replace(".Views", "").Replace(".UI", "").Replace(".Controls", "");
}
return string.Join('.', parts);
}

private string GetComponentNamespace(INamedTypeSymbol typeToGenerate)
Expand Down
28 changes: 28 additions & 0 deletions src/BlazorBindings.Maui/Elements/Behavior.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements
{
public abstract partial class Behavior : BindableObject
{
static Behavior()
{
RegisterAdditionalHandlers();
}

public new MC.Behavior NativeControl => (MC.Behavior)((BindableObject)this).NativeControl;



static partial void RegisterAdditionalHandlers();
}
}
Loading