Jelajahi Sumber

avalonia: added utilities and made margin of Tab Strip bigger

Kenric Nugteren 2 minggu lalu
induk
melakukan
f457bcbb1f

+ 115 - 0
InABox.Avalonia/AvaloniaUtils.cs

@@ -0,0 +1,115 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.LogicalTree;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Avalonia;
+
+public static class AvaloniaUtils
+{
+
+    #region Grid Children
+
+    public static int GetRow(this Grid grid, ILogical dependencyObject)
+    {
+        while (true)
+        {
+            var parent = dependencyObject.GetLogicalParent();
+            if (parent == null)
+                return -1;
+            if (parent == grid)
+                return dependencyObject is Control control ? Grid.GetRow(control) : -1;
+            dependencyObject = parent;
+        }
+    }
+
+    public static int GetRowSpan(this Grid grid, ILogical dependencyObject)
+    {
+        while (true)
+        {
+            var parent = dependencyObject.GetLogicalParent();
+            if (parent == null)
+                return -1;
+            if (parent == grid)
+                return dependencyObject is Control control ? Grid.GetRowSpan(control) : -1;
+            dependencyObject = parent;
+        }
+    }
+   
+    public static int GetColumn(this Grid grid, ILogical dependencyObject)
+    {
+        while (true)
+        {
+            var parent = dependencyObject.GetLogicalParent();
+            if (parent == null)
+                return -1;
+            if (parent == grid)
+                return dependencyObject is Control control ? Grid.GetColumn(control) : -1;
+            dependencyObject = parent;
+        }
+    }
+
+    public static int GetColumnSpan(this Grid grid, ILogical dependencyObject)
+    {
+        while (true)
+        {
+            var parent = dependencyObject.GetLogicalParent();
+            if (parent == null)
+                return -1;
+            if (parent == grid)
+                return dependencyObject is Control control ? Grid.GetColumnSpan(control) : -1;
+            dependencyObject = parent;
+        }
+    }
+   
+    public static void SetGridPosition(this AvaloniaObject element, int row, int column, int rowspan = 1, int colspan = 1)
+    {
+        element.SetValue(Grid.ColumnProperty, column);
+        element.SetValue(Grid.ColumnSpanProperty, Math.Max(1, colspan));
+        element.SetValue(Grid.RowProperty, row);
+        element.SetValue(Grid.RowSpanProperty, Math.Max(1, rowspan));
+    }
+    public static Grid AddChild(this Grid grid, Control element, int row, int column, int rowSpan = 1, int colSpan = 1)
+    {
+        element.SetGridPosition(row, column, rowSpan, colSpan);
+        grid.Children.Add(element);
+        return grid;
+    }
+
+    #endregion
+
+    #region Grid Columns + Rows
+
+    public static ColumnDefinition AddColumn(this Grid grid, GridUnitType type, double value = 1)
+    {
+        var colDef = new ColumnDefinition { Width = new GridLength(value, type) };
+        grid.ColumnDefinitions.Add(colDef);
+        return colDef;
+    }
+    public static ColumnDefinition AddColumn(this Grid grid, double value)
+    {
+        var colDef = new ColumnDefinition { Width = new GridLength(value) };
+        grid.ColumnDefinitions.Add(colDef);
+        return colDef;
+    }
+
+    public static RowDefinition AddRow(this Grid grid, GridUnitType type, double value = 1)
+    {
+        var rowDef = new RowDefinition { Height = new GridLength(value, type) };
+        grid.RowDefinitions.Add(rowDef);
+        return rowDef;
+    }
+    public static RowDefinition AddRow(this Grid grid, double value)
+    {
+        var rowDef = new RowDefinition { Height = new GridLength(value) };
+        grid.RowDefinitions.Add(rowDef);
+        return rowDef;
+    }
+
+    #endregion
+
+}

+ 38 - 0
InABox.Avalonia/ImageUtils.cs

@@ -0,0 +1,38 @@
+using Avalonia.Media;
+using Avalonia.Media.Imaging;
+using SkiaSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Avalonia;
+
+public static class ImageUtils
+{
+    public static Bitmap? ImageFromBytes(byte[]? value, bool transparent = true)
+    {
+        Bitmap? result = null;
+        if (value?.Any() == true)
+        {
+            if (transparent)
+            {
+                var skb = SKBitmap.Decode(value);
+                var transparentColor = skb.GetPixel(0, 0);
+                var newPixels = skb.Pixels.Select(x => Equals(x, transparentColor) ? SKColors.Transparent : x);
+                skb.Pixels = newPixels.ToArray();
+                SKImage image = SKImage.FromPixels(skb.PeekPixels());
+                SKData encoded = image.Encode(SKEncodedImageFormat.Png, 100);
+                value = encoded.ToArray();
+            }
+            
+            using (var stream = new MemoryStream(value))
+            {
+                result = new Bitmap(stream);
+            }
+        }
+
+        return result;
+    }
+}

+ 1 - 1
InABox.Avalonia/Theme/Classes/TabItem.axaml

@@ -10,7 +10,7 @@
 		<Setter Property="ItemsPanel">
 			<Setter.Value>
 				<ItemsPanelTemplate>
-					<UniformGrid Rows="1"/>
+					<UniformGrid Rows="1" Margin="1"/>
 				</ItemsPanelTemplate>
 			</Setter.Value>
 		</Setter>

+ 1 - 1
InABox.Avalonia/Theme/Classes/TabStrip.axaml

@@ -8,7 +8,7 @@
 		<Setter Property="ItemsPanel">
 			<Setter.Value>
 				<ItemsPanelTemplate>
-					<UniformGrid Rows="1"/>
+					<UniformGrid Rows="1" Margin="1"/>
 				</ItemsPanelTemplate>
 			</Setter.Value>
 		</Setter>