From 8682a3f4ea19d23bd15f04d63799510af541aab5 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Thu, 17 Oct 2024 17:52:32 +0800 Subject: [PATCH] support register new event name --- docs/changes.md | 3 +++ src/base/events.c | 43 ++++++++++++++++++++++++++++++++++++++++++- src/base/events.h | 26 ++++++++++++++++++++++++-- tests/events_test.cc | 13 +++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 8c6ce146f..a17593cc4 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,8 @@ # 最新动态 +2024/10/17 + * 增加给给事件注册名称的功能(感谢兆坤提供补丁) + 2024/10/15 * 修复拼写错误(感谢兆坤提供补丁) * 完善csv (感谢兆坤提供补丁) diff --git a/src/base/events.c b/src/base/events.c index fc725ba23..04b115c9e 100644 --- a/src/base/events.c +++ b/src/base/events.c @@ -23,6 +23,47 @@ #include "tkc/time_now.h" #include "base/lcd_orientation_helper.h" +#include "tkc/object_default.h" + +static tk_object_t* s_custom_event_names = NULL; + +ret_t event_register_custom_name(int32_t event_type, const char* name) { + return_value_if_fail(name != NULL, RET_BAD_PARAMS); + + if (s_custom_event_names == NULL) { + s_custom_event_names = object_default_create_ex(FALSE); + return_value_if_fail(s_custom_event_names != NULL, RET_OOM); + } + return_value_if_fail(!tk_object_has_prop(s_custom_event_names, name), RET_FAIL); + + return tk_object_set_prop_int32(s_custom_event_names, name, event_type); +} + +ret_t event_unregister_custom_name(const char* name) { + ret_t ret = RET_NOT_FOUND; + return_value_if_fail(name != NULL, RET_BAD_PARAMS); + + if (s_custom_event_names != NULL) { + ret = tk_object_remove_prop(s_custom_event_names, name); + if (RET_OK == ret) { + int32_t size = tk_object_get_prop_int(s_custom_event_names, TK_OBJECT_PROP_SIZE, 0); + if (0 == size) { + TK_OBJECT_UNREF(s_custom_event_names); + } + } + } + + return ret; +} + +static inline int32_t event_get_custom_name(const char* name) { + if (s_custom_event_names == NULL) { + return EVT_NONE; + } else { + return tk_object_get_prop_int32(s_custom_event_names, name, EVT_NONE); + } +} + wheel_event_t* wheel_event_cast(event_t* event) { return_value_if_fail(event != NULL, NULL); return_value_if_fail(event->type == EVT_WHEEL || event->type == EVT_WHEEL_BEFORE_CHILDREN, NULL); @@ -384,7 +425,7 @@ int32_t event_from_name(const char* name) { default: break; } - return EVT_NONE; + return event_get_custom_name(name); } widget_animator_event_t* widget_animator_event_cast(event_t* event) { diff --git a/src/base/events.h b/src/base/events.h index 61559fc08..3c3d6a1bf 100644 --- a/src/base/events.h +++ b/src/base/events.h @@ -1361,16 +1361,38 @@ event_t* ui_load_event_init(ui_load_event_t* event, void* target, widget_t* root * @annotation ["scriptable"] * 事件基类。 */ + /** * @method event_from_name - * 将事件名转换成事件的值。 + * 将事件名转换成事件的类型。 * @annotation ["scriptable", "static"] * @param {const char*} name 事件名。 * - * @return {int32_t} 返回事件的值。 + * @return {int32_t} 返回事件的类型。 */ int32_t event_from_name(const char* name); +/** + * @method event_register_custom_name + * 给事件注册名称。 + * @annotation ["scriptable", "static"] + * @param {int32_t} event_type 事件类型。 + * @param {const char*} name 事件名。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t event_register_custom_name(int32_t event_type, const char* name); + +/** + * @method event_unregister_custom_name + * 注销事件名称。 + * @annotation ["scriptable", "static"] + * @param {const char*} name 事件名。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t event_unregister_custom_name(const char* name); + #define STR_ON_EVENT_PREFIX "on:" #define STR_GLOBAL_EVENT_PREFIX "global" #define STR_GLOBAL_VARS_CHANGED "global_vars_changed" diff --git a/tests/events_test.cc b/tests/events_test.cc index a88c62e28..bc9509d26 100644 --- a/tests/events_test.cc +++ b/tests/events_test.cc @@ -55,3 +55,16 @@ TEST(Events, from_name) { ASSERT_EQ(event_from_name("key_down_before_children"), EVT_KEY_DOWN_BEFORE_CHILDREN); ASSERT_EQ(event_from_name("key_up_before_children"), EVT_KEY_UP_BEFORE_CHILDREN); } + +TEST(Events, from_custom_name) { + int32_t custom1_evt_type = EVT_USER_START + 1; + const char* custom1_evt_name = "custom1"; + + ASSERT_EQ(event_from_name(custom1_evt_name), EVT_NONE); + + ASSERT_EQ(event_register_custom_name(custom1_evt_type, custom1_evt_name), RET_OK); + ASSERT_EQ(event_from_name(custom1_evt_name), custom1_evt_type); + ASSERT_EQ(event_unregister_custom_name(custom1_evt_name), RET_OK); + + ASSERT_EQ(event_from_name(custom1_evt_name), EVT_NONE); +}