Skip to content

Commit

Permalink
add framerate to native project
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuzy committed Mar 3, 2023
1 parent 621f515 commit a5ec59b
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 26 deletions.
71 changes: 68 additions & 3 deletions SharpConstraintLayout.Maui.Native.Example/MainPage.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
using Android.Graphics;
using Android.Views;
using Android.Widget;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Storage;
using SharpConstraintLayout.Maui.DebugTool;
using SharpConstraintLayout.Maui.Native.Example.Tool;
using SharpConstraintLayout.Maui.Widget;
using SharpConstraintLayout.Maui.Helper.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Orientation = SharpConstraintLayout.Maui.Widget.Orientation;

namespace SharpConstraintLayout.Maui.Native.Example
{
Expand All @@ -25,6 +23,14 @@ public MainPage(Context? context) : base(context)
Id = View.GenerateViewId();
this.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
this.SetBackgroundColor(Color.HotPink);
var textview = new TextView(context);
var fr = new BlogFrameRate.FrameRateCalculator();
fr.FrameRateUpdated += (value) =>
{
MainThread.BeginInvokeOnMainThread(() => textview.Text = value.Frames.ToString());
};
fr.Start();

var buttonList = new LinearLayout(context) { Orientation = Android.Widget.Orientation.Horizontal, LayoutParameters = new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent) };
var baseAlignBt = new Button(context) { Text = "BaseAlign" };
var baselineBt = new Button(context) { Text = "Baseline" };
Expand All @@ -36,11 +42,13 @@ public MainPage(Context? context) : base(context)
var constraintLayoutInScrollViewBt = new Button(context) { Text = "ConstraintLayoutInScrollView" };
var circleConstraintBt = new Button(context) { Text = "CircleConstraint" };
var flowPerformanceBt = new Button(context) { Text = "FlowPerformance" };
var flowOfAndroidPerformanceBt = new Button(context) { Text = "AndroidFlowPerformance" };
var wrapPanelPerformanceBt = new Button(context) { Text = "WrapPanelPerformance" };
var groupBt = new Button(context) { Text = "Group" };
var placeholderBt = new Button(context) { Text = "Placeholder" };
var sizeBt = new Button(context) { Text = "Size" };
var nestedConstraintLayoutBt = new Button(context) { Text = "NestedConstraintLayout" };
buttonList.AddView(textview);
buttonList.AddView(baseAlignBt);
buttonList.AddView(baselineBt);
buttonList.AddView(guidelineBt);
Expand All @@ -51,6 +59,7 @@ public MainPage(Context? context) : base(context)
buttonList.AddView(constraintLayoutInScrollViewBt);
buttonList.AddView(circleConstraintBt);
buttonList.AddView(flowPerformanceBt);
buttonList.AddView(flowOfAndroidPerformanceBt);
buttonList.AddView(wrapPanelPerformanceBt);
buttonList.AddView(groupBt);
buttonList.AddView(placeholderBt);
Expand Down Expand Up @@ -124,6 +133,11 @@ public MainPage(Context? context) : base(context)
layout.RemoveAllElements();
FlowPerformanceTest(layout);
};
flowOfAndroidPerformanceBt.Click += (sender, e) =>
{
layout.RemoveAllElements();
AndroidFlowPerformanceTest(layout);
};
wrapPanelPerformanceBt.Click += (sender, e) =>
{
layout.RemoveAllElements();
Expand Down Expand Up @@ -153,6 +167,57 @@ public MainPage(Context? context) : base(context)
//ConstraintLayoutInScrollViewTest(this);
}

