Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AspectRatioLayout.cs
1using System;
2using System.ComponentModel;
3using Microsoft.Maui.Controls;
4using Microsoft.Maui.Layouts;
5
7{
8
9 public class AspectRatioLayoutManager : LayoutManager
10 {
11 public AspectRatioLayoutManager(AspectRatioLayout layout) : base(layout)
12 {
13 }
14
15 public new AspectRatioLayout Layout => (AspectRatioLayout)base.Layout;
16
17 public override Size Measure(double widthConstraint, double heightConstraint)
18 {
19 double Aspect = this.Layout.AspectRatio;
20 Thickness Padding = this.Layout.Padding;
21 double AvailableWidth = Math.Max(0, widthConstraint - Padding.HorizontalThickness);
22 double AvailableHeight = Math.Max(0, heightConstraint - Padding.VerticalThickness);
23
24 double Width = AvailableWidth;
25 double Height = Width / Aspect;
26
27 if (AvailableHeight > 0 && Height > AvailableHeight)
28 {
29 Height = AvailableHeight;
30 Width = Height * Aspect;
31 }
32
33 foreach (IView Child in this.Layout)
34 {
35 Child.Measure(Width, Height);
36 }
37
38 return new Size(Width + Padding.HorizontalThickness, Height + Padding.VerticalThickness);
39 }
40
41 public override Size ArrangeChildren(Rect bounds)
42 {
43 Thickness Padding = this.Layout.Padding;
44 double ChildX = Padding.Left;
45 double ChildY = Padding.Top;
46 double ChildWidth = Math.Max(0, bounds.Width - Padding.HorizontalThickness);
47 double ChildHeight = Math.Max(0, bounds.Height - Padding.VerticalThickness);
48
49 foreach (IView Child in this.Layout)
50 {
51 Child.Arrange(new Rect(ChildX, ChildY, ChildWidth, ChildHeight));
52 }
53
54 return bounds.Size;
55 }
56 }
57 public class AspectRatioLayout : Layout
58 {
59 public static readonly BindableProperty AspectRatioProperty =
60 BindableProperty.Create(
61 nameof(AspectRatio),
62 typeof(double),
63 typeof(AspectRatioLayout),
64 1.0,
65 propertyChanged: OnAspectRatioChanged);
66
67 [TypeConverter(typeof(Converters.AspectRatioTypeConverter))]
68 public double AspectRatio
69 {
70 get { return (double)this.GetValue(AspectRatioProperty); }
71 set { this.SetValue(AspectRatioProperty, value); }
72 }
73
74 private static void OnAspectRatioChanged(BindableObject bindable, object oldValue, object newValue)
75 {
76 if (bindable is AspectRatioLayout AspectRatioView)
77 {
78 AspectRatioView.InvalidateMeasure();
79 }
80 }
81
82 protected override ILayoutManager CreateLayoutManager()
83 {
84 return new AspectRatioLayoutManager(this);
85 }
86 }
87}