From 2c41456a1559700fafc6c2788edb88461589e795 Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Tue, 21 Jan 2025 23:44:03 +0000 Subject: [PATCH] add grids --- .../Resources/CustomBootstrapperSchema.json | 31 ++++++- .../Bootstrapper/CustomDialog.Converters.cs | 3 + .../Bootstrapper/CustomDialog.Creator.cs | 6 +- .../Bootstrapper/CustomDialog.Elements.cs | 92 +++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) diff --git a/Bloxstrap/Resources/CustomBootstrapperSchema.json b/Bloxstrap/Resources/CustomBootstrapperSchema.json index 48fd5ea..11321f9 100644 --- a/Bloxstrap/Resources/CustomBootstrapperSchema.json +++ b/Bloxstrap/Resources/CustomBootstrapperSchema.json @@ -16,7 +16,11 @@ "LayoutTransform": "Transform", "Opacity": "double", "OpacityMask": "Brush", - "RenderTransformOrigin": "Point" + "RenderTransformOrigin": "Point", + "Grid.Row": "int", + "Grid.RowSpan": "int", + "Grid.Column": "int", + "Grid.ColumnSpan": "int" } }, "Control": { @@ -119,6 +123,30 @@ "IsAnimated": "bool" } }, + "Grid": { + "SuperClass": "FrameworkElement", + "IsCreatable": true, + "Attributes": { + "RowDefinitions": "object", + "ColumnDefinitions": "object" + } + }, + "RowDefinition": { + "IsCreatable": true, + "Attributes": { + "Height": "GridLength", + "MinHeight": "double", + "MaxHeight": "double" + } + }, + "ColumnDefinition": { + "IsCreatable": true, + "Attributes": { + "Width": "GridLength", + "MinWidth": "double", + "MaxWidth": "double" + } + }, "ScaleTransform": { "IsCreatable": true, "Attributes": { @@ -277,6 +305,7 @@ "ImageSource": {}, "Transform": { "CanHaveElement": true }, "FontFamily": {}, + "GridLength": {}, "Visibility": { "Values": [ "Visible", diff --git a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Converters.cs b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Converters.cs index 9e1abe1..8426110 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Converters.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Converters.cs @@ -66,6 +66,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper private static object? GetCornerRadiusFromXElement(XElement xmlElement, string attributeName) => GetTypeFromXElement(CornerRadiusConverter, xmlElement, attributeName); + public static GridLengthConverter GridLengthConverter { get; } = new GridLengthConverter(); + private static object? GetGridLengthFromXElement(XElement xmlElement, string attributeName) => GetTypeFromXElement(GridLengthConverter, xmlElement, attributeName); + private static BrushConverter? _brushConverter = null; private static BrushConverter BrushConverter { get => _brushConverter ??= new BrushConverter(); } diff --git a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs index 6f131e8..ca5b473 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs @@ -30,6 +30,7 @@ namespace Bloxstrap.UI.Elements.Bootstrapper ["TextBlock"] = HandleXmlElement_TextBlock, ["MarkdownTextBlock"] = HandleXmlElement_MarkdownTextBlock, ["Image"] = HandleXmlElement_Image, + ["Grid"] = HandleXmlElement_Grid, ["SolidColorBrush"] = HandleXmlElement_SolidColorBrush, ["ImageBrush"] = HandleXmlElement_ImageBrush, @@ -47,7 +48,10 @@ namespace Bloxstrap.UI.Elements.Bootstrapper ["Ellipse"] = HandleXmlElement_Ellipse, ["Line"] = HandleXmlElement_Line, - ["Rectangle"] = HandleXmlElement_Rectangle + ["Rectangle"] = HandleXmlElement_Rectangle, + + ["RowDefinition"] = HandleXmlElement_RowDefinition, + ["ColumnDefinition"] = HandleXmlElement_ColumnDefinition }; private static T HandleXml(CustomDialog dialog, XElement xmlElement) where T : class diff --git a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Elements.cs b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Elements.cs index 3d39e9e..ceb9caa 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Elements.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Elements.cs @@ -316,6 +316,16 @@ namespace Bloxstrap.UI.Elements.Bootstrapper int zIndex = ParseXmlAttributeClamped(xmlElement, "ZIndex", defaultValue: 0, min: 0, max: 1000); Panel.SetZIndex(uiElement, zIndex); + int gridRow = ParseXmlAttribute(xmlElement, "Grid.Row", 0); + Grid.SetRow(uiElement, gridRow); + int gridRowSpan = ParseXmlAttribute(xmlElement, "Grid.RowSpan", 1); + Grid.SetRowSpan(uiElement, gridRowSpan); + + int gridColumn = ParseXmlAttribute(xmlElement, "Grid.Column", 0); + Grid.SetColumn(uiElement, gridColumn); + int gridColumnSpan = ParseXmlAttribute(xmlElement, "Grid.ColumnSpan", 1); + Grid.SetColumnSpan(uiElement, gridColumnSpan); + ApplyTransformations_UIElement(dialog, uiElement, xmlElement); ApplyEffects_UIElement(dialog, uiElement, xmlElement); } @@ -614,6 +624,88 @@ namespace Bloxstrap.UI.Elements.Bootstrapper return image; } + + private static RowDefinition HandleXmlElement_RowDefinition(CustomDialog dialog, XElement xmlElement) + { + var rowDefinition = new RowDefinition(); + + var height = GetGridLengthFromXElement(xmlElement, "Height"); + if (height != null) + rowDefinition.Height = (GridLength)height; + + rowDefinition.MinHeight = ParseXmlAttribute(xmlElement, "MinHeight", 0); + rowDefinition.MaxHeight = ParseXmlAttribute(xmlElement, "MaxHeight", double.PositiveInfinity); + + return rowDefinition; + } + + private static ColumnDefinition HandleXmlElement_ColumnDefinition(CustomDialog dialog, XElement xmlElement) + { + var columnDefinition = new ColumnDefinition(); + + var width = GetGridLengthFromXElement(xmlElement, "Width"); + if (width != null) + columnDefinition.Width = (GridLength)width; + + columnDefinition.MinWidth = ParseXmlAttribute(xmlElement, "MinWidth", 0); + columnDefinition.MaxWidth = ParseXmlAttribute(xmlElement, "MaxWidth", double.PositiveInfinity); + + return columnDefinition; + } + + private static void HandleXmlElement_Grid_RowDefinitions(Grid grid, CustomDialog dialog, XElement xmlElement) + { + foreach (var element in xmlElement.Elements()) + { + var rowDefinition = HandleXml(dialog, element); + grid.RowDefinitions.Add(rowDefinition); + } + } + + private static void HandleXmlElement_Grid_ColumnDefinitions(Grid grid, CustomDialog dialog, XElement xmlElement) + { + foreach (var element in xmlElement.Elements()) + { + var columnDefinition = HandleXml(dialog, element); + grid.ColumnDefinitions.Add(columnDefinition); + } + } + + private static Grid HandleXmlElement_Grid(CustomDialog dialog, XElement xmlElement) + { + var grid = new Grid(); + HandleXmlElement_FrameworkElement(dialog, grid, xmlElement); + + bool rowsSet = false; + bool columnsSet = false; + + foreach (var element in xmlElement.Elements()) + { + if (element.Name == "Grid.RowDefinitions") + { + if (rowsSet) + throw new Exception("Grid can only have one RowDefinitions defined"); + rowsSet = true; + + HandleXmlElement_Grid_RowDefinitions(grid, dialog, element); + } + else if (element.Name == "Grid.ColumnDefinitions") + { + if (columnsSet) + throw new Exception("Grid can only have one ColumnDefinitions defined"); + columnsSet = true; + + HandleXmlElement_Grid_ColumnDefinitions(grid, dialog, element); + } + else + { + var uiElement = HandleXml(dialog, element); + grid.Children.Add(uiElement); + } + } + + return grid; + } #endregion } }