-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoenixForm.cs
More file actions
256 lines (221 loc) · 8.15 KB
/
PhoenixForm.cs
File metadata and controls
256 lines (221 loc) · 8.15 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
using Phoenix.Core;
using Phoenix.Helpers;
using Phoenix._System;
namespace Phoenix
{
public partial class PhoenixForm : Form
{
private Store _localStore = new Store();
private static Provider _provider = new Provider();
private PrivatePhoenixFormDictionary _subComponents = new PrivatePhoenixFormDictionary();
public delegate void TryCloseForm(Action onClose);
/// <summary>
/// The accessor that returns store of the form.
/// </summary>
[Browsable(false)]
public Store Store => _localStore;
/// <summary>
/// The accessor that returns static provider of the form.
/// </summary>
[Browsable(false)]
public static Provider Provider => _provider;
/// <summary>
/// An accessor called when the form is closed, allowing you to cancel or confirm the closing of the form.
/// </summary>
[Browsable(false)]
protected TryCloseForm OnTryCloseForm { get; set; } = null;
/// <summary>
/// Returns a list of connected sub-components to the form.
/// </summary>
[Browsable(false)]
protected PrivatePhoenixFormDictionary SubComponents => _subComponents;
internal event Action<dynamic> FormDidHide;
internal event Action<dynamic> FormDidShow;
internal Store GetStoreByType(string storeType = StoreTypes.LOCAL)
{
return storeType == StoreTypes.LOCAL ? _localStore : Provider.GlobalStore;
}
/// <summary>
/// A method that initializes the forms.
/// </summary>
protected void Init(params Type[] formTypes)
{
PContainer.Append(Name, this);
InitializeEvents();
Provider.GlobalStore.StoreType = StoreTypes.GLOBAL;
Provider.GlobalStore.CombinedStores += StoreCombined;
formTypes.ToList().ForEach((Type formType) =>
{
FormActivator.ProtectsAndValidatesPassedTypes(formType);
var formConstructor = FormActivator.CreateFormConstructor(formType);
FormActivator.AddFormConstructor(formType.Name, formConstructor);
});
}
/// <summary>
/// Method for connecting a sub-component to a form.
/// </summary>
protected void ConnectSubComponents(params PhoenixForm[] components)
{
Mathf.For(components.Length, (i) =>
{
PhoenixForm component = components[i];
_subComponents.Add(component.Name, component);
component.InitializeEvents();
component.EnableFormHiding();
});
}
internal void InitializeEvents()
{
Shown += FormDidFirstMount;
FormDidShow += FormDidMount;
FormDidHide += FormWillUnmount;
FormAddedListeners();
_localStore.DidChangeStore += StoreDidUpdate;
_localStore.WillChangeStore += StoreWillUpdate;
_localStore.Render += Render;
_localStore.CombinedStores += StoreCombined;
}
/// <summary>
/// A method to disable full form closing.
/// </summary>
internal void EnableFormHiding()
{
FormClosing += new FormClosingEventHandler(PhoenixClosing);
}
/// <summary>
/// A method to override disabling the closure of a form.
/// </summary>
internal void DisableFormHiding()
{
FormClosing -= new FormClosingEventHandler(PhoenixClosing);
}
private PhoenixException GetComponentException(string name)
{
return new PhoenixException(
$@"The component with the name ['{name}'] does not exist on the form - [{Name}]!",
new KeyNotFoundException()
);
}
/// <summary>
/// A method for searching a component on a form by its name.
/// As Generic, the type is passed to the type that will need to be returned on success.
/// </summary>
public T GetComponent<T>(string name) where T : Control
{
try
{
return Converting.ToType<T>(Controls.Find(name, true).ToArray()[0]);
}
catch (Exception)
{
throw GetComponentException(name);
}
}
/// <summary>
/// A method for searching a component on a form by its name.
/// </summary>
public Control GetComponent(string name)
{
try
{
return Controls.Find(name, true).ToArray()[0];
}
catch (Exception)
{
throw GetComponentException(name);
}
}
/// <summary>
/// A method for finding components on a form by their type.
/// Returns an array of found components in the form.
/// </summary>
public T[] GetComponent<T>() where T : Control
{
List<T> result = new List<T>();
foreach (Control control in Controls.OfType<T>())
result.Add((T)control);
return result.ToArray();
}
/// <summary>
/// The method hides the control from the user.
/// </summary>
public void Hide(dynamic args = null)
{
base.Hide();
FormDidHide?.Invoke(args);
}
/// <summary>
/// Form display method.
/// </summary>
public void Show(dynamic args = null)
{
base.Show();
FormDidShow?.Invoke(args);
}
/// <summary>
/// The method completely destroys the form from the system.
/// </summary>
public void Destroy()
{
PContainer.Delete(Name);
DisableFormHiding();
Close();
}
private void PhoenixClosing(object sender, FormClosingEventArgs e)
{
Action hideAction = () =>
{
e.Cancel = true;
Hide();
};
if (e.CloseReason != CloseReason.ApplicationExitCall)
{
if (!TypeMatchers.IsNull(OnTryCloseForm))
{
OnTryCloseForm(hideAction);
e.Cancel = true;
return;
}
hideAction();
}
}
/// <summary>
/// Lifecycle method, executed when the form is first launched.
/// </summary>
protected virtual void FormDidFirstMount(object sender, EventArgs e) { }
/// <summary>
/// Lifecycle method, executed when the form is created.
/// </summary>
protected virtual void FormDidMount(dynamic args) { }
/// <summary>
/// Lifecycle method, executed when the form is destroyed.
/// </summary>
protected virtual void FormWillUnmount(dynamic args) { }
/// <summary>
/// The lifecycle method is executed once when the form is first launched,
/// starting the listeners initialized in this method.
/// </summary>
protected virtual void FormAddedListeners() { }
/// <summary>
/// The store lifecycle method, which is called after it has been updated.
/// </summary>
protected virtual void StoreDidUpdate(Storage prevStore, Storage currentStore) { }
/// <summary>
/// The store lifecycle method, which is called before updating it.
/// </summary>
protected virtual void StoreWillUpdate(Storage prevStore, Storage currentStore) { }
/// <summary>
/// The lifecycle method is triggered when the local or global store of the form has been combined.
/// </summary>
protected virtual void StoreCombined(string storeType) { }
/// <summary>
/// A lifecycle method that is called after a data update to display up-to-date information.
/// </summary>
protected virtual void Render() {}
}
}