evisum/src/bin/ui/ui_cache.c

110 lines
2.2 KiB
C
Raw Normal View History

#include "ui_cache.h"
Evisum_Ui_Cache *
evisum_ui_item_cache_new(Evas_Object *parent,
Evas_Object *(create_cb)(Evas_Object *), int size)
{
Evisum_Ui_Cache *cache = malloc(sizeof(Evisum_Ui_Cache));
if (!cache) return NULL;
cache->parent = parent;
cache->item_create_cb = create_cb;
cache->items = NULL;
2021-01-01 09:33:16 -08:00
cache->time = time(NULL);
for (int i = 0; i < size; i++)
{
Item_Cache *it = calloc(1, sizeof(Item_Cache));
if (it)
{
it->obj = cache->item_create_cb(parent);
cache->items = eina_list_append(cache->items, it);
}
}
return cache;
}
Item_Cache *
evisum_ui_item_cache_item_get(Evisum_Ui_Cache *cache)
{
Eina_List *l;
Item_Cache *it;
EINA_LIST_FOREACH(cache->items, l, it)
{
if (it->used == 0)
{
it->used = 1;
return it;
}
}
it = calloc(1, sizeof(Item_Cache));
if (it)
{
it->obj = cache->item_create_cb(cache->parent);
it->used = 1;
cache->items = eina_list_append(cache->items, it);
}
return it;
}
2021-01-01 09:33:16 -08:00
static int
_clean_cb(const void *p1, const void *p2)
{
Item_Cache *c1, *c2;
c1 = (Item_Cache *) p1;
c2 = (Item_Cache *) p1;
if (c1->used == 0 || c2->used == 0)
return -1;
return 1;
}
Eina_Bool
evisum_ui_item_cache_item_release(Evisum_Ui_Cache *cache, Evas_Object *obj)
{
Item_Cache *it;
Eina_List *l;
Eina_Bool released = EINA_FALSE;
2021-01-01 09:33:16 -08:00
time_t now = time(NULL);
if ((now - cache->time) > 60)
{
cache->items = eina_list_sort(cache->items,
eina_list_count(cache->items),
_clean_cb);
cache->time = now;
}
EINA_LIST_FOREACH(cache->items, l, it)
{
if (it->obj == obj)
{
it->used = 0;
evas_object_hide(it->obj);
released = EINA_TRUE;
break;
}
}
return released;
}
void
evisum_ui_item_cache_free(Evisum_Ui_Cache *cache)
{
Item_Cache *it;
evas_object_del(cache->parent);
EINA_LIST_FREE(cache->items, it)
{
free(it);
}
free(cache);
}