improve ui_builder

This commit is contained in:
lixianjing 2021-06-09 13:58:02 +08:00
parent 77b855b6cf
commit 9994c5bfef
8 changed files with 64 additions and 25 deletions

View File

@ -46,6 +46,7 @@ static ret_t refresh_in_timer(const timer_info_t* info) {
widget_t* preview_ui(const char* filename) {
str_t s;
uint32_t size = 0;
widget_t* root = NULL;
char name[TK_NAME_LEN + 1];
ui_builder_t* builder = NULL;
uint8_t* content = NULL;
@ -62,7 +63,7 @@ widget_t* preview_ui(const char* filename) {
}
filename_to_name(filename, name, TK_NAME_LEN);
builder = ui_builder_default(name);
builder = ui_builder_default_create(name);
printf("preview %s\n", filename);
return_value_if_fail(content != NULL, NULL);
ui_loader_load(loader, content, size, builder);
@ -86,7 +87,10 @@ widget_t* preview_ui(const char* filename) {
str_reset(&s);
}
return builder->root;
root = builder->root;
ui_builder_destroy(builder);
return root;
}
#include "tkc/path.h"

View File

@ -65,3 +65,13 @@ ret_t ui_builder_on_end(ui_builder_t* b) {
return RET_OK;
}
}
ret_t ui_builder_destroy(ui_builder_t* b) {
return_value_if_fail(b != NULL, RET_BAD_PARAMS);
if (b->destroy) {
return b->destroy(b);
} else {
return RET_OK;
}
}

View File

