Skip to content

Commit

Permalink
Better naming!
Browse files Browse the repository at this point in the history
  • Loading branch information
albyrock87 committed Sep 25, 2024
1 parent e658842 commit 109874c
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 189 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Cross-platform layouts and utilities for MAUI applications simplify dealing with

- Have you ever dreamed of having an `if` statement in XAML?
```csharp
<layouts:ConditionedTemplate Value="{Binding HasPermission}"
<layouts:ToggleTemplate Value="{Binding HasPermission}"
TrueTemplate="{StaticResource AdminFormTemplate}"
FalseTemplate="{StaticResource PermissionRequestTemplate}" />
```
Expand Down
26 changes: 13 additions & 13 deletions Samples/Nalu.Maui.Sample/Pages/FivePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@
</Label.FormattedText>
</Label>
<Button Text="Navigate to /One/Three" Command="{Binding GoToThreeCommand}" />
<nalu:ConditionedTemplate Value="True" ContentBindingContext="A true statement">
<nalu:ConditionedTemplate.TrueTemplate>
<nalu:ToggleTemplate Value="True" ContentBindingContext="A true statement">
<nalu:ToggleTemplate.WhenTrue>
<DataTemplate x:DataType="x:String">
<Label Text="{Binding .}" />
</DataTemplate>
</nalu:ConditionedTemplate.TrueTemplate>
<nalu:ConditionedTemplate.FalseTemplate>
</nalu:ToggleTemplate.WhenTrue>
<nalu:ToggleTemplate.WhenFalse>
<DataTemplate>
<Label Text="This is false" />
</DataTemplate>
</nalu:ConditionedTemplate.FalseTemplate>
</nalu:ConditionedTemplate>
</nalu:ToggleTemplate.WhenFalse>
</nalu:ToggleTemplate>
<Border BindingContext="Hello world"
BackgroundColor="LightBlue"
StrokeShape="RoundRectangle 24">
<nalu:Component Padding="16,8" x:DataType="x:String">
<nalu:ViewBox Padding="16,8" x:DataType="x:String">
<Label Text="{Binding .}"/>
</nalu:Component>
</nalu:ViewBox>
</Border>
<nalu:TemplatedComponent>
<nalu:TemplatedComponent.ContentTemplate>
<nalu:TemplateBox>
<nalu:TemplateBox.ContentTemplate>
<DataTemplate>
<HorizontalStackLayout>
<Label Text="Projected => " />
<nalu:ProjectContainer />
<nalu:TemplateContentPresenter />
</HorizontalStackLayout>
</DataTemplate>
</nalu:TemplatedComponent.ContentTemplate>
</nalu:TemplateBox.ContentTemplate>
<Label Text="I'm here!" />
</nalu:TemplatedComponent>
</nalu:TemplateBox>
</VerticalStackLayout>
</ContentPage.Content>
</ContentPage>
4 changes: 2 additions & 2 deletions Samples/Nalu.Maui.Sample/Pages/OnePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
</Label.FormattedText>
</Label>

<layouts:Component ContentBindingContext="{Binding Animal}">
<layouts:ViewBox ContentBindingContext="{Binding Animal}">
<Label Text="{Binding Name}" x:DataType="pageModels:AnimalModel" />
</layouts:Component>
</layouts:ViewBox>

<Button Command="{Binding PushThreeCommand}"
Text="Push Three"
Expand Down
2 changes: 1 addition & 1 deletion Source/Nalu.Maui.Layouts/Layouts/ComponentLayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Nalu;
using Microsoft.Maui.Layouts;

/// <summary>
/// Layout manager for <see cref="Component"/>.
/// Layout manager for <see cref="ViewBox"/>.
/// </summary>
/// <param name="contentView">The layout using this <see cref="ILayoutManager"/>.</param>
public class ComponentLayoutManager(IContentView contentView) : ILayoutManager
Expand Down
71 changes: 0 additions & 71 deletions Source/Nalu.Maui.Layouts/Layouts/ConditionedTemplate.cs

This file was deleted.

16 changes: 0 additions & 16 deletions Source/Nalu.Maui.Layouts/Layouts/ProjectContainer.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
namespace Nalu;

