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

[C, iOS, Android] LayoutCompression #1136

Merged
merged 4 commits into from Sep 13, 2017
Merged

Conversation

StephaneDelcroix
Copy link
Member

@StephaneDelcroix StephaneDelcroix commented Sep 12, 2017

Description of Change

Enable Layout Compression for iOS and Android.

NOTE: this feature is still in beta, and opt-in only

Layout Compression allows the user to avoid creating renderers for Layouts (i.e. StackLayout, ContentView, ...).

Pros:

  • less renderers to create, manage and dispose
  • less UI elements on screen

Cons:

  • compressed layouts can't have BG colors, gesture recognizers
  • no transformation
  • ...

Sample:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	x:Class="Xamarin.Forms.Controls.CompressedLayout"
	Padding="0,20,0,0">
	<StackLayout Padding="6" CompressedLayout.IsHeadless="true">
		<Label Text="First"/>
		<StackLayout Padding="6" CompressedLayout.IsHeadless="true">
			<Label Text="Second"/>
			<ContentView Padding="6" CompressedLayout.IsHeadless="true" BackgroundColor="Pink" >
				<Label x:Name="this" Text="Third"/>
			</ContentView>
			<Label Text="Fourth"/>
		</StackLayout>
		<Label Text="Fifth"/>
	</StackLayout>
</ContentPage>

Enabling LayoutCompression for this layout only creates a Renderer for the page, and one for each label. The BackgroundColor property on the inner ContentView is ignored.

Bugs Fixed

/

API Changes

Added:

public static readonly BindableProperty CompressedLayout.IsHeadlessProperty;

Behavioral Changes

/

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard
  • Consolidate commits as makes sense

