Преглед на файлове

avalonia: Added DFStringFieldControl

Kenric Nugteren преди 2 месеца
родител
ревизия
73aadadbde

+ 122 - 0
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFStringFieldControl.cs

@@ -0,0 +1,122 @@
+using Avalonia.Controls;
+using Avalonia.Input;
+using Avalonia.Interactivity;
+using Avalonia.Layout;
+using Avalonia.Media;
+using CommunityToolkit.Mvvm.Input;
+using DialogHostAvalonia;
+using InABox.Avalonia;
+using InABox.Avalonia.Components;
+using InABox.Core;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PRS.Avalonia.DigitalForms;
+
+partial class DFStringFieldControl : DigitalFormFieldControl<DFLayoutStringField, DFLayoutStringFieldProperties, string, string?>
+{
+    private string? text;
+
+    private TextBox? TextBox;
+    private Button? Btn;
+    private TextBlock? TextBlock;
+
+    [NotNull]
+    private string? Text
+    {
+        get => (Field.Properties.PopupEditor ? text : TextBox?.Text) ?? "";
+        set
+        {
+            if (Field.Properties.PopupEditor)
+            {
+                text = value;
+                TextBlock!.Text = value;
+            }
+            else if(TextBox is not null)
+            {
+                TextBox.Text = value;
+            }
+        }
+    }
+
+    public override void SetSerializedValue(string? value)
+    {
+        Text = value;
+    }
+
+    public override string? GetSerializedValue()
+    {
+        return Text;
+    }
+
+    protected override Control Create()
+    {
+        if (Field.Properties.PopupEditor)
+        {
+            var panel = new DockPanel();
+
+            Btn = new Button { Content = "..." };
+            Btn.Command = PopupCommand;
+
+            TextBlock = new TextBlock();
+            TextBlock.Classes.Add("DFStringFieldControl");
+
+            DockPanel.SetDock(Btn, Dock.Left);
+            DockPanel.SetDock(TextBlock, Dock.Right);
+            panel.Children.Add(Btn);
+            panel.Children.Add(TextBlock);
+
+            return panel;
+        }
+        else
+        {
+            TextBox = new TextBox();
+            TextBox.Text = Field.Properties.Default;
+            TextBox.TextWrapping = Field.Properties.TextWrapping ? TextWrapping.Wrap : TextWrapping.NoWrap;
+            TextBox.TextAlignment = TextAlignment.Left;
+            TextBox.VerticalContentAlignment = VerticalAlignment.Center;
+            TextBox.VerticalAlignment = VerticalAlignment.Stretch;
+            TextBox.TextChanged += (sender, e) => ChangeField();
+
+            return TextBox;
+        }
+    }
+
+    public override void SetBackground(IBrush brush, bool defaultColour)
+    {
+        if (Field.Properties.PopupEditor)
+        {
+            Background = brush;
+        }
+        else
+        {
+            TextBox!.Background = brush;
+        }
+    }
+
+    [RelayCommand]
+    private async Task Popup()
+    {
+        var result = await Navigation.Popup<TextEditViewModel, string?>(x =>
+        {
+            x.Text = Text;
+            x.Multiline = true;
+        });
+        if(result is not null)
+        {
+            Text = result;
+            ChangeField();
+        }
+    }
+
+    public override string GetValue() => Text;
+
+    public override void SetValue(string? value) => Text = value;
+
+    protected override bool IsEmpty() => string.IsNullOrWhiteSpace(Text);
+}

+ 9 - 0
PRS.Avalonia/PRS.Avalonia/Theme/FormControlStyles.axaml

@@ -18,4 +18,13 @@
         <Setter Property="Foreground" Value="{DynamicResource PrsButtonForeground}" />
 		<Setter Property="CornerRadius" Value="{DynamicResource PrsCornerRadius}"/>
 	</Style>
+	
+	<Style Selector=":is(forms|DigitalFormControl).DFFieldControl TextBlock.DFStringFieldControl">
+        <Setter Property="Foreground" Value="{DynamicResource PrsButtonForeground}" />
+		<Setter Property="VerticalAlignment" Value="Center"/>
+		<Setter Property="TextWrapping" Value="NoWrap"/>
+		<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
+		<Setter Property="MaxLines" Value="1"/>
+		<Setter Property="Margin" Value="5,0"/>
+	</Style>
 </Styles>