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

Maui renderer refactor spike #186

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion System.Maui.Core/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace System.Maui
{
[RenderWith(typeof(_EntryRenderer))]
public class Entry : InputView, IFontElement, ITextAlignmentElement, IEntryController, IElementConfiguration<Entry>
public partial class Entry : InputView, IFontElement, ITextAlignmentElement, IEntryController, IElementConfiguration<Entry>
{
public static readonly BindableProperty ReturnTypeProperty = BindableProperty.Create(nameof(ReturnType), typeof(ReturnType), typeof(Entry), ReturnType.Default);

Expand Down
2 changes: 1 addition & 1 deletion System.Maui.Core/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Maui
{
[ContentProperty("Text")]
[RenderWith(typeof(_LabelRenderer))]
public class Label : View, IFontElement, ITextElement, ITextAlignmentElement, ILineHeightElement, IElementConfiguration<Label>, IDecorableTextElement, IPaddingElement
public partial class Label : View, IFontElement, ITextElement, ITextAlignmentElement, ILineHeightElement, IElementConfiguration<Label>, IDecorableTextElement, IPaddingElement
{
public static readonly BindableProperty HorizontalTextAlignmentProperty = TextAlignmentElement.HorizontalTextAlignmentProperty;

Expand Down
13 changes: 13 additions & 0 deletions System.Maui.Core/Maui/IEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Maui
{
public partial class Entry : ITextInput
{
TextType IText.TextType => TextType.Text;

Color IText.Color => TextColor;
}
}
25 changes: 25 additions & 0 deletions System.Maui.Core/Maui/IFrameworkElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Maui
{
public interface IFrameworkElement
{
//bool IsEnabled { get; }
//Color BackgroundColor { get; }
//Rectangle Frame { get; }
//IViewRenderer Renderer { get; set; }
//IFrameworkElement Parent { get; }

//void Arrange(Rectangle bounds);
//SizeRequest Measure(double widthConstraint, double heightConstraint);

//SizeRequest DesiredSize { get; }
//bool IsMeasureValid { get; }
//bool IsArrangeValid { get; }

//void InvalidateMeasure();
//void InvalidateArrange();
}
}
13 changes: 13 additions & 0 deletions System.Maui.Core/Maui/ILabel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace System.Maui
{
public interface ILabel : IText
{
double LineHeight { get; }
FormattedString FormattedText { get; }
Font Font { get; }
Color TextColor { get; }
TextTransform TextTransform { get; }

string UpdateFormsText(string text, TextTransform textTransform);
}
}
11 changes: 11 additions & 0 deletions System.Maui.Core/Maui/IText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace System.Maui
{
public interface IText : IView
{
string Text { get; }

TextType TextType { get; }
//TODO: Add fonts and Colors
Color Color { get; }
}
}
14 changes: 14 additions & 0 deletions System.Maui.Core/Maui/ITextInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace System.Maui
{
public interface ITextInput : IText
{
int MaxLength { get; }
string Placeholder { get; }
Color PlaceholderColor { get; }
new string Text { get; set; }
TextTransform TextTransform { get; }

string UpdateFormsText(string text, TextTransform textTransform);
//string IText.Text => Text;
}
}
11 changes: 11 additions & 0 deletions System.Maui.Core/Maui/IView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace System.Maui
{
public interface IView : IFrameworkElement
{

//Alignment GetVerticalAlignment(ILayout layout) => Alignment.Fill;
//Alignment GetHorizontalAlignment(ILayout layout) => Alignment.Fill;
bool IsEnabled { get; }
Color BackgroundColor { get; }
}
}
15 changes: 15 additions & 0 deletions System.Maui.Core/Maui/IViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Maui.Core;

namespace System.Maui {
public interface IMauiRenderer
{
//void SetView (IFrameworkElement view);
//void UpdateValue (string property);
//void Remove (IFrameworkElement view);
object NativeView { get; }
//bool HasContainer { get; set; }
//SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint);
//void SetFrame (Rectangle frame);
}
}
11 changes: 11 additions & 0 deletions System.Maui.Core/Maui/Label.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Maui
{
public partial class Label : ILabel
{
Color IText.Color => TextColor;
}
}
2 changes: 2 additions & 0 deletions System.Maui.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
[assembly: InternalsVisibleTo("System.Maui.CarouselView")]
[assembly: InternalsVisibleTo("System.Maui.DualScreen")]
[assembly: InternalsVisibleTo("System.Maui.DualScreen.UnitTests")]


[assembly: Preserve]

[assembly: XmlnsDefinition("http://xamarin.com/schemas/2014/forms", "System.Maui")]
Expand Down
20 changes: 17 additions & 3 deletions System.Maui.Core/System.Maui.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.0</TargetFrameworks>
</PropertyGroup>
<MauiBuild>true</MauiBuild>
<TargetFrameworks>netstandard2.0;netstandard1.0</TargetFrameworks>
<!--<TargetFrameworks Condition=" '$(MauiBuild)' == 'true' ">netstandard2.1</TargetFrameworks>-->
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
</PropertyGroup>
<PropertyGroup Condition="'$(MauiBuild)' == 'true'">
<LangVersion>8.0</LangVersion>
<DefineConstants>$(DefineConstants);__MAUI__</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="$(MauiBuild) != true ">
<Compile Remove="Maui\**\*.cs" />
<None Include="Maui\**\*.Ios.cs" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\System.Maui.Platform\System.Maui.Platform.csproj" />
</ItemGroup>
Expand All @@ -14,6 +26,8 @@
<EmbeddedResource Remove="Internals\Legacy\**" />
<None Remove="Internals\Legacy\**" />
</ItemGroup>


<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.0' ">
<Compile Include="Internals\Legacy\**" />
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace System.Maui.Platform.Android.FastRenderers
{
public class ButtonRenderer : AppCompatButton,
public partial class ButtonRenderer : AppCompatButton,
IBorderVisualElementRenderer, IButtonLayoutRenderer, IVisualElementRenderer, IViewRenderer, ITabStop,
AView.IOnAttachStateChangeListener, AView.IOnFocusChangeListener, AView.IOnClickListener, AView.IOnTouchListener
{
Expand Down
37 changes: 23 additions & 14 deletions System.Maui.Platform.Android/FastRenderers/LabelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
using Android.Views;
using Android.Widget;
using AView = Android.Views.View;
#if !__MAUI__
using ILabel = global::System.Maui.Label;
#endif

namespace System.Maui.Platform.Android.FastRenderers
{
public class LabelRenderer : FormsTextView, IVisualElementRenderer, IViewRenderer, ITabStop
public partial class LabelRenderer : FormsTextView, IVisualElementRenderer, IViewRenderer, ITabStop
{
int? _defaultLabelFor;
bool _disposed;
Expand Down Expand Up @@ -390,45 +393,51 @@ void UpdateMaxLines()

void UpdateText()
{
UpdateText(Element, this);
}

internal static void UpdateText(ILabel Element, FormsTextView formsTextView)
{
var renderer = (LabelRenderer)formsTextView;
if (Element.FormattedText != null)
{
FormattedString formattedText = Element.FormattedText ?? Element.Text;
#pragma warning disable 618 // We will need to update this when .Font goes away
TextFormatted = _spannableString = formattedText.ToAttributed(Element.Font, Element.TextColor, this);
formsTextView.TextFormatted = renderer._spannableString = formattedText.ToAttributed(Element.Font, Element.TextColor, renderer);
#pragma warning restore 618
_wasFormatted = true;
renderer._wasFormatted = true;
}
else
{
if (_wasFormatted)
if (renderer._wasFormatted)
{
SetTextColor(_labelTextColorDefault);
_lastUpdateColor = Color.Default;
renderer.SetTextColor(renderer._labelTextColorDefault);
renderer._lastUpdateColor = Color.Default;
}

switch (Element.TextType)
{
case TextType.Html:
if (System.Maui.Maui.IsNougatOrNewer)
Control.SetText(Html.FromHtml(Element.Text ?? string.Empty, FromHtmlOptions.ModeCompact), BufferType.Spannable);
renderer.SetText(Html.FromHtml(Element.Text ?? string.Empty, FromHtmlOptions.ModeCompact), BufferType.Spannable);
else
#pragma warning disable CS0618 // Type or member is obsolete
Control.SetText(Html.FromHtml(Element.Text ?? string.Empty), BufferType.Spannable);
renderer.SetText(Html.FromHtml(Element.Text ?? string.Empty), BufferType.Spannable);
#pragma warning restore CS0618 // Type or member is obsolete
break;

default:
Text = Element.UpdateFormsText(Element.Text, Element.TextTransform);
renderer.Text = Element.UpdateFormsText(Element.Text, Element.TextTransform);
break;
}

UpdateColor();
UpdateFont();

_wasFormatted = false;
renderer.UpdateColor();
renderer.UpdateFont();

renderer._wasFormatted = false;
}

_lastSizeRequest = null;
renderer._lastSizeRequest = null;
}

void UpdateLineHeight()
Expand Down
75 changes: 75 additions & 0 deletions System.Maui.Platform.Android/Maui/AbstractViewRenderer.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.ComponentModel;
using System.Maui.Platform.Android;
using System.Runtime.Versioning;
using Android.Content;
using Android.Views;

namespace System.Maui.Platform
{
public partial class AbstractViewRenderer<TVirtualView, TNativeView> : IAndroidViewRenderer
{
public void SetContext (Context context) => Context = context;
public Context Context { get; private set; }

//public void SetFrame(Rectangle frame)
//{
// var nativeView = View;
// if (nativeView == null)
// return;

// if (frame.Width < 0 || frame.Height < 0)
// {
// // This is just some initial Forms value nonsense, nothing is actually laying out yet
// return;
// }

// var left = Context.ToPixels(frame.Left);
// var top = Context.ToPixels(frame.Top);
// var bottom = Context.ToPixels(frame.Bottom);
// var right = Context.ToPixels(frame.Right);
// var width = Context.ToPixels(frame.Width);
// var height = Context.ToPixels(frame.Height);

// if (nativeView.LayoutParameters == null)
// {
// nativeView.LayoutParameters = new ViewGroup.LayoutParams((int)width, (int)height);
// }
// else
// {
// nativeView.LayoutParameters.Width = (int)width;
// nativeView.LayoutParameters.Height = (int)height;
// }

// nativeView.Layout((int)left, (int)top, (int)right, (int)bottom);
//}

//public virtual SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
//{
// if (TypedNativeView == null)
// {
// return new SizeRequest(Size.Zero);
// }

// var deviceWidthConstraint = Context.ToPixels(widthConstraint);
// var deviceHeightConstraint = Context.ToPixels(heightConstraint);

// var widthSpec = MeasureSpecMode.AtMost.MakeMeasureSpec((int)deviceWidthConstraint);
// var heightSpec = MeasureSpecMode.AtMost.MakeMeasureSpec((int)deviceHeightConstraint);

// TypedNativeView.Measure(widthSpec, heightSpec);

// var deviceIndependentSize = Context.FromPixels(TypedNativeView.MeasuredWidth, TypedNativeView.MeasuredHeight);

// return new SizeRequest(deviceIndependentSize);
//}

//void SetupContainer () {
// ContainerView = new Core.Controls.ContainerView (this.Context) {
// MainView = this.TypedNativeView,
// };
//}

//void RemoveContainer () {
//}
}
}
Loading