feat(tabview): support getting tab buttons by index

This commit is contained in:
Gabor Kiss-Vamosi
2025-08-16 12:31:01 +02:00
committed by Felipe Neves
parent 38000e8ca2
commit fbc8dbe6a7
6 changed files with 50 additions and 0 deletions
+1
View File
@@ -96,6 +96,7 @@ void lv_xml_init(void)
lv_xml_widget_register("lv_tabview", lv_xml_tabview_create, lv_xml_tabview_apply);
lv_xml_widget_register("lv_tabview-tab_bar", lv_xml_tabview_tab_bar_create, lv_xml_tabview_tab_bar_apply);
lv_xml_widget_register("lv_tabview-tab", lv_xml_tabview_tab_create, lv_xml_tabview_tab_apply);
lv_xml_widget_register("lv_tabview-tab_button", lv_xml_tabview_tab_button_create, lv_xml_tabview_tab_button_apply);
lv_xml_widget_register("lv_chart", lv_xml_chart_create, lv_xml_chart_apply);
lv_xml_widget_register("lv_chart-cursor", lv_xml_chart_cursor_create, lv_xml_chart_cursor_apply);
lv_xml_widget_register("lv_chart-series", lv_xml_chart_series_create, lv_xml_chart_series_apply);
@@ -87,6 +87,33 @@ void lv_xml_tabview_tab_apply(lv_xml_parser_state_t * state, const char ** attrs
lv_xml_obj_apply(state, attrs);
}
void * lv_xml_tabview_tab_button_create(lv_xml_parser_state_t * state, const char ** attrs)
{
lv_obj_t * tv = lv_xml_state_get_parent(state);
int32_t btn_cnt = lv_tabview_get_tab_count(tv);
if(btn_cnt == 0) {
LV_LOG_WARN("There are no buttons on the tab view. Get tab buttons when the tabs are already created");
return NULL;
}
const char * index_str = lv_xml_get_value_of(attrs, "index");
int32_t index_int = index_str ? lv_xml_atoi(index_str) : 0;
if(LV_ABS(index_int) >= btn_cnt) {
LV_LOG_WARN("tabindex is out of range, using the first tab instead");
index_int = 0;
}
void * item = lv_tabview_get_tab_button(tv, index_int);
return item;
}
void lv_xml_tabview_tab_button_apply(lv_xml_parser_state_t * state, const char ** attrs)
{
/*Apply the common properties, e.g. width, height, styles flags etc*/
lv_xml_obj_apply(state, attrs);
}
/**********************
* STATIC FUNCTIONS
**********************/
@@ -30,6 +30,8 @@ void * lv_xml_tabview_tab_bar_create(lv_xml_parser_state_t * state, const char *
void lv_xml_tabview_tab_bar_apply(lv_xml_parser_state_t * state, const char ** attrs);
void * lv_xml_tabview_tab_create(lv_xml_parser_state_t * state, const char ** attrs);
void lv_xml_tabview_tab_apply(lv_xml_parser_state_t * state, const char ** attrs);
void * lv_xml_tabview_tab_button_create(lv_xml_parser_state_t * state, const char ** attrs);
void lv_xml_tabview_tab_button_apply(lv_xml_parser_state_t * state, const char ** attrs);
/**********************
* MACROS
+7
View File
@@ -242,6 +242,13 @@ uint32_t lv_tabview_get_tab_active(lv_obj_t * obj)
return tabview->tab_cur;
}
lv_obj_t * lv_tabview_get_tab_button(lv_obj_t * obj, int32_t idx)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
return lv_obj_get_child_by_type(lv_tabview_get_tab_bar(obj), idx, &lv_button_class);
}
uint32_t lv_tabview_get_tab_count(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
+9
View File
@@ -88,6 +88,15 @@ uint32_t lv_tabview_get_tab_count(lv_obj_t * obj);
*/
uint32_t lv_tabview_get_tab_active(lv_obj_t * obj);
/**
* Get a given tab button by index
* @param obj pointer to a tabview widget
* @param idx zero based index of the tab button to get.
* < 0 means start counting tab button from the back (-1 is the last tab button)
* @return pointer to the tab button, or NULL if the index was out of range
*/
lv_obj_t * lv_tabview_get_tab_button(lv_obj_t * obj, int32_t idx);
/**
* Get the widget where the container of each tab is created
* @param obj pointer to a tabview widget
+4
View File
@@ -16,5 +16,9 @@ Example
<element name="tab" type="lv_obj" access="add">
<arg name="text" type="string"/>
</element>
<element name="tab_button" type="lv_obj" access="get">
<arg name="index" type="int"/>
</element>
</api>
</widget>