forked from GMPrakhar/MAUI-Designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementCreator.cs
201 lines (185 loc) · 5.43 KB
/
ElementCreator.cs
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
using Microsoft.Maui.Controls.Shapes;
using System;
using System.Collections.Generic;
using Microsoft.Maui.Controls;
namespace MAUIDesigner
{
internal abstract class ElementFactory
{
public abstract View CreateElement();
}
internal class LabelFactory : ElementFactory
{
public override View CreateElement()
{
return new Label
{
Margin = new Thickness(20),
MinimumHeightRequest = 20,
MinimumWidthRequest = 20,
Text = "Drag me!"
};
}
}
internal class EditorFactory : ElementFactory
{
public override View CreateElement()
{
return new Editor
{
Margin = new Thickness(20),
MinimumHeightRequest = 20,
MinimumWidthRequest = 20,
Text = "Type here",
IsEnabled = true
};
}
}
internal class LayoutFactory : ElementFactory
{
public override View CreateElement()
{
return new StackLayout
{
Margin = new Thickness(20),
MinimumHeightRequest = 20,
MinimumWidthRequest = 20,
HeightRequest = 50,
WidthRequest = 50,
BackgroundColor = Colors.Coral
};
}
}
internal class RectangleFactory : ElementFactory
{
public override View CreateElement()
{
return new Rectangle
{
Margin = new Thickness(20),
MinimumHeightRequest = 20,
MinimumWidthRequest = 20,
HeightRequest = 50,
WidthRequest = 100
};
}
}
internal class RoundRectangleFactory : ElementFactory
{
public override View CreateElement()
{
return new RoundRectangle
{
Margin = new Thickness(20),
MinimumHeightRequest = 20,
MinimumWidthRequest = 20,
HeightRequest = 60,
WidthRequest = 120,
CornerRadius = new CornerRadius(10),
Fill = new SolidColorBrush(Colors.LightBlue)
};
}
}
internal class ButtonFactory : ElementFactory
{
public override View CreateElement()
{
return new Button
{
Margin = new Thickness(10),
MinimumHeightRequest = 20,
MinimumWidthRequest = 20,
Text = "Click me!"
};
}
}
internal class EllipseFactory : ElementFactory
{
public override View CreateElement()
{
return new Ellipse
{
Margin = new Thickness(20),
MinimumHeightRequest = 44,
MinimumWidthRequest = 88
};
}
}
internal class LineFactory : ElementFactory
{
public override View CreateElement()
{
return new Line
{
Margin = new Thickness(20),
MinimumHeightRequest = 44,
MinimumWidthRequest = 88
};
}
}
internal class EntryFactory : ElementFactory
{
public override View CreateElement()
{
return new Entry
{
Margin = new Thickness(20),
MinimumHeightRequest = 44,
MinimumWidthRequest = 44,
Placeholder = "Enter text"
};
}
}
internal class FrameFactory : ElementFactory
{
public override View CreateElement()
{
return new Frame
{
Margin = new Thickness(20),
MinimumHeightRequest = 44,
MinimumWidthRequest = 44,
HasShadow = false
};
}
}
internal class ImageButtonFactory : ElementFactory
{
public override View CreateElement()
{
return new ImageButton
{
Margin = new Thickness(20),
MinimumHeightRequest = 44,
MinimumWidthRequest = 44
};
}
}
internal static class ElementCreator
{
private static readonly Dictionary<string, ElementFactory> factories = new()
{
{ nameof(Label), new LabelFactory() },
{ nameof(Editor), new EditorFactory() },
{ nameof(StackLayout), new LayoutFactory() },
{ nameof(Rectangle), new RectangleFactory() },
{ nameof(RoundRectangle), new RoundRectangleFactory() },
{ nameof(Button), new ButtonFactory() },
{ nameof(Ellipse), new EllipseFactory() },
{ nameof(Line), new LineFactory() },
{ nameof(Entry), new EntryFactory() },
{ nameof(Frame), new FrameFactory() },
{ nameof(ImageButton), new ImageButtonFactory() }
};
internal static View Create(string elementTypeName)
{
if (factories.TryGetValue(elementTypeName, out var factory))
{
return factory.CreateElement();
}
var elementType = typeof(View).Assembly.GetTypes().FirstOrDefault(t => t.Name == elementTypeName);
var newElement = Activator.CreateInstance(elementType) as View;
return newElement;
}
}
}