@ -41,6 +41,7 @@ typedef ret_t (*ui_builder_on_widget_prop_t)(ui_builder_t* b, const char* name,
typedef ret_t (*ui_builder_on_widget_prop_end_t)(ui_builder_t* b);
typedef ret_t (*ui_builder_on_widget_end_t)(ui_builder_t* b);
typedef ret_t (*ui_builder_on_end_t)(ui_builder_t* b);
typedef ret_t (*ui_builder_destroy_t)(ui_builder_t* b);
/**
* @class ui_builder_t
@ -70,6 +71,7 @@ struct _ui_builder_t {
ui_builder_on_widget_prop_end_t on_widget_prop_end;
ui_builder_on_widget_end_t on_widget_end;
ui_builder_on_end_t on_end;
ui_builder_destroy_t destroy;
widget_t* root;
widget_t* widget;
const char* name;
@ -144,6 +146,17 @@ ret_t ui_builder_on_start(ui_builder_t* builder);
*/
ret_t ui_builder_on_end(ui_builder_t* builder);
/**
* @method ui_builder_destroy
* builder
*
* @param {ui_builder_t*} builder builder对象
*
* @return {ret_t} RET_OK表示成功
*
*/
ret_t ui_builder_destroy(ui_builder_t* builder);
#define UI_DATA_MAGIC 0x11221212
END_C_DECLS

View File

@ -33,12 +33,13 @@ ret_t ui_loader_load(ui_loader_t* loader, const uint8_t* data, uint32_t size, ui
widget_t* ui_loader_load_widget(const char* name) {
ui_loader_t* loader = default_ui_loader();
ui_builder_t* builder = ui_builder_default(name);
ui_builder_t* builder = ui_builder_default_create(name);
const asset_info_t* ui = assets_manager_ref(assets_manager(), ASSET_TYPE_UI, name);
return_value_if_fail(ui != NULL, NULL);
ui_loader_load(loader, ui->data, ui->size, builder);
assets_manager_unref(assets_manager(), ui);
ui_builder_destroy(builder);
return builder->root;
}

View File

@ -96,17 +96,22 @@ static ret_t ui_builder_default_on_end(ui_builder_t* b) {
return RET_OK;
}
static ui_builder_t s_ui_builder;
ui_builder_t* ui_builder_default(const char* name) {
memset(&s_ui_builder, 0x00, sizeof(ui_builder_t));
s_ui_builder.on_widget_start = ui_builder_default_on_widget_start;
s_ui_builder.on_widget_prop = ui_builder_default_on_widget_prop;
s_ui_builder.on_widget_prop_end = ui_builder_default_on_widget_prop_end;
s_ui_builder.on_widget_end = ui_builder_default_on_widget_end;
s_ui_builder.on_end = ui_builder_default_on_end;
s_ui_builder.name = name;
return &s_ui_builder;
static ret_t ui_builder_default_destroy(ui_builder_t* b) {
TKMEM_FREE(b);
return RET_OK;
}
ui_builder_t* ui_builder_default_create(const char* name) {
ui_builder_t* builder = TKMEM_ZALLOC(ui_builder_t);
return_value_if_fail(builder != NULL, NULL);
builder->on_widget_start = ui_builder_default_on_widget_start;
builder->on_widget_prop = ui_builder_default_on_widget_prop;
builder->on_widget_prop_end = ui_builder_default_on_widget_prop_end;
builder->on_widget_end = ui_builder_default_on_widget_end;
builder->on_end = ui_builder_default_on_end;
builder->destroy = ui_builder_default_destroy;
builder->name = name;
return builder;
}

View File

@ -37,16 +37,16 @@ BEGIN_C_DECLS
*/
/**
* @method ui_builder_default
* @method ui_builder_default_create
* @annotation ["constructor"]
*
* ui\_builder对象
* ui\_builder对象
*
* @param {const char*} name
*
* @return {ui_builder_t*} ui\_builder对象
*/
ui_builder_t* ui_builder_default(const char* name);
ui_builder_t* ui_builder_default_create(const char* name);
widget_t* window_open(const char* name);
widget_t* dialog_open(const char* name);

View File

@ -19,7 +19,7 @@ TEST(UILoader, basic) {
widget_desc_t desc;
ui_binary_writer_t ui_binary_writer;
ui_loader_t* loader = default_ui_loader();
ui_builder_t* builder = ui_builder_default("");
ui_builder_t* builder = ui_builder_default_create("");
ui_builder_t* writer =
ui_binary_writer_init(&ui_binary_writer, wbuffer_init(&wbuffer, data, sizeof(data)));
@ -78,6 +78,7 @@ TEST(UILoader, basic) {
ASSERT_EQ(strcmp(widget_get_child(DIALOG(builder->root)->client, 1)->name, "cancel"), 0);
widget_destroy(builder->root);
ui_builder_destroy(builder);
}
TEST(UILoader, ext) {
@ -88,7 +89,7 @@ TEST(UILoader, ext) {
widget_desc_t desc;
ui_binary_writer_t ui_binary_writer;
ui_loader_t* loader = default_ui_loader();
ui_builder_t* builder = ui_builder_default("");
ui_builder_t* builder = ui_builder_default_create("");
ui_builder_t* writer =
ui_binary_writer_init(&ui_binary_writer, wbuffer_init(&wbuffer, data, sizeof(data)));
@ -137,4 +138,5 @@ TEST(UILoader, ext) {
ASSERT_EQ(cancel->h, 40);
widget_destroy(builder->root);
ui_builder_destroy(builder);
}

View File

@ -7,7 +7,7 @@ TEST(UILoaderXML, basic) {
widget_t* ok = NULL;
widget_t* cancel = NULL;
ui_loader_t* loader = xml_ui_loader();
ui_builder_t* builder = ui_builder_default("");
ui_builder_t* builder = ui_builder_default_create("");
const char* str =
"<dialog x=\"0\" y=\"0\" w=\"400\" h=\"300\">\
<dialog_title style=\"default\" x=\"0\" y=\"0\" w=\"100%\" h=\"30\" text=\"Hello AWTK\" /> \
@ -34,6 +34,7 @@ TEST(UILoaderXML, basic) {
ASSERT_EQ(strcmp(widget_get_child(DIALOG(builder->root)->client, 1)->name, "cancel"), 0);
widget_destroy(builder->root);
ui_builder_destroy(builder);
}
TEST(UILoaderXML, attr) {
@ -42,7 +43,7 @@ TEST(UILoaderXML, attr) {
widget_t* b3 = NULL;
widget_t* b4 = NULL;
ui_loader_t* loader = xml_ui_loader();
ui_builder_t* builder = ui_builder_default("");
ui_builder_t* builder = ui_builder_default_create("");
const char* str =
"<dialog margin=\"0\" x=\"0\" y=\"0\" w=\"400\" h=\"300\">\
<dialog_client style=\"border\" x=\"0\" y=\"bottom\" w=\"100%\" h=\"-30\">\
@ -57,6 +58,7 @@ TEST(UILoaderXML, attr) {
ASSERT_EQ(builder->root != NULL, true);
widget_layout(builder->root);
ui_builder_destroy(builder);
b1 = widget_lookup(builder->root, "b1", TRUE);
ASSERT_EQ(b1 != NULL, true);
ASSERT_EQ(b1->x, 10);
@ -95,7 +97,7 @@ TEST(UILoaderXML, attr) {
TEST(UILoaderXML, prop1) {
widget_t* root = NULL;
ui_loader_t* loader = xml_ui_loader();
ui_builder_t* builder = ui_builder_default("");
ui_builder_t* builder = ui_builder_default_create("");
const char* str =
"<dialog>\
<property name=\"x\">1</property>\
@ -116,13 +118,14 @@ TEST(UILoaderXML, prop1) {
ASSERT_EQ(root->h, 4);
ASSERT_STREQ(root->tr_text, "123<abc>123");
ui_builder_destroy(builder);
widget_destroy(root);
}
TEST(UILoaderXML, prop2) {
widget_t* root = NULL;
ui_loader_t* loader = xml_ui_loader();
ui_builder_t* builder = ui_builder_default("");
ui_builder_t* builder = ui_builder_default_create("");
const char* str =
"<dialog>\
<property name=\"x\">1</property>\
@ -145,4 +148,5 @@ TEST(UILoaderXML, prop2) {
ASSERT_STREQ(root->tr_text, "123<abc>123");
widget_destroy(root);
ui_builder_destroy(builder);
}