From 33c6ec90f3be4b535a385576e3302f5bc807df8f Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Fri, 10 May 2024 05:54:01 +0200 Subject: [PATCH] fix(event): stop event event processing when requested (#6113) --- src/core/lv_obj_event.c | 8 ++-- tests/src/test_cases/test_event.c | 79 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/core/lv_obj_event.c b/src/core/lv_obj_event.c index 1977466c3..82b83883d 100644 --- a/src/core/lv_obj_event.c +++ b/src/core/lv_obj_event.c @@ -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; diff --git a/tests/src/test_cases/test_event.c b/tests/src/test_cases/test_event.c index f827f5686..faea2fbe4 100644 --- a/tests/src/test_cases/test_event.c +++ b/tests/src/test_cases/test_event.c @@ -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