summaryrefslogtreecommitdiff
path: root/src/lib/elementary/efl_ui_grid_position_manager.c
blob: f4dfd78c319d9662e4586d9592e3f38de71509c4 (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
#ifdef HAVE_CONFIG_H
#include "elementary_config.h"
#endif

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

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

typedef struct {
   Eina_Accessor *content_acc, *size_acc;
   unsigned int size;
   Eina_Rect viewport;
   Eina_Vector2 scroll_position;
   Efl_Ui_Layout_Orientation dir;
   struct {
      unsigned int start_id, end_id;
   } prev_run;
   Eina_Size2D max_min_size;
   Eina_Size2D last_viewport_size;
   Eina_Size2D prev_min_size;
   struct {
      int columns;
      int rows;
   } current_display_table;
} Efl_Ui_Grid_Position_Manager_Data;

static inline void
vis_change_segment(Efl_Ui_Grid_Position_Manager_Data *pd, int a, int b, Eina_Bool flag)
{
   for (int i = MIN(a, b); i < MAX(a, b); ++i)
     {
        Efl_Gfx_Entity *ent;

        EINA_SAFETY_ON_FALSE_RETURN(eina_accessor_data_get(pd->content_acc, i, (void**) &ent));
        if (ent && !efl_ui_focus_object_focus_get(ent))
          {
             efl_gfx_entity_visible_set(ent, flag);
          }
     }
}

static void
_reposition_content(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd)
{
   Eina_Size2D space_size;
   int relevant_space_size, relevant_viewport;
   unsigned int start_id, end_id, step;

   if (!pd->size) return;
   if (pd->max_min_size.w <= 0 || pd->max_min_size.h <= 0) return;
   if (pd->current_display_table.columns <= 0 || pd->current_display_table.rows <= 0) return;

   //space size contains the amount of space that is outside the viewport (either to the top or to the left)
   space_size.w = (MAX(pd->last_viewport_size.w - pd->viewport.w, 0))*pd->scroll_position.x;
   space_size.h = (MAX(pd->last_viewport_size.h - pd->viewport.h, 0))*pd->scroll_position.y;

   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     {
        relevant_space_size = space_size.h;
        relevant_viewport = pd->viewport.h;
        step = pd->max_min_size.h;
     }
   else
     {
        relevant_space_size = space_size.w;
        relevant_viewport = pd->viewport.w;
        step = pd->max_min_size.w;
     }
   start_id =  MIN((MAX(relevant_space_size,0) / step)*pd->current_display_table.columns, pd->size);
   end_id =  MIN((((MAX(relevant_space_size,0) + relevant_viewport + step) / step)*pd->current_display_table.columns)+1, pd->size);

   EINA_SAFETY_ON_FALSE_RETURN(start_id <= end_id);
   EINA_SAFETY_ON_FALSE_RETURN(start_id <= pd->size);

   //to performance optimize the whole widget, we are setting the objects that are outside the viewport to visibility false
   //The code below ensures that things outside the viewport are always hidden, and things inside the viewport are visible
   if (end_id < pd->prev_run.start_id || start_id > pd->prev_run.end_id)
     {
        //it is important to first make the segment visible here, and then hide the rest
        //otherwise we get a state where item_container has 0 subchildren, which triggers a lot of focus logic.
        vis_change_segment(pd, start_id, end_id, EINA_TRUE);
        vis_change_segment(pd, pd->prev_run.start_id, pd->prev_run.end_id, EINA_FALSE);
     }
   else
     {
        vis_change_segment(pd, pd->prev_run.start_id, start_id, (pd->prev_run.start_id > start_id));
        vis_change_segment(pd, pd->prev_run.end_id, end_id, (pd->prev_run.end_id < end_id));
     }

   for (unsigned int i = start_id; i < end_id; ++i)
     {
        Eina_Rect geom;
        Efl_Gfx_Entity *ent;
        geom.size = pd->max_min_size;
        geom.pos = pd->viewport.pos;

        if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
          {
             geom.x += pd->max_min_size.w*(i%pd->current_display_table.columns);
             geom.y += pd->max_min_size.h*(i/pd->current_display_table.columns);
             geom.y -= (relevant_space_size);
          }
        else
          {
             geom.x += pd->max_min_size.w*(i/pd->current_display_table.columns);
             geom.y += pd->max_min_size.h*(i%pd->current_display_table.columns);
             geom.x -= (relevant_space_size);
          }

        EINA_SAFETY_ON_FALSE_RETURN(eina_accessor_data_get(pd->content_acc, i, (void**) &ent));
        //printf(">%d (%d, %d, %d, %d) %p\n", i, geom.x, geom.y, geom.w, geom.h, ent);
        efl_gfx_entity_geometry_set(ent, geom);
     }
   pd->prev_run.start_id = start_id;
   pd->prev_run.end_id = end_id;
}

static inline void
_flush_abs_size(Eo *obj, Efl_Ui_Grid_Position_Manager_Data *pd)
{
   int minor, major;
   Eina_Size2D vp_size;

   if (!pd->size) return;
   if (pd->max_min_size.w <= 0 || pd->max_min_size.h <= 0) return;

   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     {
        major = pd->viewport.w/pd->max_min_size.w;
        pd->current_display_table.columns = major;
     }
   else
     {
        major = pd->viewport.h/pd->max_min_size.h;
        pd->current_display_table.columns = major;
     }

   if (major <= 0) return;
   minor = ceil((double)pd->size/(double)major);

   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     pd->current_display_table.rows = minor;
   else
     pd->current_display_table.rows = minor;

   /*
    * calculate how much size we need with major in the given orientation.
    * The
    */
   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     {
        vp_size.w = pd->viewport.w;
        vp_size.h = minor*pd->max_min_size.h;
     }
   else
     {
        vp_size.h = pd->viewport.h;
        vp_size.w = minor*pd->max_min_size.w;
     }
   if (vp_size.h != pd->last_viewport_size.h || vp_size.w != pd->last_viewport_size.w)
     {
        pd->last_viewport_size = vp_size;
        efl_event_callback_call(obj, EFL_UI_ITEM_POSITION_MANAGER_EVENT_CONTENT_SIZE_CHANGED, &vp_size);
     }
}

static inline void
_update_min_size(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, int added_index)
{
   Eina_Size2D elemsize;

   EINA_SAFETY_ON_FALSE_RETURN(eina_accessor_data_get(pd->size_acc, added_index, (void*)&elemsize));
   pd->max_min_size.w = MAX(pd->max_min_size.w, elemsize.w);
   pd->max_min_size.h = MAX(pd->max_min_size.h, elemsize.h);
}

static inline void
_flush_min_size(Eo *obj, Efl_Ui_Grid_Position_Manager_Data *pd)
{
   Eina_Size2D min_size = pd->max_min_size;

   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     min_size.h = -1;
   else
     min_size.w = -1;

   if (pd->prev_min_size.w != min_size.w || pd->prev_min_size.h != min_size.h)
     {
        pd->prev_min_size = min_size;
        efl_event_callback_call(obj, EFL_UI_ITEM_POSITION_MANAGER_EVENT_CONTENT_MIN_SIZE_CHANGED, &min_size);
     }
}

EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_item_position_manager_data_access_set(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, Eina_Accessor *obj_access, Eina_Accessor *size_access, int size)
{
   pd->size_acc = size_access;
   pd->content_acc = obj_access;
   pd->size = size;
}

EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_item_position_manager_viewport_set(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, Eina_Rect viewport)
{
   pd->viewport = viewport;
   _flush_abs_size(obj, pd);
   _reposition_content(obj, pd);
}

EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_item_position_manager_scroll_position_set(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, double x, double y)
{
   pd->scroll_position.x = x;
   pd->scroll_position.y = y;
   _reposition_content(obj, pd);
}

EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_item_position_manager_item_added(Eo *obj, Efl_Ui_Grid_Position_Manager_Data *pd, int added_index, Efl_Gfx_Entity *subobj EINA_UNUSED)
{
   pd->size ++;

   efl_gfx_entity_visible_set(subobj, EINA_FALSE);
   _update_min_size(obj, pd, added_index);
   _flush_min_size(obj, pd);
   _flush_abs_size(obj, pd);
   _reposition_content(obj, pd); //FIXME we might can skip that
}

EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_item_position_manager_item_removed(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, int removed_index EINA_UNUSED, Efl_Gfx_Entity *subobj EINA_UNUSED)
{
   EINA_SAFETY_ON_FALSE_RETURN(pd->size > 0);
   pd->size --;
   pd->prev_run.start_id = MIN(pd->prev_run.start_id, pd->size);
   pd->prev_run.end_id = MIN(pd->prev_run.end_id, pd->size);
   //we ignore here that we might loose the item giving the current max min size
   _flush_abs_size(obj, pd);
   _reposition_content(obj, pd); //FIXME we might can skip that
   efl_gfx_entity_visible_set(subobj, EINA_TRUE);
}


EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_item_position_manager_item_size_changed(Eo *obj, Efl_Ui_Grid_Position_Manager_Data *pd, int start_id, int end_id)
{
   for (int i = start_id; i <= end_id; ++i)
     {
        _update_min_size(obj, pd, i);
     }

   _flush_min_size(obj, pd);
   _flush_abs_size(obj, pd);
   _reposition_content(obj, pd); //FIXME we could check if this is needed or not
}

EOLIAN static void
_efl_ui_grid_position_manager_efl_ui_layout_orientable_orientation_set(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, Efl_Ui_Layout_Orientation dir)
{
   pd->dir = dir;
   _flush_min_size(obj, pd);
   _flush_abs_size(obj, pd);
   _reposition_content(obj, pd); //FIXME we could check if this is needed or not
}


EOLIAN static Efl_Ui_Layout_Orientation
_efl_ui_grid_position_manager_efl_ui_layout_orientable_orientation_get(const Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd)
{
   return pd->dir;
}

EOLIAN static Eina_Rect
_efl_ui_grid_position_manager_efl_ui_item_position_manager_position_single_item(Eo *obj EINA_UNUSED, Efl_Ui_Grid_Position_Manager_Data *pd, int idx)
{
   Eina_Rect geom;
   Eina_Size2D space_size;
   unsigned int relevant_space_size;

   if (!pd->size) return EINA_RECT(0, 0, 0, 0);
   if (pd->max_min_size.w <= 0 || pd->max_min_size.h <= 0) return EINA_RECT(0, 0, 0, 0);
   if (pd->current_display_table.columns <= 0 || pd->current_display_table.rows <= 0) return EINA_RECT(0, 0, 0, 0);

   //space size contains the amount of space that is outside the viewport (either to the top or to the left)
   space_size.w = (MAX(pd->last_viewport_size.w - pd->viewport.w, 0))*pd->scroll_position.x;
   space_size.h = (MAX(pd->last_viewport_size.h - pd->viewport.h, 0))*pd->scroll_position.y;

   EINA_SAFETY_ON_FALSE_RETURN_VAL(space_size.w >= 0 && space_size.h >= 0, EINA_RECT(0, 0, 0, 0));
   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     {
        relevant_space_size = space_size.h;
     }
   else
     {
        relevant_space_size = space_size.w;
     }
   geom.size = pd->max_min_size;
   geom.pos = pd->viewport.pos;

   if (pd->dir == EFL_UI_LAYOUT_ORIENTATION_VERTICAL)
     {
        geom.x += pd->max_min_size.w*(idx%pd->current_display_table.columns);
        geom.y += pd->max_min_size.h*(idx/pd->current_display_table.columns);
        geom.y -= (relevant_space_size);
     }
   else
     {
        geom.x += pd->max_min_size.w*(idx/pd->current_display_table.columns);
        geom.y += pd->max_min_size.h*(idx%pd->current_display_table.columns);
        geom.x -= (relevant_space_size);
     }

   return geom;
}

#include "efl_ui_grid_position_manager.eo.c"