add long_press_time prop for button

This commit is contained in:
lixianjing 2020-08-18 17:43:15 +08:00
parent ca8fbfb07c
commit 8d9ee24406
5 changed files with 53 additions and 2 deletions

View File

@ -2,6 +2,7 @@
* 2020/08/18
* 修复了一些潜在的内存泄露。
* button支持设置长按时间。
* 2020/08/17
* 增加文档《项目描述文件 project.json 介绍》(感谢朝泽提供补丁)

View File

@ -605,6 +605,12 @@ BEGIN_C_DECLS
*/
#define WIDGET_PROP_REPEAT "repeat"
/**
* @const WIDGET_PROP_LONG_PRESS_TIME
* (ms)
*/
#define WIDGET_PROP_LONG_PRESS_TIME "long_press_time"
/**
* @const WIDGET_PROP_ENABLE_LONG_PRESS
*

View File

@ -97,7 +97,8 @@ static ret_t button_on_event(widget_t* widget, event_t* e) {
if (button->repeat > 0) {
button->timer_id = timer_add(button_on_repeat, widget, button->repeat);
} else if (button->enable_long_press) {
button->timer_id = timer_add(button_on_long_press, widget, TK_LONG_PRESS_TIME);
assert(button->long_press_time > 0);
button->timer_id = timer_add(button_on_long_press, widget, button->long_press_time);
}
widget_grab(widget->parent, widget);
@ -145,6 +146,15 @@ ret_t button_set_repeat(widget_t* widget, int32_t repeat) {
return RET_OK;
}
ret_t button_set_long_press_time(widget_t* widget, uint32_t long_press_time) {
button_t* button = BUTTON(widget);
return_value_if_fail(button != NULL, RET_BAD_PARAMS);
button->long_press_time = long_press_time;
return RET_OK;
}
ret_t button_set_enable_long_press(widget_t* widget, bool_t enable_long_press) {
button_t* button = BUTTON(widget);
return_value_if_fail(button != NULL, RET_BAD_PARAMS);
@ -161,6 +171,9 @@ static ret_t button_get_prop(widget_t* widget, const char* name, value_t* v) {
if (tk_str_eq(name, WIDGET_PROP_REPEAT)) {
value_set_int(v, button->repeat);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_LONG_PRESS_TIME)) {
value_set_int(v, button->long_press_time);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_ENABLE_LONG_PRESS)) {
value_set_bool(v, button->enable_long_press);
return RET_OK;
@ -175,6 +188,9 @@ static ret_t button_get_prop_default_value(widget_t* widget, const char* name, v
if (tk_str_eq(name, WIDGET_PROP_REPEAT)) {
value_set_int(v, 0);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_LONG_PRESS_TIME)) {
value_set_int(v, TK_LONG_PRESS_TIME);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_ENABLE_LONG_PRESS)) {
value_set_bool(v, FALSE);
return RET_OK;
@ -188,6 +204,8 @@ static ret_t button_set_prop(widget_t* widget, const char* name, const value_t*
if (tk_str_eq(name, WIDGET_PROP_REPEAT)) {
return button_set_repeat(widget, value_int(v));
} else if (tk_str_eq(name, WIDGET_PROP_LONG_PRESS_TIME)) {
return button_set_long_press_time(widget, value_int(v));
} else if (tk_str_eq(name, WIDGET_PROP_ENABLE_LONG_PRESS)) {
return button_set_enable_long_press(widget, value_bool(v));
}
@ -199,7 +217,8 @@ static ret_t button_on_destroy(widget_t* widget) {
return button_remove_timer(widget);
}
static const char* const s_button_properties[] = {WIDGET_PROP_REPEAT, NULL};
static const char* const s_button_properties[] = {WIDGET_PROP_REPEAT, WIDGET_PROP_LONG_PRESS_TIME,
WIDGET_PROP_ENABLE_LONG_PRESS, NULL};
TK_DECL_VTABLE(button) = {.size = sizeof(button_t),
.type = WIDGET_TYPE_BUTTON,
@ -226,6 +245,7 @@ widget_t* button_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
button->pressed = FALSE;
button->enable_long_press = FALSE;
button->timer_id = TK_INVALID_ID;
button->long_press_time = TK_LONG_PRESS_TIME;
return widget;
}

View File

@ -98,6 +98,14 @@ typedef struct _button_t {
*/
bool_t enable_long_press;
/**
* @property {uint32_t} long_press_time
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
* (ms)
*
*/
uint32_t long_press_time;
/*private*/
int32_t timer_id;
int32_t repeat_nr;
@ -149,6 +157,17 @@ widget_t* button_cast(widget_t* widget);
*/
ret_t button_set_repeat(widget_t* widget, int32_t repeat);
/**
* @method button_set_long_press_time
*
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {uint32_t} long_press_time ()
*
* @return {ret_t} RET_OK表示成功
*/
ret_t button_set_long_press_time(widget_t* widget, uint32_t long_press_time);
/**
* @method button_set_enable_long_press
*

View File

@ -32,6 +32,11 @@ TEST(Button, basic) {
ASSERT_EQ(widget_set_prop(w, WIDGET_PROP_REPEAT, &v1), RET_OK);
ASSERT_EQ(widget_get_prop(w, WIDGET_PROP_REPEAT, &v2), RET_OK);
ASSERT_EQ(value_int(&v1), value_int(&v2));
value_set_int(&v1, 4000);
ASSERT_EQ(widget_set_prop(w, WIDGET_PROP_LONG_PRESS_TIME, &v1), RET_OK);
ASSERT_EQ(widget_get_prop(w, WIDGET_PROP_LONG_PRESS_TIME, &v2), RET_OK);
ASSERT_EQ(value_int(&v1), value_int(&v2));
value_set_bool(&v1, TRUE);
ASSERT_EQ(widget_set_prop_str(w, WIDGET_PROP_ENABLE_LONG_PRESS, "true"), RET_OK);