This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository efl.

View the commit online.

commit 9e22275104c62035d4c316eddbbee5addcfa7c31
Author: Cedric BAIL <[email protected]>
AuthorDate: Tue Nov 10 18:41:15 2026 -0600

    elementary: exact_model: sizes follow their rows when one comes or goes
    
    The Exact_Model keeps a size under the index of its row and never moved
    it when a row was inserted or removed, and since the Exact_Model's
    answer is authoritative for the view, every row after the change
    reported the size of the row that had been at its index, and a row that
    came in had the one of the row it pushed, so it was never measured.
    
    Listen to the root's own child added and removed events before anyone
    else (the view, the position manager) does, and shift the stored sizes,
    width and height, by one row from the index on: the slots are written
    back first, then only the blocks from the one holding the index are
    expanded and compressed again, the blocks of zeros that the shift does
    not reach are left alone. The total height is lowered by the height of
    the row that left.
    
    The Average_Model keeps its values in the rows themselves and has
    nothing to move.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 src/lib/elementary/efl_ui_exact_model.c | 270 +++++++++++++++++++++++++++++---
 src/tests/elementary/efl_ui_model.c     | 122 +++++++++++++++
 2 files changed, 369 insertions(+), 23 deletions(-)

diff --git a/src/lib/elementary/efl_ui_exact_model.c b/src/lib/elementary/efl_ui_exact_model.c
index eb363696a2..c177a4901c 100644
--- a/src/lib/elementary/efl_ui_exact_model.c
+++ b/src/lib/elementary/efl_ui_exact_model.c
@@ -2,6 +2,7 @@
 # include <config.h>
 #endif
 
+#include <limits.h>
 #include <Elementary.h>
 #include "elm_priv.h"
 
@@ -53,6 +54,22 @@ struct _Efl_Ui_Exact_Model_Data
    } slot[3];
 };
 
+static void _efl_ui_exact_model_child_added(void *data, const Efl_Event *event);
+static void _efl_ui_exact_model_child_removed(void *data, const Efl_Event *event);
+
+// Before anything that is told of the change (the view, the position manager),
+// the sizes have to be where the rows now are.
+//
+// The callbacks of an object are called in the order of their priority value,
+// the lowest first, and a priority is a short: the lowest value there is runs
+// before any other, including the ones of EFL_CALLBACK_PRIORITY_BEFORE that the
+// composite model and others use.
+#define EFL_UI_EXACT_MODEL_FIRST SHRT_MIN
+
+EFL_CALLBACKS_ARRAY_DEFINE(exact_model_callbacks,
+                           { EFL_MODEL_EVENT_CHILD_ADDED, _efl_ui_exact_model_child_added },
+                           { EFL_MODEL_EVENT_CHILD_REMOVED, _efl_ui_exact_model_child_removed });
+
 static Efl_Object *
 _efl_ui_exact_model_efl_object_constructor(Eo *obj, Efl_Ui_Exact_Model_Data *pd)
 {
@@ -61,7 +78,12 @@ _efl_ui_exact_model_efl_object_constructor(Eo *obj, Efl_Ui_Exact_Model_Data *pd)
    if (parent && efl_isa(parent, EFL_UI_EXACT_MODEL_CLASS))
      pd->parent = efl_data_scope_get(efl_parent_get(obj), EFL_UI_EXACT_MODEL_CLASS);
 
-   return efl_constructor(efl_super(obj, EFL_UI_EXACT_MODEL_CLASS));
+   obj = efl_constructor(efl_super(obj, EFL_UI_EXACT_MODEL_CLASS));
+   // (only the root keeps sizes)
+   if (obj && !pd->parent)
+     efl_event_callback_array_priority_add(obj, exact_model_callbacks(),
+                                           EFL_UI_EXACT_MODEL_FIRST, pd);
+   return obj;
 }
 
 // The compressed block of zeros that fills the gaps is shared by every model,
@@ -211,6 +233,34 @@ _efl_ui_exact_model_buffer_expand(unsigned int list_index, unsigned int *buffer,
    return buffer;
 }
 
