Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
fix: the dropdown menu should be destroyed along with the target
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Mar 1, 2020
1 parent 3d7a2ec commit 2d5a89b
Showing 1 changed file with 64 additions and 24 deletions.
88 changes: 64 additions & 24 deletions src/ui/components/dropdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ static struct DropdwonModule {
void Dropdown_UpdatePosition(LCUI_Widget w)
{
float x, y;
LCDesign_Dropdown data = Widget_GetData(w, self.menu);
LCDesign_Dropdown data;

data = Widget_GetData(w, self.menu);
if (!data->target) {
Widget_Move(w, 0, 0);
return;
Expand Down Expand Up @@ -114,7 +116,9 @@ void Dropdown_UpdatePosition(LCUI_Widget w)

void Dropdown_Hide(LCUI_Widget w)
{
LCDesign_Dropdown data = Widget_GetData(w, self.menu);
LCDesign_Dropdown data;

data = Widget_GetData(w, self.menu);
if (data->target) {
Widget_RemoveClass(data->target, "active");
}
Expand All @@ -126,6 +130,7 @@ void Dropdown_Show(LCUI_Widget w)
{
LCUI_Widget root = LCUIWidget_GetRoot();
LCDesign_Dropdown data = Widget_GetData(w, self.menu);

if (data->target) {
Widget_AddClass(data->target, "active");
}
Expand All @@ -145,11 +150,13 @@ void Dropdown_Toggle(LCUI_Widget w)
}
}

static void Dropdown_OnClickOutside(LCUI_Widget w,
LCUI_WidgetEvent e, void *arg)
static void Dropdown_OnClickOutside(LCUI_Widget w, LCUI_WidgetEvent e,
void *arg)
{
LCUI_Widget target;
LCDesign_Dropdown dropdown = Widget_GetData(e->data, self.menu);
LCDesign_Dropdown dropdown;

dropdown = Widget_GetData(e->data, self.menu);
for (target = e->target; target; target = target->parent) {
if (target == dropdown->target) {
return;
Expand Down Expand Up @@ -179,24 +186,17 @@ static void Dropdown_OnClick(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)

static void Dropdown_Init(LCUI_Widget w)
{
const size_t size = sizeof(LCDesign_DropdownRec);
LCDesign_Dropdown data = Widget_AddData(w, self.menu, size);
LCDesign_Dropdown data;

data = Widget_AddData(w, self.menu, sizeof(LCDesign_DropdownRec));
data->target = NULL;
data->position = SV_BOTTOM_LEFT;
data->handler_id = Widget_BindEvent(LCUIWidget_GetRoot(), "click",
Dropdown_OnClickOutside,
w, NULL);
Dropdown_OnClickOutside, w, NULL);
Widget_BindEvent(w, "click", Dropdown_OnClick, NULL, NULL);
Widget_AddClass(w, "dropdown-menu");
}

static void Dropdown_Destroy(LCUI_Widget w)
{
LCDesign_Dropdown data = Widget_GetData(w, self.menu);
Widget_UnbindEventByHandlerId(LCUIWidget_GetRoot(), data->handler_id);
}

static void DropdownTarget_OnClick(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
{
if (w->disabled) {
Expand All @@ -206,29 +206,69 @@ static void DropdownTarget_OnClick(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
e->cancel_bubble = TRUE;
}

void Dropdown_BindTarget(LCUI_Widget w, LCUI_Widget target)
static void DropdownTarget_OnDestroy(LCUI_Widget w, LCUI_WidgetEvent e,
void *arg)
{
LCDesign_Dropdown data = Widget_GetData(w, self.menu);
data->target = target;
LCDesign_Dropdown data;

data = Widget_GetData(e->data, self.menu);
data->target = NULL;
Widget_Destroy(e->data);
}

static void Dropdown_UnbindTarget(LCUI_Widget w)
{
LCDesign_Dropdown data;

data = Widget_GetData(w, self.menu);
if (data->target) {
Widget_UnbindEvent(target, "click", DropdownTarget_OnClick);
Widget_UnbindEvent(data->target, "click",
DropdownTarget_OnClick);
Widget_UnbindEvent(data->target, "destroy",
DropdownTarget_OnDestroy);
}
data->target = NULL;
}

static void Dropdown_Destroy(LCUI_Widget w)
{
LCDesign_Dropdown data;

Dropdown_UnbindTarget(w);
data = Widget_GetData(w, self.menu);
Widget_UnbindEventByHandlerId(LCUIWidget_GetRoot(), data->handler_id);
}

void Dropdown_BindTarget(LCUI_Widget w, LCUI_Widget target)
{
LCDesign_Dropdown data;

data = Widget_GetData(w, self.menu);
Dropdown_UnbindTarget(w);
if (target) {
Widget_BindEvent(target, "click", DropdownTarget_OnClick, w,
NULL);
Widget_BindEvent(target, "destroy", DropdownTarget_OnDestroy, w,
NULL);
}
Widget_BindEvent(target, "click", DropdownTarget_OnClick, w, NULL);
data->target = target;
}

void Dropdown_SetPosition(LCUI_Widget w, LCUI_StyleValue position)
{
LCDesign_Dropdown data = Widget_GetData(w, self.menu);
LCDesign_Dropdown data;

data = Widget_GetData(w, self.menu);
data->position = position;
Dropdown_UpdatePosition(w);
}

static void Dropdown_SetAttr(LCUI_Widget w, const char *name,
const char *value)
static void Dropdown_SetAttr(LCUI_Widget w, const char *name, const char *value)
{
LCDesign_Dropdown menu;

if (strcmp(name, "data-position") == 0) {
LCDesign_Dropdown menu = Widget_GetData(w, self.menu);
menu = Widget_GetData(w, self.menu);
int position = LCUI_GetStyleValue(value);
if (position <= 0) {
position = SV_BOTTOM_LEFT;
Expand Down

0 comments on commit 2d5a89b

Please sign in to comment.