1 /**
2  * @file lv_flex.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "../lv_layouts.h"
10 
11 #if LV_USE_FLEX
12 
13 /*********************
14  *      DEFINES
15  *********************/
16 
17 /**********************
18  *      TYPEDEFS
19  **********************/
20 typedef struct {
21     lv_flex_align_t main_place;
22     lv_flex_align_t cross_place;
23     lv_flex_align_t track_place;
24     uint8_t row : 1;
25     uint8_t wrap : 1;
26     uint8_t rev : 1;
27 } flex_t;
28 
29 typedef struct {
30     lv_obj_t * item;
31     lv_coord_t min_size;
32     lv_coord_t max_size;
33     lv_coord_t final_size;
34     uint32_t grow_value;
35     uint32_t clamped : 1;
36 } grow_dsc_t;
37 
38 typedef struct {
39     lv_coord_t track_cross_size;
40     lv_coord_t track_main_size;         /*For all items*/
41     lv_coord_t track_fix_main_size;     /*For non grow items*/
42     uint32_t item_cnt;
43     grow_dsc_t * grow_dsc;
44     uint32_t grow_item_cnt;
45     uint32_t grow_dsc_calc : 1;
46 } track_t;
47 
48 /**********************
49  *  GLOBAL PROTOTYPES
50  **********************/
51 
52 /**********************
53  *  STATIC PROTOTYPES
54  **********************/
55 static void flex_update(lv_obj_t * cont, void * user_data);
56 static int32_t find_track_end(lv_obj_t * cont, flex_t * f, int32_t item_start_id, lv_coord_t max_main_size,
57                               lv_coord_t item_gap, track_t * t);
58 static void children_repos(lv_obj_t * cont, flex_t * f, int32_t item_first_id, int32_t item_last_id, lv_coord_t abs_x,
59                            lv_coord_t abs_y, lv_coord_t max_main_size, lv_coord_t item_gap, track_t * t);
60 static void place_content(lv_flex_align_t place, lv_coord_t max_size, lv_coord_t content_size, lv_coord_t item_cnt,
61                           lv_coord_t * start_pos, lv_coord_t * gap);
62 static lv_obj_t * get_next_item(lv_obj_t * cont, bool rev, int32_t * item_id);
63 
64 /**********************
65  *  GLOBAL VARIABLES
66  **********************/
67 uint16_t LV_LAYOUT_FLEX;
68 lv_style_prop_t LV_STYLE_FLEX_FLOW;
69 lv_style_prop_t LV_STYLE_FLEX_MAIN_PLACE;
70 lv_style_prop_t LV_STYLE_FLEX_CROSS_PLACE;
71 lv_style_prop_t LV_STYLE_FLEX_TRACK_PLACE;
72 lv_style_prop_t LV_STYLE_FLEX_GROW;
73 
74 /**********************
75  *  STATIC VARIABLES
76  **********************/
77 
78 /**********************
79  *      MACROS
80  **********************/
81 
82 /**********************
83  *   GLOBAL FUNCTIONS
84  **********************/
85 
86 /*=====================
87  * Setter functions
88  *====================*/
89 
lv_flex_init(void)90 void lv_flex_init(void)
91 {
92     LV_LAYOUT_FLEX = lv_layout_register(flex_update, NULL);
93 
94     LV_STYLE_FLEX_FLOW = lv_style_register_prop(LV_STYLE_PROP_FLAG_NONE);
95     LV_STYLE_FLEX_MAIN_PLACE = lv_style_register_prop(LV_STYLE_PROP_LAYOUT_REFR);
96     LV_STYLE_FLEX_CROSS_PLACE = lv_style_register_prop(LV_STYLE_PROP_LAYOUT_REFR);
97     LV_STYLE_FLEX_TRACK_PLACE = lv_style_register_prop(LV_STYLE_PROP_LAYOUT_REFR);
98     LV_STYLE_FLEX_GROW = lv_style_register_prop(LV_STYLE_PROP_LAYOUT_REFR);
99 }
100 
lv_obj_set_flex_flow(lv_obj_t * obj,lv_flex_flow_t flow)101 void lv_obj_set_flex_flow(lv_obj_t * obj, lv_flex_flow_t flow)
102 {
103     lv_obj_set_style_flex_flow(obj, flow, 0);
104     lv_obj_set_style_layout(obj, LV_LAYOUT_FLEX, 0);
105 }
106 
lv_obj_set_flex_align(lv_obj_t * obj,lv_flex_align_t main_place,lv_flex_align_t cross_place,lv_flex_align_t track_place)107 void lv_obj_set_flex_align(lv_obj_t * obj, lv_flex_align_t main_place, lv_flex_align_t cross_place,
108                            lv_flex_align_t track_place)
109 {
110     lv_obj_set_style_flex_main_place(obj, main_place, 0);
111     lv_obj_set_style_flex_cross_place(obj, cross_place, 0);
112     lv_obj_set_style_flex_track_place(obj, track_place, 0);
113     lv_obj_set_style_layout(obj, LV_LAYOUT_FLEX, 0);
114 }
115 
lv_obj_set_flex_grow(lv_obj_t * obj,uint8_t grow)116 void lv_obj_set_flex_grow(lv_obj_t * obj, uint8_t grow)
117 {
118     lv_obj_set_style_flex_grow(obj, grow, 0);
119     lv_obj_mark_layout_as_dirty(lv_obj_get_parent(obj));
120 }
121 
lv_style_set_flex_flow(lv_style_t * style,lv_flex_flow_t value)122 void lv_style_set_flex_flow(lv_style_t * style, lv_flex_flow_t value)
123 {
124     lv_style_value_t v = {
125         .num = (int32_t)value
126     };
127     lv_style_set_prop(style, LV_STYLE_FLEX_FLOW, v);
128 }
129 
lv_style_set_flex_main_place(lv_style_t * style,lv_flex_align_t value)130 void lv_style_set_flex_main_place(lv_style_t * style, lv_flex_align_t value)
131 {
132     lv_style_value_t v = {
133         .num = (int32_t)value
134     };
135     lv_style_set_prop(style, LV_STYLE_FLEX_MAIN_PLACE, v);
136 }
137 
lv_style_set_flex_cross_place(lv_style_t * style,lv_flex_align_t value)138 void lv_style_set_flex_cross_place(lv_style_t * style, lv_flex_align_t value)
139 {
140     lv_style_value_t v = {
141         .num = (int32_t)value
142     };
143     lv_style_set_prop(style, LV_STYLE_FLEX_CROSS_PLACE, v);
144 }
145 
lv_style_set_flex_track_place(lv_style_t * style,lv_flex_align_t value)146 void lv_style_set_flex_track_place(lv_style_t * style, lv_flex_align_t value)
147 {
148     lv_style_value_t v = {
149         .num = (int32_t)value
150     };
151     lv_style_set_prop(style, LV_STYLE_FLEX_TRACK_PLACE, v);
152 }
153 
lv_style_set_flex_grow(lv_style_t * style,uint8_t value)154 void lv_style_set_flex_grow(lv_style_t * style, uint8_t value)
155 {
156     lv_style_value_t v = {
157         .num = (int32_t)value
158     };
159     lv_style_set_prop(style, LV_STYLE_FLEX_GROW, v);
160 }
161 
lv_obj_set_style_flex_flow(lv_obj_t * obj,lv_flex_flow_t value,lv_style_selector_t selector)162 void lv_obj_set_style_flex_flow(lv_obj_t * obj, lv_flex_flow_t value, lv_style_selector_t selector)
163 {
164     lv_style_value_t v = {
165         .num = (int32_t) value
166     };
167     lv_obj_set_local_style_prop(obj, LV_STYLE_FLEX_FLOW, v, selector);
168 }
169 
lv_obj_set_style_flex_main_place(lv_obj_t * obj,lv_flex_align_t value,lv_style_selector_t selector)170 void lv_obj_set_style_flex_main_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector)
171 {
172     lv_style_value_t v = {
173         .num = (int32_t) value
174     };
175     lv_obj_set_local_style_prop(obj, LV_STYLE_FLEX_MAIN_PLACE, v, selector);
176 }
177 
lv_obj_set_style_flex_cross_place(lv_obj_t * obj,lv_flex_align_t value,lv_style_selector_t selector)178 void lv_obj_set_style_flex_cross_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector)
179 {
180     lv_style_value_t v = {
181         .num = (int32_t) value
182     };
183     lv_obj_set_local_style_prop(obj, LV_STYLE_FLEX_CROSS_PLACE, v, selector);
184 }
185 
lv_obj_set_style_flex_track_place(lv_obj_t * obj,lv_flex_align_t value,lv_style_selector_t selector)186 void lv_obj_set_style_flex_track_place(lv_obj_t * obj, lv_flex_align_t value, lv_style_selector_t selector)
187 {
188     lv_style_value_t v = {
189         .num = (int32_t) value
190     };
191     lv_obj_set_local_style_prop(obj, LV_STYLE_FLEX_TRACK_PLACE, v, selector);
192 }
193 
lv_obj_set_style_flex_grow(lv_obj_t * obj,uint8_t value,lv_style_selector_t selector)194 void lv_obj_set_style_flex_grow(lv_obj_t * obj, uint8_t value, lv_style_selector_t selector)
195 {
196     lv_style_value_t v = {
197         .num = (int32_t) value
198     };
199     lv_obj_set_local_style_prop(obj, LV_STYLE_FLEX_GROW, v, selector);
200 }
201 
202 /**********************
203  *   STATIC FUNCTIONS
204  **********************/
205 
flex_update(lv_obj_t * cont,void * user_data)206 static void flex_update(lv_obj_t * cont, void * user_data)
207 {
208     LV_LOG_INFO("update %p container", (void *)cont);
209     LV_UNUSED(user_data);
210 
211     flex_t f;
212     lv_flex_flow_t flow = lv_obj_get_style_flex_flow(cont, LV_PART_MAIN);
213     f.row = flow & _LV_FLEX_COLUMN ? 0 : 1;
214     f.wrap = flow & _LV_FLEX_WRAP ? 1 : 0;
215     f.rev = flow & _LV_FLEX_REVERSE ? 1 : 0;
216     f.main_place = lv_obj_get_style_flex_main_place(cont, LV_PART_MAIN);
217     f.cross_place = lv_obj_get_style_flex_cross_place(cont, LV_PART_MAIN);
218     f.track_place = lv_obj_get_style_flex_track_place(cont, LV_PART_MAIN);
219 
220     bool rtl = lv_obj_get_style_base_dir(cont, LV_PART_MAIN) == LV_BASE_DIR_RTL ? true : false;
221     lv_coord_t track_gap = !f.row ? lv_obj_get_style_pad_column(cont, LV_PART_MAIN) : lv_obj_get_style_pad_row(cont,
222                                                                                                                LV_PART_MAIN);
223     lv_coord_t item_gap = f.row ? lv_obj_get_style_pad_column(cont, LV_PART_MAIN) : lv_obj_get_style_pad_row(cont,
224                                                                                                              LV_PART_MAIN);
225     lv_coord_t max_main_size = (f.row ? lv_obj_get_content_width(cont) : lv_obj_get_content_height(cont));
226     lv_coord_t border_width = lv_obj_get_style_border_width(cont, LV_PART_MAIN);
227     lv_coord_t abs_y = cont->coords.y1 + lv_obj_get_style_pad_top(cont,
228                                                                   LV_PART_MAIN) + border_width - lv_obj_get_scroll_y(cont);
229     lv_coord_t abs_x = cont->coords.x1 + lv_obj_get_style_pad_left(cont,
230                                                                    LV_PART_MAIN) + border_width - lv_obj_get_scroll_x(cont);
231 
232     lv_flex_align_t track_cross_place = f.track_place;
233     lv_coord_t * cross_pos = (f.row ? &abs_y : &abs_x);
234 
235     lv_coord_t w_set = lv_obj_get_style_width(cont, LV_PART_MAIN);
236     lv_coord_t h_set = lv_obj_get_style_height(cont, LV_PART_MAIN);
237 
238     /*Content sized objects should squeezed the gap between the children, therefore any alignment will look like `START`*/
239     if((f.row && h_set == LV_SIZE_CONTENT && cont->h_layout == 0) ||
240        (!f.row && w_set == LV_SIZE_CONTENT && cont->w_layout == 0)) {
241         track_cross_place = LV_FLEX_ALIGN_START;
242     }
243 
244     if(rtl && !f.row) {
245         if(track_cross_place == LV_FLEX_ALIGN_START) track_cross_place = LV_FLEX_ALIGN_END;
246         else if(track_cross_place == LV_FLEX_ALIGN_END) track_cross_place = LV_FLEX_ALIGN_START;
247     }
248 
249     lv_coord_t total_track_cross_size = 0;
250     lv_coord_t gap = 0;
251     uint32_t track_cnt = 0;
252     int32_t track_first_item;
253     int32_t next_track_first_item;
254 
255     if(track_cross_place != LV_FLEX_ALIGN_START) {
256         track_first_item = f.rev ? cont->spec_attr->child_cnt - 1 : 0;
257         track_t t;
258         while(track_first_item < (int32_t)cont->spec_attr->child_cnt && track_first_item >= 0) {
259             /*Search the first item of the next row*/
260             t.grow_dsc_calc = 0;
261             next_track_first_item = find_track_end(cont, &f, track_first_item, max_main_size, item_gap, &t);
262             total_track_cross_size += t.track_cross_size + track_gap;
263             track_cnt++;
264             track_first_item = next_track_first_item;
265         }
266 
267         if(track_cnt) total_track_cross_size -= track_gap;   /*No gap after the last track*/
268 
269         /*Place the tracks to get the start position*/
270         lv_coord_t max_cross_size = (f.row ? lv_obj_get_content_height(cont) : lv_obj_get_content_width(cont));
271         place_content(track_cross_place, max_cross_size, total_track_cross_size, track_cnt, cross_pos, &gap);
272     }
273 
274     track_first_item = f.rev ? cont->spec_attr->child_cnt - 1 : 0;
275 
276     if(rtl && !f.row) {
277         *cross_pos += total_track_cross_size;
278     }
279 
280     while(track_first_item < (int32_t)cont->spec_attr->child_cnt && track_first_item >= 0) {
281         track_t t;
282         t.grow_dsc_calc = 1;
283         /*Search the first item of the next row*/
284         next_track_first_item = find_track_end(cont, &f, track_first_item, max_main_size, item_gap, &t);
285 
286         if(rtl && !f.row) {
287             *cross_pos -= t.track_cross_size;
288         }
289         children_repos(cont, &f, track_first_item, next_track_first_item, abs_x, abs_y, max_main_size, item_gap, &t);
290         track_first_item = next_track_first_item;
291         lv_mem_buf_release(t.grow_dsc);
292         t.grow_dsc = NULL;
293         if(rtl && !f.row) {
294             *cross_pos -= gap + track_gap;
295         }
296         else {
297             *cross_pos += t.track_cross_size + gap + track_gap;
298         }
299     }
300     LV_ASSERT_MEM_INTEGRITY();
301 
302     if(w_set == LV_SIZE_CONTENT || h_set == LV_SIZE_CONTENT) {
303         lv_obj_refr_size(cont);
304     }
305 
306     lv_event_send(cont, LV_EVENT_LAYOUT_CHANGED, NULL);
307 
308     LV_TRACE_LAYOUT("finished");
309 }
310 
311 /**
312  * Find the last item of a track
313  */
find_track_end(lv_obj_t * cont,flex_t * f,int32_t item_start_id,lv_coord_t max_main_size,lv_coord_t item_gap,track_t * t)314 static int32_t find_track_end(lv_obj_t * cont, flex_t * f, int32_t item_start_id, lv_coord_t max_main_size,
315                               lv_coord_t item_gap, track_t * t)
316 {
317     lv_coord_t w_set = lv_obj_get_style_width(cont, LV_PART_MAIN);
318     lv_coord_t h_set = lv_obj_get_style_height(cont, LV_PART_MAIN);
319 
320     /*Can't wrap if the size if auto (i.e. the size depends on the children)*/
321     if(f->wrap && ((f->row && w_set == LV_SIZE_CONTENT) || (!f->row && h_set == LV_SIZE_CONTENT))) {
322         f->wrap = false;
323     }
324     lv_coord_t(*get_main_size)(const lv_obj_t *) = (f->row ? lv_obj_get_width : lv_obj_get_height);
325     lv_coord_t(*get_cross_size)(const lv_obj_t *) = (!f->row ? lv_obj_get_width : lv_obj_get_height);
326 
327     t->track_main_size = 0;
328     t->track_fix_main_size = 0;
329     t->grow_item_cnt = 0;
330     t->track_cross_size = 0;
331     t->item_cnt = 0;
332     t->grow_dsc = NULL;
333 
334     int32_t item_id = item_start_id;
335 
336     lv_obj_t * item = lv_obj_get_child(cont, item_id);
337     while(item) {
338         if(item_id != item_start_id && lv_obj_has_flag(item, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK)) break;
339 
340         if(!lv_obj_has_flag_any(item, LV_OBJ_FLAG_IGNORE_LAYOUT | LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_FLOATING)) {
341             uint8_t grow_value = lv_obj_get_style_flex_grow(item, LV_PART_MAIN);
342             if(grow_value) {
343                 t->grow_item_cnt++;
344                 t->track_fix_main_size += item_gap;
345                 if(t->grow_dsc_calc) {
346                     grow_dsc_t * new_dsc = lv_mem_buf_get(sizeof(grow_dsc_t) * (t->grow_item_cnt));
347                     LV_ASSERT_MALLOC(new_dsc);
348                     if(new_dsc == NULL) return item_id;
349 
350                     if(t->grow_dsc) {
351                         lv_memcpy(new_dsc, t->grow_dsc, sizeof(grow_dsc_t) * (t->grow_item_cnt - 1));
352                         lv_mem_buf_release(t->grow_dsc);
353                     }
354                     new_dsc[t->grow_item_cnt - 1].item = item;
355                     new_dsc[t->grow_item_cnt - 1].min_size = f->row ? lv_obj_get_style_min_width(item,
356                                                                                                  LV_PART_MAIN) : lv_obj_get_style_min_height(item, LV_PART_MAIN);
357                     new_dsc[t->grow_item_cnt - 1].max_size = f->row ? lv_obj_get_style_max_width(item,
358                                                                                                  LV_PART_MAIN) : lv_obj_get_style_max_height(item, LV_PART_MAIN);
359                     new_dsc[t->grow_item_cnt - 1].grow_value = grow_value;
360                     new_dsc[t->grow_item_cnt - 1].clamped = 0;
361                     t->grow_dsc = new_dsc;
362                 }
363             }
364             else {
365                 lv_coord_t item_size = get_main_size(item);
366                 if(f->wrap && t->track_fix_main_size + item_size > max_main_size) break;
367                 t->track_fix_main_size += item_size + item_gap;
368             }
369 
370             t->track_cross_size = LV_MAX(get_cross_size(item), t->track_cross_size);
371             t->item_cnt++;
372         }
373 
374         item_id += f->rev ? -1 : +1;
375         if(item_id < 0) break;
376         item = lv_obj_get_child(cont, item_id);
377     }
378 
379     if(t->track_fix_main_size > 0) t->track_fix_main_size -= item_gap; /*There is no gap after the last item*/
380 
381     /*If there is at least one "grow item" the track takes the full space*/
382     t->track_main_size = t->grow_item_cnt ? max_main_size : t->track_fix_main_size;
383 
384     /*Have at least one item in a row*/
385     if(item && item_id == item_start_id) {
386         item = cont->spec_attr->children[item_id];
387         get_next_item(cont, f->rev, &item_id);
388         if(item) {
389             t->track_cross_size = get_cross_size(item);
390             t->track_main_size = get_main_size(item);
391             t->item_cnt = 1;
392         }
393     }
394 
395     return item_id;
396 }
397 
398 /**
399  * Position the children in the same track
400  */
children_repos(lv_obj_t * cont,flex_t * f,int32_t item_first_id,int32_t item_last_id,lv_coord_t abs_x,lv_coord_t abs_y,lv_coord_t max_main_size,lv_coord_t item_gap,track_t * t)401 static void children_repos(lv_obj_t * cont, flex_t * f, int32_t item_first_id, int32_t item_last_id, lv_coord_t abs_x,
402                            lv_coord_t abs_y, lv_coord_t max_main_size, lv_coord_t item_gap, track_t * t)
403 {
404     void (*area_set_main_size)(lv_area_t *, lv_coord_t) = (f->row ? lv_area_set_width : lv_area_set_height);
405     lv_coord_t (*area_get_main_size)(const lv_area_t *) = (f->row ? lv_area_get_width : lv_area_get_height);
406     lv_coord_t (*area_get_cross_size)(const lv_area_t *) = (!f->row ? lv_area_get_width : lv_area_get_height);
407 
408     /*Calculate the size of grow items first*/
409     uint32_t i;
410     bool grow_reiterate  = true;
411     while(grow_reiterate) {
412         grow_reiterate = false;
413         lv_coord_t grow_value_sum = 0;
414         lv_coord_t grow_max_size = t->track_main_size - t->track_fix_main_size;
415         for(i = 0; i < t->grow_item_cnt; i++) {
416             if(t->grow_dsc[i].clamped == 0) {
417                 grow_value_sum += t->grow_dsc[i].grow_value;
418             }
419             else {
420                 grow_max_size -= t->grow_dsc[i].final_size;
421             }
422         }
423         lv_coord_t grow_unit;
424 
425         for(i = 0; i < t->grow_item_cnt; i++) {
426             if(t->grow_dsc[i].clamped == 0) {
427                 LV_ASSERT(grow_value_sum != 0);
428                 grow_unit = grow_max_size / grow_value_sum;
429                 lv_coord_t size = grow_unit * t->grow_dsc[i].grow_value;
430                 lv_coord_t size_clamp = LV_CLAMP(t->grow_dsc[i].min_size, size, t->grow_dsc[i].max_size);
431 
432                 if(size_clamp != size) {
433                     t->grow_dsc[i].clamped = 1;
434                     grow_reiterate = true;
435                 }
436                 t->grow_dsc[i].final_size = size_clamp;
437                 grow_value_sum -= t->grow_dsc[i].grow_value;
438                 grow_max_size  -= t->grow_dsc[i].final_size;
439             }
440         }
441     }
442 
443     bool rtl = lv_obj_get_style_base_dir(cont, LV_PART_MAIN) == LV_BASE_DIR_RTL ? true : false;
444 
445     lv_coord_t main_pos = 0;
446 
447     lv_coord_t place_gap = 0;
448     place_content(f->main_place, max_main_size, t->track_main_size, t->item_cnt, &main_pos, &place_gap);
449     if(f->row && rtl) main_pos += lv_obj_get_content_width(cont);
450 
451     lv_obj_t * item = lv_obj_get_child(cont, item_first_id);
452     /*Reposition the children*/
453     while(item && item_first_id != item_last_id) {
454         if(lv_obj_has_flag_any(item, LV_OBJ_FLAG_IGNORE_LAYOUT | LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_FLOATING)) {
455             item = get_next_item(cont, f->rev, &item_first_id);
456             continue;
457         }
458         lv_coord_t grow_size = lv_obj_get_style_flex_grow(item, LV_PART_MAIN);
459         if(grow_size) {
460             lv_coord_t s = 0;
461             for(i = 0; i < t->grow_item_cnt; i++) {
462                 if(t->grow_dsc[i].item == item) {
463                     s = t->grow_dsc[i].final_size;
464                     break;
465                 }
466             }
467 
468             if(f->row) {
469                 item->w_layout = 1;
470                 item->h_layout = 0;
471             }
472             else {
473                 item->h_layout = 1;
474                 item->w_layout = 0;
475             }
476 
477             if(s != area_get_main_size(&item->coords)) {
478                 lv_obj_invalidate(item);
479 
480                 lv_area_t old_coords;
481                 lv_area_copy(&old_coords, &item->coords);
482                 area_set_main_size(&item->coords, s);
483                 lv_event_send(item, LV_EVENT_SIZE_CHANGED, &old_coords);
484                 lv_event_send(lv_obj_get_parent(item), LV_EVENT_CHILD_CHANGED, item);
485                 lv_obj_invalidate(item);
486             }
487         }
488         else {
489             item->w_layout = 0;
490             item->h_layout = 0;
491         }
492 
493         lv_coord_t cross_pos = 0;
494         switch(f->cross_place) {
495             case LV_FLEX_ALIGN_CENTER:
496                 /*Round up the cross size to avoid rounding error when dividing by 2
497                  *The issue comes up e,g, with column direction with center cross direction if an element's width changes*/
498                 cross_pos = (((t->track_cross_size + 1) & (~1)) - area_get_cross_size(&item->coords)) / 2;
499                 break;
500             case LV_FLEX_ALIGN_END:
501                 cross_pos = t->track_cross_size - area_get_cross_size(&item->coords);
502                 break;
503             default:
504                 break;
505         }
506 
507         if(f->row && rtl) main_pos -= area_get_main_size(&item->coords);
508 
509         /*Handle percentage value of translate*/
510         lv_coord_t tr_x = lv_obj_get_style_translate_x(item, LV_PART_MAIN);
511         lv_coord_t tr_y = lv_obj_get_style_translate_y(item, LV_PART_MAIN);
512         lv_coord_t w = lv_obj_get_width(item);
513         lv_coord_t h = lv_obj_get_height(item);
514         if(LV_COORD_IS_PCT(tr_x)) tr_x = (w * LV_COORD_GET_PCT(tr_x)) / 100;
515         if(LV_COORD_IS_PCT(tr_y)) tr_y = (h * LV_COORD_GET_PCT(tr_y)) / 100;
516 
517         lv_coord_t diff_x = abs_x - item->coords.x1 + tr_x;
518         lv_coord_t diff_y = abs_y - item->coords.y1 + tr_y;
519         diff_x += f->row ? main_pos : cross_pos;
520         diff_y += f->row ? cross_pos : main_pos;
521 
522         if(diff_x || diff_y) {
523             lv_obj_invalidate(item);
524             item->coords.x1 += diff_x;
525             item->coords.x2 += diff_x;
526             item->coords.y1 += diff_y;
527             item->coords.y2 += diff_y;
528             lv_obj_invalidate(item);
529             lv_obj_move_children_by(item, diff_x, diff_y, false);
530         }
531 
532         if(!(f->row && rtl)) main_pos += area_get_main_size(&item->coords) + item_gap + place_gap;
533         else main_pos -= item_gap + place_gap;
534 
535         item = get_next_item(cont, f->rev, &item_first_id);
536     }
537 }
538 
539 /**
540  * Tell a start coordinate and gap for a placement type.
541  */
place_content(lv_flex_align_t place,lv_coord_t max_size,lv_coord_t content_size,lv_coord_t item_cnt,lv_coord_t * start_pos,lv_coord_t * gap)542 static void place_content(lv_flex_align_t place, lv_coord_t max_size, lv_coord_t content_size, lv_coord_t item_cnt,
543                           lv_coord_t * start_pos, lv_coord_t * gap)
544 {
545     if(item_cnt <= 1) {
546         switch(place) {
547             case LV_FLEX_ALIGN_SPACE_BETWEEN:
548             case LV_FLEX_ALIGN_SPACE_AROUND:
549             case LV_FLEX_ALIGN_SPACE_EVENLY:
550                 place = LV_FLEX_ALIGN_CENTER;
551                 break;
552             default:
553                 break;
554         }
555     }
556 
557     switch(place) {
558         case LV_FLEX_ALIGN_CENTER:
559             *gap = 0;
560             *start_pos += (max_size - content_size) / 2;
561             break;
562         case LV_FLEX_ALIGN_END:
563             *gap = 0;
564             *start_pos += max_size - content_size;
565             break;
566         case LV_FLEX_ALIGN_SPACE_BETWEEN:
567             *gap = (lv_coord_t)(max_size - content_size) / (lv_coord_t)(item_cnt - 1);
568             break;
569         case LV_FLEX_ALIGN_SPACE_AROUND:
570             *gap += (lv_coord_t)(max_size - content_size) / (lv_coord_t)(item_cnt);
571             *start_pos += *gap / 2;
572             break;
573         case LV_FLEX_ALIGN_SPACE_EVENLY:
574             *gap = (lv_coord_t)(max_size - content_size) / (lv_coord_t)(item_cnt + 1);
575             *start_pos += *gap;
576             break;
577         default:
578             *gap = 0;
579     }
580 }
581 
get_next_item(lv_obj_t * cont,bool rev,int32_t * item_id)582 static lv_obj_t * get_next_item(lv_obj_t * cont, bool rev, int32_t * item_id)
583 {
584     if(rev) {
585         (*item_id)--;
586         if(*item_id >= 0) return cont->spec_attr->children[*item_id];
587         else return NULL;
588     }
589     else {
590         (*item_id)++;
591         if((*item_id) < (int32_t)cont->spec_attr->child_cnt) return cont->spec_attr->children[*item_id];
592         else return NULL;
593     }
594 }
595 
596 #endif /*LV_USE_FLEX*/
597