|
@@ -0,0 +1,185 @@
|
|
|
+using Avalonia.Controls;
|
|
|
+using Avalonia.Media;
|
|
|
+using InABox.Avalonia;
|
|
|
+using InABox.Avalonia.Components;
|
|
|
+using InABox.Avalonia.Converters;
|
|
|
+using InABox.Core;
|
|
|
+using Svg;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using Topten.RichTextKit.Editor;
|
|
|
+
|
|
|
+namespace PRS.Avalonia.DigitalForms;
|
|
|
+
|
|
|
+internal class DFSignaturePadControl : DigitalFormFieldControl<DFLayoutSignaturePad, DFLayoutSignaturePadProperties, byte[], byte[]?>
|
|
|
+{
|
|
|
+ private Border Border = null!;
|
|
|
+
|
|
|
+ private Button Save = null!;
|
|
|
+ private Button Apply = null!;
|
|
|
+ private Button Clear = null!;
|
|
|
+
|
|
|
+ private InkCanvas Canvas = null!;
|
|
|
+ private Image Image = null!;
|
|
|
+
|
|
|
+ private byte[]? _value = null;
|
|
|
+
|
|
|
+ private bool _enabled = true;
|
|
|
+ private bool _changed = false;
|
|
|
+
|
|
|
+ private IBrush? _backgroundColor = new SolidColorBrush(Colors.LightYellow);
|
|
|
+ private IBrush? _disabledBackground = new SolidColorBrush(Colors.LightGray);
|
|
|
+
|
|
|
+ protected override Control Create()
|
|
|
+ {
|
|
|
+ Border = new Border();
|
|
|
+ Border.Classes.Add("Standard");
|
|
|
+
|
|
|
+ var grid = new Grid();
|
|
|
+ grid.AddColumn(GridUnitType.Star);
|
|
|
+ grid.AddColumn(GridUnitType.Star);
|
|
|
+ grid.AddColumn(GridUnitType.Star);
|
|
|
+
|
|
|
+ grid.AddRow(GridUnitType.Star);
|
|
|
+ grid.AddRow(GridUnitType.Auto);
|
|
|
+
|
|
|
+ Canvas = new InkCanvas();
|
|
|
+ Image = new Image
|
|
|
+ {
|
|
|
+ IsVisible = false
|
|
|
+ };
|
|
|
+
|
|
|
+ Save = CreateButton("Save New", SaveSignature, 0, false);
|
|
|
+ Apply = CreateButton("Apply Saved", ApplySignature, 1, ViewModelBase.Repositories.Me.Signature?.Any() == true);
|
|
|
+ Clear = CreateButton("Clear", ClearSignature, 2, false);
|
|
|
+
|
|
|
+ Canvas.LineColour = Colors.Blue;
|
|
|
+
|
|
|
+ Canvas.Changed += (o, e) =>
|
|
|
+ {
|
|
|
+ _changed = true;
|
|
|
+ UpdateStatus();
|
|
|
+ ChangeField();
|
|
|
+ };
|
|
|
+
|
|
|
+ grid.Children.Add(Save);
|
|
|
+ grid.Children.Add(Apply);
|
|
|
+ grid.Children.Add(Clear);
|
|
|
+ grid.AddChild(Canvas, 0, 0, rowSpan: 1, colSpan: 3);
|
|
|
+ grid.AddChild(Image, 0, 0, rowSpan: 1, colSpan: 3);
|
|
|
+
|
|
|
+ Border.Child = grid;
|
|
|
+
|
|
|
+ return Border;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ClearSignature()
|
|
|
+ {
|
|
|
+ Canvas.Clear();
|
|
|
+ _value = null;
|
|
|
+ UpdateValue();
|
|
|
+ ChangeField();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ApplySignature()
|
|
|
+ {
|
|
|
+ _value = ViewModelBase.Repositories.Me.Signature;
|
|
|
+ UpdateValue();
|
|
|
+ ChangeField();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SaveSignature()
|
|
|
+ {
|
|
|
+ var data = Canvas.SaveImage();
|
|
|
+ _value = data;
|
|
|
+ ViewModelBase.Repositories.Me.Signature = data;
|
|
|
+ ViewModelBase.Repositories.Me.Save("Updated Signature");
|
|
|
+
|
|
|
+ UpdateValue();
|
|
|
+ ChangeField();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateValue()
|
|
|
+ {
|
|
|
+ if(_value?.Any() == true)
|
|
|
+ {
|
|
|
+ Image.Source = ByteArrayToImageSourceConverter.Instance.Convert(_value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Canvas.Clear();
|
|
|
+ }
|
|
|
+ _changed = false;
|
|
|
+
|
|
|
+ UpdateStatus();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateStatus()
|
|
|
+ {
|
|
|
+ Canvas.IsVisible = _value?.Any() != true && _enabled;
|
|
|
+ Image.IsVisible = !Canvas.IsVisible;
|
|
|
+
|
|
|
+ Save.IsVisible = _enabled;
|
|
|
+ Apply.IsVisible = _enabled;
|
|
|
+ Clear.IsVisible = _enabled;
|
|
|
+
|
|
|
+ Save.IsEnabled = _enabled && _changed;
|
|
|
+ Apply.IsEnabled = _enabled && ViewModelBase.Repositories.Me.Signature?.Any() == true;
|
|
|
+ Clear.IsEnabled = _enabled && (_value?.Any() == true || _changed);
|
|
|
+ Border.Background = Canvas.IsVisible ? _backgroundColor : _disabledBackground;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Button CreateButton(string text, Action clicked, int column, bool enabled)
|
|
|
+ {
|
|
|
+ var button = new Button();
|
|
|
+ button.Content = text;
|
|
|
+ button.Classes.Add("Standard");
|
|
|
+ Grid.SetRow(button, 1);
|
|
|
+ Grid.SetColumn(button, column);
|
|
|
+ button.Click += (o, e) => clicked();
|
|
|
+ button.IsEnabled = enabled;
|
|
|
+ return button;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override byte[] GetValue()
|
|
|
+ {
|
|
|
+ return GetSerializedValue() ?? [];
|
|
|
+ }
|
|
|
+
|
|
|
+ public override byte[]? GetSerializedValue()
|
|
|
+ {
|
|
|
+ return Canvas.SaveImage();
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void SetBackground(IBrush brush, bool defaultColour)
|
|
|
+ {
|
|
|
+ _backgroundColor = brush;
|
|
|
+ Border.Background = Canvas.IsVisible ? _backgroundColor : _disabledBackground;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void SetEnabled(bool enabled)
|
|
|
+ {
|
|
|
+ _enabled = enabled;
|
|
|
+ IsEnabled = _enabled;
|
|
|
+ UpdateStatus();
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void SetSerializedValue(byte[]? value)
|
|
|
+ {
|
|
|
+ SetValue(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void SetValue(byte[]? value)
|
|
|
+ {
|
|
|
+ _value = value;
|
|
|
+ UpdateValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override bool IsEmpty()
|
|
|
+ {
|
|
|
+ return _value is not null;
|
|
|
+ }
|
|
|
+}
|