/// <summary>
/// A <see cref="Component"/> that uses a <see cref="DataTemplate"/> to render content.
/// A <see cref="ViewBox"/> that uses a <see cref="DataTemplate"/> or <see cref="DataTemplateSelector"/> to render content.
/// </summary>
public class TemplatedComponent : TemplatedComponentBase
public class TemplateBox : TemplateBoxBase
{
/// <summary>
/// Bindable property for <see cref="ContentTemplate"/> property.
/// </summary>
public static readonly BindableProperty ContentTemplateProperty = BindableProperty.Create(
nameof(ContentTemplate),
typeof(DataTemplate),
typeof(TemplatedComponent),
typeof(TemplateBox),
propertyChanged: ContentTemplateChanged);

/// <summary>
Expand All @@ -25,7 +25,7 @@ public DataTemplate? ContentTemplate

private static void ContentTemplateChanged(BindableObject bindable, object? oldvalue, object? newvalue)
{
if (bindable is TemplatedComponent templateLayout)
if (bindable is TemplateBox templateLayout)
{
templateLayout.SetTemplate(newvalue as DataTemplate);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
namespace Nalu;

/// <summary>
/// A <see cref="Component"/> base class that uses a <see cref="DataTemplate"/> to render content.
/// A <see cref="ViewBox"/> base class that uses a <see cref="DataTemplate"/> to render content.
/// </summary>
[ContentProperty(nameof(ProjectedContent))]
public abstract class TemplatedComponentBase : ComponentBase
[ContentProperty(nameof(TemplateContent))]
public abstract class TemplateBoxBase : ViewBoxBase
{
/// <summary>
/// Bindable property for <see cref="ProjectedContent"/> property.
/// Bindable property for <see cref="TemplateContent"/> property.
/// </summary>
public static readonly BindableProperty ProjectedContentProperty = BindableProperty.Create(
nameof(ProjectedContent),
public static readonly BindableProperty TemplateContentProperty = BindableProperty.Create(
nameof(TemplateContent),
typeof(IView),
typeof(TemplatedComponent));
typeof(TemplateBox));

private bool _changingTemplate;

/// <summary>
/// Gets or sets the content to be projected through `ProjectContainer`.
/// Gets or sets the content to be projected through <see cref="TemplateContentPresenter"/> component.
/// </summary>
public IView? ProjectedContent
public IView? TemplateContent
{
get => (IView?)GetValue(ProjectedContentProperty);
set => SetValue(ProjectedContentProperty, value);
get => (IView?)GetValue(TemplateContentProperty);
set => SetValue(TemplateContentProperty, value);
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions Source/Nalu.Maui.Layouts/Layouts/TemplateContentPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Nalu;

/// <summary>
/// A <see cref="ViewBox"/> to display the <see cref="TemplateBoxBase.TemplateContent" />.
/// </summary>
public class TemplateContentPresenter : ViewBox
{
/// <summary>
/// Initializes a new instance of the <see cref="TemplateContentPresenter"/> class.
/// </summary>
public TemplateContentPresenter()
{
var binding = new Binding(nameof(TemplateBox.TemplateContent), source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(TemplateBoxBase)));
SetBinding(ContentProperty, binding);
}
}
71 changes: 71 additions & 0 deletions Source/Nalu.Maui.Layouts/Layouts/ToggleTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace Nalu;

/// <summary>
/// A <see cref="ViewBox"/> that uses a <see cref="DataTemplate"/> to render content based on a boolean value.
/// </summary>
public class ToggleTemplate : TemplateBoxBase
{
/// <summary>
/// Bindable property for <see cref="WhenTrue"/> property.
/// </summary>
public static readonly BindableProperty WhenTrueProperty =
BindableProperty.Create(nameof(WhenTrue), typeof(DataTemplate), typeof(ToggleTemplate), propertyChanged: ConditionChanged);

/// <summary>
/// Bindable property for <see cref="WhenFalse"/> property.
/// </summary>
public static readonly BindableProperty WhenFalseProperty =
BindableProperty.Create(nameof(WhenFalse), typeof(DataTemplate), typeof(ToggleTemplate), propertyChanged: ConditionChanged);

/// <summary>
/// Bindable property for <see cref="Value"/> property.
/// </summary>
public static readonly BindableProperty ValueProperty = BindableProperty.Create(nameof(Value), typeof(bool?), typeof(ToggleTemplate), propertyChanged: ConditionChanged);

/// <summary>
/// Gets or sets the <see cref="DataTemplate"/> to use when the value is false.
/// </summary>
public DataTemplate? WhenFalse
{
get => (DataTemplate?)GetValue(WhenFalseProperty);
set => SetValue(WhenFalseProperty, value);
}

/// <summary>
/// Gets or sets the <see cref="DataTemplate"/> to use when the value is true.
/// </summary>
public DataTemplate? WhenTrue
{
get => (DataTemplate?)GetValue(WhenTrueProperty);
set => SetValue(WhenTrueProperty, value);
}

/// <summary>
/// Gets or sets the value to determine which template to use.
/// </summary>
public bool? Value
{
get => (bool?)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}

private void SetTemplateFromValue()
{
var template = Value switch
{
true => WhenTrue,
false => WhenFalse,
_ => null,
};

SetTemplate(template);
}

private static void ConditionChanged(BindableObject bindable, object oldvalue, object newvalue)
{
if (bindable is ToggleTemplate chooseTemplate)
{
chooseTemplate.SetTemplateFromValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Nalu;
/// Can be used as a replacement of <see cref="ContentView"/> (which as-of .NET 8 uses Compatibility.Layout).
/// </remarks>
[ContentProperty(nameof(Content))]
public class Component : ComponentBase
public class ViewBox : ViewBoxBase
{
/// <summary>
/// Bindable property for <see cref="Content"/> property.
/// </summary>
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(IView), typeof(Component), propertyChanged: OnContentPropertyChanged);
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(IView), typeof(ViewBox), propertyChanged: OnContentPropertyChanged);

/// <summary>
/// Gets or sets the content of the layout.
Expand All @@ -30,5 +30,5 @@ public IView? Content
protected override void SetContent(IView? content) => SetValue(ContentProperty, content);

private static void OnContentPropertyChanged(BindableObject bindable, object? oldValue, object? newValue)
=> ((Component)bindable).OnContentPropertyChanged((IView?)oldValue, (IView?)newValue);
=> ((ViewBox)bindable).OnContentPropertyChanged((IView?)oldValue, (IView?)newValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ namespace Nalu;
using Microsoft.Maui.Layouts;

/// <summary>
/// ContentLayoutBase is a base class for a layout that is used to display a single view.
/// <see cref="ViewBoxBase"/> is a base class a <see cref="IContentView"/> that is used to display a single view.
/// </summary>
/// <remarks>
/// Can be used as a replacement of <see cref="ContentView"/> (which as-of .NET 8 uses Compatibility.Layout).
/// </remarks>
public abstract class ComponentBase : View, IContentView
public abstract class ViewBoxBase : View, IContentView
{
private ILayoutManager? _layoutManager;
private ILayoutManager LayoutManager => _layoutManager ??= new ComponentLayoutManager(this);
Expand All @@ -19,20 +16,20 @@ public abstract class ComponentBase : View, IContentView
public static readonly BindableProperty PaddingProperty = BindableProperty.Create(
nameof(Padding),
typeof(Thickness),
typeof(ComponentBase),
typeof(ViewBoxBase),
default(Thickness),
propertyChanged: OnPaddingPropertyChanged);

private static void OnPaddingPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
=> ((ComponentBase)bindable).OnPaddingPropertyChanged((Thickness)oldvalue, (Thickness)newvalue);
=> ((ViewBoxBase)bindable).OnPaddingPropertyChanged((Thickness)oldvalue, (Thickness)newvalue);

/// <summary>
/// Bindable property for <see cref="ContentBindingContext"/> property.
/// </summary>
public static readonly BindableProperty ContentBindingContextProperty = BindableProperty.Create(
nameof(ContentBindingContext),
typeof(object),
typeof(Component),
typeof(ViewBox),
propertyChanged: ContentBindingContextPropertyChanged);

/// <summary>
Expand Down Expand Up @@ -157,7 +154,7 @@ protected virtual void SetContent(IView? content)

private static void ContentBindingContextPropertyChanged(BindableObject bindable, object? oldvalue, object? newvalue)
{
if (bindable is ComponentBase contentLayout)
if (bindable is ViewBoxBase contentLayout)
{
contentLayout.ContentBindingContextPropertyChanged(oldvalue, newvalue);
}
Expand Down
Loading

0 comments on commit 109874c

Please sign in to comment.