private void AndroidFlowPerformanceTest(ConstraintLayout page)
{
var platformLayout = new AndroidX.ConstraintLayout.Widget.ConstraintLayout(page.Context);
page.AddElement(platformLayout);
using (var layoutSet = new FluentConstraintSet())
{
layoutSet.Clone(page);
layoutSet
.Select(platformLayout)
.TopToTop().BottomToBottom().LeftToLeft()
.Width(SizeBehavier.MatchParent)
.Height(SizeBehavier.MatchParent);
layoutSet.ApplyTo(page);
}

(var FirstButton, var SecondButton, var ThirdCanvas, var FouthTextBlock, var FifthTextBox, var SixthRichTextBlock) = CreateControls();

int buttonCount = 50;
var flow = new AndroidX.ConstraintLayout.Helper.Widget.Flow(page.Context) { };
flow.Id = View.GenerateViewId();
flow.SetOrientation(AndroidX.ConstraintLayout.Helper.Widget.Flow.Horizontal);
flow.SetWrapMode(AndroidX.ConstraintLayout.Helper.Widget.Flow.WrapChain);
flow.SetHorizontalStyle(AndroidX.ConstraintLayout.Helper.Widget.Flow.ChainPacked);
platformLayout.AddView(flow);

platformLayout.AddView(FifthTextBox);
//Generate 1000 Button,all add to page
var buttonList = new List<Button>();
List<int> ids = new List<int>();
ids.Add(FifthTextBox.Id);
for (int i = 0; i < buttonCount; i++)
{
var button = new Button(page.Context);
button.Id = View.GenerateViewId();
button.Text = "Button" + i;
buttonList.Add(button);
platformLayout.AddView(button);
ids.Add(button.Id);
}
flow.SetReferencedIds(ids.ToArray());
var set = new AndroidX.ConstraintLayout.Widget.ConstraintSet();
set.Clone(platformLayout);
set.Connect(flow.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Left, platformLayout.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Left);
set.Connect(flow.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Top, platformLayout.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Top);
set.Connect(flow.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Right, platformLayout.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Right);
set.Connect(flow.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Bottom, platformLayout.Id, AndroidX.ConstraintLayout.Widget.ConstraintSet.Bottom);
set.ConstrainWidth(flow.Id, AndroidX.ConstraintLayout.Widget.ConstraintLayout.LayoutParams.MatchConstraint);
set.ConstrainWidth(flow.Id, AndroidX.ConstraintLayout.Widget.ConstraintLayout.LayoutParams.MatchConstraint);
set.ApplyTo(platformLayout);
}

void ConstraintLayoutInScrollViewTest(ConstraintLayout page)
{
(Button FirstButton, Button SecondButton, View ThirdCanvas, TextView FouthTextBlock, EditText FifthTextBox, TextView SixthRichTextBlock) = CreateControls();
Expand Down
40 changes: 28 additions & 12 deletions SharpConstraintLayout.Maui.Native.Example/MainPage.MaciOS.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
using CoreGraphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIKit;
using SharpConstraintLayout.Maui.Widget;
using Foundation;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Storage;
using SharpConstraintLayout.Maui.DebugTool;
using SharpConstraintLayout.Maui.Native.Example.Tool;
using SharpConstraintLayout.Maui.Widget;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using UIKit;

namespace SharpConstraintLayout.Maui.Native.Example
{
public partial class MainPage
{
class Log : ILogger
{
public IDisposable BeginScope<TState>(TState state)
{
throw new NotImplementedException();
}

public bool IsEnabled(LogLevel logLevel)
{
throw new NotImplementedException();
}

void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
Debug.WriteLine(formatter.Invoke(state, exception));
}
}

public ConstraintLayout Page;

public MainPage(CGRect frame)
{
ConstraintLayout.DEBUG = false;
ConstraintLayout.MEASURE_MEASURELAYOUT = true;
//ConstraintLayout.MEASUREEVERYWIDGET = true;
//ConstraintLayout.MEASUREEVERYCHILD = true;
ConstraintLayout.MEASUREEVERYCHILD = true;

Page = new ConstraintLayout()
Page = new ConstraintLayout(new Log())
{
AutosizesSubviews = true,
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight,
BackgroundColor = UIColor.SystemGray
BackgroundColor = UIColor.Red
};
var buttonList = new UIStackView();
var baseAlignBt = new UIButton(); baseAlignBt.SetTitle("BaseAlign", UIControlState.Normal);
Expand Down Expand Up @@ -301,4 +317,4 @@ public UIView GetPage()
return Page;
}
}
}
}
12 changes: 11 additions & 1 deletion SharpConstraintLayout.Maui.Native.Example/MainPage.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Maui.Storage;
using BlogFrameRate;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Storage;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -26,6 +28,13 @@ public MainPage()
ConstraintLayout.MEASURE_MEASURELAYOUT = true;
//ConstraintLayout.MEASUREEVERYWIDGET = true;
//ConstraintLayout.MEASUREEVERYCHILD = true;
var textview = new TextBlock();
var fr = new FrameRateCalculator();
fr.FrameRateUpdated += (value) =>
{
MainThread.BeginInvokeOnMainThread(() => textview.Text = value.Frames.ToString());
};
fr.Start();

