question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Absolute layout LayoutBounds and LayoutFlag extension

See original GitHub issue

Absolute layout LayoutBounds and LayoutFlag markup extension

  • Proposed
  • Prototype
  • Implementation
  • Unit Tests
  • Documentation
  • Sample

Link to Discussion

https://github.com/CommunityToolkit/Maui.Markup/discussions/22

Summary

I propose a ViewInAbsoluteLayoutExtensions static class, where LayoutBounds and LayoutFlag static methods (and their overloads) are implemented.

In most cases when using AbsoluteLayout, every child view will need to define two attached properties, LayoutBounds and LayoutFlag, where LayoutBounds is a Rectangle struct and LayoutFlag is an AbsoluteLayoutFlags Enum. Thus, the ViewInAbsoluteLayoutExtensions will offer the child View the ability to define these two attached properties declaratively.

Example:

BoxView box= new BoxView
{
   Color = Colors.Red,
}.LayoutBounds(0.5, 0.5, 100, 100)
.LayoutFlags(AbsoluteLayoutFlags.PositionProportional);

Motivation

Currently In MCT Markup, there are no declarative way to define LayoutBounds and LayoutFlag (like Row and Column for child Views in Grid)

Detailed Design

The ViewInAbsoluteLayoutEx tensions static class should at the very least include 4 extension methods to cover the 4 basic use cases,

  • setting AbsoluteLayoutFlags
public static TView LayoutFlags<TView>(this TView view, AbsoluteLayoutFlags flag) where TView : View
{
    view.SetValue(AbsoluteLayout.LayoutFlagsProperty, flag);
    return view;
}
  • setting position only LayoutBounds value (Not sure if this implementation is correct)
public static TView LayoutBounds<TView>(this TView view, Point point) where TView : View
{
    view.SetValue(AbsoluteLayout.LayoutBoundsProperty, point);
    return view;
}
  • setting position and size LayoutBounds value
public static TView LayoutBounds<TView>(this TView view, Rectangle rectangle) where TView : View
{
    view.SetValue(AbsoluteLayout.LayoutBoundsProperty, rectangle);
    return view;
}

Additionally, LayoutBounds method should have overloads mostly for the purpose of ease of use. Allowing the user to input in x,y,width,height directly, instead of encapsulating these values in a Rectangle struct.

public static TView LayoutBounds<TView>(this TView view, int x, int y) where TView : View
public static TView LayoutBounds<TView,TCoord>(this TView view, TCoord x, TCoord y) where TView : View where TCoord : Enum
public static TView LayoutBounds<TView>(this TView view, int x, int y, int width , int height) where TView : View
public static TView LayoutBounds<TView, TCoord>(this TView view, TCoord x, TCoord y, TCoord width, TCoord height) where TView : View where TCoord : Enum
public static TView LayoutBounds<TView>(this TView view, Point point, Size size) where TView : View

There also needs to be a private ToDouble helper method in this class, as double is expected input type for Rectangle struct

static double ToDouble(this Enum enumValue) => Convert.ToDouble(enumValue, CultureInfo.InvariantCulture);

Drawbacks

None that i can think of.

Alternatives

None that i can think of.

Unresolved Questions

  • For child View objects that need to use AbsoluteValue.AutoSize, the implementation still needs more testing.
  • Sometimes if the user only wants to change the position or the size of the child View, should they be given the option to do that with one of the overloads?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
danielftzcommented, Mar 16, 2022

Thank you guys. Yes, I will open a pull request.

1reaction
brminnickcommented, Mar 16, 2022

Congrats @danielftz! This Proposal has officially been approved and can be implemented!

Would you like to also do the implementation and open a Pull Request?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AbsoluteLayout extensions - .NET MAUI Community Toolkit
The LayoutFlags extension method allows you to set a flag that indicates that the layout bounds position and size values for a child...
Read more >
absolute-layout-extensions.md
The LayoutFlags extension method allows you to set a flag that indicates that the layout bounds position and size values for a child...
Read more >
AbsoluteLayout | Android Developers
Per-child layout information associated with AbsoluteLayout. ... Flag requesting you to add views that are marked as not important for autofill (see ...
Read more >
Xamarin Forms - AbsoluteLayout - How does works positions
With AbsoluteLayoutFlag.All, the Rectangle bounds parameters have the following meaning: x means the percentage of the remaining space (i.e ...
Read more >
AbsoluteLayout in Xamarin Forms Made Simple
Layout Flags. Indicates how values specified in the layout bounds will be interpreted. It can be Absolute or Proportional. Proportional: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found