blob: 8ec6d5b2f0f79184052031fc5b1204bbe3e83814 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
using System.Threading.Tasks;
using Avalonia.Markup.Xaml;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using UVtools.WPF.Extensions;
using UVtools.WPF.Windows;
namespace UVtools.WPF.Controls.Tools
{
public class ToolControl : UserControlEx
{
private Operation _baseOperation;
public Operation BaseOperation
{
get => _baseOperation;
set
{
if(!RaiseAndSetIfChanged(ref _baseOperation, value)) return;
if (DataContext is null) return;
ResetDataContext();
}
}
public ToolWindow ParentWindow { get; set; } = null;
public bool CanRun { get; set; } = true;
public ToolControl()
{
InitializeComponent();
}
public ToolControl(Operation operation) : this()
{
BaseOperation = operation;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
public virtual void Callback(ToolWindow.Callbacks callback) { }
public virtual bool UpdateOperation() => true;
/*public virtual void SetOperation(Operation operation)
{
BaseOperation = operation;
ResetDataContext();
}*/
/// <summary>
/// Validates if is safe to continue with operation
/// </summary>
/// <returns></returns>
public virtual async Task<bool> ValidateForm()
{
if (BaseOperation is null) return true;
if (!UpdateOperation()) return false;
return await ValidateFormFromString(BaseOperation.Validate());
}
/// <summary>
/// Validates if is safe to continue with operation, if not shows a message box with the error
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public async Task<bool> ValidateFormFromString(string text)
{
if (string.IsNullOrEmpty(text)) return true;
await ParentWindow.MessageBoxError(text);
return false;
}
/// <summary>
/// Validates if is safe to continue with operation, if not shows a message box with the error
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public async Task<bool> ValidateFormFromString(StringTag text) => await ValidateFormFromString(text?.ToString());
}
}
|