+// Writes the slot back under the block it holds (not the one that evicts it),
+// when it was written to, and leaves it holding nothing.
+static void
+_efl_ui_exact_model_slot_commit(Efl_Ui_Exact_Model_Data *root, unsigned char found)
+{
+   unsigned int block = root->slot[found].start_offset;
+
+   if (!root->slot[found].defined) return;
+
+   // (a slot that was only read has nothing to commit)
+   if (root->slot[found].width &&
+       root->slot[found].decompressed.width &&
+       root->slot[found].dirty.width)
+     root->compressed.width =
+       _efl_ui_exact_model_slot_compress(block, root->compressed.width,
+                                         root->slot[found].width);
+   if (root->slot[found].height &&
+       root->slot[found].decompressed.height &&
+       root->slot[found].dirty.height)
+     root->compressed.height =
+       _efl_ui_exact_model_slot_compress(block, root->compressed.height,
+                                         root->slot[found].height);
+   root->slot[found].decompressed.width = EINA_FALSE;
+   root->slot[found].decompressed.height = EINA_FALSE;
+   root->slot[found].dirty.width = EINA_FALSE;
+   root->slot[found].dirty.height = EINA_FALSE;
+}
+
 static unsigned char
 _efl_ui_exact_model_slot_find(Efl_Ui_Exact_Model_Data *root, unsigned int index,
                               Eina_Bool width_get, Eina_Bool height_get)
@@ -247,28 +297,7 @@ _efl_ui_exact_model_slot_find(Efl_Ui_Exact_Model_Data *root, unsigned int index,
                found = lookup;
           }
 
-        // Commit change back to the stored buffer list
-        if (root->slot[found].defined &&
-            (root->slot[found].width ||
-             root->slot[found].height))
-          {
-             // (written back under the block it holds, not the one that evicts it)
-             unsigned int block = root->slot[found].start_offset;
-
-             // (a slot that was only read has nothing to commit)
-             if (root->slot[found].width &&
-                 root->slot[found].decompressed.width &&
-                 root->slot[found].dirty.width)
-               root->compressed.width =
-                 _efl_ui_exact_model_slot_compress(block, root->compressed.width,
-                                                   root->slot[found].width);
-             if (root->slot[found].height &&
-                 root->slot[found].decompressed.height &&
-                 root->slot[found].dirty.height)
-               root->compressed.height =
-                 _efl_ui_exact_model_slot_compress(block, root->compressed.height,
-                                                   root->slot[found].height);
-          }
+        _efl_ui_exact_model_slot_commit(root, found);
 
         root->slot[found].defined = EINA_TRUE;
         root->slot[found].decompressed.width = EINA_FALSE;
@@ -300,6 +329,201 @@ _efl_ui_exact_model_slot_find(Efl_Ui_Exact_Model_Data *root, unsigned int index,
    return found;
 }
 
