taitank-safe 0.1.0

taitank in safe rust
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 *
 * Tencent is pleased to support the open source community by making Taitank available.
 * Copyright (C) 2021 THL A29 Limited, a Tencent company.  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 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.
 *
 */

#include "taitank.h"

#include "taitank_util.h"

namespace taitank {

TaitankNodeRef NodeCreate() { return new TaitankNode(); }

TaitankNodeRef NodeCreateWithConfig(TaitankConfigRef config) { return new TaitankNode(config); }

void NodeFree(TaitankNodeRef node) {
  if (node == nullptr) return;
  // free self
  delete node;
}

void NodeFreeRecursive(TaitankNodeRef node) {
  if (node == nullptr) {
    return;
  }

  while (node->ChildCount() > 0) {
    TaitankNodeRef child = node->GetChild(0);
    NodeFreeRecursive(child);
  }

  NodeFree(node);
}

void SetDirection(TaitankNodeRef node, TaitankDirection direction) {
  if (node == nullptr || node->style_.direction_ == direction) {
    return;
  }

  node->style_.direction_ = direction;
  node->MarkAsDirty();
}

bool SetMeasureFunction(TaitankNodeRef node, TaitankMeasureFunction measure_function) {
  if (node == nullptr) return false;

  return node->SetMeasureFunction(measure_function);
}

void SetWidth(TaitankNodeRef node, float width) {
  if (node == nullptr || FloatIsEqual(node->style_.dim_[DIMENSION_WIDTH], width)) {
    return;
  }

  node->style_.dim_[DIMENSION_WIDTH] = width;
  node->MarkAsDirty();
}

void SetHeight(TaitankNodeRef node, float height) {
  if (node == nullptr || FloatIsEqual(node->style_.dim_[DIMENSION_HEIGHT], height)) return;

  node->style_.dim_[DIMENSION_HEIGHT] = height;
  node->MarkAsDirty();
}

void SetFlex(TaitankNodeRef node, float flex) {
  if (node == nullptr || FloatIsEqual(node->style_.flex_, flex)) return;
  if (FloatIsEqual(flex, 0.0f)) {
    SetFlexGrow(node, 0.0f);
    SetFlexShrink(node, 0.0f);
  } else if (flex > 0.0f) {
    SetFlexGrow(node, flex);
    SetFlexShrink(node, 1.0f);
  } else {
    SetFlexGrow(node, 0.0f);
    SetFlexShrink(node, -flex);
  }
  node->style_.flex_ = flex;
  node->MarkAsDirty();
}

void SetFlexGrow(TaitankNodeRef node, float flex_grow) {
  if (node == nullptr || FloatIsEqual(node->style_.flex_grow_, flex_grow)) return;

  node->style_.flex_grow_ = flex_grow;
  node->MarkAsDirty();
}

void SetFlexShrink(TaitankNodeRef node, float flex_shrink) {
  if (node == nullptr || FloatIsEqual(node->style_.flex_shrink_, flex_shrink)) return;

  node->style_.flex_shrink_ = flex_shrink;
  node->MarkAsDirty();
}

void SetFlexBasis(TaitankNodeRef node, float flex_basis) {
  if (node == nullptr || FloatIsEqual(node->style_.flex_basis_, flex_basis)) return;

  node->style_.flex_basis_ = flex_basis;
  node->MarkAsDirty();
}

void SetFlexDirection(TaitankNodeRef node, FlexDirection direction) {
  if (node == nullptr || node->style_.flex_direction_ == direction) return;

  node->style_.flex_direction_ = direction;
  node->MarkAsDirty();
}

void SetPositionType(TaitankNodeRef node, PositionType position_type) {
  if (node == nullptr || node->style_.position_type_ == position_type) return;
  node->style_.position_type_ = position_type;
  node->MarkAsDirty();
}

void SetPosition(TaitankNodeRef node, CSSDirection dir, float value) {
  if (node == nullptr || FloatIsEqual(node->style_.position_[dir], value)) return;
  if (node->style_.SetPosition(dir, value)) {
    node->MarkAsDirty();
  }
}

void SetMargin(TaitankNodeRef node, CSSDirection dir, float value) {
  if (node == nullptr) return;
  if (node->style_.SetMargin(dir, value)) {
    node->MarkAsDirty();
  }
}

void SetMarginAuto(TaitankNodeRef node, CSSDirection dir) { SetMargin(node, dir, VALUE_AUTO); }

void SetPadding(TaitankNodeRef node, CSSDirection dir, float value) {
  if (node == nullptr) return;
  if (node->style_.SetPadding(dir, value)) {
    node->MarkAsDirty();
  }
}

void SetBorder(TaitankNodeRef node, CSSDirection dir, float value) {
  if (node == nullptr) return;
  if (node->style_.SetBorder(dir, value)) {
    node->MarkAsDirty();
  }
}

void SetFlexWrap(TaitankNodeRef node, FlexWrapMode wrap_mode) {
  if (node == nullptr || node->style_.flex_wrap_ == wrap_mode) return;

  node->style_.flex_wrap_ = wrap_mode;
  node->MarkAsDirty();
}

void SetJustifyContent(TaitankNodeRef node, FlexAlign justify) {
  if (node == nullptr || node->style_.justify_content_ == justify) return;
  node->style_.justify_content_ = justify;
  node->MarkAsDirty();
}

void SetAlignContent(TaitankNodeRef node, FlexAlign align) {
  if (node == nullptr || node->style_.align_content_ == align) return;
  node->style_.align_content_ = align;
  node->MarkAsDirty();
}

void SetAlignItems(TaitankNodeRef node, FlexAlign align) {
  if (node == nullptr || node->style_.align_items_ == align) return;
  // FlexAlignStart == FlexAlignBaseline
  node->style_.align_items_ = align;
  node->MarkAsDirty();
}

void SetAlignSelf(TaitankNodeRef node, FlexAlign align) {
  if (node == nullptr || node->style_.align_self_ == align) return;
  node->style_.align_self_ = align;
  node->MarkAsDirty();
}

void SetDisplay(TaitankNodeRef node, DisplayType display_type) {
  if (node == nullptr) return;
  node->SetDisplayType(display_type);
}

void SetMaxWidth(TaitankNodeRef node, float value) {
  if (node == nullptr || FloatIsEqual(node->style_.max_dim_[DIMENSION_WIDTH], value)) return;
  node->style_.max_dim_[DIMENSION_WIDTH] = value;
  node->MarkAsDirty();
}

void SetMaxHeight(TaitankNodeRef node, float value) {
  if (node == nullptr || FloatIsEqual(node->style_.max_dim_[DIMENSION_HEIGHT], value)) return;
  node->style_.max_dim_[DIMENSION_HEIGHT] = value;
  node->MarkAsDirty();
}

void SetMinWidth(TaitankNodeRef node, float value) {
  if (node == nullptr || FloatIsEqual(node->style_.min_dim_[DIMENSION_WIDTH], value)) return;
  node->style_.min_dim_[DIMENSION_WIDTH] = value;
  node->MarkAsDirty();
}

void SetMinHeight(TaitankNodeRef node, float value) {
  if (node == nullptr || FloatIsEqual(node->style_.min_dim_[DIMENSION_HEIGHT], value)) return;
  node->style_.min_dim_[DIMENSION_HEIGHT] = value;
  node->MarkAsDirty();
}

void SetOverflow(TaitankNodeRef node, OverflowType overflow_type) {
  if (node == nullptr || overflow_type == node->style_.overflow_type_) return;

  node->style_.overflow_type_ = overflow_type;
  node->MarkAsDirty();
}

void SetNodeType(TaitankNodeRef node, NodeType node_type) {
  if (node == nullptr || node_type == node->style_.node_type_) return;
  node->style_.node_type_ = node_type;
  //  node->markAsDirty();
}

float GetLeft(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->layout_result_.position[CSS_LEFT];
}

float GetTop(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->layout_result_.position[CSS_TOP];
}

float GetRight(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->layout_result_.position[CSS_RIGHT];
}

float GetBottom(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->layout_result_.position[CSS_BOTTOM];
}

float GetWidth(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->layout_result_.dim[DIMENSION_WIDTH];
}

float GetHeight(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->layout_result_.dim[DIMENSION_HEIGHT];
}

float GetMaxWidth(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.max_dim_[DIMENSION_WIDTH];
}

float GetMaxHeight(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.max_dim_[DIMENSION_HEIGHT];
}

float GetMinWidth(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.min_dim_[DIMENSION_WIDTH];
}

float GetMinHeight(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.min_dim_[DIMENSION_HEIGHT];
}

float GetMargin(TaitankNodeRef node, CSSDirection dir) {
  if (node == nullptr || dir > CSS_BOTTOM) return 0;
  return node->layout_result_.margin[dir];
}

float GetPadding(TaitankNodeRef node, CSSDirection dir) {
  if (node == nullptr || dir > CSS_BOTTOM) return 0;
  return node->layout_result_.padding[dir];
}

float GetBorder(TaitankNodeRef node, CSSDirection dir) {
  if (node == nullptr || dir > CSS_BOTTOM) return 0;
  return node->layout_result_.border[dir];
}

float GetFlexGrow(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.flex_grow_;
}

float GetFlexShrink(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.flex_shrink_;
}

DisplayType GetDisplay(TaitankNodeRef node) {
  if (node == nullptr) return DISPLAY_TYPE_FLEX;
  return node->style_.display_type_;
}

float GetFlexBasis(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->style_.GetFlexBasis();
}

FlexDirection GetFlexDirection(TaitankNodeRef node) {
  if (node == nullptr) return FLEX_DIRECTION_ROW;
  return node->style_.flex_direction_;
}

FlexAlign GetJustifyContent(TaitankNodeRef node) {
  if (node == nullptr) return FLEX_ALIGN_START;
  return node->style_.align_content_;
}

FlexAlign GetAlignSelf(TaitankNodeRef node) {
  if (node == nullptr) return FLEX_ALIGN_AUTO;
  return node->style_.align_self_;
}

FlexAlign GetAlignItems(TaitankNodeRef node) {
  if (node == nullptr) return FLEX_ALIGN_START;
  return node->style_.align_items_;
}

PositionType GetPositionType(TaitankNodeRef node) {
  if (node == nullptr) return POSITION_TYPE_RELATIVE;
  return node->style_.position_type_;
}

FlexWrapMode GetFlexWrap(TaitankNodeRef node) {
  if (node == nullptr) return FLEX_NO_WRAP;
  return node->style_.flex_wrap_;
}

OverflowType HPNodeLayoutGetOverflow(TaitankNodeRef node) {
  if (node == nullptr) return OVERFLOW_VISIBLE;
  return node->style_.overflow_type_;
}

bool GetHadOverflow(TaitankNodeRef node) {
  if (node == nullptr) return false;
  return node->layout_result_.had_overflow;
}

void SetConfig(TaitankNodeRef node, TaitankConfigRef config) { node->SetConfig(config); }

void ConfigFree(TaitankConfigRef config) { delete config; }

TaitankConfigRef ConfigGetDefault() {
  static TaitankConfigRef defaultConfig = new TaitankConfig();
  return defaultConfig;
}

void SetContext(TaitankNodeRef node, void *context) {
  if (node) {
    node->SetContext(context);
  }
}

void *GetContext(TaitankNodeRef node) {
  if (node) {
    return node->GetContext();
  }
  return nullptr;
}

bool InsertChild(TaitankNodeRef node, TaitankNodeRef child, uint32_t index) {
  if (node == nullptr) return false;

  return node->InsertChild(child, index);
}

bool RemoveChild(TaitankNodeRef node, TaitankNodeRef child) {
  if (node == nullptr) return false;
  return node->RemoveChild(child);
}

uint32_t ChildCount(TaitankNodeRef node) {
  if (node == nullptr) return 0;
  return node->ChildCount();
}

TaitankNodeRef GetChild(TaitankNodeRef node, uint32_t index) {
  if (node == nullptr) return 0;
  if (index >= node->ChildCount()) return nullptr;
  return node->GetChild(index);
}

bool GetHasNewLayout(TaitankNodeRef node) {
  if (node == nullptr) return false;
  return node->GetHasNewLayout();
}

void SetHasNewLayout(TaitankNodeRef node, bool has_new_layout) {
  if (node == nullptr) return;
  node->SetHasNewLayout(has_new_layout);
}

void MarkDirty(TaitankNodeRef node) {
  if (node == nullptr) return;
  node->MarkAsDirty();
}

bool IsDirty(TaitankNodeRef node) {
  if (node == nullptr) return false;
  return node->is_dirty_;
}

void DoLayout(TaitankNodeRef node, float parent_width, float parent_height,
              TaitankDirection direction, void *layoutContext) {
  if (node == nullptr) return;

  node->Layout(parent_width, parent_height, node->GetConfig(), direction, layoutContext);
}

void Print(TaitankNodeRef node) {
  if (node == nullptr) return;
  node->PrintNode();
}

bool Reset(TaitankNodeRef node) {
  if (node == nullptr || node->ChildCount() != 0 || node->GetParent() != nullptr) return false;

  return node->Reset();
}
}  // namespace taitank