Measure Forms elements

This commit is contained in:
Frank A. Krueger 2017-11-09 17:23:41 -08:00
parent 36367a3158
commit a4a940795e
7 changed files with 61 additions and 19 deletions

View File

@ -8,8 +8,14 @@ namespace Ooui.Forms.Extensions
public static SizeRequest GetSizeRequest (this Ooui.Element self, double widthConstraint, double heightConstraint,
double minimumWidth = -1, double minimumHeight = -1)
{
var request = new Size (double.PositiveInfinity, double.PositiveInfinity);
var minimum = new Size (double.PositiveInfinity, double.PositiveInfinity);
Size s = self.Text.MeasureSize (self.Style);
var request = new Size (
double.IsPositiveInfinity (s.Width) ? double.PositiveInfinity : s.Width,
double.IsPositiveInfinity (s.Height) ? double.PositiveInfinity : s.Height);
var minimum = new Size (minimumWidth < 0 ? request.Width : minimumWidth,
minimumHeight < 0 ? request.Height : minimumHeight);
return new SizeRequest (request, minimum);
}
}

View File

@ -21,5 +21,18 @@ namespace Ooui.Forms.Extensions
return "end";
}
}
public static Size MeasureSize (this string text, Style style)
{
if (string.IsNullOrEmpty (text))
return Size.Zero;
var fontHeight = 16.0;
var charWidth = 8.0;
var width = text.Length * charWidth;
return new Size (width, fontHeight);
}
}
}

View File

@ -78,7 +78,19 @@ namespace Xamarin.Forms
public double GetNamedSize (NamedSize size, Type targetElementType, bool useOldSizes)
{
throw new NotImplementedException ();
switch (size) {
default:
case NamedSize.Default:
return 16;
case NamedSize.Micro:
return 9;
case NamedSize.Small:
return 12;
case NamedSize.Medium:
return 22;
case NamedSize.Large:
return 32;
}
}
public Task<Stream> GetStreamAsync (Uri uri, CancellationToken cancellationToken)

View File

@ -14,5 +14,7 @@ namespace Ooui.Forms
void SetElement (VisualElement element);
void SetElementSize (Size size);
SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint);
}
}

View File

@ -70,7 +70,11 @@ namespace Ooui.Forms
public SizeRequest GetNativeSize (VisualElement view, double widthConstraint, double heightConstraint)
{
return new SizeRequest (new Size (100, 100));
var renderView = GetRenderer (view);
if (renderView == null || renderView.NativeView == null)
return new SizeRequest (Size.Zero);
return renderView.GetDesiredSize (widthConstraint, heightConstraint);
}
public void SetPage (Page newRoot)

View File

@ -17,7 +17,7 @@ namespace Ooui.Forms.Renderers
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
if (!_perfectSizeValid) {
_perfectSize = GetPerfectSize ();
_perfectSize = base.GetDesiredSize (double.PositiveInfinity, double.PositiveInfinity);
_perfectSize.Minimum = new Size (Math.Min (10, _perfectSize.Request.Width), _perfectSize.Request.Height);
_perfectSizeValid = true;
}
@ -50,11 +50,6 @@ namespace Ooui.Forms.Renderers
return result;
}
SizeRequest GetPerfectSize ()
{
return new SizeRequest (new Size (100, 22));
}
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Label> e)
{
if (e.NewElement != null) {

View File

@ -6,35 +6,45 @@ namespace Samples
{
public class XamarinFormsSample
{
public void Publish ()
Page MakePage ()
{
Forms.Init ();
var countLabel = new Label {
Text = "0",
BackgroundColor = Color.Gold,
HorizontalOptions = LayoutOptions.Center,
};
var countButton = new Button {
Text = "Increase",
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
countButton.Clicked += (sender, e) => {
var v = int.Parse (countLabel.Text);
countLabel.Text = (v + 1).ToString ();
};
var page = new ContentPage {
Content = new StackLayout {
return new ContentPage {
Content = new StackLayout {
BackgroundColor = Color.Khaki,
Children = {
new Label { Text = "Hello World!" },
Children = {
new Label {
Text = "Hello World!",
FontSize = 32,
HorizontalOptions = LayoutOptions.Center,
},
countLabel,
countButton,
},
},
};
}
page.Publish ("/xamarin-forms");
page.PublishShared ("/xamarin-forms-shared");
}
public void Publish ()
{
var page = MakePage ();
page.Publish ("/xamarin-forms-shared");
Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().CreateElement ());
}
}
}