this.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Colors.HotPink);
var buttonList = new StackPanel() { Orientation = Microsoft.UI.Xaml.Controls.Orientation.Horizontal };
Expand All @@ -45,6 +54,7 @@ public MainPage()
var placeholderBt = new Button() { Content = "Placeholder" };
var sizeBt = new Button() { Content = "Size" };
var nestedConstraintLayoutBt = new Button() { Content = "NestedConstraintLayout" };
buttonList.Children.Add(textview);
buttonList.Children.Add(baseAlignBt);
buttonList.Children.Add(baselineBt);
buttonList.Children.Add(guidelineBt);
Expand Down
18 changes: 17 additions & 1 deletion SharpConstraintLayout.Maui.Native.Example/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,24 @@ void GroupTest(ConstraintLayout page)
};
}

void FlowPerformanceTest(ConstraintLayout page)
void FlowPerformanceTest(ConstraintLayout rootPage)
{
#if ANDROID
var page = new ConstraintLayout(rootPage.Context);
#else
var page = new ConstraintLayout();
#endif
rootPage.AddElement(page);
using(var rootSet = new FluentConstraintSet())
{
rootSet.Clone(rootPage);
rootSet.Select(page)
.Width(SizeBehavier.MatchParent)
.Height(SizeBehavier.MatchParent);
rootSet.ApplyTo(rootPage);
}
page.ConstrainWidth = (int)SizeBehavier.MatchParent;
page.ConstrainHeight = (int)SizeBehavier.MatchParent;
(var FirstButton, var SecondButton, var ThirdCanvas, var FouthTextBlock, var FifthTextBox, var SixthRichTextBlock) = CreateControls();

int buttonCount = 50;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">

</ItemGroup>
<ItemGroup>
<Compile Include="..\SharpConstraintLayout.Maui.Example\Tool\FrameRateCalculator.Android.cs" Link="Tool\FrameRateCalculator.Android.cs" />
<Compile Include="..\SharpConstraintLayout.Maui.Example\Tool\FrameRateCalculator.iOS.cs" Link="Tool\FrameRateCalculator.iOS.cs" />
<Compile Include="..\SharpConstraintLayout.Maui.Example\Tool\FrameRateCalculator.Windows.cs" Link="Tool\FrameRateCalculator.Windows.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpConstraintLayout.Maui.Native\SharpConstraintLayout.Maui.Native.csproj" />
Expand Down
18 changes: 9 additions & 9 deletions SharpConstraintLayout.Maui/Widget/UIElementExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
using UIKit;
using CoreGraphics;
#elif __ANDROID__
using UIElement = Android.Views.View;
using FrameworkElement = Android.Views.View;
using AndroidX.ConstraintLayout.Widget;
using Android.Views;
using AndroidX.ConstraintLayout.Widget;
using FrameworkElement = Android.Views.View;
using UIElement = Android.Views.View;
#endif
namespace SharpConstraintLayout.Maui.Widget
{
Expand Down Expand Up @@ -155,14 +155,14 @@ platformView is UIKit.UITextView ||
/// <returns></returns>
public static int GetId(this UIElement element)
{
//#if __ANDROID__
// if (view.Id == UIElement.NoId)
// view.Id = UIElement.GenerateViewId();
// return view.Id;
//#else
#if __ANDROID__ && !__MAUI__
if (element.Id == UIElement.NoId)
element.Id = UIElement.GenerateViewId();
return element.Id;
#endif
#if __MAUI__
if (element.StyleId == null)
return element.GetHashCode();
return element.GetHashCode();
return element.StyleId.GetHashCode();
#else
return element.GetHashCode();
Expand Down

0 comments on commit a5ec59b

Please sign in to comment.