+// Stores the content of a block in the list node that holds it, or at the end
+// of the list when it is the one after the last.
+static void
+_efl_ui_exact_model_block_store(Eina_List **compressed, Eina_List *l,
+                                unsigned int block, unsigned int *buffer)
+{
+   Eina_Binbuf *old;
+   Eina_Binbuf *tbuf;
+   Eina_Binbuf *cbuf;
+
+   if (!l)
+     {
+        *compressed = _efl_ui_exact_model_slot_compress(block, *compressed, buffer);
+        return;
+     }
+
+   tbuf = eina_binbuf_manage_new((unsigned char *) buffer,
+                                 EFL_UI_EXACT_MODEL_CONTENT_LENGTH, EINA_TRUE);
+   if (!tbuf) return;
+   cbuf = emile_compress(tbuf, EMILE_LZ4, EMILE_COMPRESSOR_FAST);
+   eina_binbuf_free(tbuf);
+   if (!cbuf) return;
+
+   old = eina_list_data_get(l);
+   eina_list_data_set(l, cbuf);
+   if (old != _zero_block)
+     {
+        eina_binbuf_free(old);
+     }
+   else
+     {
+        _efl_ui_exact_model_zero_unref();
+     }
+}
+
+static void
+_efl_ui_exact_model_block_load(Eina_List *l, unsigned int *buffer)
+{
+   Eina_Binbuf *tmp;
+
+   if (!l)
+     {
+        memset(buffer, 0, EFL_UI_EXACT_MODEL_CONTENT_LENGTH);
+        return;
+     }
+   tmp = eina_binbuf_manage_new((unsigned char *) buffer,
+                                EFL_UI_EXACT_MODEL_CONTENT_LENGTH, EINA_TRUE);
+   if (!tmp)
+     {
+        memset(buffer, 0, EFL_UI_EXACT_MODEL_CONTENT_LENGTH);
+        return;
+     }
+   emile_expand(eina_list_data_get(l), tmp, EMILE_LZ4);
+   eina_binbuf_free(tmp);
+}
+
+// A row is inserted at index: every size from there on is one row further, and
+// the row that is now at index has none. Only the blocks from the one holding
+// index on are touched, and the blocks of zeros that the shift does not reach
+// are left alone.
+static void
+_efl_ui_exact_model_list_insert(Eina_List **compressed, unsigned int index)
+{
+   unsigned int buffer[EFL_UI_EXACT_MODEL_CONTENT];
+   unsigned int block = index / EFL_UI_EXACT_MODEL_CONTENT;
+   unsigned int start = index % EFL_UI_EXACT_MODEL_CONTENT;
+   unsigned int carry = 0, out;
+   Eina_List *l;
+
+   l = eina_list_nth_list(*compressed, block);
+   // Everything from here on is zero, and stays so
+   if (!l) return;
+
+   for (;; block++)
+     {
+        Eina_List *next = eina_list_next(l);
+
+        if ((block > (index / EFL_UI_EXACT_MODEL_CONTENT)) && !carry &&
+            (eina_list_data_get(l) == _zero_block))
+          goto advance;
+
+        _efl_ui_exact_model_block_load(l, buffer);
+        out = buffer[EFL_UI_EXACT_MODEL_CONTENT - 1];
+        memmove(buffer + start + 1, buffer + start,
+                (EFL_UI_EXACT_MODEL_CONTENT - 1 - start) * sizeof (unsigned int));
+        buffer[start] = carry;
+        carry = out;
+        _efl_ui_exact_model_block_store(compressed, l, block, buffer);
+
+     advance:
+        start = 0;
+        if (!next)
+          {
+             // What fell out of the last block is a size too
+             if (carry)
+               {
+                  memset(buffer, 0, sizeof (buffer));
+                  buffer[0] = carry;
+                  _efl_ui_exact_model_block_store(compressed, NULL, block + 1, buffer);
+               }
+             break;
+          }
+        l = next;
+     }
+}
+
+// A row is removed at index: every size after it is one row closer. What the
+// size of the row was is returned.
+static unsigned int
+_efl_ui_exact_model_list_remove(Eina_List *compressed, unsigned int index)
+{
+   unsigned int bufs[2][EFL_UI_EXACT_MODEL_CONTENT];
+   unsigned int block = index / EFL_UI_EXACT_MODEL_CONTENT;
+   unsigned int start = index % EFL_UI_EXACT_MODEL_CONTENT;
+   unsigned int removed;
+   unsigned int cur = 0;
+   Eina_List *l;
+
+   l = eina_list_nth_list(compressed, block);
+   // Never written, so never measured
+   if (!l) return 0;
+
+   _efl_ui_exact_model_block_load(l, bufs[cur]);
+   removed = bufs[cur][start];
+   for (;; block++)
+     {
+        Eina_List *next = eina_list_next(l);
+        Eina_Bool unchanged;
+
+        _efl_ui_exact_model_block_load(next, bufs[!cur]);
+        unchanged = (block > (index / EFL_UI_EXACT_MODEL_CONTENT)) &&
+          (eina_list_data_get(l) == _zero_block) &&
+          (!next || (eina_list_data_get(next) == _zero_block));
+        if (!unchanged)
+          {
+             memmove(bufs[cur] + start, bufs[cur] + start + 1,
+                     (EFL_UI_EXACT_MODEL_CONTENT - 1 - start) * sizeof (unsigned int));
+             bufs[cur][EFL_UI_EXACT_MODEL_CONTENT - 1] = bufs[!cur][0];
+             _efl_ui_exact_model_block_store(&compressed, l, block, bufs[cur]);
+          }
+        start = 0;
+        if (!next) break;
+        l = next;
+        cur = !cur;
+     }
+   return removed;
+}
+
+// The sizes follow their rows when one comes or goes. The sizes are kept by
+// index, and the rows after the one that came or went have another one now.
+static void
+_efl_ui_exact_model_shift(Efl_Ui_Exact_Model_Data *root, unsigned int index, Eina_Bool added)
+{
+   unsigned char i;
+
+   // What is kept expanded in the slots has to be in the list to be moved
+   for (i = 0; i < EINA_C_ARRAY_LENGTH(root->slot); i++)
+     {
+        _efl_ui_exact_model_slot_commit(root, i);
+        root->slot[i].defined = EINA_FALSE;
+        root->slot[i].usage = 0;
+     }
+
+   if (added)
+     {
+        _efl_ui_exact_model_list_insert(&root->compressed.width, index);
+        _efl_ui_exact_model_list_insert(&root->compressed.height, index);
+     }
+   else
+     {
+        unsigned int h;
+
+        _efl_ui_exact_model_list_remove(root->compressed.width, index);
+        h = _efl_ui_exact_model_list_remove(root->compressed.height, index);
+        if (h & EFL_UI_EXACT_MODEL_MEASURED)
+          root->total_size.height -= h & ~EFL_UI_EXACT_MODEL_MEASURED;
+     }
+}
+
+static void
+_efl_ui_exact_model_child_added(void *data, const Efl_Event *event)
+{
+   const Efl_Model_Children_Event *ev = event->info;
+
+   _efl_ui_exact_model_shift(data, ev->index, EINA_TRUE);
+}
+
+static void
+_efl_ui_exact_model_child_removed(void *data, const Efl_Event *event)
+{
+   const Efl_Model_Children_Event *ev = event->info;
+
+   _efl_ui_exact_model_shift(data, ev->index, EINA_FALSE);
+}
+
 static Eina_Future *
 _efl_ui_exact_model_efl_model_property_set(Eo *obj, Efl_Ui_Exact_Model_Data *pd,
                                            const char *property, Eina_Value *value)
