-
Notifications
You must be signed in to change notification settings - Fork 2
/
Knockoff.Binding.pas
339 lines (293 loc) · 9.59 KB
/
Knockoff.Binding.pas
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
{***************************************************************************}
{ }
{ Knockoff prototype }
{ }
{ Copyright (c) 2015 Stefan Glienke - All rights reserved }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit Knockoff.Binding;
interface
uses
Classes;
type
BindAttribute = class(TCustomAttribute)
private
fTargetName: string;
fSourceName: string;
public
constructor Create(const targetName, sourceName: string);
procedure ApplyBinding(const target: TComponent; const source: TObject); virtual;
end;
BindOptionsAttribute = class(BindAttribute)
public
constructor Create(const sourceName: string);
procedure ApplyBinding(const target: TComponent; const source: TObject); override;
end;
BindOptionsCaptionAttribute = class(TCustomAttribute)
private
fOptionsCaption: string;
public
constructor Create(const optionsCaption: string);
property OptionsCaption: string read fOptionsCaption;
end;
BindOptionsTextAttribute = class(TCustomAttribute)
private
fOptionsText: string;
public
constructor Create(const optionsText: string);
property OptionsText: string read fOptionsText;
end;
procedure ApplyBindings(const view: TComponent; const viewModel: TObject);
procedure ApplyBindingsByConventions(const view: TComponent; const viewModel: TObject);
procedure Bind(const target: TComponent; const targetExpression: string;
const source: TObject; const sourceExpression: string);
implementation
uses
Generics.Collections,
Rtti,
StdCtrls,
StrUtils,
SysUtils,
Types,
Knockoff.Binding.Components,
Knockoff.Observable;
var
ctx: TRttiContext;
procedure ApplyBindings(const view: TComponent; const viewModel: TObject);
var
field: TRttiField;
attr: TCustomAttribute;
begin
if viewModel is TComponent then
if TComponent(viewModel).Owner = nil then
view.InsertComponent(TComponent(viewModel));
for field in ctx.GetType(view.ClassInfo).GetFields do
for attr in field.GetAttributes do
if attr is BindAttribute then
BindAttribute(attr).ApplyBinding(
field.GetValue(view).AsType<TComponent>,
viewModel);
end;
procedure ApplyBindingsByConventions(const view: TComponent; const viewModel: TObject);
var
target: TComponent;
typ: TRttiType;
prop: TRttiProperty;
begin
if viewModel is TComponent then
if TComponent(viewModel).Owner = nil then
view.InsertComponent(TComponent(viewModel));
typ := ctx.GetType(viewModel.ClassInfo);
for target in view do
begin
prop := typ.GetProperty(target.Name);
if prop <> nil then
begin
// hardcoded for now
if target is TEdit then
Bind(target, 'Value', viewModel, target.Name)
else if target is TLabel then
Bind(target, 'Text', viewModel, target.Name);
end;
end;
end;
function CreateObservable(instance: TObject;
const expression: string): IObservable;
function CreateRootProp(const prop: TRttiProperty; const instance: TObject): IObservable;
begin
Result := TDependentObservable.Create(
function: TValue
begin
Result := prop.GetValue(instance);
end,
procedure(const value: TValue)
begin
prop.SetValue(instance, value);
end);
end;
function CreateSubProp(const prop: TRttiProperty; const observable: IObservable): IObservable;
begin
Result := TDependentObservable.Create(
function: TValue
var
instance: TObject;
begin
instance := observable.Value.AsObject;
if Assigned(instance) then
Result := prop.GetValue(instance)
else
Result := nil;
end,
procedure(const value: TValue)
var
instance: TObject;
begin
instance := observable.Value.AsObject;
if Assigned(instance) then
prop.SetValue(instance, value);
end);
end;
var
expressions: TStringDynArray;
i: Integer;
typ: TRttiType;
prop: TRttiProperty;
begin
Result := nil;
expressions := SplitString(expression, '.');
typ := ctx.GetType(instance.ClassInfo);
for i := 0 to High(expressions) do
begin
prop := typ.GetProperty(expressions[i]);
if Assigned(prop) then
begin
if StartsText('Observable<', prop.PropertyType.Name)
or StartsText('ObservableArray<', prop.PropertyType.Name) then
begin
Result := prop.GetValue(instance).AsInterface as IObservable;
typ := prop.PropertyType.BaseType.GetMethod('Invoke').ReturnType;
end
else
begin
if i = 0 then
Result := CreateRootProp(prop, instance)
else
Result := CreateSubProp(prop, Result);
typ := prop.PropertyType;
end;
if i < High(expressions) then
instance := Result.Value.AsObject;
end;
end;
end;
procedure Bind(const target: TComponent; const targetExpression: string;
const source: TObject; const sourceExpression: string);
var
observable: IObservable;
typ: TRttiType;
method: TRttiMethod;
bindingClass: TBindingClass;
begin
observable := CreateObservable(source, sourceExpression);
if not Assigned(observable) then
begin
typ := ctx.GetType(source.ClassInfo);
method := typ.GetMethod(sourceExpression);
if Assigned(method) then
observable := TObservable.Create(
function: TValue
begin
Result := method.Invoke(source, []);
end);
end;
Assert(Assigned(observable), 'expression not found: ' + sourceExpression);
bindingClass := GetBindingClass(target, targetExpression);
if Assigned(bindingClass) then
bindingClass.Create(target, observable)
else
TComponentBinding.Create(target, observable, targetExpression);
end;
{$REGION 'BindAttribute'}
constructor BindAttribute.Create(const targetName, sourceName: string);
begin
inherited Create;
fTargetName := targetName;
fSourceName := sourceName;
end;
procedure BindAttribute.ApplyBinding(const target: TComponent;
const source: TObject);
begin
Bind(target, fTargetName, source, fSourceName);
end;
{$ENDREGION}
{$REGION 'BindOptionsAttribute'}
constructor BindOptionsAttribute.Create(const sourceName: string);
begin
inherited Create('options', sourceName);
end;
procedure BindOptionsAttribute.ApplyBinding(const target: TComponent;
const source: TObject);
var
a: TCustomAttribute;
t: TRttiType;
p: TRttiProperty;
items: TStrings;
l: TList<TObject>;
o: TObject;
i: Integer;
s: string;
optionsCaption: string;
optionsText: string;
begin
for a in ctx.GetType(target.Owner.ClassInfo).GetField(target.Name).GetAttributes do
begin
if a is BindOptionsCaptionAttribute then
optionsCaption := BindOptionsCaptionAttribute(a).OptionsCaption
else if a is BindOptionsTextAttribute then
optionsText := BindOptionsTextAttribute(a).OptionsText;
end;
t := ctx.GetType(source.ClassInfo);
p := t.GetProperty(fSourceName);
if Assigned(p) then
// just hardcode this for the Items property for now, make it dynamic later
if target is TComboBox then
begin
items := TComboBox(target).Items;
items.Clear;
if optionsCaption <> '' then
begin
items.Add(optionsCaption);
TComboBox(target).ItemIndex := 0;
end;
if p.PropertyType.Handle = TypeInfo(TArray<string>) then
begin
for s in p.GetValue(source).AsType<TArray<string>> do
items.Add(s);
end else
if StartsText('TList<', p.PropertyType.Name) then
begin
// assume that it is a list of objects for now
l := TList<TObject>(p.GetValue(source).AsObject);
for i := 0 to l.Count - 1 do
begin
o := l[i];
if optionsText <> '' then
p := ctx.GetType(o.ClassInfo).GetProperty(optionsText);
if Assigned(p) then
items.AddObject(p.GetValue(o).ToString, o)
else
items.AddObject(o.ToString, o);
end;
end;
end;
end;
{$ENDREGION}
{$REGION 'BindOptionsCaptionAttribute'}
constructor BindOptionsCaptionAttribute.Create(const optionsCaption: string);
begin
inherited Create;
fOptionsCaption := optionsCaption;
end;
{$ENDREGION}
{$REGION 'BindOptionsTextAttribute'}
constructor BindOptionsTextAttribute.Create(const optionsText: string);
begin
inherited Create;
fOptionsText := optionsText;
end;
{$ENDREGION}
end.