@@ -34,7 +34,9 @@ public static NumberKeyListener Create(InputTypes inputTypes)
if ((inputTypes & InputTypes.NumberFlagDecimal) == 0)
{
// If decimal isn't allowed, we can just use the Android version
#pragma warning disable 0618 // Find issues with Android API usage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part of the next line is giving an obsolete warning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On X.A 7.5.0.15 (Alpha channel at this time): 'DigitsKeyListener.GetInstance(bool, bool)' is obsolete

[Register ("getInstance", "(ZZ)Landroid/text/method/DigitsKeyListener;", ""), Obsolete ("deprecated")]
public unsafe static DigitsKeyListener GetInstance (bool sign, bool @decimal);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in master

public VisualElementPackager(IVisualElementRenderer renderer)
VisualElement _element;

IElementController ElementController => _element;// _renderer.Element as IElementController;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably remove this comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (CompressedLayout.GetIsHeadless(child)) {
child.IsPlatformEnabled = true;
FillChildrenWithRenderers(child);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a tab on the next line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -13,16 +13,20 @@ public class VisualElementPackager : IDisposable

bool _isDisposed;

IElementController ElementController => Renderer.Element as IElementController;
IElementController ElementController => _element;// Renderer.Element as IElementController;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably remove this comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

@samhouts samhouts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elegant!

public VisualElementPackager(IVisualElementRenderer renderer)
VisualElement _element;

IElementController ElementController => _element;// _renderer.Element as IElementController;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -237,6 +237,13 @@ protected void UpdateChildrenLayout()
double w = Math.Max(0, width - Padding.HorizontalThickness);
double h = Math.Max(0, height - Padding.VerticalThickness);

var isHeadless = CompressedLayout.GetIsHeadless(this);
var headlessOffset = CompressedLayout.GetHeadlessOffset(this);
for (var i = 0; i < LogicalChildrenInternal.Count; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces on their own lines. You're in Core territory, now! muwahaha

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't scratch that itch! :)

fixed by removing the braces.

if (_childViews == null)
_childViews = new List<IVisualElementRenderer>();
if (CompressedLayout.GetIsHeadless(view)) {
var _packager = new VisualElementPackager(_renderer, view);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the underscore in the variable name is a little confusing here, I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. at some point that variable was a field...

@adrianknight89
Copy link
Contributor

compressed layouts can't have BG colors, gesture recognizers
no transformation

Are these limitations permanent or to be fixed in the future?

<ContentView Padding="6" CompressedLayout.IsHeadless="true" BackgroundColor="Pink" >

Should there be a compile-time / run-time error for this instead of silent fail of the bgcolor set? The reason I ask this is I don't like it when people pollute their visual tree with redundant stuff. If bgcolor has no use there, then it shouldn't be there?

@StephaneDelcroix
Copy link
Member Author

@adrianknight89 at this point we have no plan for supporting BgColor on headless layouts, nor do we have plans for validating that case

@StephaneDelcroix StephaneDelcroix merged commit 8ee62e8 into master Sep 13, 2017
@StephaneDelcroix StephaneDelcroix deleted the LayoutCompression branch September 13, 2017 09:19
@alexsorokoletov
Copy link

@StephaneDelcroix thank you for this PR and this feature. Wondering, where can one read about limitations and why they are there. Maybe it's just me can't find the information

@bentmar
Copy link
Contributor

bentmar commented Oct 23, 2017

Can we please have some more information on how to use this?

@davidortinau
Copy link
Contributor

@bentmar I'll have a blog out shortly with more samples, and then docs to come. Really any layout that you don't need a background on, doesn't transform, and doesn't accept gestures is a candidate. So just add CompressedLayout.IsHeadless="true" to it. I suspect we may discover some other cons as we test further. So far the reduction of renderers created is looking good, especially if you have lots of layout nesting. In my page I saw nearly a 50% reduction, though granted it was a poorly constructed page in many respects to begin with.

@BlueRaja
Copy link

Seems like this should be on by default, and only disabled when there's a background/transform/gesture/explicitly disabled?

@samhouts samhouts added this to the 2.5.0 milestone May 5, 2018
@samhouts samhouts modified the milestones: 2.5.0, 2.3.0 Jun 27, 2018
@samhouts samhouts modified the milestones: 2.5.0, 2.3.0 Aug 23, 2019
mattleibow pushed a commit that referenced this pull request Jan 28, 2021
mattleibow pushed a commit that referenced this pull request Jan 28, 2021
* Implement vertical accuracy in GeoLocation API (#1103)

* Location: add property 'VerticalAccuracy' (#1099)

* vertical accuracy is only available in Android API level 26 and above (#1099)

* update Samples app to show vertical accuracy on GeolocationPage

* add missing documentation bits for Location.VerticalAccuracy

* .gitattributes: get better diff context for C# code (#1115)

* Location.VerticalAccuracy: add runtime check for Android version (#1099) (#1116)

* the compile-time check is not enough
* it crashed on old Android versions (<8.0)

* GH-1102 Fix launcher on older devices (#1120)

* Update Launcher.ios.tvos.cs

* OpenUrlAsync was introduced in iOS 10 not 12

https://docs.microsoft.com/en-us/dotnet/api/uikit.uiapplication.openurlasync?view=xamarin-ios-sdk-12

* Fix trailing whitespace

* Really? fix whitespace

* Really! Fix whitespace

* Fixes #1129 - set empty required declarations on UWP (#1133)

* Fixes #1129 - set empty required declarations on UWP

* Update Permissions.uwp.cs

* Update Xamarin.Essentials.csproj (#1136)

* GH-1142 AccessBackgroundLocation only when compile & running Q (#1143)

* AccessBackgroundLocation only when compile & running Q

* put if compile check around permission

* GH-1121 If VC is null use TraitCollection (#1144)

* Fixes #1123 (#1126)

* Update PlacemarkExtensions.xml (#1132)

Co-authored-by: Janus Weil <janus@gcc.gnu.org>
Co-authored-by: James Montemagno <james.montemagno@gmail.com>
Co-authored-by: Michael <Michael@ZPF.fr>
mattleibow pushed a commit that referenced this pull request Jan 28, 2021
* Implement vertical accuracy in GeoLocation API (#1103)

* Location: add property 'VerticalAccuracy' (#1099)

* vertical accuracy is only available in Android API level 26 and above (#1099)

* update Samples app to show vertical accuracy on GeolocationPage

* add missing documentation bits for Location.VerticalAccuracy

* .gitattributes: get better diff context for C# code (#1115)

* Location.VerticalAccuracy: add runtime check for Android version (#1099) (#1116)

* the compile-time check is not enough
* it crashed on old Android versions (<8.0)

* GH-1102 Fix launcher on older devices (#1120)

* Update Launcher.ios.tvos.cs

* OpenUrlAsync was introduced in iOS 10 not 12

https://docs.microsoft.com/en-us/dotnet/api/uikit.uiapplication.openurlasync?view=xamarin-ios-sdk-12

* Fix trailing whitespace

* Really? fix whitespace

* Really! Fix whitespace

* Fixes #1129 - set empty required declarations on UWP (#1133)

* Fixes #1129 - set empty required declarations on UWP

* Update Permissions.uwp.cs

* Update Xamarin.Essentials.csproj (#1136)

* GH-1142 AccessBackgroundLocation only when compile & running Q (#1143)

* AccessBackgroundLocation only when compile & running Q

* put if compile check around permission

* GH-1121 If VC is null use TraitCollection (#1144)

* Fixes #1123 (#1126)

* Update PlacemarkExtensions.xml (#1132)

Co-authored-by: Janus Weil <janus@gcc.gnu.org>
Co-authored-by: James Montemagno <james.montemagno@gmail.com>
Co-authored-by: Michael <Michael@ZPF.fr>

Co-authored-by: Jonathan Dick <jodick@microsoft.com>
Co-authored-by: Janus Weil <janus@gcc.gnu.org>
Co-authored-by: Michael <Michael@ZPF.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants