summaryrefslogtreecommitdiff
path: root/src/lib/elementary/efl_ui_collection_view.c
blob: 7eb6fe42d16b697106f19986d200e9fb3f86d767 (plain)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
// Note: @1.23 Initial release has infrastructure to support more mode than homogeneous, but isn't exposed in the API nor supported.

#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#define ELM_LAYOUT_PROTECTED
#define EFL_UI_SCROLL_MANAGER_PROTECTED
#define EFL_UI_SCROLLBAR_PROTECTED
#define EFL_UI_WIDGET_FOCUS_MANAGER_PROTECTED

#include <Efl_Ui.h>
#include <Elementary.h>
#include "elm_widget.h"
#include "elm_priv.h"
#include "inttypes.h"

#include "efl_ui_collection_view_focus_manager.eo.h"

#ifndef VIEWPORT_ENABLE
# undef VIEWPORT_ENABLE
#endif

typedef struct _Efl_Ui_Collection_View_Data Efl_Ui_Collection_View_Data;
typedef struct _Efl_Ui_Collection_Viewport Efl_Ui_Collection_Viewport;
typedef struct _Efl_Ui_Collection_View_Focus_Manager_Data Efl_Ui_Collection_View_Focus_Manager_Data;
typedef struct _Efl_Ui_Collection_Item Efl_Ui_Collection_Item;
typedef struct _Efl_Ui_Collection_Item_Lookup Efl_Ui_Collection_Item_Lookup;
typedef struct _Efl_Ui_Collection_Request Efl_Ui_Collection_Request;

struct _Efl_Ui_Collection_Item
{
   Efl_Gfx_Entity *entity;
   Efl_Model *model;
};

struct _Efl_Ui_Collection_Item_Lookup
{
   EINA_RBTREE;

   unsigned int index;
   Efl_Ui_Collection_Item item;
};

struct _Efl_Ui_Collection_Viewport
{
   Efl_Ui_Collection_Item *items;

   unsigned int offset;
   uint16_t count;
};

struct _Efl_Ui_Collection_Request
{
   Eina_Future *f;

   unsigned int offset;
   unsigned int length;

   Eina_Bool need_size : 1;
   Eina_Bool need_entity : 1;
   Eina_Bool entity_requested : 1;
};

struct _Efl_Ui_Collection_View_Data
{
   Efl_Ui_Factory *factory;
   Efl_Ui_Position_Manager_Entity *manager;
   Efl_Ui_Scroll_Manager *scroller;
   Efl_Ui_Pan *pan;
   Efl_Gfx_Entity *sizer;
   Efl_Model *model;
   Efl_Model *multi_selectable_async_model;

#ifdef VIEWPORT_ENABLE
   Efl_Ui_Collection_Viewport *viewport[3];
#endif
   Eina_Rbtree *cache;

   Eina_List *requests; // Array of Efl_Ui_Collection_Request in progress

   struct {
      Efl_Gfx_Entity *last; // The last item of the collection, so focus can start by the end if necessary.
      Efl_Gfx_Entity *previously; // The previously selected item in the collection, so focus can come back to it.
   } focus;

   unsigned int start_id;
   unsigned int end_id;

   Eina_Size2D content_min_size;

   Efl_Ui_Layout_Orientation direction;
   Efl_Ui_Select_Mode mode;

   struct {
      Eina_Bool w : 1;
      Eina_Bool h : 1;
   } match_content;

   Efl_Ui_Position_Manager_Request_Range current_range;
};

struct _Efl_Ui_Collection_View_Focus_Manager_Data
{
   Efl_Ui_Collection_View *collection;
};

static const char *COLLECTION_VIEW_MANAGED = "_collection_view.managed";
static const char *COLLECTION_VIEW_MANAGED_YES = "yes";

#define MY_CLASS EFL_UI_COLLECTION_VIEW_CLASS

#define MY_DATA_GET(obj, pd)                                            \
  Efl_Ui_Collection_View_Data *pd = efl_data_scope_get(obj, MY_CLASS);

static Eina_Bool _entity_request(Efl_Ui_Collection_View *obj, Efl_Ui_Collection_Request *request);
static void _idle_cb(void *data, const Efl_Event *event);

static int
_cache_tree_lookup(const Eina_Rbtree *node, const void *key,
                   int length EINA_UNUSED, void *data EINA_UNUSED)
{
   const Efl_Ui_Collection_Item_Lookup *n = (Efl_Ui_Collection_Item_Lookup *)node;
   const unsigned int *index = key;

   if (n->index > *index)
     return 1;
   if (n->index < *index)
     return -1;
   return 0;
}

static Eina_Rbtree_Direction
_cache_tree_cmp(const Eina_Rbtree *left, const Eina_Rbtree *right, void *data EINA_UNUSED)
{
   Efl_Ui_Collection_Item_Lookup *l = (Efl_Ui_Collection_Item_Lookup *)left;
   Efl_Ui_Collection_Item_Lookup *r = (Efl_Ui_Collection_Item_Lookup *)right;

   return l->index < r->index ? EINA_RBTREE_LEFT : EINA_RBTREE_RIGHT;
}

static Eina_Value
_undo_item_selected_then(Eo *item, void *data EINA_UNUSED, Eina_Error err)
{
   Eina_Value *get;
   Eina_Bool item_selected = efl_ui_selectable_selected_get(item);
   Eina_Bool model_selected = EINA_FALSE;

   get = efl_model_property_get(efl_ui_view_model_get(item), "self.selected");
   eina_value_bool_get(get, &model_selected);
   eina_value_free(get);

   if ((!!model_selected) != (!!item_selected))
     efl_ui_selectable_selected_set(item, model_selected);

   return eina_value_error_init(err);
}

static void
_selected_item_cb(void *data EINA_UNUSED, const Efl_Event *ev)
{
   // Link back property to model, maybe just trigger event on the item should be enough
   Eina_Value *get;
   Eina_Bool item_selected = efl_ui_selectable_selected_get(ev->object);
   Eina_Bool model_selected = EINA_FALSE;
   Eina_Value set = eina_value_bool_init(!!item_selected);

   get = efl_model_property_get(efl_ui_view_model_get(ev->object), "self.selected");
   eina_value_bool_get(get, &model_selected);
   eina_value_free(get);

   if ((!!model_selected) != (!!item_selected))
     {
        Eina_Future *f;

        f = efl_model_property_set(efl_ui_view_model_get(ev->object), "self.selected", &set);

        // In case the mode is preventing the change, we need to update the UI back. So handle error case
        efl_future_then(ev->object, f,
                        .error = _undo_item_selected_then);
     }

   eina_value_flush(&set);
}

static void
_redirect_item_cb(void *data, const Efl_Event *ev)
{
   Eo *obj = data;

#define REDIRECT_EVT(Desc, Item_Desc)                           \
   if (Desc == ev->desc)                                        \
     {                                                          \
        Efl_Ui_Item_Clickable_Clicked item_clicked;             \
        Efl_Input_Clickable_Clicked *clicked = ev->info;        \
                                                                \
        item_clicked.clicked = *clicked;                        \
        item_clicked.item = ev->object;                         \
                                                                \
        efl_event_callback_call(obj, Item_Desc, &item_clicked); \
     }
#define REDIRECT_EVT_PRESS(Desc, Item_Desc)                           \
   if (Desc == ev->desc)                                        \
     {                                                          \
        Efl_Ui_Item_Clickable_Pressed item_pressed;             \
        int *button = ev->info;        \
                                                                \
        item_pressed.button = *button;                        \
        item_pressed.item = ev->object;                         \
                                                                \
        efl_event_callback_call(obj, Item_Desc, &item_pressed); \
     }

   REDIRECT_EVT_PRESS(EFL_INPUT_EVENT_PRESSED, EFL_UI_EVENT_ITEM_PRESSED);
   REDIRECT_EVT_PRESS(EFL_INPUT_EVENT_UNPRESSED, EFL_UI_EVENT_ITEM_UNPRESSED);
   REDIRECT_EVT_PRESS(EFL_INPUT_EVENT_LONGPRESSED, EFL_UI_EVENT_ITEM_LONGPRESSED);
   REDIRECT_EVT(EFL_INPUT_EVENT_CLICKED_ANY, EFL_UI_EVENT_ITEM_CLICKED_ANY);
   REDIRECT_EVT(EFL_INPUT_EVENT_CLICKED, EFL_UI_EVENT_ITEM_CLICKED);
#undef REDIRECT_EVT
#undef REDIRECT_EVT_PRESS
}

EFL_CALLBACKS_ARRAY_DEFINE(active_item_cbs,
  { EFL_UI_EVENT_SELECTED_CHANGED, _selected_item_cb },
  { EFL_INPUT_EVENT_PRESSED, _redirect_item_cb },
  { EFL_INPUT_EVENT_UNPRESSED, _redirect_item_cb },
  { EFL_INPUT_EVENT_LONGPRESSED, _redirect_item_cb },
  { EFL_INPUT_EVENT_CLICKED, _redirect_item_cb },
  { EFL_INPUT_EVENT_CLICKED_ANY, _redirect_item_cb });

static void
_entity_cleanup(Efl_Ui_Collection_View *obj, Efl_Ui_Factory *factory,
                Efl_Ui_Collection_Item *item, Eina_Array *scheduled_release)
{
   Efl_Gfx_Entity *entities[1];

   entities[0] = item->entity;
   if (!entities[0]) return ;

   efl_event_callback_array_del(entities[0], active_item_cbs(), obj);
   efl_replace(&item->entity, NULL);
   efl_event_callback_call(obj, EFL_UI_COLLECTION_VIEW_EVENT_ITEM_UNREALIZED, entities[0]);
   if (!scheduled_release)
     {
        efl_ui_factory_release(factory, EINA_C_ARRAY_ITERATOR_NEW(entities));
     }
   else
     {
        eina_array_push(scheduled_release, entities[0]);
     }
}

static void
_item_cleanup(Efl_Ui_Collection_View *obj, Efl_Ui_Factory *factory,
              Efl_Ui_Collection_Item *item, Eina_Array *scheduled_release)
{
   efl_replace(&item->model, NULL);

   _entity_cleanup(obj, factory, item, scheduled_release);
}

static void
_cache_item_free(Eina_Rbtree *node, void *data)
{
   Efl_Ui_Collection_Item_Lookup *n = (void*) node;
   MY_DATA_GET(data, pd);

   _item_cleanup(data, pd->factory, &n->item, NULL);
   free(n);
}

static void
_cache_cleanup(Efl_Ui_Collection_View *obj, Efl_Ui_Collection_View_Data *pd)
{
   eina_rbtree_delete(pd->cache, _cache_item_free, obj);
   pd->cache = NULL;
}

static void
_all_cleanup(Efl_Ui_Collection_View *obj, Efl_Ui_Collection_View_Data *pd)
{
   Efl_Ui_Collection_Request *request;
   Eina_List *l, *ll;
#ifdef VIEWPORT_ENABLE
   unsigned int i;
#endif

   _cache_cleanup(obj, pd);
#ifdef VIEWPORT_ENABLE
   for (i = 0; i < 3; i++)
     {
        unsigned int j;

        if (!pd->viewport[i]) continue;

        for (j = 0; j < pd->viewport[i]->count; j++)
          _item_cleanup(obj, pd->factory, &(pd->viewport[i]->items[j]));
     }
#endif

   efl_replace(&pd->focus.previously, NULL);
   efl_replace(&pd->focus.last, NULL);

   EINA_LIST_FOREACH_SAFE(pd->requests, l, ll, request)
     eina_future_cancel(request->f);
}

static inline Eina_Bool
_size_from_model(Efl_Model *model, Eina_Size2D *r, const char *width, const char *height)
{
   Eina_Value *vw, *vh;
   Eina_Bool success = EINA_FALSE;

   EINA_SAFETY_ON_NULL_RETURN_VAL(model, EINA_FALSE);

   vw = efl_model_property_get(model, width);
   vh = efl_model_property_get(model, height);

   if (eina_value_type_get(vw) == EINA_VALUE_TYPE_ERROR ||
       eina_value_type_get(vh) == EINA_VALUE_TYPE_ERROR)
     goto on_error;

   if (!eina_value_int_convert(vw, &(r->w))) r->w = 0;
   if (!eina_value_int_convert(vh, &(r->h))) r->h = 0;

   success = EINA_TRUE;

 on_error:
   eina_value_free(vw);
   eina_value_free(vh);

   return success;
}

static inline void
_size_to_model(Efl_Model *model, Eina_Size2D state)
{
   Eina_Value vw, vh;

   vw = eina_value_int_init(state.w);
   vh = eina_value_int_init(state.h);

   efl_model_property_set(model, "self.width", &vw);
   efl_model_property_set(model, "self.height", &vh);

   eina_value_flush(&vw);
   eina_value_flush(&vh);
}

#define ITEM_BASE_SIZE_FROM_MODEL(Model, Size) _size_from_model(Model, &Size, "item.width", "item.height")
#define ITEM_SIZE_FROM_MODEL(Model, Size) _size_from_model(Model, &Size, "self.width", "self.height")

static Eina_List *
_request_add(Eina_List *requests, Efl_Ui_Collection_Request **request,
             unsigned int index, Eina_Bool need_entity)
{
   if (!(*request)) goto create;

   if ((*request)->offset + (*request)->length == index)
     {
        if (need_entity) (*request)->need_entity = EINA_TRUE;
        if (!need_entity) (*request)->need_size = EINA_TRUE;
        (*request)->length += 1;
        return requests;
     }

   requests = eina_list_append(requests, *request);

 create:
   *request = calloc(1, sizeof (Efl_Ui_Collection_Request));
   if (!(*request)) return requests;
   (*request)->offset = index;
   (*request)->length = 1;
   // At this point, we rely on the model caching ability to avoid recreating model
   (*request)->need_entity = !!need_entity;
   (*request)->need_size = EINA_TRUE;

   return requests;
}

static Eina_Value
_model_fetched_cb(Eo *obj, void *data, const Eina_Value v)
{
   MY_DATA_GET(obj, pd);
   Efl_Ui_Collection_Request *request = data;
   Efl_Model *child;
   unsigned int i, len;
   Eina_Bool request_entity = EINA_FALSE;

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
     {
        Efl_Ui_Collection_Item_Lookup *insert;
        Eina_Size2D item_size;
#ifdef VIEWPORT_ENABLE
        unsigned int v;

        for (v = 0; v < 3; ++v)
          {
             if (!pd->viewport[v]) continue;

             if ((pd->viewport[v]->offset <= request->offset + i) &&
                 (request->offset + i < pd->viewport[v]->offset + pd->viewport[v]->count))
               {
                  unsigned int index = request->offset + i - pd->viewport[v]->offset;

                  efl_replace(&pd->viewport[v]->items[index].model, child);
                  child = NULL;
                  break;
               }
          }
#endif

        // When requesting a model, it should not be in the cache prior to the request
        if (!child) continue;

        unsigned int search_index = request->offset + i;

        insert = (void*) eina_rbtree_inline_lookup(pd->cache, &search_index,
                                                   sizeof (search_index), _cache_tree_lookup,
                                                   NULL);
        if (insert)
          {
             if (!insert->item.entity && request->need_entity)
               {
                  //drop the old model here, overwrite with model + view
                  efl_replace(&insert->item.model, child);
               }
             else
               ERR("Inserting a model that was already fetched, dropping new model %u", search_index);
          }
        else
          {
             insert = calloc(1, sizeof (Efl_Ui_Collection_Item_Lookup));
             if (!insert) continue;
             insert->index = request->offset + i;
             insert->item.model = efl_ref(child);
             pd->cache = eina_rbtree_inline_insert(pd->cache, EINA_RBTREE_GET(insert), _cache_tree_cmp, NULL);
          }

        if (!ITEM_SIZE_FROM_MODEL(insert->item.model, item_size))
          request_entity = EINA_TRUE;
     }

   if (request_entity)
     {
        request->need_entity = EINA_TRUE;

        if (!request->entity_requested)
          _entity_request(obj, request);
     }
   else if (request->need_size)
     {
        efl_ui_position_manager_entity_item_size_changed(pd->manager, request->offset,
                                                         request->offset + len);
     }

   return v;
}

static void
_model_free_cb(Eo *o, void *data, const Eina_Future *dead_future EINA_UNUSED)
{
   MY_DATA_GET(o, pd);
   Efl_Ui_Collection_Request *request = data;

   if (!request->entity_requested)
     {
        pd->requests = eina_list_remove(pd->requests, request);
        free(request);
     }
}

static Eina_Value
_entity_fetch_cb(Eo *obj, void *data EINA_UNUSED, const Eina_Value v)
{
   MY_DATA_GET(obj, pd);
   Efl_Model *child;
   Eina_Future *r;
   Eina_Array tmp;
   unsigned int i, len;

   eina_array_step_set(&tmp, sizeof (Eina_Array), 4);

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
     {
        eina_array_push(&tmp, child);
     }

   r = efl_ui_view_factory_create_with_event(pd->factory, eina_array_iterator_new(&tmp));

   eina_array_flush(&tmp);

   return eina_future_as_value(r);
}

static inline unsigned int
_lookup_entity_index(Efl_Gfx_Entity *entity, Efl_Model **model)
{
   Efl_Model *fetch;

   fetch = efl_ui_view_model_get(entity);
   if (model) *model = fetch;
   return efl_composite_model_index_get(fetch);
}

static void
_last_entity_update(Efl_Ui_Collection_View_Data *pd, Efl_Gfx_Entity *entity)
{
   Efl_Model *new_model, *old_model;
   unsigned int new_index, old_index;

   if (!pd->focus.last) goto replace;

   new_index = _lookup_entity_index(entity, &new_model);
   old_index = _lookup_entity_index(pd->focus.last, &old_model);

   if (new_index <= old_index) return;

 replace:
   efl_replace(&pd->focus.last, entity);
}

static inline Eina_Bool
_entity_propagate(Efl_Model *model, Efl_Gfx_Entity *entity)
{
   Eina_Size2D item_size;

   if (efl_key_data_get(entity, "efl.ui.widget.factory.size_set"))
     {
        return EINA_FALSE;
     }

   if (ITEM_SIZE_FROM_MODEL(model, item_size))
     {
        efl_gfx_hint_size_min_set(entity, item_size);
        efl_canvas_group_need_recalculate_set(entity, EINA_FALSE);
        if (efl_isa(entity, EFL_UI_ITEM_CLASS)) efl_ui_item_calc_locked_set(entity, EINA_TRUE);
        return EINA_FALSE;
     }

   efl_canvas_group_calculate(entity);
   item_size = efl_gfx_hint_size_combined_min_get(entity);
   efl_canvas_group_need_recalculate_set(entity, EINA_FALSE);

   _size_to_model(model, item_size);
   return EINA_TRUE;
}

static Eina_Value
_entity_fetched_cb(Eo *obj, void *data, const Eina_Value v)
{
   MY_DATA_GET(obj, pd);
   Efl_Ui_Collection_Request *request = data;
   Efl_Gfx_Entity *child;
   unsigned int i, len;
   unsigned int updated_size_start_id = 0, updated_entity_start_id = 0;
   Eina_Bool updated_size = EINA_FALSE, updated_entity = EINA_FALSE;
   Evas *e;

   e = evas_object_evas_get(obj);
   evas_event_freeze(e);

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
     {
        Efl_Ui_Collection_Item_Lookup *lookup;
        unsigned int search_index;
        //unsigned int v;

        efl_key_data_set(child, COLLECTION_VIEW_MANAGED, COLLECTION_VIEW_MANAGED_YES);
        /* fix eventing in scroller by ensuring collection items are in the scroller hierarchy */
        efl_ui_item_container_set(child, obj);
        efl_ui_widget_sub_object_add(obj, child);
        efl_canvas_group_member_add(pd->pan, child);
        efl_gfx_entity_visible_set(child, EINA_FALSE);

#ifdef VIEWPORT_ENABLE
        for (v = 0; v < 3; ++v)
          {
             if (!pd->viewport[v]) continue;

             if ((pd->viewport[v]->offset <= request->offset + i) &&
                 (request->offset + i < pd->viewport[v]->offset + pd->viewport[v]->count))
               {
                  unsigned int index = request->offset + i - pd->viewport[v]->offset;

                  if (pd->viewport[v]->items[index].entity)
                    {
                       ERR("Entity already existing for id %d", i);
                       efl_unref(pd->viewport[v]->items[index].entity);
                       efl_del(pd->viewport[v]->items[index].entity);
                       pd->viewport[v]->items[index].entity = NULL;
                    }

                  efl_replace(&pd->viewport[v]->items[index].entity, child);
                  if (_entity_propagate(pd->viewport[v]->items[index].model, child))
                    {
                       if (!updated_size)
                         {
                            updated_size = EINA_TRUE;
                            updated_size_start_id = index;
                         }
                    }
                  else
                    {
                       if (updated_size)
                         {
                            efl_ui_position_manager_entity_item_size_changed(pd->manager,
                                                                             updated_size_start_id,
                                                                             index - 1);
                            updated_size = EINA_FALSE;
                         }
                    }
                  child = NULL;
                  break;
               }
          }
#endif
        // When requesting an entity, the model should already be in the cache
        if (!child) continue;

        search_index = request->offset + i;

        lookup = (void*) eina_rbtree_inline_lookup(pd->cache, &search_index,
                                                   sizeof (search_index), _cache_tree_lookup,
                                                   NULL);

        if (!lookup)
          {
             Efl_Gfx_Entity *entities[1] = { child };
             efl_ui_factory_release(pd->factory, EINA_C_ARRAY_ITERATOR_NEW(entities));
             continue;
          }
        if (lookup->item.entity)
          {
             ERR("Entity already existing for id %u", search_index);
             _entity_cleanup(obj, pd->factory, &lookup->item, NULL);
          }

        lookup->item.entity = efl_ref(child);
        efl_event_callback_array_add(child, active_item_cbs(), obj);
        efl_event_callback_call(obj, EFL_UI_COLLECTION_VIEW_EVENT_ITEM_REALIZED, child);

        if (!updated_entity)
          {
             updated_entity = EINA_TRUE;
             updated_entity_start_id = search_index;
          }

        if (_entity_propagate(lookup->item.model, child))
          {
             if (!updated_size)
               {
                  updated_size = EINA_TRUE;
                  updated_size_start_id = search_index;
               }
          }
        else
          {
             if (updated_size)
               {
                  efl_ui_position_manager_entity_item_size_changed(pd->manager,
                                                                   updated_size_start_id,
                                                                   search_index - 1);
                  updated_size = EINA_FALSE;
               }
          }
     }

   evas_event_thaw(e);
   evas_event_thaw_eval(e);

   // Check if the last child is also the list item in the list
   _last_entity_update(pd, child);

   // Currently position manager will flush its entire size cache on update, so only do
   // it when necessary to improve performance.
   if (updated_size || request->need_size)
     {
        efl_ui_position_manager_entity_item_size_changed(pd->manager,
                                                         updated_size_start_id,
                                                         request->offset + i - 1);
        updated_size = EINA_FALSE;
     }

   // Notify the position manager that new entity are ready to display
   if (updated_entity)
     {
        efl_ui_position_manager_entity_entities_ready(pd->manager,
                                                      updated_entity_start_id,
                                                      request->offset + i - 1);

        efl_event_callback_del(efl_main_loop_get(), EFL_LOOP_EVENT_IDLE, _idle_cb, obj);
        efl_event_callback_add(efl_main_loop_get(), EFL_LOOP_EVENT_IDLE, _idle_cb, obj);
     }
   return v;
}

static void
_entity_free_cb(Eo *o, void *data, const Eina_Future *dead_future EINA_UNUSED)
{
   MY_DATA_GET(o, pd);
   Efl_Ui_Collection_Request *request = data;

   pd->requests = eina_list_remove(pd->requests, request);
   free(request);
}

static Eina_Bool
_focus_lookup(Efl_Ui_Collection_View_Data *pd, unsigned int search_index,
              Efl_Gfx_Entity **entity, Efl_Model **model)
{
   unsigned int idx;

   if (entity) *entity = pd->focus.last;
   if (pd->focus.last)
     {
        idx = _lookup_entity_index(pd->focus.last, model);
        if (idx == search_index) return EINA_TRUE;
     }
   if (entity) *entity = pd->focus.previously;
   if (pd->focus.previously)
     {
        idx = _lookup_entity_index(pd->focus.previously, model);
        if (idx == search_index) return EINA_TRUE;
     }

   if (entity) *entity = NULL;
   if (model) *model = NULL;
   return EINA_FALSE;
}

static Efl_Ui_Collection_Item_Lookup *
_build_from_focus(Efl_Ui_Collection_View_Data *pd, unsigned int search_index,
                  Efl_Model **model)
{
   Efl_Ui_Collection_Item_Lookup *insert;
   Efl_Gfx_Entity *entity = NULL;

   // Not found in the cache lookup, but just maybe
   if (!_focus_lookup(pd, search_index, &entity, model)) return NULL;

   // Lucky us, let's add it to the cache
   insert = calloc(1, sizeof (Efl_Ui_Collection_Item_Lookup));
   if (!insert) return NULL;

   insert->index = search_index;
   insert->item.model = efl_ref(*model);
   insert->item.entity = efl_ref(entity);

   pd->cache = eina_rbtree_inline_insert(pd->cache, EINA_RBTREE_GET(insert),
                                         _cache_tree_cmp, NULL);

   return insert;
}

static Eina_List *
_cache_size_fetch(Eina_List *requests, Efl_Ui_Collection_Request **request,
                  Efl_Ui_Collection_View_Data *pd,
                  unsigned int search_index,
                  Efl_Ui_Position_Manager_Size_Batch_Entity *target,
                  Eina_Size2D item_base)
{
   Efl_Ui_Collection_Item_Lookup *lookup;
   Efl_Model *model = NULL;
   Eina_Size2D item_size = item_base;

   if (!pd->cache) goto not_found;

   lookup = (void*) eina_rbtree_inline_lookup(pd->cache, &search_index,
                                              sizeof (search_index), _cache_tree_lookup,
                                              NULL);
   // In the cache we should always have model, so no need to check for it
   if (lookup)
     {
        model = lookup->item.model;
     }
   else
     {
        lookup = _build_from_focus(pd, search_index, &model);
        if (!lookup) goto not_found;
     }

   // If we do not know the size
   if (!ITEM_SIZE_FROM_MODEL(model, item_size))
     {
        if (lookup->item.entity)
          {
             ERR("Got a model '%s' and an item '%s', but no size. Recalculating.",
                 efl_debug_name_get(model), efl_debug_name_get(lookup->item.entity));
             _entity_propagate(model, lookup->item.entity);
             if (!ITEM_SIZE_FROM_MODEL(model, item_size))
               {
                  CRI("No size for itme '%s' after recalculating. This is bad.",
                      efl_debug_name_get(lookup->item.entity));
               }
          }
        else if (!ITEM_BASE_SIZE_FROM_MODEL(pd->model, item_size))
          {
             INF("No base size yet available. Making things up.");
             item_size.w = 1;
             item_size.h = 1;
          }
     }

   target->size = item_size;
   target->element_depth = 0;
   target->depth_leader = EINA_FALSE;
   return requests;

 not_found:
   requests = _request_add(requests, request, search_index, EINA_FALSE);

   target->size = item_size;
   target->element_depth = 0;
   target->depth_leader = EINA_FALSE;
   return requests;
}

static Eina_List *
_cache_entity_fetch(Eina_List *requests, Efl_Ui_Collection_Request **request,
                    Efl_Ui_Collection_View_Data *pd,
                    unsigned int search_index,
                    Efl_Ui_Position_Manager_Object_Batch_Entity *target)
{
   Efl_Ui_Collection_Item_Lookup *lookup;
   Efl_Model *model = NULL;

   if (!pd->cache) goto not_found;

   lookup = (void*) eina_rbtree_inline_lookup(pd->cache, &search_index,
                                              sizeof (search_index), _cache_tree_lookup,
                                              NULL);
   if (!lookup) lookup = _build_from_focus(pd, search_index, &model);
   if (!lookup) goto not_found;
   if (!lookup->item.entity) goto not_found;

   if (target) target->entity = lookup->item.entity;
   goto finish;

 not_found:
   requests = _request_add(requests, request, search_index, EINA_TRUE);

   if (target) target->entity = NULL;
 finish:
   if (!target) return requests;

   target->element_depth = 0;
   target->depth_leader = EINA_FALSE;

   return requests;
}

static Eina_Bool
_entity_request(Efl_Ui_Collection_View *obj, Efl_Ui_Collection_Request *request)
{
   if (request->entity_requested) return EINA_TRUE;
   request->f = efl_future_then(obj, request->f,
                                .success_type = EINA_VALUE_TYPE_ARRAY,
                                .success = _entity_fetch_cb);
   request->f = efl_future_then(obj, request->f,
                                .success_type = EINA_VALUE_TYPE_ARRAY,
                                .success = _entity_fetched_cb,
                                .data = request,
                                .free = _entity_free_cb);
   request->entity_requested = EINA_TRUE;
   request->need_entity = EINA_TRUE;

   return EINA_TRUE;
}

static inline Eina_Bool
_entity_inflight_request(Efl_Ui_Collection_View *obj,
                         Efl_Ui_Collection_Request *request,
                         Efl_Ui_Collection_Request *inflight)
{
   inflight->need_size |= request->need_size;
   if (request->need_entity == EINA_FALSE) return EINA_TRUE;

   return _entity_request(obj, inflight);
}

static Eina_List *
_batch_request_flush(Eina_List *requests,
                     Efl_Ui_Collection_View *obj,
                     Efl_Ui_Collection_View_Data *pd)
{
   Efl_Ui_Collection_Request *request;
   Eina_List *ll, *next_list_item;

   EINA_LIST_FOREACH_SAFE(requests, ll, next_list_item, request)
     {
        // Check request intersection with all pending request
        Efl_Ui_Collection_Request *inflight;
        Efl_Model *model;
        Eina_List *l;

        EINA_LIST_FOREACH(pd->requests, l, inflight)
          {
             unsigned int istart = inflight->offset;
             unsigned int iend = inflight->offset + inflight->length;
             unsigned int rstart = request->offset;
             unsigned int rend = request->offset + request->length;

             // Way before
             if (rend < istart) continue;
             // Way after
             if (rstart >= iend) continue;

             // request included in current inflight request
             if (rstart >= istart && rend <= iend)
               {
                  if (!_entity_inflight_request(obj, request, inflight)) continue;

                  // In this case no need to start a request
                  requests = eina_list_remove_list(requests, ll);
                  free(request);
                  request = NULL;
                  break;
               }

             // request overflow left and right
             if (rstart < istart && iend < rend)
               {
                  if (!_entity_inflight_request(obj, request, inflight)) continue;

                  // Remove the center portion of the request by emitting a new one
                  Efl_Ui_Collection_Request *rn;

                  rn = calloc(1, sizeof (Efl_Ui_Collection_Request));
                  if (!rn) break;

                  rn->offset = iend;
                  rn->length = rend - iend;
                  rn->need_entity = request->need_entity;
                  rn->need_size = request->need_size;

                  requests = eina_list_append(requests, rn);

                  request->length = istart - rstart;
                  continue;
               }

             // request overflow left
             if (rstart < istart && rend > istart && rend <= iend)
               {
                  if (!_entity_inflight_request(obj, request, inflight)) continue;
                  request->length = istart - rstart;
                  continue;
               }

             // request overflow right
             if (rstart >= istart && rstart < iend && iend <= rend)
               {
                  if (!_entity_inflight_request(obj, request, inflight)) continue;
                  request->offset = iend;
                  request->length = rend - iend;
                  continue;
               }
          }

        if (!request) continue;

        model = pd->model;
        // Are we ready yet
        if (!model)
          {
             requests = eina_list_remove_list(requests, ll);
             free(request);
             continue;
          }
        // Is the request inside the limit of the model?
        if (request->offset >= efl_model_children_count_get(model))
          {
             requests = eina_list_remove_list(requests, ll);
             free(request);
             continue;
          }
        // Is its limit outside the model limit?
        if (request->offset + request->length >= efl_model_children_count_get(model))
          {
             request->length = efl_model_children_count_get(model) - request->offset;
          }

        // We now have a request, time to trigger a fetch
        // We assume here that we are always fetching the model (model_requested must be true)
        request->f = efl_model_children_slice_get(model, request->offset, request->length);
        request->f = efl_future_then(obj, request->f,
                                     .success = _model_fetched_cb,
                                     .data = request,
                                     .free = _model_free_cb);

        eina_list_move_list(&pd->requests, &requests, ll);
     }
   return eina_list_free(requests);
}

static Efl_Ui_Position_Manager_Size_Batch_Result
_batch_size_cb(void *data, Efl_Ui_Position_Manager_Size_Call_Config conf, Eina_Rw_Slice memory)
{
   MY_DATA_GET(data, pd);
   Efl_Ui_Position_Manager_Size_Batch_Entity *sizes;
   Efl_Ui_Collection_Request *request = NULL;
   Efl_Ui_Position_Manager_Size_Batch_Result result = {0};
   Efl_Model *parent;
   Eina_List *requests = NULL;
   Eina_Size2D item_base = {0};
   unsigned int limit;
   unsigned int idx = 0;

   // get the approximate value from the tree node
   parent = pd->model;

   sizes = memory.mem;
   //count = efl_model_children_count_get(parent);
   limit = conf.range.end_id - conf.range.start_id;
   ITEM_BASE_SIZE_FROM_MODEL(parent, item_base);

   // Look in the temporary cache now for the beginning of the buffer
#ifdef VIEWPORT_ENABLE
   if (pd->viewport[0] && ((unsigned int)(conf.range.start_id + idx) < pd->viewport[0]->offset))
     {
        while ((unsigned int)(conf.range.start_id + idx) < pd->viewport[0]->offset && idx < limit)
          {
             unsigned int search_index = conf.range.start_id + idx;
             requests = _cache_size_fetch(requests, &request, pd,
                                          search_index, &sizes[idx], item_base);
             idx++;
          }
     }

   // Then look in our buffer view if the needed information can be found there
   for (i = 0; i < 3; ++i)
     {
        if (!pd->viewport[i]) continue;

        while (idx < limit &&
               (pd->viewport[i]->offset <= conf.range.start_id + idx) &&
               (conf.range.start_id + idx < (pd->viewport[i]->offset + pd->viewport[i]->count)))
          {
             unsigned int offset = conf.range.start_id + idx - pd->viewport[i]->offset;
             Efl_Model *model = pd->viewport[i]->items[offset].model;
             Efl_Gfx_Entity *entity = pd->viewport[i]->items[offset].entity;
             Eina_Bool entity_request = EINA_FALSE;

             if (model)
               {
                  Eina_Size2D item_size;
                  Eina_Bool found = EINA_FALSE;

                  if (ITEM_SIZE_FROM_MODEL(model, item_size))
                    found = EINA_TRUE;
                  if (!found && entity)
                    {
                       item_size = efl_gfx_hint_size_combined_min_get(entity);
                       //if the size is 0 here, then we are running into trouble,
                       //fetch size from the parent model, where some fallback is defined
                       if (item_size.h == 0 && item_size.w == 0)
                         {
                            item_size = item_base;
                            found = EINA_TRUE;
                         }
                       else
                         {
                            _size_to_model(model, item_size);
                            found = EINA_TRUE;
                         }

                    }

                  if (found)
                    {
                       sizes[idx].size = item_size;
                       sizes[idx].element_depth = 0;
                       sizes[idx].depth_leader = EINA_FALSE;
                       goto done;
                    }

                  // We will need an entity to calculate this size
                  entity_request = EINA_TRUE;
               }
             // No data, add to the requests
             requests = _request_add(requests, &request, conf.range.start_id + idx, entity_request);

             sizes[idx].size = item_base;
             sizes[idx].element_depth = 0;
             sizes[idx].depth_leader = EINA_FALSE;

          done:
             idx++;
          }
     }

   // Look in the temporary cache now for the end of the buffer
   while (idx < limit)
     {
        unsigned int search_index = conf.range.start_id + idx;
        requests = _cache_size_fetch(requests, &request, pd,
                                     search_index, &sizes[idx], item_base);
        idx++;
     }
#endif

   /* if (conf.cache_request) */
   /*   { */
   /*      printf("CACHING SIZE CALL\n"); */
   /*      while (idx < limit) */
   /*          { */
   /*             sizes[idx].depth_leader = EINA_FALSE; */
   /*             sizes[idx].element_depth = 0; */
   /*             sizes[idx].size = pd->last_base; */
   /*             idx++; */
   /*        } */
   /*      fprintf(stderr, "read with no fetch\n"); */
   /*   } */
   /* else */
     {
        while (idx < limit)
          {
             unsigned int search_index = conf.range.start_id + idx;
             requests = _cache_size_fetch(requests, &request, pd,
                                          search_index, &sizes[idx], item_base);
             idx++;
          }


        // Done, but flush request first
        if (request) requests = eina_list_append(requests, request);

        requests = _batch_request_flush(requests, data, pd);
     }

   // Get the amount of filled item
   result.filled_items = limit;

   return result;
}

static Efl_Ui_Position_Manager_Object_Batch_Result
_batch_entity_cb(void *data, Efl_Ui_Position_Manager_Request_Range range, Eina_Rw_Slice memory)
{
   MY_DATA_GET(data, pd);
   Efl_Ui_Position_Manager_Object_Batch_Entity *entities;
   Efl_Ui_Collection_Request *request = NULL;
   Efl_Ui_Position_Manager_Object_Batch_Result result = {0};
   Eina_List *requests = NULL;
#ifdef VIEWPORT_ENABLE
   Efl_Model *parent;
#endif
   unsigned int limit;
   unsigned int idx = 0;

   //parent = pd->model;

   entities = memory.mem;
   //count = efl_model_children_count_get(parent);
   limit = range.end_id - range.start_id;

   // Look in the temporary cache now for the beginning of the buffer
#ifdef VIEWPORT_ENABLE
   if (pd->viewport[0] && ((unsigned int)(range.start_id + idx) < pd->viewport[0]->offset))
     {
        while (idx < limit && (unsigned int)(range.start_id + idx) < pd->viewport[0]->offset)
          {
             unsigned int search_index = range.start_id + idx;

             requests = _cache_entity_fetch(requests, &request, pd,
                                            search_index, &entities[idx]);

             idx++;
          }
     }

   // Then look in our buffer view if the needed information can be found there
   for (i = 0; i < 3; ++i)
     {
        if (!pd->viewport[i]) continue;

        while (idx < limit &&
               (pd->viewport[i]->offset <= range.start_id + idx) &&
               (range.start_id + idx < (pd->viewport[i]->offset + pd->viewport[i]->count)))
          {
             unsigned int offset = range.start_id + idx - pd->viewport[i]->offset;
             Efl_Gfx_Entity *entity = pd->viewport[i]->items[offset].entity;

             if (!entity)
               {
                  // No data, add to the requests
                  requests = _request_add(requests, &request, range.start_id + idx, EINA_TRUE);

                  entities[idx].entity = NULL;
                  entities[idx].depth_leader = EINA_FALSE;
                  entities[idx].element_depth = 0;
               }
             else
               {
                  entities[idx].entity = entity;
                  entities[idx].depth_leader = EINA_FALSE;
                  entities[idx].element_depth = 0;
               }

             idx++;
          }
     }
#endif

   // Look in the temporary cache now for the end of the buffer
   while (idx < limit)
     {
        unsigned int search_index = range.start_id + idx;

        requests = _cache_entity_fetch(requests, &request, pd,
                                       search_index, &entities[idx]);
        idx++;
     }
   // Done, but flush request first
   if (request)
     {
        requests = eina_list_append(requests, request);
     }

   requests = _batch_request_flush(requests, data, pd);

   // Get the amount of filled item
   result.filled_items = limit;

   return result;
}


#if 0
static void
_batch_free_cb(void *data)
{
   efl_unref(data);
}
#endif

static void
flush_min_size(Eo *obj, Efl_Ui_Collection_View_Data *pd)
{
   Eina_Size2D tmp = pd->content_min_size;

   if (!pd->match_content.w)
     tmp.w = -1;

   if (!pd->match_content.h)
     tmp.h = -1;

   efl_gfx_hint_size_restricted_min_set(obj, tmp);
}

static void
_manager_content_size_changed_cb(void *data, const Efl_Event *ev)
{
   Eina_Size2D *size = ev->info;
   MY_DATA_GET(data, pd);

   efl_gfx_entity_size_set(pd->sizer, *size);
}

static void
_manager_content_min_size_changed_cb(void *data, const Efl_Event *ev)
{
   Eina_Size2D *size = ev->info;
   MY_DATA_GET(data, pd);

   pd->content_min_size = *size;

   flush_min_size(data, pd);
}

#ifdef VIEWPORT_ENABLE
static Eina_List *
_viewport_walk_fill(Eina_List *requests,
                    Efl_Ui_Collection_View *obj,
                    Efl_Ui_Collection_View_Data *pd,
                    Efl_Ui_Collection_Viewport *viewport)
{
   Efl_Ui_Collection_Request *current = NULL;
   unsigned int j;

   for (j = 0; j < viewport->count; j++)
     {
        Efl_Ui_Collection_Item_Lookup *lookup;
        unsigned int index = viewport->offset + j;

        if (viewport->items[j].model) goto check_entity;

        lookup = (void*) eina_rbtree_inline_lookup(pd->cache, &index,
                                                   sizeof (index), _cache_tree_lookup,
                                                   NULL);

        if (lookup)
          {
             efl_replace(&viewport->items[j].model, lookup->item.model);
             efl_replace(&viewport->items[j].entity, lookup->item.entity);
             efl_replace(&lookup->item.entity, NULL); // Necessary to avoid premature release

             pd->cache = eina_rbtree_inline_remove(pd->cache, EINA_RBTREE_GET(lookup),
                                                   _cache_tree_cmp, NULL);
             _cache_item_free(EINA_RBTREE_GET(lookup), obj);
          }

     check_entity:
        if (viewport->items[j].entity) continue ;
        requests = _request_add(requests, &current, index, EINA_TRUE);
     }

   // We do break request per viewport, just in case we generate to big batch at once
   if (current) requests = eina_list_append(requests, current);

   return requests;
}

#endif

// An RbTree has the nice property of sorting content. The smaller than the root being in
// son[1] and the greater than the root in son[0]. Using this we can efficiently walk the
// tree once to take note of all the item that need cleaning.
static void
_mark_lesser(Efl_Ui_Collection_Item_Lookup *root, Eina_Array *mark, const unsigned int lower)
{
   if (!root) return ;

   if (root->index < lower)
     {
        eina_array_push(mark, root);
        _mark_lesser((void*) EINA_RBTREE_GET(root)->son[1], mark, lower);
     }
   else
     {
        _mark_lesser((void*) EINA_RBTREE_GET(root)->son[0], mark, lower);
        _mark_lesser((void*) EINA_RBTREE_GET(root)->son[1], mark, lower);
     }
}

static void
_mark_ge(Efl_Ui_Collection_Item_Lookup *root, Eina_Array *mark, const unsigned int upper)
{
   if (!root) return ;

   if (root->index >= upper)
     {
        eina_array_push(mark, root);
        _mark_ge((void*) EINA_RBTREE_GET(root)->son[0], mark, upper);
        _mark_ge((void*) EINA_RBTREE_GET(root)->son[1], mark, upper);
     }
   else
     {
        _mark_ge((void*) EINA_RBTREE_GET(root)->son[0], mark, upper);
     }
}

// we walk the tree twice, once for everything below the limit and once for everything above
// then we do free each item individually.
static void
_idle_cb(void *data, const Efl_Event *event EINA_UNUSED)
{
   Efl_Ui_Collection_Item_Lookup *lookup;
   Eina_Array mark;
   Eina_Array scheduled_release;
   MY_DATA_GET(data, pd);
   const unsigned int length = pd->current_range.end_id - pd->current_range.start_id;
   const unsigned int lower_end = MAX((long)pd->current_range.start_id - (long)length/2, 0);
   const unsigned int upper_end = pd->current_range.end_id + length/2;
   Eina_Array_Iterator iterator;
   unsigned int i;

   eina_array_step_set(&mark, sizeof (Eina_Array), 16);
   eina_array_step_set(&scheduled_release, sizeof (Eina_Array), 16);

   _mark_lesser((void*) pd->cache, &mark, lower_end);
   _mark_ge((void*) pd->cache, &mark, upper_end);

   EINA_ARRAY_ITER_NEXT(&mark, i, lookup, iterator)
     {
        pd->cache = (void*) eina_rbtree_inline_remove(pd->cache,
                                                      EINA_RBTREE_GET(lookup),
                                                      _cache_tree_cmp, NULL);
        _item_cleanup(data, pd->factory, &lookup->item, &scheduled_release);
        free(lookup);
     }
   eina_array_flush(&mark);

   efl_ui_factory_release(pd->factory, eina_array_iterator_new(&scheduled_release));
   eina_array_flush(&scheduled_release);

   efl_event_callback_del(efl_main_loop_get(), EFL_LOOP_EVENT_IDLE, _idle_cb, data);
}

#ifndef VIEWPORT_ENABLE
static void
_manager_content_visible_range_changed_cb(void *data, const Efl_Event *ev)
{
   Efl_Ui_Position_Manager_Range_Update *event = ev->info;
   unsigned int count;
   unsigned int lower_end;
   unsigned int upper_end;
   long length;
   Efl_Ui_Collection_Request *request = NULL;
   Eina_List *requests = NULL;
   unsigned int idx;
   MY_DATA_GET(data, pd);

   pd->current_range.start_id = event->start_id;
   pd->current_range.end_id = event->end_id;

   count = efl_model_children_count_get(efl_ui_view_model_get(data));

   length = pd->current_range.end_id - pd->current_range.start_id;
   lower_end = MAX((long)pd->current_range.start_id - (length / 2), 0);
   upper_end = MIN(pd->current_range.end_id + (length / 2), count);

   idx = lower_end;
   while (idx < upper_end)
     {
        unsigned int search_index = idx;

        requests = _cache_entity_fetch(requests, &request, pd,
                                       search_index, NULL);

        idx++;
     }
   // Done, but flush request first
   if (request) requests = eina_list_append(requests, request);

   requests = _batch_request_flush(requests, data, pd);
}
#endif

#ifdef VIEWPORT_ENABLE
static void
_manager_content_visible_range_changed_cb(void *data, const Efl_Event *ev)
{
   Efl_Ui_Position_Manager_Range_Update *event = ev->info;
   MY_DATA_GET(data, pd);
   Eina_List *requests = NULL;
   long baseid;
   unsigned int delta, marginup, margindown;
   unsigned int upperlimit_offset, lowerlimit_offset;
   unsigned int i;

   pd->start_id = event->start_id;
   pd->end_id = event->end_id;

   delta = pd->end_id - pd->start_id;

   // First time setting up the viewport, so trigger request as we see fit
   if (!pd->viewport[0])
     {
        baseid = pd->start_id - delta;

        for (i = 0; i < 3; i++)
          {
             pd->viewport[i] = calloc(1, sizeof (Efl_Ui_Collection_Viewport));
             if (!pd->viewport[i]) continue;

             pd->viewport[i]->offset = MAX(baseid + delta * i, 0);
             pd->viewport[i]->count = delta;
             pd->viewport[i]->items = calloc(delta, sizeof (Efl_Ui_Collection_Item));
             if (!pd->viewport[i]->items) continue ;

             requests = _viewport_walk_fill(requests, data, pd, pd->viewport[i]);
          }

        goto flush_requests;
     }

   // Compute limit offset
   upperlimit_offset = delta * 3 + pd->viewport[0]->offset;
   lowerlimit_offset = 0;

   // Adjust the viewport for size or to much offset change in two step

   // Trying to resize first if there size is in bigger/smaller than 25% of the original size
   margindown = delta * 75 / 100;
   marginup = delta * 125 / 100;
   if (margindown < pd->viewport[0]->count &&
       pd->viewport[0]->count < marginup)
     {
        // Trying to do the resize in an optimized way is complex, let's do it simple
        Efl_Ui_Collection_Item *items[3];
        unsigned int j = 0, t = 1;

        for (i = 0; i < 3; i++)
          {
             unsigned int m;

             items[i] = calloc(delta, sizeof (Efl_Ui_Collection_Item));
             if (!items[i]) continue;

             for (m = 0; m < delta && t < 3; m++)
               {
                  items[i][m] = pd->viewport[t]->items[j];

                  j++;
                  if (j < pd->viewport[t]->count) continue;

                  j = 0;
                  t++;
                  if (t == 3) break;
               }

             // Preserve last updated index to later build a request
             if (t == 3)
               {
                  upperlimit_offset = pd->viewport[0]->offset + i * delta + m;

                  t = 4; // So that we never come back here again
               }
          }

        // For now destroy leftover object, could be cached
        for (i = t; i < 3; i++)
          {
             for (; j < pd->viewport[i]->count; j++)
               {
                  _item_cleanup(pd->factory, &pd->viewport[i]->items[j]);
               }
             j = 0;
          }

        // And now define viewport back
        for (i = 0; i < 3; i++)
          {
             free(pd->viewport[i]->items);
             pd->viewport[i]->items = items[i];
             pd->viewport[i]->count = delta;
             pd->viewport[i]->offset = pd->viewport[0]->offset + delta * i;
          }
     }

   // We decided that resizing was unecessary
   delta = pd->viewport[0]->count;

   // Try to keep the visual viewport in between half of the first and last viewport

   // start_id is in the first half of the first viewport, assume upward move
   // start_id + delta is in the second half of the last viewport, assume upward move
   if (pd->viewport[0]->offset + delta / 2 < pd->start_id ||
       pd->start_id + delta > pd->viewport[2]->offset + delta / 2)
     {
        // We could optimize this to actually just move viewport around in most cases
        Efl_Ui_Collection_Item *items[3];
        unsigned int j = 0, t = 0;
        unsigned int target, current;

        // Case where are at the top
        if (pd->start_id < delta && pd->viewport[0]->offset == 0) goto build_request;

        // Trying to adjust the offset to maintain it in the center viewport +/- delta/2
        baseid = (pd->start_id < delta) ? 0 : pd->start_id - delta;

        // Lookup for starting point
        lowerlimit_offset = pd->viewport[0]->offset;
        target = baseid;

        // cleanup before target
        for (current = pd->viewport[t]->offset; current < target; current++)
          {
             _item_cleanup(pd->factory, &pd->viewport[t]->items[j]);

             j++;
             if (j < pd->viewport[t]->count) continue;

             j = 0;
             t++;
             if (t == 3) break;
          }

        // Allocation and copy
        for (i = 0; i < 3; i++)
          {
             unsigned int m;

             items[i] = calloc(delta, sizeof (Efl_Ui_Collection_Item));
             if (!items[i]) continue;

             for (m = 0; m < delta && t < 3; m++, target++)
               {
                  if (target < pd->viewport[t]->offset) continue ;
                  items[i][m] = pd->viewport[t]->items[j];

                  j++;
                  if (j < pd->viewport[t]->count) continue;

                  j = 0;
                  t++;
                  if (t == 3) break;
               }

             // Preserve last updated index to later build a request
             if (t == 3)
               {
                  if (upperlimit_offset > pd->viewport[0]->offset + i * delta + m)
                    {
                       upperlimit_offset = pd->viewport[0]->offset + i * delta + m;
                    }

                  t = 4; // So that we never come back here again
               }
          }

        // For now destroy leftover object, could be cached
        for (i = t; i < 3; i++)
          {
             for (; j < pd->viewport[i]->count; j++)
               {
                  _item_cleanup(pd->factory, &pd->viewport[i]->items[j]);
               }
             j = 0;
          }

        // And now define viewport back
        for (i = 0; i < 3; i++)
          {
             free(pd->viewport[i]->items);
             pd->viewport[i]->items = items[i];
             pd->viewport[i]->offset = baseid + delta * i;
          }
     }

 build_request:
   // Check if the first viewport has all the lower part of it filled with objects
   if (pd->viewport[0]->offset < lowerlimit_offset)
     {
        Efl_Ui_Collection_Request *request;

        request = calloc(1, sizeof (Efl_Ui_Collection_Request));
        if (request) return ;

        request->offset = lowerlimit_offset;
        // This length work over multiple viewport as they are contiguous
        request->length = lowerlimit_offset - pd->viewport[0]->offset;
        request->need_size = EINA_TRUE;
        request->need_entity = EINA_TRUE;

        requests = eina_list_append(requests, request);
     }

   // Check if the last viewport has all the upper part of it filler with objects
   if (pd->viewport[2]->offset + pd->viewport[2]->count > upperlimit_offset)
     {
        Efl_Ui_Collection_Request *request;

        request = calloc(1, sizeof (Efl_Ui_Collection_Request));
        if (request) return ;

        request->offset = upperlimit_offset;
        // This length work over multiple viewport as they are contiguous
        request->length = pd->viewport[2]->offset + pd->viewport[2]->count - upperlimit_offset;
        request->need_size = EINA_TRUE;
        request->need_entity = EINA_TRUE;

        requests = eina_list_append(requests, request);
     }

 flush_requests:
   requests = _batch_request_flush(requests, data, pd);
}
#endif

EFL_CALLBACKS_ARRAY_DEFINE(manager_cbs,
 { EFL_UI_POSITION_MANAGER_ENTITY_EVENT_CONTENT_SIZE_CHANGED, _manager_content_size_changed_cb },
 { EFL_UI_POSITION_MANAGER_ENTITY_EVENT_CONTENT_MIN_SIZE_CHANGED, _manager_content_min_size_changed_cb },
 { EFL_UI_POSITION_MANAGER_ENTITY_EVENT_VISIBLE_RANGE_CHANGED, _manager_content_visible_range_changed_cb }
)

static void
_item_scroll_internal(Eo *obj EINA_UNUSED,
                      Efl_Ui_Collection_View_Data *pd,
                      unsigned int index,
                      double align EINA_UNUSED,
                      Eina_Bool anim)
{
   Eina_Rect ipos, view;
   Eina_Position2D vpos;

   if (!pd->scroller) return;

   ipos = efl_ui_position_manager_entity_position_single_item(pd->manager, index);
   view = efl_ui_scrollable_viewport_geometry_get(pd->scroller);
   vpos = efl_ui_scrollable_content_pos_get(pd->scroller);

   ipos.x = ipos.x + vpos.x - view.x;
   ipos.y = ipos.y + vpos.y - view.y;

   //FIXME scrollable needs some sort of align, the docs do not even garantee to completely move in the element
   efl_ui_scrollable_scroll(pd->scroller, ipos, anim);
}

// Exported function

EOLIAN static void
_efl_ui_collection_view_factory_set(Eo *obj EINA_UNUSED, Efl_Ui_Collection_View_Data *pd,
                                  Efl_Ui_Factory *factory)
{
   if (pd->factory) efl_ui_property_bind(pd->factory, "selected", NULL);
   efl_replace(&pd->factory, factory);
   if (pd->factory) efl_ui_property_bind(pd->factory, "selected", "self.selected");
}

EOLIAN static Efl_Ui_Factory *
_efl_ui_collection_view_factory_get(const Eo *obj EINA_UNUSED, Efl_Ui_Collection_View_Data *pd)
{
   return pd->factory;
}

static void
_unref_cb(void *data)
{
   Eo *obj = data;

   efl_unref(obj);
}

EOLIAN static void
_efl_ui_collection_view_position_manager_set(Eo *obj, Efl_Ui_Collection_View_Data *pd,
                                             Efl_Ui_Position_Manager_Entity *manager)
{
   Efl_Model *model;
   unsigned int count;

   if (manager)
     EINA_SAFETY_ON_FALSE_RETURN(efl_isa(manager, EFL_UI_POSITION_MANAGER_ENTITY_INTERFACE));

   if (pd->manager)
     {
        efl_event_callback_array_del(pd->manager, manager_cbs(), obj);
        efl_del(pd->manager);
     }
   pd->manager = manager;
   if (!pd->manager) return;

   // Start watching change on model from here on
   model = pd->model;
   count = model ? efl_model_children_count_get(model) : 0;

   efl_parent_set(pd->manager, obj);
   efl_event_callback_array_add(pd->manager, manager_cbs(), obj);
        switch(efl_ui_position_manager_entity_version(pd->manager, 1))
          {
            case 1:
              efl_ui_position_manager_data_access_v1_data_access_set(pd->manager,
                efl_provider_find(obj, EFL_UI_WIN_CLASS),
                efl_ref(obj), _batch_entity_cb, _unref_cb,
                efl_ref(obj), _batch_size_cb, _unref_cb,
                count);
            break;
          }

   if (efl_finalized_get(obj))
     efl_ui_position_manager_entity_viewport_set(pd->manager, efl_ui_scrollable_viewport_geometry_get(obj));
   efl_ui_layout_orientation_set(pd->manager, pd->direction);
}

EOLIAN static Efl_Ui_Position_Manager_Entity *
_efl_ui_collection_view_position_manager_get(const Eo *obj EINA_UNUSED,
                                             Efl_Ui_Collection_View_Data *pd)
{
   return pd->manager;
}

static void
_efl_model_count_changed(void *data, const Efl_Event *event EINA_UNUSED)
{
   Efl_Ui_Collection_Request *request = NULL;
   Eina_List *requests = NULL;
   MY_DATA_GET(data, pd);
   unsigned int index;
   unsigned int count = 0;

   count = efl_model_children_count_get(pd->model);
   if (pd->focus.last)
     {
        index = _lookup_entity_index(pd->focus.last, NULL);

        if (index + 1 == count)
          return ;
     }

   // The last item is not the last item anymore
   requests = _request_add(requests, &request, count, EINA_TRUE);
   requests = _batch_request_flush(requests, data, pd);

   // We are not triggering efl_ui_position_manager_entity_data_access_set as it is can
   // only be slow, we rely on child added/removed instead (If we were to not rely on
   // child added/removed we could maybe use count changed)
}

static void
_efl_model_properties_changed(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
   // We could here watch if the global base size item change and notify of a global change
   // But I can not find a proper way to do it for the object that are not visible, which
   // is kind of the point...
}

static void
_cache_cleanup_above(Efl_Ui_Collection_View *obj, Efl_Ui_Collection_View_Data *pd, unsigned int index)
{
   Efl_Ui_Collection_Item_Lookup *lookup;
   Eina_Array scheduled_release;
   Eina_Array mark;
   Eina_Array_Iterator iterator;
   unsigned int i;

   eina_array_step_set(&mark, sizeof (Eina_Array), 16);
   eina_array_step_set(&scheduled_release, sizeof (Eina_Array), 16);

   _mark_ge((void*) pd->cache, &mark, index);

   EINA_ARRAY_ITER_NEXT(&mark, i, lookup, iterator)
     {
        pd->cache = (void*) eina_rbtree_inline_remove(pd->cache,
                                                      EINA_RBTREE_GET(lookup),
                                                      _cache_tree_cmp, NULL);
        _item_cleanup(obj, pd->factory, &lookup->item, &scheduled_release);
        free(lookup);
     }
   eina_array_flush(&mark);

   efl_ui_factory_release(pd->factory, eina_array_iterator_new(&scheduled_release));
   eina_array_flush(&scheduled_release);
}

static void
_efl_model_child_added(void *data, const Efl_Event *event)
{
   // At the moment model only append child, but let's try to handle it theorically correct
   Efl_Model_Children_Event *ev = event->info;
   MY_DATA_GET(data, pd);
#ifdef VIEWPORT_ENABLE
   Eina_List *requests = NULL;
   unsigned int i;
#endif

   _cache_cleanup_above(data, pd, ev->index);

   // Check if we really have something to do
#ifdef VIEWPORT_ENABLE
   if (!pd->viewport[0]) goto notify_manager;

   // Insert the child in the viewport if necessary
   for (i = 0; i < 3; i++)
     {
        Efl_Ui_Collection_Request *request;
        unsigned int o;
        unsigned int j;

        if (ev->index < pd->viewport[i]->offset)
          {
             pd->viewport[i]->offset++;
             continue;
          }
        if (pd->viewport[i]->offset + pd->viewport[i]->count < ev->index)
          {
             continue;
          }

        for (j = 2; j > i; j--)
          {
             _item_cleanup(pd->factory, &pd->viewport[j]->items[pd->viewport[j]->count - 1]);
             memmove(&pd->viewport[j]->items[1],
                     &pd->viewport[j]->items[0],
                     (pd->viewport[j]->count - 1) * sizeof (Efl_Ui_Collection_Item));
             pd->viewport[j]->items[0] = pd->viewport[j - 1]->items[pd->viewport[j - 1]->count - 1];
             pd->viewport[j - 1]->items[pd->viewport[j - 1]->count - 1].entity = NULL;
             pd->viewport[j - 1]->items[pd->viewport[j - 1]->count - 1].model = NULL;
          }
        o = ev->index - pd->viewport[i]->offset;
        memmove(&pd->viewport[j]->items[o],
                &pd->viewport[j]->items[o + 1],
                (pd->viewport[j]->count - 1 - o) * sizeof (Efl_Ui_Collection_Item));
        pd->viewport[j]->items[o].entity = NULL;
        pd->viewport[j]->items[o].model = efl_ref(ev->child);

        request = calloc(1, sizeof (Efl_Ui_Collection_Request));
        if (!request) break;
        request->offset = ev->index;
        request->length = 1;
        request->need_size = EINA_TRUE;
        request->need_entity = EINA_TRUE;

        requests = eina_list_append(requests, request);

        requests = _batch_request_flush(requests, data, pd);

        break;
     }

 notify_manager:
#endif
   efl_ui_position_manager_entity_item_added(pd->manager, ev->index, NULL);
}

static void
_efl_model_child_removed(void *data, const Efl_Event *event)
{
   Efl_Model_Children_Event *ev = event->info;
   MY_DATA_GET(data, pd);
   Eina_List *requests = NULL;
#ifdef VIEWPORT_ENABLE
   unsigned int i;
#endif
   unsigned int upper_end;
   long length;
   unsigned int count;
   unsigned int request_length;

   // FIXME: later optimization, instead of reloading everyone, we could actually track index and self
   // update would be more efficient, but it is also more tricky
   _cache_cleanup_above(data, pd, ev->index);

   count = efl_model_children_count_get(event->object);
   length = pd->current_range.end_id - pd->current_range.start_id;
   upper_end = MIN(pd->current_range.end_id + (length / 2), count);

   // Check if we really have something to do
#ifdef VIEWPORT_ENABLE
   if (!pd->viewport[0]) goto notify_manager;

   // Insert the child in the viewport if necessary
   for (i = 0; i < 3; i++)
     {
        Efl_Ui_Collection_Request *request;
        unsigned int o;

        if (ev->index < pd->viewport[i]->offset)
          {
             pd->viewport[i]->offset--;
             continue;
          }
        if (pd->viewport[i]->offset + pd->viewport[i]->count < ev->index)
          {
             continue;
          }

        o = ev->index - pd->viewport[i]->offset;
        _item_cleanup(pd->factory, &pd->viewport[i]->items[o]);
        for (; i < 3; i++)
          {
             memmove(&pd->viewport[i]->items[o],
                     &pd->viewport[i]->items[o + 1],
                     (pd->viewport[i]->count - 1 - o) * sizeof (Efl_Ui_Collection_Item));
             if (i + 1 < 3)
               {
                  pd->viewport[i]->items[pd->viewport[i]->count - 1] = pd->viewport[i + 1]->items[0];
               }
             else
               {
                  pd->viewport[i]->items[pd->viewport[i]->count - 1].entity = NULL;
                  pd->viewport[i]->items[pd->viewport[i]->count - 1].model = NULL;
               }
             o = 0;
          }

        request = calloc(1, sizeof (Efl_Ui_Collection_Request));
        if (!request) break;
        request->offset = pd->viewport[2]->offset + pd->viewport[i]->count - 1;
        request->length = 1;
        request->need_size = EINA_TRUE;
        request->need_entity = EINA_TRUE;

        requests = eina_list_append(requests, request);

        requests = _batch_request_flush(requests, data, pd);

        break;
     }

 notify_manager:
#endif
   request_length = upper_end - ev->index;

   if (request_length > 0)
     {
        Efl_Ui_Collection_Request *request = NULL;

        requests = _request_add(requests, &request, ev->index, EINA_TRUE);
        request->length = request_length;
        requests = eina_list_append(requests, request);
        requests = _batch_request_flush(requests, data, pd);
     }

   efl_ui_position_manager_entity_item_removed(pd->manager, ev->index, NULL);
}

EFL_CALLBACKS_ARRAY_DEFINE(model_cbs,
                           { EFL_MODEL_EVENT_CHILDREN_COUNT_CHANGED, _efl_model_count_changed },
                           { EFL_MODEL_EVENT_PROPERTIES_CHANGED, _efl_model_properties_changed },
                           { EFL_MODEL_EVENT_CHILD_ADDED, _efl_model_child_added },
                           { EFL_MODEL_EVENT_CHILD_REMOVED, _efl_model_child_removed })

static void
_efl_ui_collection_view_model_changed(void *data, const Efl_Event *event)
{
   Efl_Model_Changed_Event *ev = event->info;
   Eina_List *requests = NULL;
   MY_DATA_GET(data, pd);
   Eina_Iterator *it;
   const char *property;
   Efl_Model *model = NULL;
   unsigned int count;
   Efl_Model *mselect = NULL;
   Eina_Bool selection = EINA_FALSE, sizing = EINA_FALSE;

   // Cleanup all object, pending request to prepare refetching everything
   _all_cleanup(data, pd);
   if (pd->model) efl_event_callback_array_del(pd->model, model_cbs(), data);
   if (pd->multi_selectable_async_model)
     {
        efl_event_callback_forwarder_del(pd->multi_selectable_async_model,
                                         EFL_UI_SELECTABLE_EVENT_SELECTION_CHANGED,
                                         data);
        efl_composite_detach(data, pd->multi_selectable_async_model);
     }

   if (!ev->current)
     {
        efl_replace(&pd->model, NULL);
        efl_replace(&pd->multi_selectable_async_model, NULL);
        return ;
     }

   it = efl_model_properties_get(ev->current);
   EINA_ITERATOR_FOREACH(it, property)
     {
        // Check if the model provide selection
        if (eina_streq(property, "child.selected"))
          selection = EINA_TRUE;
        // Check if the model provide sizing logic
        else if (eina_streq(property, _efl_model_property_itemw) ||
                 eina_streq(property, _efl_model_property_itemh))
          sizing = EINA_TRUE;
     }
   eina_iterator_free(it);

   if (selection)
     {
        // Search the composition of model for the one providing MULTI_SELECTABLE_ASYNC
        mselect = ev->current;
        while (mselect &&
               !efl_isa(mselect, EFL_UI_MULTI_SELECTABLE_INDEX_RANGE_INTERFACE) &&
               efl_isa(mselect, EFL_COMPOSITE_MODEL_CLASS))
          mselect = efl_ui_view_model_get(mselect);

        if (!efl_isa(mselect, EFL_UI_MULTI_SELECTABLE_INDEX_RANGE_INTERFACE))
          {
             mselect = NULL;
             selection = EINA_FALSE;
          }
     }

   // Try to build the minimal chain of necessary model for collection view
   model = ev->current;

   // Build and connect the selection model properly
   if (!mselect)
     {
        mselect = model = efl_add_ref(EFL_UI_SELECT_MODEL_CLASS, data,
                                      efl_ui_view_model_set(efl_added, model),
                                      efl_loop_model_volatile_make(efl_added));
     }
   efl_replace(&pd->multi_selectable_async_model, mselect);
   efl_composite_attach(data, pd->multi_selectable_async_model);
   efl_event_callback_forwarder_add(pd->multi_selectable_async_model,
                                    EFL_UI_SELECTABLE_EVENT_SELECTION_CHANGED,
                                    data);

   if (!sizing) model = efl_add_ref(EFL_UI_HOMOGENEOUS_MODEL_CLASS, data,
                                    efl_ui_view_model_set(efl_added, model),
                                    efl_loop_model_volatile_make(efl_added));

   efl_replace(&pd->model, model);
   efl_event_callback_array_add(pd->model, model_cbs(), data);

   if (mselect) efl_unref(mselect);
   if (!sizing) efl_unref(model);

   count = efl_model_children_count_get(model);

#ifdef VIEWPORT_ENABLE
   for (i = 0; i < 3; i++)
     {

        if (!pd->viewport[i]) continue ;
        if (pd->viewport[i]->count == 0) continue ;

        request = calloc(1, sizeof (Efl_Ui_Collection_Request));
        if (!request) continue ;

        request->offset = pd->viewport[i]->offset;
        request->length = pd->viewport[i]->count;
        request->need_size = EINA_TRUE;
        request->need_entity = EINA_TRUE;

        requests = eina_list_append(requests, request);
     }
#endif

   // Fetch last item if necessary for later focus
   if (efl_model_children_count_get(model))
     {
        Efl_Ui_Collection_Request *request = NULL;
        uint64_t index = efl_model_children_count_get(model) - 1;

        requests = _request_add(requests, &request, index, EINA_TRUE);
     }

   // Flush all pending request
   requests = _batch_request_flush(requests, data, pd);

   switch (efl_ui_position_manager_entity_version(pd->manager, 1))
     {
       case 1:
         efl_ui_position_manager_data_access_v1_data_access_set(pd->manager,
           efl_provider_find(data, EFL_UI_WIN_CLASS),
           efl_ref(data), _batch_entity_cb, _unref_cb,
           efl_ref(data), _batch_size_cb, _unref_cb,
           count);
       break;
     }
   efl_ui_position_manager_entity_item_size_changed(pd->manager, 0, count - 1);
}

static void
_pan_viewport_changed_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   MY_DATA_GET(data, pd);
   Eina_Rect rect = efl_ui_scrollable_viewport_geometry_get(data);

   efl_ui_position_manager_entity_viewport_set(pd->manager, rect);
}

static void
_pan_position_changed_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   MY_DATA_GET(data, pd);
   Eina_Position2D pos = efl_ui_pan_position_get(pd->pan);
   Eina_Position2D max = efl_ui_pan_position_max_get(pd->pan);
   Eina_Vector2 rpos = {0.0, 0.0};

   if (max.x > 0.0)
     rpos.x = (double)pos.x/(double)max.x;
   if (max.y > 0.0)
     rpos.y = (double)pos.y/(double)max.y;

   efl_ui_position_manager_entity_scroll_position_set(pd->manager, rpos.x, rpos.y);
}

EFL_CALLBACKS_ARRAY_DEFINE(pan_events_cb,
  {EFL_UI_PAN_EVENT_PAN_CONTENT_POSITION_CHANGED, _pan_position_changed_cb},
  {EFL_GFX_ENTITY_EVENT_SIZE_CHANGED, _pan_viewport_changed_cb},
  {EFL_GFX_ENTITY_EVENT_POSITION_CHANGED, _pan_viewport_changed_cb},
)

EOLIAN static Efl_Object *
_efl_ui_collection_view_efl_object_constructor(Eo *obj, Efl_Ui_Collection_View_Data *pd)
{
   pd->direction = EFL_UI_LAYOUT_ORIENTATION_VERTICAL;
   obj = efl_constructor(efl_super(obj, EFL_UI_COLLECTION_VIEW_CLASS));

   if (!elm_widget_theme_klass_get(obj))
     elm_widget_theme_klass_set(obj, "collection");

   efl_wref_add(efl_add(EFL_CANVAS_RECTANGLE_CLASS, evas_object_evas_get(obj)), &pd->sizer);
   efl_gfx_color_set(pd->sizer, 0, 0, 0, 0);

   efl_wref_add(efl_add(EFL_UI_PAN_CLASS, obj), &pd->pan);
   efl_content_set(pd->pan, pd->sizer);
   efl_event_callback_array_add(pd->pan, pan_events_cb(), obj);

   efl_wref_add(efl_add(EFL_UI_SCROLL_MANAGER_CLASS, obj), &pd->scroller);
   efl_composite_attach(obj, pd->scroller);
   efl_ui_mirrored_set(pd->scroller, efl_ui_mirrored_get(obj));
   efl_ui_scroll_manager_pan_set(pd->scroller, pd->pan);

   efl_ui_scroll_connector_bind(obj, pd->scroller);

   efl_event_callback_add(obj, EFL_UI_VIEW_EVENT_MODEL_CHANGED,
                          _efl_ui_collection_view_model_changed, obj);

   return obj;
}

EOLIAN static void
_efl_ui_collection_view_efl_object_invalidate(Eo *obj,
                                              Efl_Ui_Collection_View_Data *pd)
{
   efl_ui_collection_view_position_manager_set(obj, NULL);
   efl_event_callback_del(obj, EFL_UI_VIEW_EVENT_MODEL_CHANGED,
                          _efl_ui_collection_view_model_changed, obj);

   _all_cleanup(obj, pd);

   //pd pan is given to edje, which reparents it, which forces us to manually deleting it
   if (pd->pan)
     efl_del(pd->pan);

   efl_invalidate(efl_super(obj, EFL_UI_COLLECTION_VIEW_CLASS));
}

EOLIAN static void
_efl_ui_collection_view_efl_ui_layout_orientable_orientation_set(Eo *obj EINA_UNUSED,
                                                                 Efl_Ui_Collection_View_Data *pd,
                                                                 Efl_Ui_Layout_Orientation dir)
{
   if (pd->direction == dir) return;

   pd->direction = dir;
   if (pd->manager) efl_ui_layout_orientation_set(pd->manager, dir);
}

