From bcc2fb5838323aa6b26518c0c43353a7d9dfe67a Mon Sep 17 00:00:00 2001 From: xianjimli Date: Mon, 18 Jun 2018 07:18:37 +0800 Subject: [PATCH] add pages_set_active_by_name --- src/base/pages.c | 21 +++++++++++++++++++++ src/base/pages.h | 14 ++++++++++++-- tests/pages_test.cc | 27 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/pages_test.cc diff --git a/src/base/pages.c b/src/base/pages.c index 65b73b53c..855c0173f 100644 --- a/src/base/pages.c +++ b/src/base/pages.c @@ -37,6 +37,27 @@ ret_t pages_set_active(widget_t* widget, uint32_t index) { return RET_OK; } +ret_t pages_set_active_by_name(widget_t* widget, const char* name) { + uint32_t i = 0; + uint32_t nr = 0; + widget_t** children = NULL; + return_value_if_fail(widget != NULL && name != NULL, RET_BAD_PARAMS); + + if (widget->children && widget->children->elms) { + nr = widget->children->size; + children = (widget_t**)(widget->children->elms); + + for (i = 0; i < nr; i++) { + widget_t* iter = children[i]; + if (iter->name.str && str_fast_equal(iter->name.str, name)) { + return pages_set_active(widget, i); + } + } + } + + return RET_NOT_FOUND; +} + static widget_t* pages_find_target(widget_t* widget, xy_t x, xy_t y) { pages_t* pages = PAGES(widget); return_value_if_fail(pages != NULL, NULL); diff --git a/src/base/pages.h b/src/base/pages.h index f55818b39..e704b22b0 100644 --- a/src/base/pages.h +++ b/src/base/pages.h @@ -58,14 +58,24 @@ widget_t* pages_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h); /** * @method pages_set_active - * 设置进度条的进度。 + * 设置当前的Page。 * @param {widget_t*} widget 控件对象。 - * @param {uint32_t} index 当Page的序号。 + * @param {uint32_t} index 当前Page的序号。 * * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 */ ret_t pages_set_active(widget_t* widget, uint32_t index); +/** + * @method pages_set_active_by_name + * 通过页面的名字设置当前的Page。 + * @param {widget_t*} widget 控件对象。 + * @param {char*} name 当前Page的名字。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t pages_set_active_by_name(widget_t* widget, const char* name); + #define PAGES(widget) ((pages_t*)(widget)) END_C_DECLS diff --git a/tests/pages_test.cc b/tests/pages_test.cc new file mode 100644 index 000000000..8137a2890 --- /dev/null +++ b/tests/pages_test.cc @@ -0,0 +1,27 @@ +#include "base/pages.h" +#include "base/view.h" +#include "gtest/gtest.h" + +TEST(Pages, basic) { + widget_t* pages = pages_create(NULL, 0, 0, 100, 100); + widget_t* p0 = view_create(pages, 0, 0, 100, 100); + widget_t* p1 = view_create(pages, 0, 0, 100, 100); + widget_t* p2 = view_create(pages, 0, 0, 100, 100); + + widget_set_name(p0, "p0"); + widget_set_name(p1, "p1"); + widget_set_name(p2, "p2"); + + ASSERT_EQ(PAGES(pages)->active, 0); + + pages_set_active(pages, 1); + ASSERT_EQ(PAGES(pages)->active, 1); + + pages_set_active_by_name(pages, "p2"); + ASSERT_EQ(PAGES(pages)->active, 2); + + pages_set_active_by_name(pages, "not found"); + ASSERT_EQ(PAGES(pages)->active, 2); + + widget_destroy(pages); +}