forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogControl.cs
More file actions
197 lines (159 loc) · 5.7 KB
/
DialogControl.cs
File metadata and controls
197 lines (159 loc) · 5.7 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace TensorStack.WPF.Controls
{
public class DialogControl : Window, INotifyPropertyChanged, System.Windows.Forms.IWin32Window
{
private readonly WindowInteropHelper _interopHelper;
public DialogControl()
{
_interopHelper = new WindowInteropHelper(this);
AllowsTransparency = true;
SnapsToDevicePixels = true;
WindowStyle = WindowStyle.None;
SizeToContent = SizeToContent.WidthAndHeight;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.Fant);
RenderOptions.SetClearTypeHint(this, ClearTypeHint.Enabled);
TextOptions.SetTextFormattingMode(this, TextFormattingMode.Ideal);
TextOptions.SetTextRenderingMode(this, TextRenderingMode.ClearType);
TextOptions.SetTextHintingMode(this, TextHintingMode.Fixed);
CloseCommand = new AsyncRelayCommand(CloseAsync);
Loaded += (s, e) => CreateOpenAnimation();
}
public nint Handle => _interopHelper.Handle;
public AsyncRelayCommand CloseCommand { get; }
public WindowBase OwnerWindow => Owner as WindowBase;
public virtual new bool ShowDialog()
{
Opacity = 0;
return base.ShowDialog() ?? false;
}
public virtual Task<bool> ShowDialogAsync()
{
Opacity = 0;
return Task.FromResult(ShowDialog());
}
protected virtual Task CloseAsync()
{
Close();
OwnerWindow.IsDialogVisible = false;
return Task.CompletedTask;
}
protected virtual Task SaveAsync()
{
return CreateCloseAnimation(true);
}
protected virtual bool CanExecuteSave()
{
return true;
}
protected virtual Task CancelAsync()
{
return CreateCloseAnimation(false);
}
protected virtual bool CanExecuteCancel()
{
return true;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_interopHelper.EnsureHandle();
this.RegisterDisplayMonitor();
}
private void CreateOpenAnimation()
{
OwnerWindow.IsDialogVisible = true; // Dialog Open
var fadeInAnimation = new DoubleAnimation
{
From = 0,
To = 1,
Duration = new Duration(TimeSpan.FromMilliseconds(200)),
FillBehavior = FillBehavior.Stop,
EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut }
};
fadeInAnimation.Completed += (s, e) => { Opacity = 1; };
BeginAnimation(OpacityProperty, fadeInAnimation);
}
private Task CreateCloseAnimation(bool dialogResult)
{
OwnerWindow.IsDialogVisible = false; // Dialog Closed
var tcs = new TaskCompletionSource();
var fadeOutAnimation = new DoubleAnimation
{
From = 1,
To = 0,
Duration = new Duration(TimeSpan.FromMilliseconds(200)),
FillBehavior = FillBehavior.Stop,
EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut }
};
fadeOutAnimation.Completed += (s, e) =>
{
try
{
DialogResult = dialogResult;
}
finally
{
tcs.SetResult();
}
};
BeginAnimation(OpacityProperty, fadeOutAnimation);
return tcs.Task;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string property = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
{
if (!Equals(field, newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
return false;
}
#endregion
}
public readonly record struct DialogResult
{
private readonly bool _result;
private readonly bool _dontAskAgain;
public DialogResult(bool result, bool dontAskAgain)
{
_result = result;
_dontAskAgain = dontAskAgain;
}
public readonly bool Result => _result;
public readonly bool DontAskAgain => _dontAskAgain;
public static implicit operator bool(DialogResult dialogResult)
{
return dialogResult._result;
}
public static implicit operator DialogResult(bool result)
{
return new DialogResult(result, false);
}
public static bool operator true(DialogResult dialogResult)
{
return dialogResult._result;
}
public static bool operator false(DialogResult dialogResult)
{
return !dialogResult._result;
}
}
}