feat(label): add property (#6575)

Signed-off-by: Neo Xu <neo.xu1990@gmail.com>
This commit is contained in:
Neo Xu
2024-08-01 15:10:49 +08:00
committed by GitHub
parent 49ef8de5d7
commit 7380149b07
6 changed files with 109 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ enum {
/*Define the property ID for every widget here. */
LV_PROPERTY_OBJ_START = 0x0100, /* lv_obj.c */
LV_PROPERTY_IMAGE_START = 0x0200, /* lv_image.c */
LV_PROPERTY_LABEL_START = 0x0300, /* lv_label.c */
/*Special ID, use it to extend ID and make sure it's unique and compile time determinant*/
LV_PROPERTY_ID_BUILTIN_LAST = 0xffff, /*ID of 0x10000 ~ 0xfffffff is reserved for user*/

View File

@@ -60,6 +60,31 @@ static void calculate_x_coordinate(int32_t * x, const lv_text_align_t align, con
/**********************
* STATIC VARIABLES
**********************/
#if LV_USE_OBJ_PROPERTY
static const lv_property_ops_t properties[] = {
{
.id = LV_PROPERTY_LABEL_TEXT,
.setter = lv_label_set_text,
.getter = lv_label_get_text,
},
{
.id = LV_PROPERTY_LABEL_LONG_MODE,
.setter = lv_label_set_long_mode,
.getter = lv_label_get_long_mode,
},
{
.id = LV_PROPERTY_LABEL_TEXT_SELECTION_START,
.setter = lv_label_set_text_selection_start,
.getter = lv_label_get_text_selection_start,
},
{
.id = LV_PROPERTY_LABEL_TEXT_SELECTION_END,
.setter = lv_label_set_text_selection_end,
.getter = lv_label_get_text_selection_end,
},
};
#endif
const lv_obj_class_t lv_label_class = {
.constructor_cb = lv_label_constructor,
.destructor_cb = lv_label_destructor,
@@ -69,6 +94,18 @@ const lv_obj_class_t lv_label_class = {
.instance_size = sizeof(lv_label_t),
.base_class = &lv_obj_class,
.name = "label",
#if LV_USE_OBJ_PROPERTY
.prop_index_start = LV_PROPERTY_LABEL_START,
.prop_index_end = LV_PROPERTY_LABEL_END,
.properties = properties,
.properties_count = sizeof(properties) / sizeof(properties[0]),
#if LV_USE_OBJ_PROPERTY_NAME
.property_names = lv_label_property_names,
.names_count = sizeof(lv_label_property_names) / sizeof(lv_property_name_t),
#endif
#endif
};
/**********************

View File

@@ -59,6 +59,16 @@ typedef _lv_label_long_mode_t lv_label_long_mode_t;
typedef uint8_t lv_label_long_mode_t;
#endif /*DOXYGEN*/
#if LV_USE_OBJ_PROPERTY
enum {
LV_PROPERTY_ID(LABEL, TEXT, LV_PROPERTY_TYPE_TEXT, 0),
LV_PROPERTY_ID(LABEL, LONG_MODE, LV_PROPERTY_TYPE_INT, 1),
LV_PROPERTY_ID(LABEL, TEXT_SELECTION_START, LV_PROPERTY_TYPE_INT, 2),
LV_PROPERTY_ID(LABEL, TEXT_SELECTION_END, LV_PROPERTY_TYPE_INT, 3),
LV_PROPERTY_LABEL_END,
};
#endif
typedef struct {
lv_obj_t obj;
char * text;

View File

@@ -0,0 +1,26 @@
/**
* GENERATED FILE, DO NOT EDIT IT!
* @file lv_label_properties.c
*/
#include "../label/lv_label.h"
#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME
#if LV_USE_LABEL
/**
* Label widget property names, name must be in order.
* Generated code from properties.py
*/
/* *INDENT-OFF* */
const lv_property_name_t lv_label_property_names[4] = {
{"long_mode", LV_PROPERTY_LABEL_LONG_MODE,},
{"text", LV_PROPERTY_LABEL_TEXT,},
{"text_selection_end", LV_PROPERTY_LABEL_TEXT_SELECTION_END,},
{"text_selection_start", LV_PROPERTY_LABEL_TEXT_SELECTION_START,},
};
#endif /*LV_USE_LABEL*/
/* *INDENT-ON* */
#endif

View File

@@ -11,6 +11,7 @@
#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME
extern const lv_property_name_t lv_image_property_names[11];
extern const lv_property_name_t lv_label_property_names[4];
extern const lv_property_name_t lv_obj_property_names[73];
extern const lv_property_name_t lv_style_property_names[111];
#endif

View File

@@ -271,4 +271,38 @@ void test_obj_property_name(void)
#endif
}
void test_label_properties(void)
{
#if LV_USE_OBJ_PROPERTY
lv_obj_t * obj = lv_label_create(lv_screen_active());
lv_property_t prop = { };
prop.id = LV_PROPERTY_LABEL_TEXT;
prop.ptr = "Hello world";
TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK);
TEST_ASSERT_EQUAL_STRING("Hello world", lv_label_get_text(obj));
TEST_ASSERT_EQUAL_STRING("Hello world", lv_obj_get_property(obj, LV_PROPERTY_LABEL_TEXT).ptr);
prop.id = LV_PROPERTY_LABEL_LONG_MODE;
prop.num = LV_LABEL_LONG_SCROLL;
TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK);
TEST_ASSERT_EQUAL_INT(LV_LABEL_LONG_SCROLL, lv_label_get_long_mode(obj));
TEST_ASSERT_EQUAL_INT(LV_LABEL_LONG_SCROLL, lv_obj_get_property(obj, LV_PROPERTY_LABEL_LONG_MODE).num);
#if LV_LABEL_TEXT_SELECTION
prop.id = LV_PROPERTY_LABEL_TEXT_SELECTION_START;
prop.num = 2;
TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK);
TEST_ASSERT_EQUAL_INT(2, lv_label_get_text_selection_start(obj));
TEST_ASSERT_EQUAL_INT(2, lv_obj_get_property(obj, LV_PROPERTY_LABEL_TEXT_SELECTION_START).num);
prop.id = LV_PROPERTY_LABEL_TEXT_SELECTION_END;
prop.num = 5;
TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK);
TEST_ASSERT_EQUAL_INT(5, lv_label_get_text_selection_end(obj));
TEST_ASSERT_EQUAL_INT(5, lv_obj_get_property(obj, LV_PROPERTY_LABEL_TEXT_SELECTION_END).num);
#endif
#endif
}
#endif