Forráskód Böngészése

Finished up option and boolean controls

Kenric Nugteren 3 hónapja
szülő
commit
f63c9e1b90

+ 6 - 4
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/DigitalFormViewer.axaml.cs

@@ -244,7 +244,8 @@ public partial class DigitalFormViewer : UserControl, IDFRenderer
             control.SetControl(item);
             if(control is IDigitalFormField fieldControl && item is DFLayoutField field)
             {
-                fieldControl.SetBackground(GetItemBackgroundBrush(field));
+                var brush = GetItemBackgroundBrush(field);
+                fieldControl.SetBackground(brush ?? FieldBrush, brush is null);
             }
         }
         return control;
@@ -397,7 +398,7 @@ public partial class DigitalFormViewer : UserControl, IDFRenderer
         return fieldControl?.GetData(dataField);
     }
 
-    private SolidColorBrush GetItemBackgroundBrush(DFLayoutControl item, System.Drawing.Color? colour = null)
+    private SolidColorBrush? GetItemBackgroundBrush(DFLayoutControl item, System.Drawing.Color? colour = null)
     {
         if(colour != null)
         {
@@ -406,7 +407,7 @@ public partial class DigitalFormViewer : UserControl, IDFRenderer
         }
 
         if (item is DFLayoutField field)
-            return field.GetPropertyValue<bool>("Required") ? RequiredFieldBrush : FieldBrush;
+            return field.GetPropertyValue<bool>("Required") ? RequiredFieldBrush : null;
         else
             return new SolidColorBrush(Colors.Transparent);
     }
@@ -419,7 +420,8 @@ public partial class DigitalFormViewer : UserControl, IDFRenderer
         var fieldControl = GetFieldControl(field);
         if (fieldControl is null) return;
 
-        fieldControl.SetBackground(GetItemBackgroundBrush(field, colour));
+        var background = GetItemBackgroundBrush(field, colour);
+        fieldControl.SetBackground(background ?? FieldBrush, background is null);
     }
 
     public void LoadValues(DFLoadStorage data)

+ 5 - 2
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFBooleanFieldControl.cs

@@ -46,8 +46,11 @@ class DFBooleanFieldControl : DigitalFormFieldControl<DFLayoutBooleanField, DFLa
         _ => OptionControl.IsEmpty()
     };
 
-    public override void SetBackground(IBrush brush)
+    public override void SetBackground(IBrush brush, bool defaultColour)
     {
-        OptionControl.Background = brush;
+        if (!defaultColour)
+        {
+            Background = brush;
+        }
     }
 }

+ 1 - 1
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFDateFieldControl.cs

@@ -35,7 +35,7 @@ class DFDateFieldControl : DigitalFormFieldControl<DFLayoutDateField, DFLayoutDa
 
     protected override bool IsEmpty() => Date.Date == null || Date.Date == DateTime.MinValue;
 
-    public override void SetBackground(IBrush brush)
+    public override void SetBackground(IBrush brush, bool defaultColour)
     {
         Date.Background = brush;
     }

+ 50 - 17
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/DFOptionFieldControl.cs

@@ -28,6 +28,12 @@ public class ButtonsOptionControl : ContentControl, IOptionControl
 
     private TabStrip _tabStrip;
 
+    IBrush? IOptionControl.Background
+    {
+        get => _tabStrip.Background;
+        set => _tabStrip.Background = value;
+    }
+
     public ButtonsOptionControl(string[] options, Action onChanged)
     {
         OnChanged = onChanged;
@@ -69,29 +75,32 @@ public class ButtonsOptionControl : ContentControl, IOptionControl
     public bool IsEmpty() => GetValue() == null;
 }
 
