support register new event name

This commit is contained in:
lixianjing 2024-10-17 17:52:32 +08:00
parent 4e0e447d49
commit 8682a3f4ea
4 changed files with 82 additions and 3 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2024/10/17
* 增加给给事件注册名称的功能(感谢兆坤提供补丁)
2024/10/15
* 修复拼写错误(感谢兆坤提供补丁)
* 完善csv (感谢兆坤提供补丁)

View File

@ -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) {

View File

@ -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"

View File

@ -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);
}