From cb70e8ec0c832b76ce46e5fddca3310f6b9f5dfb Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Sun, 20 Oct 2024 21:36:30 +0100 Subject: [PATCH] unify handlers --- .../Bootstrapper/CustomDialog.Creator.cs | 144 ++++++++---------- 1 file changed, 62 insertions(+), 82 deletions(-) diff --git a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs index 6f2caf15..9a339e0c 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs @@ -14,6 +14,8 @@ namespace Bloxstrap.UI.Elements.Bootstrapper { public partial class CustomDialog { + private class DummyFrameworkElement : FrameworkElement { } + private const int MaxElements = 100; private bool _initialised = false; @@ -23,36 +25,30 @@ public partial class CustomDialog private string ThemeDir { get; set; } = ""; - delegate UIElement? HandleXmlElementDelegate(CustomDialog dialog, XElement xmlElement); - delegate Brush HandleXmlBrushElementDelegate(CustomDialog dialog, XElement xmlElement); - delegate void HandleXmlTransformationElementDelegate(TransformGroup group, XElement xmlElement); + delegate object HandleXmlElementDelegate(CustomDialog dialog, XElement xmlElement); private static Dictionary _elementHandlerMap = new Dictionary() { - //["BloxstrapCustomBootstrapper"] = HandleXmlElement_BloxstrapCustomBootstrapper, ["TitleBar"] = HandleXmlElement_TitleBar, ["Button"] = HandleXmlElement_Button, ["ProgressBar"] = HandleXmlElement_ProgressBar, ["TextBlock"] = HandleXmlElement_TextBlock, ["MarkdownTextBlock"] = HandleXmlElement_MarkdownTextBlock, - ["Image"] = HandleXmlElement_Image - }; + ["Image"] = HandleXmlElement_Image, - private static Dictionary _brushHandlerMap = new Dictionary() - { - ["SolidColorBrush"] = HandleXmlBrush_SolidColorBrush, - ["ImageBrush"] = HandleXmlBrush_ImageBrush, - ["LinearGradientBrush"] = HandleXmlBrush_LinearGradientBrush - }; + ["SolidColorBrush"] = HandleXml_SolidColorBrush, + ["ImageBrush"] = HandleXml_ImageBrush, + ["LinearGradientBrush"] = HandleXml_LinearGradientBrush, - private static Dictionary _transformationHandlerMap = new Dictionary() - { - ["ScaleTransform"] = HandleXmlTransformationElement_ScaleTransform, - ["SkewTransform"] = HandleXmlTransformationElement_SkewTransform, - ["RotateTransform"] = HandleXmlTransformationElement_RotateTransform, - ["TranslateTransform"] = HandleXmlTransformationElement_TranslateTransform + ["GradientStop"] = HandleXml_GradientStop, + + ["ScaleTransform"] = HandleXml_ScaleTransform, + ["SkewTransform"] = HandleXml_SkewTransform, + ["RotateTransform"] = HandleXml_RotateTransform, + ["TranslateTransform"] = HandleXml_TranslateTransform }; + #region Utilities private static string GetXmlAttribute(XElement element, string attributeName, string? defaultValue = null) { @@ -233,7 +229,7 @@ private static FontStyle GetFontStyleFromXElement(XElement element) #endregion #region Transformation Elements - private static void HandleXmlTransformationElement_ScaleTransform(TransformGroup group, XElement xmlElement) + private static Transform HandleXml_ScaleTransform(CustomDialog dialog, XElement xmlElement) { var st = new ScaleTransform(); @@ -242,10 +238,10 @@ private static void HandleXmlTransformationElement_ScaleTransform(TransformGroup st.CenterX = ParseXmlAttribute(xmlElement, "CenterX", 0); st.CenterY = ParseXmlAttribute(xmlElement, "CenterY", 0); - group.Children.Add(st); + return st; } - private static void HandleXmlTransformationElement_SkewTransform(TransformGroup group, XElement xmlElement) + private static Transform HandleXml_SkewTransform(CustomDialog dialog, XElement xmlElement) { var st = new SkewTransform(); @@ -254,10 +250,10 @@ private static void HandleXmlTransformationElement_SkewTransform(TransformGroup st.CenterX = ParseXmlAttribute(xmlElement, "CenterX", 0); st.CenterY = ParseXmlAttribute(xmlElement, "CenterY", 0); - group.Children.Add(st); + return st; } - private static void HandleXmlTransformationElement_RotateTransform(TransformGroup group, XElement xmlElement) + private static Transform HandleXml_RotateTransform(CustomDialog dialog, XElement xmlElement) { var rt = new RotateTransform(); @@ -265,28 +261,20 @@ private static void HandleXmlTransformationElement_RotateTransform(TransformGrou rt.CenterX = ParseXmlAttribute(xmlElement, "CenterX", 0); rt.CenterY = ParseXmlAttribute(xmlElement, "CenterY", 0); - group.Children.Add(rt); + return rt; } - private static void HandleXmlTransformationElement_TranslateTransform(TransformGroup group, XElement xmlElement) + private static Transform HandleXml_TranslateTransform(CustomDialog dialog, XElement xmlElement) { var tt = new TranslateTransform(); tt.X = ParseXmlAttribute(xmlElement, "X", 0); tt.Y = ParseXmlAttribute(xmlElement, "Y", 0); - group.Children.Add(tt); - } - - private static void HandleXmlTransformation(TransformGroup group, XElement xmlElement) - { - if (!_transformationHandlerMap.ContainsKey(xmlElement.Name.ToString())) - throw new Exception($"Unknown transformation {xmlElement.Name}"); - - _transformationHandlerMap[xmlElement.Name.ToString()](group, xmlElement); + return tt; } - private static void ApplyTransformations_UIElement(UIElement uiElement, XElement xmlElement) + private static void ApplyTransformations_UIElement(CustomDialog dialog, UIElement uiElement, XElement xmlElement) { var renderTransform = xmlElement.Element($"{xmlElement.Name}.RenderTransform"); @@ -295,24 +283,26 @@ private static void ApplyTransformations_UIElement(UIElement uiElement, XElement var tg = new TransformGroup(); foreach (var child in renderTransform.Elements()) - HandleXmlTransformation(tg, child); + { + Transform element = HandleXml(dialog, child); + tg.Children.Add(element); + } - if (tg.Children.Any()) - uiElement.RenderTransform = tg; + uiElement.RenderTransform = tg; } } #endregion #region Brushes - private static void HandleXmlBrush_Brush(Brush brush, XElement xmlElement) + private static void HandleXml_Brush(Brush brush, XElement xmlElement) { brush.Opacity = ParseXmlAttribute(xmlElement, "Opacity", 1.0); } - private static Brush HandleXmlBrush_SolidColorBrush(CustomDialog dialog, XElement xmlElement) + private static Brush HandleXml_SolidColorBrush(CustomDialog dialog, XElement xmlElement) { var brush = new SolidColorBrush(); - HandleXmlBrush_Brush(brush, xmlElement); + HandleXml_Brush(brush, xmlElement); object? color = GetColorFromXElement(xmlElement, "Color"); if (color is Color) @@ -321,10 +311,10 @@ private static Brush HandleXmlBrush_SolidColorBrush(CustomDialog dialog, XElemen return brush; } - private static Brush HandleXmlBrush_ImageBrush(CustomDialog dialog, XElement xmlElement) + private static Brush HandleXml_ImageBrush(CustomDialog dialog, XElement xmlElement) { var imageBrush = new ImageBrush(); - HandleXmlBrush_Brush(imageBrush, xmlElement); + HandleXml_Brush(imageBrush, xmlElement); imageBrush.AlignmentX = ParseXmlAttribute(xmlElement, "AlignmentX", AlignmentX.Center); imageBrush.AlignmentY = ParseXmlAttribute(xmlElement, "AlignmentY", AlignmentY.Center); @@ -376,7 +366,7 @@ private static Brush HandleXmlBrush_ImageBrush(CustomDialog dialog, XElement xml return imageBrush; } - private static GradientStop HandleGradientStop(XElement xmlElement) + private static GradientStop HandleXml_GradientStop(CustomDialog dialog, XElement xmlElement) { var gs = new GradientStop(); @@ -389,10 +379,10 @@ private static GradientStop HandleGradientStop(XElement xmlElement) return gs; } - private static Brush HandleXmlBrush_LinearGradientBrush(CustomDialog dialog, XElement xmlElement) + private static Brush HandleXml_LinearGradientBrush(CustomDialog dialog, XElement xmlElement) { var brush = new LinearGradientBrush(); - HandleXmlBrush_Brush(brush, xmlElement); + HandleXml_Brush(brush, xmlElement); object? startPoint = GetPointFromXElement(xmlElement, "StartPoint"); if (startPoint is Point) @@ -407,24 +397,11 @@ private static Brush HandleXmlBrush_LinearGradientBrush(CustomDialog dialog, XEl brush.SpreadMethod = ParseXmlAttribute(xmlElement, "SpreadMethod", GradientSpreadMethod.Pad); foreach (var child in xmlElement.Elements()) - { - if (child.Name != "GradientStop") - throw new Exception($"{child.Name} can not be a child of LinearGradientBrush"); - - brush.GradientStops.Add(HandleGradientStop(child)); - } + brush.GradientStops.Add(HandleXml(dialog, child)); return brush; } - private static Brush HandleXmlBrush(CustomDialog dialog, XElement xmlElement) - { - if (!_brushHandlerMap.ContainsKey(xmlElement.Name.ToString())) - throw new Exception($"Unknown brush {xmlElement.Name}"); - - return _brushHandlerMap[xmlElement.Name.ToString()](dialog, xmlElement); - } - private static void ApplyBrush_UIElement(CustomDialog dialog, FrameworkElement uiElement, string name, DependencyProperty dependencyProperty, XElement xmlElement) { // check if attribute exists @@ -449,7 +426,7 @@ private static void ApplyBrush_UIElement(CustomDialog dialog, FrameworkElement u if (first == null) throw new Exception($"{xmlElement.Name} {name} is missing the brush"); - var brush = HandleXmlBrush(dialog, first); + var brush = HandleXml(dialog, first); uiElement.SetValue(dependencyProperty, brush); } #endregion @@ -506,7 +483,7 @@ private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElem ApplyBrush_UIElement(dialog, uiElement, "BorderBrush", Control.BorderBrushProperty, xmlElement); } - private static UIElement? HandleXmlElement_BloxstrapCustomBootstrapper(CustomDialog dialog, XElement xmlElement) + private static UIElement HandleXmlElement_BloxstrapCustomBootstrapper(CustomDialog dialog, XElement xmlElement) { xmlElement.SetAttributeValue("Visibility", "Collapsed"); // don't show the bootstrapper yet!!! xmlElement.SetAttributeValue("IsEnabled", "True"); @@ -523,10 +500,10 @@ private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElem dialog.Margin = new Thickness(0, 0, 0, 0); dialog.Padding = new Thickness(0, 0, 0, 0); - return null; // dont add anything + return new DummyFrameworkElement(); } - private static UIElement? HandleXmlElement_TitleBar(CustomDialog dialog, XElement xmlElement) + private static DummyFrameworkElement HandleXmlElement_TitleBar(CustomDialog dialog, XElement xmlElement) { xmlElement.SetAttributeValue("Name", "TitleBar"); // prevent two titlebars from existing xmlElement.SetAttributeValue("IsEnabled", "True"); @@ -547,7 +524,7 @@ private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElem dialog.Title = title; dialog.RootTitleBar.Title = title; - return null; // dont add anything + return new DummyFrameworkElement(); // dont add anything } private static object? GetContentFromXElement(CustomDialog dialog, XElement xmlElement) @@ -563,14 +540,14 @@ private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElem if (first == null) throw new Exception($"{xmlElement.Name} Content is missing the content"); - var uiElement = HandleXml(dialog, first); + var uiElement = HandleXml(dialog, first); return uiElement; } return null; } - private static UIElement? HandleXmlElement_Button(CustomDialog dialog, XElement xmlElement) + private static UIElement HandleXmlElement_Button(CustomDialog dialog, XElement xmlElement) { var button = new Button(); HandleXmlElement_Control(dialog, button, xmlElement); @@ -586,12 +563,12 @@ private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElem BindingOperations.SetBinding(button, Button.CommandProperty, cancelCommandBinding); } - ApplyTransformations_UIElement(button, xmlElement); + ApplyTransformations_UIElement(dialog, button, xmlElement); return button; } - private static UIElement? HandleXmlElement_ProgressBar(CustomDialog dialog, XElement xmlElement) + private static UIElement HandleXmlElement_ProgressBar(CustomDialog dialog, XElement xmlElement) { var progressBar = new ProgressBar(); HandleXmlElement_Control(dialog, progressBar, xmlElement); @@ -613,7 +590,7 @@ private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElem BindingOperations.SetBinding(progressBar, ProgressBar.ValueProperty, valueBinding); } - ApplyTransformations_UIElement(progressBar, xmlElement); + ApplyTransformations_UIElement(dialog, progressBar, xmlElement); return progressBar; } @@ -655,10 +632,10 @@ private static void HandleXmlElement_TextBlock_Base(CustomDialog dialog, TextBlo BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, textBinding); } - ApplyTransformations_UIElement(textBlock, xmlElement); + ApplyTransformations_UIElement(dialog, textBlock, xmlElement); } - private static UIElement? HandleXmlElement_TextBlock(CustomDialog dialog, XElement xmlElement) + private static UIElement HandleXmlElement_TextBlock(CustomDialog dialog, XElement xmlElement) { var textBlock = new TextBlock(); HandleXmlElement_TextBlock_Base(dialog, textBlock, xmlElement); @@ -666,7 +643,7 @@ private static void HandleXmlElement_TextBlock_Base(CustomDialog dialog, TextBlo return textBlock; } - private static UIElement? HandleXmlElement_MarkdownTextBlock(CustomDialog dialog, XElement xmlElement) + private static UIElement HandleXmlElement_MarkdownTextBlock(CustomDialog dialog, XElement xmlElement) { var textBlock = new MarkdownTextBlock(); HandleXmlElement_TextBlock_Base(dialog, textBlock, xmlElement); @@ -678,7 +655,7 @@ private static void HandleXmlElement_TextBlock_Base(CustomDialog dialog, TextBlo return textBlock; } - private static UIElement? HandleXmlElement_Image(CustomDialog dialog, XElement xmlElement) + private static UIElement HandleXmlElement_Image(CustomDialog dialog, XElement xmlElement) { var image = new Image(); HandleXmlElement_FrameworkElement(dialog, image, xmlElement); @@ -726,27 +703,30 @@ private static void HandleXmlElement_TextBlock_Base(CustomDialog dialog, TextBlo } } - ApplyTransformations_UIElement(image, xmlElement); + ApplyTransformations_UIElement(dialog, image, xmlElement); return image; } - private static UIElement? HandleXml(CustomDialog dialog, XElement xmlElement) + private static T HandleXml(CustomDialog dialog, XElement xmlElement) where T : class { if (!_elementHandlerMap.ContainsKey(xmlElement.Name.ToString())) throw new Exception($"Unknown element {xmlElement.Name}"); - var uiElement = _elementHandlerMap[xmlElement.Name.ToString()](dialog, xmlElement); - return uiElement; + var element = _elementHandlerMap[xmlElement.Name.ToString()](dialog, xmlElement); + if (element is not T) + throw new Exception($"{xmlElement.Parent!.Name} cannot have a child of {xmlElement.Name}"); + + return (T)element; } - private static void HandleAndAddXml(CustomDialog dialog, XElement xmlElement) + private static void AddXml(CustomDialog dialog, XElement xmlElement) { if (xmlElement.Name.ToString().StartsWith($"{xmlElement.Parent!.Name}.")) return; // not an xml element - var uiElement = HandleXml(dialog, xmlElement); - if (uiElement != null) + var uiElement = HandleXml(dialog, xmlElement); + if (uiElement is not DummyFrameworkElement) dialog.ElementGrid.Children.Add(uiElement); } @@ -771,7 +751,7 @@ private void HandleXmlBase(XElement xml) // handle everything else foreach (var child in xml.Elements()) - HandleAndAddXml(this, child); + AddXml(this, child); } #endregion