-public class RadioOptionControl : Grid, IOptionControl
+public class RadioOptionControl : ContentControl, IOptionControl
 {
     private Action OnChanged;
+    private Grid _grid;
 
     public RadioOptionControl(string[] options, Action onChanged)
     {
-        Margin = new Thickness(4, 2, 4, 2);
+        _grid = new Grid();
         
         OnChanged = onChanged;
 
         foreach (var option in options)
         {
-            RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
+            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
             var button = new RadioButton();
-            button.Margin = new Thickness(0, 2, 0, 2);
+            button.Margin = new(5, 0, 5, 0);
             button.Content = option.Replace("\r", "").Replace("\n", "");
             button.VerticalContentAlignment = VerticalAlignment.Center;
-            button.Padding = new Thickness(5.0F, 0.0F, 5.0F, 0.0F);
-            button.SetValue(RowProperty, Children.Count);
+            button.HorizontalAlignment = HorizontalAlignment.Stretch;
+            button.SetValue(Grid.RowProperty, _grid.Children.Count);
             button.Tag = option;
             button.IsCheckedChanged += Button_IsCheckedChanged;
-            Children.Add(button);
+            _grid.Children.Add(button);
         }
+
+        Content = _grid;
     }
 
     private void Button_IsCheckedChanged(object? sender, global::Avalonia.Interactivity.RoutedEventArgs e)
@@ -101,7 +110,7 @@ public class RadioOptionControl : Grid, IOptionControl
 
     public string? GetValue()
     {
-        foreach (var child in Children)
+        foreach (var child in _grid.Children)
         {
             if (child is RadioButton radio)
             {
@@ -116,7 +125,7 @@ public class RadioOptionControl : Grid, IOptionControl
 
     public void SetValue(string value)
     {
-        foreach (var child in Children)
+        foreach (var child in _grid.Children)
         {
             if (child is RadioButton radio)
             {
@@ -130,20 +139,34 @@ public class RadioOptionControl : Grid, IOptionControl
     public bool IsEmpty() => GetValue() == null;
 }
 
-public class ComboBoxOptionControl : ComboBox, IOptionControl
+public class ComboBoxOptionControl : ContentControl, IOptionControl
 {
     private readonly Action OnChanged;
 
+    private ComboBox _comboBox;
+
+    IBrush? IOptionControl.Background
+    {
+        get => _comboBox.Background;
+        set => _comboBox.Background = value;
+    }
+
     public ComboBoxOptionControl(string[] options, Action onChanged)
     {
-        // SetResourceReference(StyleProperty, typeof(ComboBox));
+        _comboBox = new ComboBox
+        {
+            HorizontalAlignment = HorizontalAlignment.Stretch,
+            VerticalAlignment = VerticalAlignment.Stretch
+        };
 
         OnChanged = onChanged;
 
         foreach (var option in options)
-            Items.Add(option);
+            _comboBox.Items.Add(option);
         VerticalContentAlignment = VerticalAlignment.Center;
-        SelectionChanged += ComboBoxOptionControl_SelectionChanged;
+        _comboBox.SelectionChanged += ComboBoxOptionControl_SelectionChanged;
+
+        Content = _comboBox;
     }
 
     private void ComboBoxOptionControl_SelectionChanged(object? sender, SelectionChangedEventArgs e)
@@ -153,12 +176,12 @@ public class ComboBoxOptionControl : ComboBox, IOptionControl
 
     public string? GetValue()
     {
-        return SelectedValue as string;
+        return _comboBox.SelectedValue as string;
     }
 
     public void SetValue(string value)
     {
-        SelectedValue = value;
+        _comboBox.SelectedValue = value;
     }
     public bool IsEmpty() => GetValue() == null;
 }
@@ -206,8 +229,18 @@ class DFOptionControl : DigitalFormFieldControl<DFLayoutOptionField, DFLayoutOpt
 
     protected override bool IsEmpty() => OptionControl.IsEmpty();
 
-    public override void SetBackground(IBrush brush)
+    public override void SetBackground(IBrush brush, bool defaultColour)
     {
-        OptionControl.Background = brush;
+        if(OptionControl is ButtonsOptionControl)
+        {
+            if (!defaultColour)
+            {
+                Background = brush;
+            }
+        }
+        else
+        {
+            OptionControl.Background = brush;
+        }
     }
 }

+ 8 - 2
PRS.Avalonia/PRS.Avalonia/Components/FormsEditor/Fields/IDigitalFormField.cs

@@ -43,7 +43,13 @@ internal interface IDigitalFormField
     /// <returns><see langword="true"/> if the data is valid.</returns>
     public bool Validate([NotNullWhen(false)] out string? message);
 
-    public void SetBackground(IBrush brush);
+    /// <summary>
+    /// Set the background of the field. If <paramref name="defaultColour"/> is <see langword="true"/>, then this indicates
+    /// that this is the default colour for the field (light yellow), which may appear differently.
+    /// </summary>
+    /// <param name="brush"></param>
+    /// <param name="defaultColour"></param>
+    public void SetBackground(IBrush brush, bool defaultColour);
 }
 
 internal abstract class DigitalFormFieldControl<TField, TProperties, TValue, TSerialized> : DigitalFormControl<TField>, IDigitalFormField
@@ -108,5 +114,5 @@ internal abstract class DigitalFormFieldControl<TField, TProperties, TValue, TSe
     object? IDigitalFormField.GetValue() => GetValue();
     void IDigitalFormField.SetValue(object? value) => SetValue(value != null ? (TValue)value : default);
 
-    public abstract void SetBackground(IBrush brush);
+    public abstract void SetBackground(IBrush brush, bool defaultColour);
 }

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

@@ -11,4 +11,11 @@
         <Setter Property="Foreground" Value="{DynamicResource PrsButtonForeground}" />
         <Setter Property="CornerRadius" Value="{DynamicResource PrsCornerRadius}" />
 	</Style>
+	
+	<Style Selector=":is(forms|DigitalFormControl).DFFieldControl forms|RadioOptionControl">
+        <Setter Property="BorderBrush" Value="{DynamicResource PrsButtonBorder}" />
+        <Setter Property="BorderThickness" Value="{DynamicResource PrsBorderThickness}"/>
+        <Setter Property="Foreground" Value="{DynamicResource PrsButtonForeground}" />
+		<Setter Property="CornerRadius" Value="{DynamicResource PrsCornerRadius}"/>
+	</Style>
 </Styles>