Helpers - Chase-William/LilWidgets GitHub Wiki
This page contains the documentation for all the helpers that are used internally by the library. These helpers reduce code redundancy by providing small re-usable and testable classes.
ViewRect
ViewRect
provides information about the relationship between a given width and height.
Public Propertie(s)
Name | Description |
---|---|
LimitingSpan | Indicates whether the width and height are equal in length or which is the smaller of the two. |
LimitingSpanLength | Indicates the Length of the limiting span given in the Update method parameters. |
Public Constructor(s)
Name | Parameter(s) | Description |
---|---|---|
ViewRect | Initializes a new instance of the ViewRect class. |
|
ViewRect | width, height | Initializes a new instance of the ViewRect class and passes the given parameters to the Update method for invocation. |
Public Function(s)
Name | Parameter(s) | Return | Description |
---|---|---|---|
Update | width, height | void | Determines the limiting span and updates the LimitingSpan property. Also assigns the length of the limiting span to the LimitingSpanLength property. It is recommended that this function be called in OnSizeAllocated or SizeChanged. |
Example Use Case
For example, you are drawing a circle inside a rectangle. You need the circle to only be as big as the smallest span (width, height). This is necessary so your circle does not clip off the view. Overriding the OnSizeAllocated method will allow you to be notified when the size of the view you are drawing in changes. Next, inside OnSizeAllocated add your Update method and pass in the width and height. Now inside your render function that invalidates when changes to the UI are made; You can use the ViewRect
to easily get the smaller of the two sides. Simply use the LimitingSpanLength property to get the smaller of the two spans for your calculations.
Break Down:
- Create a
ViewRect
variable and initialize it. - Call its Update method and provide the width and height of the view you are drawing in.
- Retrieve the value stored in
ViewRect
’s LimitingSpanLength property to get the smaller span (limiting span).
ViewRect viewRect = new ViewRect();
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
viewRect.Update((float)width, (float)height);
}
If you are familiar with SkiaSharp’s PaintSurface
then you know it provides the width and height in pixels. Xamarin’s OnSizeAllocated provides the width and height in density independent pixels. Therefore, you need to convert your width and height from DPI in OnSizeAllocated to pixels before you call Update.
ViewRect viewRect = new ViewRect();
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
// Util.DisplayUtil.DPI would be replace with your way for getting the current device's DPI
viewRect.Update((float)width * Util.DisplayUtil.DPI, (float)height * Util.DisplayUtil.DPI);
}