diff --git a/src/tests/elementary/efl_ui_model.c b/src/tests/elementary/efl_ui_model.c
index a9ce1a874b..24d01d85ec 100644
--- a/src/tests/elementary/efl_ui_model.c
+++ b/src/tests/elementary/efl_ui_model.c
@@ -435,6 +435,127 @@ EFL_START_TEST(efl_ui_exact_model_many_rows)
 }
 EFL_END_TEST
 
+// The property_set takes a copy: the value given is the caller's to free.
+static void
+_uint_set(Efl_Model *model, const char *property, unsigned int value)
+{
+   Eina_Value *v = eina_value_uint_new(value);
+
+   efl_future_then(model, efl_model_property_set(model, property, v));
+   eina_value_free(v);
+}
+
+static void
+_row_keep(void *data, const Efl_Event *ev)
+{
+   Efl_Model_Children_Event *cev = ev->info;
+   Eo **kept = data;
+
+   *kept = efl_ref(cev->child);
+}
+
+// Rows of every kind of size: those of the original index i keep _many_height(i).
+static void
+_many_heights_check(Many_Rows *many, unsigned int skip, unsigned int skip2)
+{
+   unsigned int i;
+
+   for (i = 0; i < MANY_ROWS; i++)
+     {
+        if (i == skip || i == skip2) continue;
+        ck_assert_uint_eq(_property_uint_expected(many->rows[i], "self.height"), _many_height(i));
+        ck_assert_uint_eq(_property_uint_expected(many->rows[i], "self.width"),
+                          1000 + _many_height(i));
+     }
+}
+
+// A row inserted in front of the others, or removed from the front or from
+// the middle, moves the rows after it: each keeps the size it was told to be,
+// whichever block it was in and goes to, and the row that came in has none.
+EFL_START_TEST(efl_ui_exact_model_insert_remove)
+{
+   Efl_Generic_Model *base_model;
+   Efl_Model *model;
+   Efl_Model_Children_Event ev = { 0 };
+   Many_Rows *many = calloc(1, sizeof (Many_Rows));
+   Eo *row, *added = NULL;
+   unsigned int i, loops, sum = 0;
+
+   base_model = efl_add_ref(EFL_GENERIC_MODEL_CLASS, efl_main_loop_get());
+   for (i = 0; i < MANY_ROWS; i++)
+     {
+        efl_model_child_add(base_model);
+     }
+   model = efl_add_ref(EFL_UI_EXACT_MODEL_CLASS, efl_main_loop_get(),
+                       efl_ui_view_model_set(efl_added, base_model));
+   ck_assert(!!model);
+
+   eina_future_then(efl_model_children_slice_get(model, 0, MANY_ROWS), _many_rows_then, many, NULL);
+   for (loops = 0; (loops < 50) && many->count < MANY_ROWS; loops++)
+     {
+        ecore_main_loop_iterate();
+     }
+   ck_assert_uint_eq(many->count, MANY_ROWS);
+
+   for (i = 0; i < MANY_ROWS; i++)
+     {
+        _uint_set(many->rows[i], "self.height", _many_height(i));
+        _uint_set(many->rows[i], "self.width", 1000 + _many_height(i));
+        sum += _many_height(i);
+     }
+   _many_heights_check(many, MANY_ROWS, MANY_ROWS);
+
+   // In front of the others, as a model that can insert announces it
+   row = efl_add(EFL_GENERIC_MODEL_CLASS, base_model);
+   ev.index = 0;
+   ev.child = row;
+   efl_event_callback_add(model, EFL_MODEL_EVENT_CHILD_ADDED, _row_keep, &added);
+   efl_event_callback_call(base_model, EFL_MODEL_EVENT_CHILD_ADDED, &ev);
+   efl_event_callback_del(model, EFL_MODEL_EVENT_CHILD_ADDED, _row_keep, &added);
+   ck_assert_ptr_ne(added, NULL);
+   ck_assert_uint_eq(efl_composite_model_index_get(added), 0);
+   ck_assert_int_eq(_property_error_expected(added, "self.height"), EAGAIN);
+   ck_assert_int_eq(_property_error_expected(added, "self.width"), EAGAIN);
+   _many_heights_check(many, MANY_ROWS, MANY_ROWS);
+   ck_assert_uint_eq(_property_uint_expected(model, "total.height"), sum);
+
+   // The row that came in is measured on its own
+   _uint_set(added, "self.height", 5);
+   ck_assert_uint_eq(_property_uint_expected(added, "self.height"), 5);
+   _many_heights_check(many, MANY_ROWS, MANY_ROWS);
+
+   // And leaves again, from the front
+   ev.child = NULL;
+   efl_event_callback_call(base_model, EFL_MODEL_EVENT_CHILD_REMOVED, &ev);
+   _many_heights_check(many, MANY_ROWS, MANY_ROWS);
+   ck_assert_uint_eq(_property_uint_expected(model, "total.height"), sum);
+
+   // A row of the second block leaves: those after are one closer
+   ck_assert_uint_eq(efl_composite_model_index_get(many->rows[1030]), 1030);
+   ev.index = 1030;
+   efl_event_callback_call(base_model, EFL_MODEL_EVENT_CHILD_REMOVED, &ev);
+   _many_heights_check(many, 1030, MANY_ROWS);
+   ck_assert_uint_eq(efl_composite_model_index_get(many->rows[1031]), 1030);
+   ck_assert_uint_eq(_property_uint_expected(model, "total.height"), sum - _many_height(1030));
+
+   // The first row leaves
+   ev.index = 0;
+   efl_event_callback_call(base_model, EFL_MODEL_EVENT_CHILD_REMOVED, &ev);
+   _many_heights_check(many, 1030, 0);
+   ck_assert_uint_eq(_property_uint_expected(model, "total.height"),
+                     sum - _many_height(1030) - _many_height(0));
+
+   efl_unref(added);
+   for (i = 0; i < MANY_ROWS; i++)
+     {
+        efl_unref(many->rows[i]);
+     }
+   free(many);
+   efl_unref(model);
+   efl_unref(base_model);
+}
+EFL_END_TEST
+
 // An Exact_Model that held sizes in several blocks is let go of: what it kept
 // compressed, the buffers of its slots and the block of zeros it fills the gaps
 // with are its to free (there is nothing to check here but valgrind). The last
@@ -495,5 +616,6 @@ efl_ui_model(TCase *tc)
    tcase_add_test(tc, efl_ui_average_model_test);
    tcase_add_test(tc, efl_ui_exact_model_test);
    tcase_add_test(tc, efl_ui_exact_model_many_rows);
+   tcase_add_test(tc, efl_ui_exact_model_insert_remove);
    tcase_add_test(tc, efl_ui_exact_model_destroyed);
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to