Skip to content

Commit fee31b0

Browse files
committed
feat(gui): auto assign an id to the event name
1 parent 2fa6cf3 commit fee31b0

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

include/LCUI/gui/widget_event.h

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ LCUI_API const char *LCUIWidget_GetEventName(int event_id);
119119
/** 获取与事件名称对应的标识号 */
120120
LCUI_API int LCUIWidget_GetEventId(const char *event_name);
121121

122+
LCUI_API void LCUI_InitWidgetEvent(LCUI_WidgetEvent e, const char *name);
123+
122124
/**
123125
* 添加部件事件绑定
124126
* @param[in] widget 目标部件

src/gui/widget_event.c

+31-17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <LCUI/cursor.h>
4242
#include <LCUI/thread.h>
4343

44+
/* clang-format off */
45+
4446
#define DBLCLICK_INTERVAL 500
4547

4648
typedef struct TouchCapturerRec_ {
@@ -92,15 +94,17 @@ static struct LCUIWidgetEvnetModule {
9294
LinkedList touch_capturers; /**< 触点占用记录 */
9395
LCUI_Widget targets[WST_TOTAL]; /**< 相关的部件 */
9496
LinkedList events; /**< 已绑定的事件 */
95-
LinkedList event_mappings; /**< 事件标识号和名称映射记录列表 */
96-
RBTree event_records; /**< 当前正执行的事件的记录 */
97-
RBTree event_names; /**< 事件名称表,以标识号作为索引 */
98-
Dict *event_ids; /**< 事件标识号表,以事件名称作为索引 */
99-
int base_event_id; /**< 事件标识号计数器 */
100-
ClickRecord click; /**< 上次鼠标点击记录 */
101-
LCUI_Mutex mutex; /**< 互斥锁 */
97+
LinkedList event_mappings; /**< 事件标识号和名称映射记录列表 */
98+
RBTree event_records; /**< 当前正执行的事件的记录 */
99+
RBTree event_names; /**< 事件名称表,以标识号作为索引 */
100+
Dict *event_ids; /**< 事件标识号表,以事件名称作为索引 */
101+
int base_event_id; /**< 事件标识号计数器 */
102+
ClickRecord click; /**< 上次鼠标点击记录 */
103+
LCUI_Mutex mutex; /**< 互斥锁 */
102104
} self;
103105

106+
/* clang-format on */
107+
104108
static void DestroyEventMapping(void *data)
105109
{
106110
EventMapping mapping = data;
@@ -168,6 +172,23 @@ static void DestroyWidgetEventHandler(void *arg)
168172
free(handler);
169173
}
170174

175+
static int GetEventId(const char *event_name)
176+
{
177+
int id = LCUIWidget_GetEventId(event_name);
178+
if (id < 0) {
179+
id = LCUIWidget_AllocEventId();
180+
LCUIWidget_SetEventName(id, event_name);
181+
}
182+
return id;
183+
}
184+
185+
void LCUI_InitWidgetEvent(LCUI_WidgetEvent e, const char *name)
186+
{
187+
e->type = GetEventId(name);
188+
e->cancel_bubble = FALSE;
189+
e->data = NULL;
190+
}
191+
171192
/**
172193
* 添加事件记录
173194
* 记录当前待处理的事件和目标部件,方便在部件被销毁时清除待处理的事件
@@ -422,11 +443,8 @@ int Widget_BindEvent(LCUI_Widget widget, const char *event_name,
422443
LCUI_WidgetEventFunc func, void *data,
423444
void(*destroy_data)(void *))
424445
{
425-
int id = LCUIWidget_GetEventId(event_name);
426-
if (id < 0) {
427-
return -1;
428-
}
429-
return Widget_BindEventById(widget, id, func, data, destroy_data);
446+
return Widget_BindEventById(widget, GetEventId(event_name),
447+
func, data, destroy_data);
430448
}
431449

432450
static int CompareEventHandlerKey(void *key, void *func_data)
@@ -459,11 +477,7 @@ int Widget_UnbindEventByHandlerId(LCUI_Widget widget, int handler_id)
459477
int Widget_UnbindEvent(LCUI_Widget widget, const char *event_name,
460478
LCUI_WidgetEventFunc func)
461479
{
462-
int id = LCUIWidget_GetEventId(event_name);
463-
if (id < 0) {
464-
return -1;
465-
}
466-
return Widget_UnbindEventById(widget, id, func);
480+
return Widget_UnbindEventById(widget, GetEventId(event_name), func);
467481
}
468482

469483
static LCUI_Widget Widget_GetNextAt(LCUI_Widget widget, int x, int y)

0 commit comments

Comments
 (0)