EOLIAN static Efl_Ui_Layout_Orientation
_efl_ui_collection_view_efl_ui_layout_orientable_orientation_get(const Eo *obj EINA_UNUSED,
                                                                 Efl_Ui_Collection_View_Data *pd)
{
   return pd->direction;
}

EOLIAN static Eina_Error
_efl_ui_collection_view_efl_ui_widget_theme_apply(Eo *obj, Efl_Ui_Collection_View_Data *pd)
{
   Eina_Error res;

   ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd, EFL_UI_THEME_APPLY_ERROR_GENERIC);
   res = efl_ui_widget_theme_apply(efl_super(obj, MY_CLASS));
   if (res == EFL_UI_THEME_APPLY_ERROR_GENERIC) return res;
   efl_ui_mirrored_set(pd->scroller, efl_ui_mirrored_get(obj));
   efl_content_set(efl_part(wd->resize_obj, "efl.content"), pd->pan);

   return res;
}

EOLIAN static void
_efl_ui_collection_view_efl_ui_scrollable_match_content_set(Eo *obj, Efl_Ui_Collection_View_Data *pd, Eina_Bool w, Eina_Bool h)
{
   if (pd->match_content.w == w && pd->match_content.h == h)
     return;

   pd->match_content.w = w;
   pd->match_content.h = h;

   efl_ui_scrollable_match_content_set(pd->scroller, w, h);
   flush_min_size(obj, pd);
}

EOLIAN static Efl_Ui_Focus_Manager *
_efl_ui_collection_view_efl_ui_widget_focus_manager_focus_manager_create(Eo *obj, Efl_Ui_Collection_View_Data *pd EINA_UNUSED, Efl_Ui_Focus_Object *root)
{
   Efl_Ui_Collection_View_Focus_Manager_Data *mpd;
   Eo *manager = efl_add(EFL_UI_COLLECTION_VIEW_FOCUS_MANAGER_CLASS, obj,
                         efl_ui_focus_manager_root_set(efl_added, root));

   mpd = efl_data_scope_get(manager, EFL_UI_COLLECTION_VIEW_FOCUS_MANAGER_CLASS);
   mpd->collection = obj;

   return manager;
}

EOLIAN static Efl_Ui_Focus_Object *
_efl_ui_collection_view_efl_ui_focus_manager_move(Eo *obj, Efl_Ui_Collection_View_Data *pd, Efl_Ui_Focus_Direction direction)
{
   Eo *new_obj, *focus;
   Eina_Size2D step;

   new_obj = efl_ui_focus_manager_move(efl_super(obj, MY_CLASS), direction);
   focus = efl_ui_focus_manager_focus_get(obj);
   step = efl_gfx_hint_size_combined_min_get(focus);
   if (!new_obj)
     {
        Eina_Rect pos = efl_gfx_entity_geometry_get(focus);
        Eina_Rect view = efl_ui_scrollable_viewport_geometry_get(pd->scroller);
        Eina_Position2D vpos = efl_ui_scrollable_content_pos_get(pd->scroller);

        pos.x = pos.x + vpos.x - view.x;
        pos.y = pos.y + vpos.y - view.y;
        Eina_Position2D max = efl_ui_pan_position_max_get(pd->pan);

        if (direction == EFL_UI_FOCUS_DIRECTION_RIGHT)
          {
             if (pos.x < max.x)
               {
                  pos.x = MIN(max.x, pos.x + step.w);
                  efl_ui_scrollable_scroll(obj, pos, EINA_TRUE);
                  new_obj = focus;
               }
          }
        else if (direction == EFL_UI_FOCUS_DIRECTION_LEFT)
          {
             if (pos.x > 0)
               {
                  pos.x = MAX(0, pos.x - step.w);
                  efl_ui_scrollable_scroll(obj, pos, EINA_TRUE);
                  new_obj = focus;
               }
          }
        else if (direction == EFL_UI_FOCUS_DIRECTION_UP)
          {
             if (pos.y > 0)
               {
                  pos.y = MAX(0, pos.y - step.h);
                  efl_ui_scrollable_scroll(obj, pos, EINA_TRUE);
                  new_obj = focus;
               }
          }
        else if (direction == EFL_UI_FOCUS_DIRECTION_DOWN)
          {
             if (pos.y < max.y)
               {
                  pos.y = MAX(0, pos.y + step.h);
                  efl_ui_scrollable_scroll(obj, pos, EINA_TRUE);
                  new_obj = focus;
               }
          }
     }
   else
     {
        Efl_Model *model;
        Eina_Value *vindex;
        unsigned int index;

        model = efl_ui_view_model_get(new_obj);
        vindex = efl_model_property_get(model, "child.index");
        if (eina_value_uint_convert(vindex, &index))
          _item_scroll_internal(obj, pd, index, .0, EINA_TRUE);
        eina_value_free(vindex);
     }

   return new_obj;
}

EOLIAN static Eina_Bool
_efl_ui_collection_view_efl_ui_widget_focus_state_apply(Eo *obj, Efl_Ui_Collection_View_Data *pd EINA_UNUSED, Efl_Ui_Widget_Focus_State current_state, Efl_Ui_Widget_Focus_State *configured_state, Efl_Ui_Widget *redirect EINA_UNUSED)
{
   return efl_ui_widget_focus_state_apply(efl_super(obj, MY_CLASS), current_state, configured_state, obj);
}

#include "efl_ui_collection_view.eo.c"

#define ITEM_IS_OUTSIDE_VISIBLE(id) id < cpd->start_id || id > cpd->end_id

static Efl_Ui_Item *
_find_item(Eo *obj EINA_UNUSED, Efl_Ui_Collection_View_Data *pd EINA_UNUSED, Eo *focused_element)
{
   if (!focused_element) return NULL;

   while (focused_element &&
          efl_key_data_get(focused_element, COLLECTION_VIEW_MANAGED) != COLLECTION_VIEW_MANAGED_YES)
     {
        focused_element = efl_ui_widget_parent_get(focused_element);
     }

   return focused_element;
}

static inline void
_assert_item_available(Eo *item, int new_id, Efl_Ui_Collection_View_Data *pd)
{
   efl_gfx_entity_visible_set(item, EINA_TRUE);
   efl_gfx_entity_geometry_set(item, efl_ui_position_manager_entity_position_single_item(pd->manager, new_id));
}
EOLIAN static void
_efl_ui_collection_view_focus_manager_efl_ui_focus_manager_manager_focus_set(Eo *obj, Efl_Ui_Collection_View_Focus_Manager_Data *pd, Efl_Ui_Focus_Object *focus)
{
   MY_DATA_GET(pd->collection, cpd);
   Efl_Ui_Item *item = NULL;
   unsigned int item_id;

   if (focus == efl_ui_focus_manager_root_get(obj))
     {
        // Find last item
        item = cpd->focus.previously;
        if (!item) item = cpd->focus.last;
        if (item) item_id = _lookup_entity_index(item, NULL);
        else item_id = efl_model_children_count_get(cpd->model) - 1;
     }
   else
     {
        item = _find_item(obj, cpd, focus);
        if (!item) return ;

        item_id = _lookup_entity_index(item, NULL);
     }

   // If this is NULL then we are before finalize, we cannot serve any sane value here
   if (!cpd->manager) return ;

   if (ITEM_IS_OUTSIDE_VISIBLE(item_id))
     {
        _assert_item_available(item, item_id, cpd);
     }
   efl_ui_focus_manager_focus_set(efl_super(obj, EFL_UI_COLLECTION_VIEW_FOCUS_MANAGER_CLASS), focus);
}

static int
_id_from_item(Efl_Ui_Item *item, unsigned int *index)
{
   Eina_Value *vindex;
   Efl_Model *model;

   model = efl_ui_view_model_get(item);

   vindex = efl_model_property_get(model, "child.index");
   EINA_SAFETY_ON_FALSE_RETURN_VAL(eina_value_uint_convert(vindex, index), EINA_FALSE);
   eina_value_free(vindex);
   return EINA_TRUE;
}

EOLIAN static Efl_Ui_Focus_Object *
_efl_ui_collection_view_focus_manager_efl_ui_focus_manager_request_move(Eo *obj, Efl_Ui_Collection_View_Focus_Manager_Data *pd, Efl_Ui_Focus_Direction direction, Efl_Ui_Focus_Object *child, Eina_Bool logical)
{
   MY_DATA_GET(pd->collection, cpd);
   Efl_Ui_Item *new_item = NULL;
   Efl_Ui_Item *item;
   unsigned int item_id;

   if (!child)
     child = efl_ui_focus_manager_focus_get(obj);

   item = _find_item(obj, cpd, child);

   //if this is NULL then we are before finalize, we cannot serve any sane value here
   if (!cpd->manager) goto end;
   if (!item) goto end;

   if (!_id_from_item(item, &item_id))
     goto end;

   if (ITEM_IS_OUTSIDE_VISIBLE(item_id))
     {
        unsigned int new_id;

        if (!efl_ui_position_manager_entity_relative_item(cpd->manager,
                                                          item_id,
                                                          direction,
                                                          &new_id))
          {
             new_item = NULL;
          }
        else
          {
             Efl_Ui_Collection_Item_Lookup *lookup;
#ifdef VIEWPORT_ENABLE
             unsigned int i;

             for (i = 0; i < 3; i++)
               {
                  if (!cpd->viewport[i]) continue;

                  if (!((cpd->viewport[i]->offset <= (unsigned int) new_id) &&
                        ((unsigned int) new_id < cpd->viewport[i]->offset + cpd->viewport[i]->count)))
                    continue;

                  new_item = cpd->viewport[i]->items[new_id - cpd->viewport[i]->offset].entity;
                  // We shouldn't get in a case where the available item is NULL
                  if (!new_item) break; // Just in case
                  _assert_item_available(new_item, new_id, cpd);
               }
#else
               unsigned int search_index = new_id;
               lookup = (void*) eina_rbtree_inline_lookup(cpd->cache, &search_index,
                                           sizeof (search_index), _cache_tree_lookup,
                                           NULL);
               if (lookup)
                 {
                    _assert_item_available(lookup->item.entity, new_id, cpd);
                    new_item = lookup->item.entity;
                 }
               else
                 {
                    ERR("This item cannot get focus right now. It should be visible first.");
                    new_item = NULL;
                 }
#endif
          }
     }
   else
     {
        new_item = efl_ui_focus_manager_request_move(efl_super(obj, EFL_UI_COLLECTION_VIEW_FOCUS_MANAGER_CLASS), direction, child, logical);
     }

 end:
   efl_replace(&cpd->focus.previously, new_item);
   return new_item;
}

#include "efl_ui_collection_view_focus_manager.eo.c"