fix(event): stop event event processing when requested (#6113)

This commit is contained in:
Gabor Kiss-Vamosi 2024-05-10 05:54:01 +02:00 committed by GitHub
parent 29d8a4d509
commit 33c6ec90f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 4 deletions

View File

@ -356,19 +356,19 @@ static lv_result_t event_send_core(lv_event_t * e)
lv_event_list_t * list = target->spec_attr ? &target->spec_attr->event_list : NULL;
res = lv_event_send(list, e, true);
if(res != LV_RESULT_OK) return res;
if(res != LV_RESULT_OK || e->stop_processing) return res;
res = lv_obj_event_base(NULL, e);
if(res != LV_RESULT_OK) return res;
if(res != LV_RESULT_OK || e->stop_processing) return res;
res = lv_event_send(list, e, false);
if(res != LV_RESULT_OK) return res;
if(res != LV_RESULT_OK || e->stop_processing) return res;
lv_obj_t * parent = lv_obj_get_parent(e->current_target);
if(parent && event_is_bubbled(e)) {
e->current_target = parent;
res = event_send_core(e);
if(res != LV_RESULT_OK) return res;
if(res != LV_RESULT_OK || e->stop_processing || e->stop_bubbling) return res;
}
return res;

View File

@ -2,6 +2,7 @@
#include "../lvgl.h"
#include "unity/unity.h"
#include "lv_test_indev.h"
static void event_object_deletion_cb(const lv_obj_class_t * cls, lv_event_t * e)
{
@ -42,4 +43,82 @@ void test_event_should_not_memory_lean(void)
TEST_ASSERT_LESS_OR_EQUAL_CHAR(initial_free_size, m2.free_size);
}
static uint32_t pre_cnt_1;
static uint32_t pre_cnt_2;
static uint32_t post_cnt_1;
static uint32_t post_cnt_2;
static bool pre_stop_1;
static bool post_stop_1;
static void event_pre_1_cb(lv_event_t * e)
{
pre_cnt_1++;
if(pre_stop_1) lv_event_stop_processing(e);
}
static void event_pre_2_cb(lv_event_t * e)
{
LV_UNUSED(e);
pre_cnt_2++;
}
static void event_post_1_cb(lv_event_t * e)
{
post_cnt_1++;
if(post_stop_1) lv_event_stop_processing(e);
}
static void event_post_2_cb(lv_event_t * e)
{
LV_UNUSED(e);
post_cnt_2++;
}
/* Add and then remove event should not memory leak */
void test_event_stop_processing(void)
{
lv_obj_t * btn = lv_button_create(lv_screen_active());
lv_obj_set_size(btn, 200, 100);
lv_obj_add_event_cb(btn, event_pre_1_cb, LV_EVENT_CLICKED | LV_EVENT_PREPROCESS, NULL);
lv_obj_add_event_cb(btn, event_pre_2_cb, LV_EVENT_CLICKED | LV_EVENT_PREPROCESS, NULL);
lv_obj_add_event_cb(btn, event_post_1_cb, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(btn, event_post_2_cb, LV_EVENT_CLICKED, NULL);
pre_cnt_1 = 0;
pre_cnt_2 = 0;
post_cnt_1 = 0;
post_cnt_2 = 0;
pre_stop_1 = false;
post_stop_1 = false;
lv_test_mouse_click_at(30, 30);
TEST_ASSERT_EQUAL(pre_cnt_1, 1);
TEST_ASSERT_EQUAL(pre_cnt_2, 1);
TEST_ASSERT_EQUAL(post_cnt_1, 1);
TEST_ASSERT_EQUAL(post_cnt_2, 1);
pre_cnt_1 = 0;
pre_cnt_2 = 0;
post_cnt_1 = 0;
post_cnt_2 = 0;
pre_stop_1 = true;
post_stop_1 = false;
lv_test_mouse_click_at(30, 30);
TEST_ASSERT_EQUAL(pre_cnt_1, 1);
TEST_ASSERT_EQUAL(pre_cnt_2, 0);
TEST_ASSERT_EQUAL(post_cnt_1, 0);
TEST_ASSERT_EQUAL(post_cnt_2, 0);
pre_cnt_1 = 0;
pre_cnt_2 = 0;
post_cnt_1 = 0;
post_cnt_2 = 0;
pre_stop_1 = false;
post_stop_1 = true;
lv_test_mouse_click_at(30, 30);
TEST_ASSERT_EQUAL(pre_cnt_1, 1);
TEST_ASSERT_EQUAL(pre_cnt_2, 1);
TEST_ASSERT_EQUAL(post_cnt_1, 1);
TEST_ASSERT_EQUAL(post_cnt_2, 0);
}
#endif