Skip to content

Commit

Permalink
feat(widget-event): add "link" event, rename "remove" event to "unlink"
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Feb 20, 2018
1 parent 9bb9bb5 commit 8c1d105
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
8 changes: 4 additions & 4 deletions include/LCUI/gui/widget_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ LCUI_BEGIN_HEADER
/** 部件事件类型枚举 */
typedef enum LCUI_WidgetEventType {
LCUI_WEVENT_NONE,
LCUI_WEVENT_ADD,
LCUI_WEVENT_READY, /**< 在初次更新尺寸和位置后 */
LCUI_WEVENT_REMOVE, /**< 在被标记需要移除时 */
LCUI_WEVENT_DESTROY, /**< 在开始彻底销毁前 */
LCUI_WEVENT_LINK, /**< link widget node to the parent widget children list */
LCUI_WEVENT_UNLINK, /**< unlink widget node from the parent widget children list */
LCUI_WEVENT_READY, /**< after widget initial layout was completed */
LCUI_WEVENT_DESTROY, /**< before destroy */
LCUI_WEVENT_MOVE, /**< 在移动位置时 */
LCUI_WEVENT_RESIZE, /**< 改变尺寸 */
LCUI_WEVENT_SHOW, /**< 显示 */
Expand Down
8 changes: 4 additions & 4 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,22 +582,22 @@ static void OnSurfaceEvent( LCUI_Widget w, LCUI_WidgetEvent e, void *arg )
root = LCUIWidget_GetRoot();
surface = LCUIDisplay_GetBindSurface( e->target );
if( display.mode == LCDM_SEAMLESS ) {
if( !surface && event_type != LCUI_WEVENT_ADD ) {
if( !surface && event_type != LCUI_WEVENT_LINK ) {
return;
}
} else if ( e->target == root ) {
if( !surface && event_type != LCUI_WEVENT_ADD ) {
if( !surface && event_type != LCUI_WEVENT_LINK ) {
return;
}
} else {
return;
}
rect = &e->target->box.graph;
switch( event_type ) {
case LCUI_WEVENT_ADD:
case LCUI_WEVENT_LINK:
LCUIDisplay_BindSurface( e->target );
break;
case LCUI_WEVENT_REMOVE:
case LCUI_WEVENT_UNLINK:
case LCUI_WEVENT_DESTROY:
LCUIDisplay_UnbindSurface( e->target );
break;
Expand Down
20 changes: 16 additions & 4 deletions src/gui/widget_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static void Widget_UpdateStatus( LCUI_Widget widget )
int Widget_Unlink( LCUI_Widget widget )
{
LCUI_Widget child;
LCUI_WidgetEventRec ev = { 0 };
LinkedListNode *node, *snode;
if( !widget->parent ) {
return -1;
Expand Down Expand Up @@ -135,16 +136,20 @@ int Widget_Unlink( LCUI_Widget widget )
node = node->next;
}
node = &widget->node;
ev.cancel_bubble = TRUE;
ev.type = LCUI_WEVENT_UNLINK;
Widget_PostEvent( widget, &ev, NULL, NULL );
LinkedList_Unlink( &widget->parent->children, node );
LinkedList_Unlink( &widget->parent->children_show, snode );
Widget_PostSurfaceEvent( widget, LCUI_WEVENT_REMOVE, TRUE );
Widget_PostSurfaceEvent( widget, LCUI_WEVENT_UNLINK, TRUE );
widget->parent = NULL;
return 0;
}

int Widget_Append( LCUI_Widget parent, LCUI_Widget widget )
{
LCUI_Widget child;
LCUI_WidgetEventRec ev = { 0 };
LinkedListNode *node, *snode;
if( !parent || !widget ) {
return -1;
Expand All @@ -167,9 +172,12 @@ int Widget_Append( LCUI_Widget parent, LCUI_Widget widget )
child->index += 1;
node = node->next;
}
ev.cancel_bubble = TRUE;
ev.type = LCUI_WEVENT_LINK;
Widget_UpdateStyle( widget, TRUE );
Widget_UpdateChildrenStyle( widget, TRUE );
Widget_PostSurfaceEvent( widget, LCUI_WEVENT_ADD, TRUE );
Widget_PostEvent( widget, &ev, NULL, NULL );
Widget_PostSurfaceEvent( widget, LCUI_WEVENT_LINK, TRUE );
Widget_UpdateTaskStatus( widget );
Widget_UpdateStatus( widget );
Widget_UpdateLayout( parent );
Expand All @@ -179,6 +187,7 @@ int Widget_Append( LCUI_Widget parent, LCUI_Widget widget )
int Widget_Prepend( LCUI_Widget parent, LCUI_Widget widget )
{
LCUI_Widget child;
LCUI_WidgetEventRec ev = { 0 };
LinkedListNode *node, *snode;
if( !parent || !widget ) {
return -1;
Expand All @@ -202,7 +211,10 @@ int Widget_Prepend( LCUI_Widget parent, LCUI_Widget widget )
child->index += 1;
node = node->next;
}
Widget_PostSurfaceEvent( widget, LCUI_WEVENT_ADD, TRUE );
ev.cancel_bubble = TRUE;
ev.type = LCUI_WEVENT_LINK;
Widget_PostEvent( widget, &ev, NULL, NULL );
Widget_PostSurfaceEvent( widget, LCUI_WEVENT_LINK, TRUE );
Widget_AddTaskForChildren( widget, LCUI_WTASK_REFRESH_STYLE );
Widget_UpdateTaskStatus( widget );
Widget_UpdateStatus( widget );
Expand Down Expand Up @@ -352,7 +364,7 @@ void Widget_Destroy( LCUI_Widget w )
}
if( root != LCUIWidget.root ) {
LCUI_WidgetEventRec e = { 0 };
e.type = LCUI_WEVENT_REMOVE;
e.type = LCUI_WEVENT_UNLINK;
w->state = LCUI_WSTATE_DELETED;
Widget_TriggerEvent( w, &e, NULL );
Widget_ExecDestroy( w );
Expand Down
6 changes: 4 additions & 2 deletions src/gui/widget_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ static LCUI_Widget Widget_GetNextAt( LCUI_Widget widget, int x, int y )
return NULL;
}

static int Widget_TriggerEventEx( LCUI_Widget widget, LCUI_WidgetEventPack pack )
static int Widget_TriggerEventEx( LCUI_Widget widget,
LCUI_WidgetEventPack pack )
{
LCUI_WidgetEvent e = &pack->event;
pack->widget = widget;
Expand Down Expand Up @@ -1065,8 +1066,9 @@ void LCUIWidget_InitEvent(void)
int id;
const char *name;
} mappings[] = {
{ LCUI_WEVENT_LINK, "link" },
{ LCUI_WEVENT_UNLINK, "unlink" },
{ LCUI_WEVENT_READY, "ready" },
{ LCUI_WEVENT_REMOVE, "remove" },
{ LCUI_WEVENT_DESTROY, "destroy" },
{ LCUI_WEVENT_MOUSEDOWN, "mousedown" },
{ LCUI_WEVENT_MOUSEUP, "mouseup" },
Expand Down
4 changes: 2 additions & 2 deletions src/gui/widget_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void LCUIWidget_FreeTasks( void )
void Widget_AddToTrash( LCUI_Widget w )
{
LCUI_WidgetEventRec e = { 0 };
e.type = LCUI_WEVENT_REMOVE;
e.type = LCUI_WEVENT_UNLINK;
w->state = LCUI_WSTATE_DELETED;
Widget_TriggerEvent( w, &e, NULL );
if( !w->parent ) {
Expand All @@ -174,7 +174,7 @@ void Widget_AddToTrash( LCUI_Widget w )
LinkedList_Unlink( &w->parent->children, &w->node );
LinkedList_Unlink( &w->parent->children_show, &w->node_show );
LinkedList_AppendNode( &self.trash, &w->node );
Widget_PostSurfaceEvent( w, LCUI_WEVENT_REMOVE, TRUE );
Widget_PostSurfaceEvent( w, LCUI_WEVENT_UNLINK, TRUE );
}

int Widget_Update( LCUI_Widget w )
Expand Down

0 comments on commit 8c1d105

Please sign in to comment.