Add dp geometry utils.
authorPiotr Czaja/Advanced Frameworks (PLT) /SRPOL/Engineer/Samsung Electronics <p.czaja@samsung.com>
Wed, 22 Sep 2021 11:19:50 +0000 (13:19 +0200)
committerPiotr Czaja/Advanced Frameworks (PLT) /SRPOL/Engineer/Samsung Electronics <p.czaja@samsung.com>
Mon, 4 Oct 2021 13:18:48 +0000 (15:18 +0200)
Fitness/Views/DpUtils.cs [new file with mode: 0644]
Fitness/Views/ExtentsInDpExtension.cs [new file with mode: 0644]
Fitness/Views/PixelSizeInDpExtension.cs [new file with mode: 0644]
Fitness/Views/PositionInDpExtension.cs [new file with mode: 0644]
Fitness/Views/SizeInDpExtension.cs [new file with mode: 0644]

diff --git a/Fitness/Views/DpUtils.cs b/Fitness/Views/DpUtils.cs
new file mode 100644 (file)
index 0000000..2f6f595
--- /dev/null
@@ -0,0 +1,63 @@
+using System;
+using Tizen.NUI;
+
+namespace Fitness.Views
+{
+    /// <summary>
+    /// Utils for converting dp pixels.
+    /// </summary>
+    public static class DpUtils
+    {
+        private const float DpPixelDivider = 2.75f;
+
+        /// <summary>
+        /// Converts dp pixels into pixels.
+        /// </summary>
+        /// <param name="pixels">The number of dp pixels.</param>
+        /// <returns>The number of pixels.</returns>
+        public static int DpToPixels(int pixels)
+        {
+            return (int)Math.Round(DpTypeConverter.Instance.ConvertToPixel(pixels / DpPixelDivider), MidpointRounding.AwayFromZero);
+        }
+
+        /// <summary>
+        /// Converts dp pixels into pixels.
+        /// </summary>
+        /// <param name="pixels">The number of dp pixels.</param>
+        /// <returns>The number of pixels.</returns>
+        public static ushort DpToPixels(ushort pixels)
+        {
+            return (ushort)Math.Round(DpTypeConverter.Instance.ConvertToPixel(pixels / DpPixelDivider), MidpointRounding.AwayFromZero);
+        }
+
+        /// <summary>
+        /// Converts dp pixels into pixels.
+        /// </summary>
+        /// <param name="pixels">The number of dp pixels.</param>
+        /// <returns>The number of pixels.</returns>
+        public static float DpToPixels(float pixels)
+        {
+            return DpTypeConverter.Instance.ConvertToPixel(pixels / DpPixelDivider);
+        }
+
+        /// <summary>
+        /// Converts dp pixels into pixels.
+        /// </summary>
+        /// <param name="size">The size to be converted to pixels.</param>
+        /// <returns>The number of pixels.</returns>
+        public static Size2D DpToPixels(Size2D size)
+        {
+            return new Size2D(DpToPixels(size.Width), DpToPixels(size.Height));
+        }
+
+        /// <summary>
+        /// Converts dp pixels into pixels.
+        /// </summary>
+        /// <param name="extents">The extents collection to be converted to pixels.</param>
+        /// <returns>The number of pixels.</returns>
+        public static Extents DpToPixels(Extents extents)
+        {
+            return new Extents(DpToPixels(extents.Start), DpToPixels(extents.End), DpToPixels(extents.Top), DpToPixels(extents.Bottom));
+        }
+    }
+}
diff --git a/Fitness/Views/ExtentsInDpExtension.cs b/Fitness/Views/ExtentsInDpExtension.cs
new file mode 100644 (file)
index 0000000..ca26ec7
--- /dev/null
@@ -0,0 +1,38 @@
+using System;
+using Tizen.NUI;
+using Tizen.NUI.Xaml;
+
+namespace Fitness.Views
+{
+    /// <summary>
+    /// Converts number of dp pixels to exact number of pixels in the target.
+    /// </summary>
+    public class ExtentsInDpExtension : IMarkupExtension
+    {
+        /// <summary>
+        /// Gets or sets the start offset in dp type.
+        /// </summary>
+        public ushort Start { get; set; }
+
+        /// <summary>
+        /// Gets or sets the end offset in dp type.
+        /// </summary>
+        public ushort End { get; set; }
+
+        /// <summary>
+        /// Gets or sets the top offset in dp type.
+        /// </summary>
+        public ushort Top { get; set; }
+
+        /// <summary>
+        /// Gets or sets the bottom offset in dp type.
+        /// </summary>
+        public ushort Bottom { get; set; }
+
+        /// <inheritdoc/>
+        public object ProvideValue(IServiceProvider serviceProvider)
+        {
+            return new Extents(DpUtils.DpToPixels(Start), DpUtils.DpToPixels(End), DpUtils.DpToPixels(Top), DpUtils.DpToPixels(Bottom));
+        }
+    }
+}
diff --git a/Fitness/Views/PixelSizeInDpExtension.cs b/Fitness/Views/PixelSizeInDpExtension.cs
new file mode 100644 (file)
index 0000000..1448e26
--- /dev/null
@@ -0,0 +1,35 @@
+using System;
+using Tizen.NUI;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Binding;
+using Tizen.NUI.Xaml;
+
+namespace Fitness.Views
+{
+    /// <summary>
+    /// Converts TextLabels's PixelSize to exact number of pixels in the target.
+    /// </summary>
+    public class PixelSizeInDpExtension : IMarkupExtension
+    {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="PixelSizeInDpExtension"/> class.
+        /// Creates markups with given size (in number of dp pixels).
+        /// </summary>
+        /// <param name="dpSize">The number of dp pixels.</param>
+        public PixelSizeInDpExtension(float dpSize = 0)
+        {
+            DpSize = dpSize;
+        }
+
+        /// <summary>
+        /// Gets or sets the size of font in dp pixels.
+        /// </summary>
+        public float DpSize { get; set; }
+
+        /// <inheritdoc/>
+        public object ProvideValue(IServiceProvider serviceProvider)
+        {
+            return DpUtils.DpToPixels(DpSize);
+        }
+    }
+}
diff --git a/Fitness/Views/PositionInDpExtension.cs b/Fitness/Views/PositionInDpExtension.cs
new file mode 100644 (file)
index 0000000..3ef3387
--- /dev/null
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tizen.NUI;
+using Tizen.NUI.Components;
+using Tizen.NUI.Xaml;
+
+namespace Fitness.Views
+{
+    /// <summary>
+    /// PositionInDpExtension.
+    /// </summary>
+    public class PositionInDpExtension : IMarkupExtension
+    {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="PositionInDpExtension"/> class.
+        /// </summary>
+        /// <param name="x">x.</param>
+        /// <param name="y">y.</param>
+        public PositionInDpExtension(int x = 0, int y = 0)
+        {
+            X = x;
+            Y = y;
+        }
+
+        /// <summary>
+        /// Gets or sets x as number of dp pixels.
+        /// </summary>
+        public int X { get; set; }
+
+        /// <summary>
+        /// Gets or sets y as number of dp pixels.
+        /// </summary>
+        public int Y { get; set; }
+
+        /// <inheritdoc/>
+        public object ProvideValue(IServiceProvider serviceProvider)
+        {
+            return new Position(DpUtils.DpToPixels(X), DpUtils.DpToPixels(Y));
+        }
+    }
+}
diff --git a/Fitness/Views/SizeInDpExtension.cs b/Fitness/Views/SizeInDpExtension.cs
new file mode 100644 (file)
index 0000000..b807964
--- /dev/null
@@ -0,0 +1,41 @@
+using System;
+using Tizen.NUI;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Binding;
+using Tizen.NUI.Xaml;
+
+namespace Fitness.Views
+{
+    /// <summary>
+    /// Creates markups with given width and height (in number of dp pixels).
+    /// </summary>
+    public class SizeInDpExtension : IMarkupExtension
+    {
+        /// <summary>
+        /// Initializes a new instance of the <see cref="SizeInDpExtension"/> class.
+        /// </summary>
+        /// <param name="width">The size width.</param>
+        /// <param name="height">The size height.</param>
+        public SizeInDpExtension(int width = 0, int height = 0)
+        {
+            Width = width;
+            Height = height;
+        }
+
+        /// <summary>
+        /// Gets or sets the width.
+        /// </summary>
+        public int Width { get; set; }
+
+        /// <summary>
+        /// Gets or sets the height.
+        /// </summary>
+        public int Height { get; set; }
+
+        /// <inheritdoc/>
+        public object ProvideValue(IServiceProvider serviceProvider)
+        {
+            return new Size2D(DpUtils.DpToPixels(Width), DpUtils.DpToPixels(Height));
+        }
+    }
+}