add pages_set_active_by_name

This commit is contained in:
xianjimli 2018-06-18 07:18:37 +08:00
parent 264b287e30
commit bcc2fb5838
3 changed files with 60 additions and 2 deletions

View File

@ -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);

View File

@ -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

27
tests/pages_test.cc Normal file
View File

@ -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);
}