Browse Source

Expanded control wip

Krzysztof Krysiński 3 months ago
parent
commit
fe08527301

+ 6 - 2
src/PixiEditor.Extensions.Sdk/Api/FlyUI/Border.cs

@@ -20,7 +20,8 @@ public class Border : SingleChildLayoutElement
     public double Height { get; set; }
 
     public Border(LayoutElement child = null, Color color = default, Edges thickness = default,
-        Edges cornerRadius = default, Edges padding = default, Edges margin = default, double width = -1, double height = -1,
+        Edges cornerRadius = default, Edges padding = default, Edges margin = default, double width = -1,
+        double height = -1,
         Color backgroundColor = default)
     {
         Child = child;
@@ -37,7 +38,10 @@ public class Border : SingleChildLayoutElement
     public override CompiledControl BuildNative()
     {
         CompiledControl control = new(UniqueId, "Border");
-        control.Children.Add(Child.BuildNative());
+        if (Child != null)
+        {
+            control.Children.Add(Child.BuildNative());
+        }
 
         control.AddProperty(Color);
         control.AddProperty(Thickness);

+ 24 - 0
src/PixiEditor.Extensions.Sdk/Api/FlyUI/Expanded.cs

@@ -0,0 +1,24 @@
+namespace PixiEditor.Extensions.Sdk.Api.FlyUI;
+
+public class Expanded : SingleChildLayoutElement
+{
+    public int Flex { get; set; }
+
+    public Expanded(LayoutElement child = null, int flex = 1)
+    {
+        Child = child;
+        Flex = flex;
+    }
+
+    public override CompiledControl BuildNative()
+    {
+        CompiledControl control = new CompiledControl(UniqueId, "Expanded");
+        control.AddProperty(Flex);
+
+        if (Child != null)
+            control.AddChild(Child.BuildNative());
+
+        BuildPendingEvents(control);
+        return control;
+    }
+}

+ 71 - 0
src/PixiEditor.Extensions/FlyUI/Elements/Expanded.cs

@@ -0,0 +1,71 @@
+using System.Collections.Immutable;
+using Avalonia.Controls;
+using PixiEditor.Extensions.UI.Overlays;
+
+namespace PixiEditor.Extensions.FlyUI.Elements;
+
+public class Expanded : SingleChildLayoutElement, IPropertyDeserializable
+{
+    private Control affectedControl;
+    private int flex = 1;
+
+    public int Flex
+    {
+        get => flex;
+        set => SetField(ref flex, value);
+    }
+
+    public Expanded(LayoutElement? child = null, int flex = 1)
+    {
+        Child = child;
+        Flex = flex;
+    }
+
+    public override Control BuildNative()
+    {
+        if (Child == null)
+        {
+            var control = new Control();
+            ExpandedDecorator.SetFlex(control, Flex);
+            affectedControl = control;
+            return control;
+        }
+
+        var child = Child.BuildNative();
+        ExpandedDecorator.SetFlex(child, Flex);
+        affectedControl = child;
+
+        return child;
+    }
+
+    protected override void AddChild(Control child)
+    {
+        if (child is not null)
+        {
+            affectedControl = child;
+            ExpandedDecorator.SetFlex(child, Flex);
+        }
+    }
+
+    protected override void RemoveChild()
+    {
+        if (affectedControl is not null)
+        {
+            ExpandedDecorator.SetFlex(affectedControl, 0);
+            affectedControl = null;
+        }
+    }
+
+    public IEnumerable<object> GetProperties()
+    {
+        yield return Flex;
+    }
+
+    public void DeserializeProperties(ImmutableList<object> values)
+    {
+        if (values.Count > 0)
+        {
+            Flex = (int)values[0];
+        }
+    }
+}

+ 13 - 0
src/PixiEditor.Extensions/UI/Overlays/ExpandedDecorator.cs

@@ -0,0 +1,13 @@
+using Avalonia;
+using Avalonia.Controls;
+
+namespace PixiEditor.Extensions.UI.Overlays;
+
+public class ExpandedDecorator
+{
+    public static readonly AttachedProperty<int> FlexProperty =
+        AvaloniaProperty.RegisterAttached<ExpandedDecorator, Control, int>("Flex");
+
+    public static void SetFlex(Control obj, int value) => obj.SetValue(FlexProperty, value);
+    public static int GetFlex(Control obj) => obj.GetValue(FlexProperty);
+}