Measure Forms elements
This commit is contained in:
parent
36367a3158
commit
a4a940795e
|
@ -8,8 +8,14 @@ namespace Ooui.Forms.Extensions
|
||||||
public static SizeRequest GetSizeRequest (this Ooui.Element self, double widthConstraint, double heightConstraint,
|
public static SizeRequest GetSizeRequest (this Ooui.Element self, double widthConstraint, double heightConstraint,
|
||||||
double minimumWidth = -1, double minimumHeight = -1)
|
double minimumWidth = -1, double minimumHeight = -1)
|
||||||
{
|
{
|
||||||
var request = new Size (double.PositiveInfinity, double.PositiveInfinity);
|
Size s = self.Text.MeasureSize (self.Style);
|
||||||
var minimum = new Size (double.PositiveInfinity, double.PositiveInfinity);
|
|
||||||
|
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);
|
return new SizeRequest (request, minimum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,5 +21,18 @@ namespace Ooui.Forms.Extensions
|
||||||
return "end";
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,7 +78,19 @@ namespace Xamarin.Forms
|
||||||
|
|
||||||
public double GetNamedSize (NamedSize size, Type targetElementType, bool useOldSizes)
|
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)
|
public Task<Stream> GetStreamAsync (Uri uri, CancellationToken cancellationToken)
|
||||||
|
|
|
@ -14,5 +14,7 @@ namespace Ooui.Forms
|
||||||
void SetElement (VisualElement element);
|
void SetElement (VisualElement element);
|
||||||
|
|
||||||
void SetElementSize (Size size);
|
void SetElementSize (Size size);
|
||||||
|
|
||||||
|
SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,11 @@ namespace Ooui.Forms
|
||||||
|
|
||||||
public SizeRequest GetNativeSize (VisualElement view, double widthConstraint, double heightConstraint)
|
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)
|
public void SetPage (Page newRoot)
|
||||||
|
|
|
@ -17,7 +17,7 @@ namespace Ooui.Forms.Renderers
|
||||||
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
|
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
|
||||||
{
|
{
|
||||||
if (!_perfectSizeValid) {
|
if (!_perfectSizeValid) {
|
||||||
_perfectSize = GetPerfectSize ();
|
_perfectSize = base.GetDesiredSize (double.PositiveInfinity, double.PositiveInfinity);
|
||||||
_perfectSize.Minimum = new Size (Math.Min (10, _perfectSize.Request.Width), _perfectSize.Request.Height);
|
_perfectSize.Minimum = new Size (Math.Min (10, _perfectSize.Request.Width), _perfectSize.Request.Height);
|
||||||
_perfectSizeValid = true;
|
_perfectSizeValid = true;
|
||||||
}
|
}
|
||||||
|
@ -50,11 +50,6 @@ namespace Ooui.Forms.Renderers
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
SizeRequest GetPerfectSize ()
|
|
||||||
{
|
|
||||||
return new SizeRequest (new Size (100, 22));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Label> e)
|
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Label> e)
|
||||||
{
|
{
|
||||||
if (e.NewElement != null) {
|
if (e.NewElement != null) {
|
||||||
|
|
|
@ -6,35 +6,45 @@ namespace Samples
|
||||||
{
|
{
|
||||||
public class XamarinFormsSample
|
public class XamarinFormsSample
|
||||||
{
|
{
|
||||||
public void Publish ()
|
Page MakePage ()
|
||||||
{
|
{
|
||||||
Forms.Init ();
|
Forms.Init ();
|
||||||
|
|
||||||
var countLabel = new Label {
|
var countLabel = new Label {
|
||||||
Text = "0",
|
Text = "0",
|
||||||
BackgroundColor = Color.Gold,
|
BackgroundColor = Color.Gold,
|
||||||
|
HorizontalOptions = LayoutOptions.Center,
|
||||||
};
|
};
|
||||||
var countButton = new Button {
|
var countButton = new Button {
|
||||||
Text = "Increase",
|
Text = "Increase",
|
||||||
VerticalOptions = LayoutOptions.FillAndExpand,
|
HorizontalOptions = LayoutOptions.FillAndExpand,
|
||||||
};
|
};
|
||||||
countButton.Clicked += (sender, e) => {
|
countButton.Clicked += (sender, e) => {
|
||||||
var v = int.Parse (countLabel.Text);
|
var v = int.Parse (countLabel.Text);
|
||||||
countLabel.Text = (v + 1).ToString ();
|
countLabel.Text = (v + 1).ToString ();
|
||||||
};
|
};
|
||||||
var page = new ContentPage {
|
return new ContentPage {
|
||||||
Content = new StackLayout {
|
Content = new StackLayout {
|
||||||
BackgroundColor = Color.Khaki,
|
BackgroundColor = Color.Khaki,
|
||||||
Children = {
|
Children = {
|
||||||
new Label { Text = "Hello World!" },
|
new Label {
|
||||||
|
Text = "Hello World!",
|
||||||
|
FontSize = 32,
|
||||||
|
HorizontalOptions = LayoutOptions.Center,
|
||||||
|
},
|
||||||
countLabel,
|
countLabel,
|
||||||
countButton,
|
countButton,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
page.Publish ("/xamarin-forms");
|
public void Publish ()
|
||||||
page.PublishShared ("/xamarin-forms-shared");
|
{
|
||||||
}
|
var page = MakePage ();
|
||||||
|
page.Publish ("/xamarin-forms-shared");
|
||||||
|
|
||||||
|
Ooui.UI.Publish ("/xamarin-forms", () => MakePage ().CreateElement ());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue