1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

feat(obj) add lv_obj_move_to_index(obj, index), renamed lv_obj_get_child_id(obj) to lv_obj_get_index(obj) (#2514)

* - renamed lv_obj_get_id(obj) to lv_obj_get_index(obj).

- added lv_obj_move_to_index(obj, index).

* automatic review comment fixed

* removed unused variable

* review issue

* restored deprecated function in header, otherwise Build Micropython with LVGL submodule / build (pull_request)  failes

* moved deprecated lv_obj_get_child_id() back to lv_obj_tree.h, otherwise Micropython will not build

* inline function did not work

* made deprecated function 'static inline'

* and now also inline

* move static inline function to lv_api_map.h again

* removed lv_obj_move_up/down

* changed log to warning for deprecated function

* redefined lv_obj_move_foreground(obj) and lv_obj_move_background(obj) as inline functions now calling lv_obj_move_to_index(obj, index).

- lv_obj_swap(obj1, obj2) added. (#2461)
This commit is contained in:
Karijn Wessing 2021-09-05 20:47:55 +02:00 committed by GitHub
parent 54338f6e57
commit 780e0efe2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 150 additions and 152 deletions

View File

@ -1,7 +1,9 @@
# Changelog
## v8.1.0 (In progress)
- lv_obj_move_up(obj) and lv_obj_move_down(obj) added. (#2461)
- renamed lv_obj_get_child_id(obj) to lv_obj_get_index(obj).
- added lv_obj_move_to_index(obj, index). (#2461)
- redefined lv_obj_move_foreground(obj) and lv_obj_move_background(obj) as inline functions now calling lv_obj_move_to_index(obj, index).
- lv_obj_swap(obj1, obj2) added. (#2461)
- feat(anim) add interface for handling lv_anim user data. (#2415)
- feat(obj) add lv_is_initialized (#2402)

View File

@ -42,7 +42,7 @@ lv_obj_del(label2);
There are 4 explicit way to bring an object to the foreground:
- Use `lv_obj_move_foreground(obj)` to explicitly tell the library to bring an object to the foreground. Similarly, use `lv_obj_move_background(obj)` to move to the background.
- Use `lv_obj_move_up(obj)` moves the object one position up in the hierarchy, Similarly, use `lv_obj_move_down(obj)` moves the object one position down in the hierarchy.
- Use `lv_obj_move_to_index(obj, index)` to set the index of the object in its parent.
- Use `lv_obj_swap(obj1, obj2)` to swap the relative position of two objects.
- When `lv_obj_set_parent(obj, new_parent)` is used, `obj` will be on the foreground on the `new_parent`.

View File

@ -65,11 +65,11 @@ for(i = 0; i < lv_obj_get_child_cnt(parent); i++) {
}
```
`lv_obj_get_child_id(obj)` returns the index of the object. That is how many younger children its parent has.
`lv_obj_get_index(obj)` returns the index of the object. That is how many younger children its parent has.
You can bring an object to the foreground or send it to the background with `lv_obj_move_foreground(obj)` and `lv_obj_move_background(obj)`.
You can move an object one position up or down in the hierargy with `lv_obj_move_up(obj)` and `lv_obj_move_down(obj)`.
You can change the index of an object in its parent using `lv_obj_move_to_index(obj, index)`.
You can swap the position of two objects with `lv_obj_swap(obj1, obj2)`.

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../lv_examples.h"
#if LV_USE_LIST && LV_BUILD_EXAMPLES
@ -58,25 +59,45 @@ static void event_handler_up(lv_event_t* e)
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_move_up(currentButton);
uint32_t index = lv_obj_get_index(currentButton);
if (index <= 0) return;
lv_obj_move_to_index(currentButton, index - 1);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
static void event_handler_center(lv_event_t* e)
{
const lv_event_code_t code = lv_event_get_code(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_t* parent = lv_obj_get_parent(currentButton);
const uint32_t pos = lv_obj_get_child_cnt(parent) / 2;
lv_obj_move_to_index(currentButton, pos);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
static void event_handler_dn(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
const lv_event_code_t code = lv_event_get_code(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
if (currentButton == NULL) return;
lv_obj_move_down(currentButton);
const uint32_t index = lv_obj_get_index(currentButton);
lv_obj_move_to_index(currentButton, index + 1);
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
static void event_handler_bottom(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
const lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED)
{
if (currentButton == NULL) return;
@ -87,28 +108,21 @@ static void event_handler_bottom(lv_event_t* e)
static void event_handler_swap(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
const lv_event_code_t code = lv_event_get_code(e);
// lv_obj_t* obj = lv_event_get_target(e);
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
{
uint32_t first = 0;
uint32_t last = lv_obj_get_child_cnt(list1);
if (last > 1)
{
last--;
while (first < last)
uint32_t cnt = lv_obj_get_child_cnt(list1);
for (int i = 0; i < 100; i++)
if (cnt > 1)
{
lv_obj_t* obj1 = lv_obj_get_child(list1, first);
lv_obj_t* obj2 = lv_obj_get_child(list1, last);
lv_obj_swap(obj1, obj2);
first++;
last--;
lv_obj_t* obj = lv_obj_get_child(list1, rand() % cnt);
lv_obj_move_to_index(obj, rand() % cnt);
if (currentButton != NULL)
{
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
if (currentButton != NULL)
{
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
}
}
}
}
@ -122,7 +136,7 @@ void lv_example_list_2(void)
/*Add buttons to the list*/
lv_obj_t* btn;
int i;
for (i = 0; i < 30; i++) {
for (i = 0; i < 15; i++) {
btn = lv_btn_create(list1);
lv_obj_set_width(btn, lv_pct(50));
lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
@ -149,6 +163,10 @@ void lv_example_list_2(void)
lv_obj_add_event_cb(btn, event_handler_up, LV_EVENT_ALL, NULL);
lv_group_remove_obj(btn);
btn = lv_list_add_btn(list2, LV_SYMBOL_LEFT, "Center");
lv_obj_add_event_cb(btn, event_handler_center, LV_EVENT_ALL, NULL);
lv_group_remove_obj(btn);
btn = lv_list_add_btn(list2, LV_SYMBOL_DOWN, "Down");
lv_obj_add_event_cb(btn, event_handler_dn, LV_EVENT_ALL, NULL);
lv_group_remove_obj(btn);

View File

@ -5,7 +5,7 @@
static void event_handler(lv_event_t * e)
{
lv_obj_t * obj = lv_event_get_target(e);
LV_LOG_USER("Button %d clicked", lv_obj_get_child_id(obj));
LV_LOG_USER("Button %d clicked", lv_obj_get_index(obj));
}
void lv_example_win_1(void)

View File

@ -6,6 +6,8 @@
/*********************
* INCLUDES
*********************/
#include <stdlib.h>
#include "lv_obj.h"
#include "lv_indev.h"
#include "../misc/lv_anim.h"
@ -28,9 +30,9 @@
/**********************
* STATIC PROTOTYPES
**********************/
static void lv_obj_del_async_cb(void * obj);
static void lv_obj_del_async_cb(void* obj);
static void obj_del_core(lv_obj_t * obj);
static lv_obj_tree_walk_res_t walk_core(lv_obj_t * obj, lv_obj_tree_walk_cb_t cb, void * user_data);
static lv_obj_tree_walk_res_t walk_core(lv_obj_t * obj, lv_obj_tree_walk_cb_t cb, void* user_data);
/**********************
* STATIC VARIABLES
@ -73,7 +75,7 @@ void lv_obj_del(lv_obj_t * obj)
}
/*Handle if the active screen was deleted*/
if(act_scr_del) {
if(act_scr_del) {
LV_LOG_WARN("the active screen was deleted")
disp->act_scr = NULL;
}
@ -159,8 +161,8 @@ void lv_obj_set_parent(lv_obj_t * obj, lv_obj_t * parent)
/*Remove the object from the old parent's child list*/
int32_t i;
for(i = lv_obj_get_child_id(obj); i <= (int32_t)lv_obj_get_child_cnt(old_parent) - 2; i++) {
old_parent->spec_attr->children[i] = old_parent->spec_attr->children[i+1];
for(i = lv_obj_get_index(obj); i <= (int32_t)lv_obj_get_child_cnt(old_parent) - 2; i++) {
old_parent->spec_attr->children[i] = old_parent->spec_attr->children[i + 1];
}
old_parent->spec_attr->child_cnt--;
if(old_parent->spec_attr->child_cnt) {
@ -195,39 +197,39 @@ void lv_obj_set_parent(lv_obj_t * obj, lv_obj_t * parent)
lv_obj_invalidate(obj);
}
void lv_obj_move_foreground(lv_obj_t * obj)
void lv_obj_move_to_index(lv_obj_t * obj, int32_t index)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_obj_t * parent = lv_obj_get_parent(obj);
uint32_t i;
for(i = lv_obj_get_child_id(obj); i < lv_obj_get_child_cnt(parent) - 1; i++) {
parent->spec_attr->children[i] = parent->spec_attr->children[i + 1];
}
parent->spec_attr->children[lv_obj_get_child_cnt(parent) - 1] = obj;
/*Notify the new parent about the child*/
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, obj);
lv_obj_invalidate(parent);
}
void lv_obj_move_background(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
const int32_t old_index = lv_obj_get_index(obj);
lv_obj_t * parent = lv_obj_get_parent(obj);
int32_t i;
for(i = lv_obj_get_child_id(obj); i > 0; i--) {
parent->spec_attr->children[i] = parent->spec_attr->children[i-1];
if(index < 0) return;
if(index >= (int32_t) lv_obj_get_child_cnt(parent)) return;
if(index == old_index) return;
int32_t i = old_index;
if(index < old_index)
{
while (i > index)
{
parent->spec_attr->children[i] = parent->spec_attr->children[i - 1];
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, parent->spec_attr->children[i]);
i--;
}
}
parent->spec_attr->children[0] = obj;
/*Notify the new parent about the child*/
else
{
while (i < index)
{
parent->spec_attr->children[i] = parent->spec_attr->children[i + 1];
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, parent->spec_attr->children[i]);
i++;
}
}
parent->spec_attr->children[index] = obj;
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, obj);
lv_obj_invalidate(parent);
}
@ -236,11 +238,11 @@ void lv_obj_swap(lv_obj_t * obj1, lv_obj_t * obj2)
LV_ASSERT_OBJ(obj1, MY_CLASS);
LV_ASSERT_OBJ(obj2, MY_CLASS);
lv_obj_t* parent = lv_obj_get_parent(obj1);
lv_obj_t* parent2 = lv_obj_get_parent(obj2);
lv_obj_t * parent = lv_obj_get_parent(obj1);
lv_obj_t * parent2 = lv_obj_get_parent(obj2);
uint_fast32_t index1 = lv_obj_get_child_id(obj1);
uint_fast32_t index2 = lv_obj_get_child_id(obj2);
uint_fast32_t index1 = lv_obj_get_index(obj1);
uint_fast32_t index2 = lv_obj_get_index(obj2);
parent->spec_attr->children[index1] = obj2;
parent2->spec_attr->children[index2] = obj1;
@ -249,57 +251,14 @@ void lv_obj_swap(lv_obj_t * obj1, lv_obj_t * obj2)
lv_event_send(parent2, LV_EVENT_CHILD_CHANGED, obj1);
lv_obj_invalidate(parent);
if( parent != parent2) {
if(parent != parent2)
{
lv_obj_invalidate(parent2);
}
lv_group_swap_obj(obj1, obj2);
}
void lv_obj_move_up(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_obj_t* parent = lv_obj_get_parent(obj);
uint_fast32_t index = lv_obj_get_child_id(obj);
if (index > 0)
{
lv_obj_t* obj2 = parent->spec_attr->children[index - 1];
parent->spec_attr->children[index - 1] = obj;
parent->spec_attr->children[index] = obj2;
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, obj);
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, obj2);
lv_obj_invalidate(parent);
lv_group_swap_obj(obj, obj2);
}
}
void lv_obj_move_down(lv_obj_t* obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_obj_t* parent = lv_obj_get_parent(obj);
uint_fast32_t index = lv_obj_get_child_id(obj);
if (index < lv_obj_get_child_cnt(parent) - 1)
{
lv_obj_t* obj2 = parent->spec_attr->children[index + 1];
parent->spec_attr->children[index + 1] = obj;
parent->spec_attr->children[index] = obj2;
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, obj);
lv_event_send(parent, LV_EVENT_CHILD_CHANGED, obj2);
lv_obj_invalidate(parent);
lv_group_swap_obj(obj, obj2);
}
}
lv_obj_t * lv_obj_get_screen(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -309,10 +268,10 @@ lv_obj_t * lv_obj_get_screen(const lv_obj_t * obj)
do {
act_par = par;
par = lv_obj_get_parent(act_par);
par = lv_obj_get_parent(act_par);
} while(par != NULL);
return (lv_obj_t*)act_par;
return (lv_obj_t *)act_par;
}
lv_disp_t * lv_obj_get_disp(const lv_obj_t * obj)
@ -370,7 +329,7 @@ uint32_t lv_obj_get_child_cnt(const lv_obj_t * obj)
return obj->spec_attr->child_cnt;
}
uint32_t lv_obj_get_child_id(const lv_obj_t * obj)
uint32_t lv_obj_get_index(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -385,8 +344,7 @@ uint32_t lv_obj_get_child_id(const lv_obj_t * obj)
return 0xFFFFFFFF; /*Shouldn't happen*/
}
void lv_obj_tree_walk(lv_obj_t * start_obj, lv_obj_tree_walk_cb_t cb, void * user_data)
void lv_obj_tree_walk(lv_obj_t * start_obj, lv_obj_tree_walk_cb_t cb, void* user_data)
{
walk_core(start_obj, cb, user_data);
}
@ -395,7 +353,7 @@ void lv_obj_tree_walk(lv_obj_t * start_obj, lv_obj_tree_walk_cb_t cb, void * use
* STATIC FUNCTIONS
**********************/
static void lv_obj_del_async_cb(void * obj)
static void lv_obj_del_async_cb(void* obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
@ -415,10 +373,10 @@ static void obj_del_core(lv_obj_t * obj)
child = lv_obj_get_child(obj, 0);
}
lv_group_t * group = lv_obj_get_group(obj);
lv_group_t* group = lv_obj_get_group(obj);
/*Reset all input devices if the object to delete is used*/
lv_indev_t * indev = lv_indev_get_next(NULL);
lv_indev_t* indev = lv_indev_get_next(NULL);
while(indev) {
if(indev->proc.types.pointer.act_obj == obj || indev->proc.types.pointer.last_obj == obj) {
lv_indev_reset(indev, obj);
@ -454,7 +412,7 @@ static void obj_del_core(lv_obj_t * obj)
}
/*Remove the object from the child list of its parent*/
else {
uint32_t id = lv_obj_get_child_id(obj);
uint32_t id = lv_obj_get_index(obj);
uint32_t i;
for(i = id; i < obj->parent->spec_attr->child_cnt - 1; i++) {
obj->parent->spec_attr->children[i] = obj->parent->spec_attr->children[i + 1];
@ -468,7 +426,7 @@ static void obj_del_core(lv_obj_t * obj)
}
static lv_obj_tree_walk_res_t walk_core(lv_obj_t * obj, lv_obj_tree_walk_cb_t cb, void * user_data)
static lv_obj_tree_walk_res_t walk_core(lv_obj_t * obj, lv_obj_tree_walk_cb_t cb, void* user_data)
{
lv_obj_tree_walk_res_t res = LV_OBJ_TREE_WALK_NEXT;

View File

@ -85,22 +85,6 @@ void lv_obj_del_async(struct _lv_obj_t * obj);
*/
void lv_obj_set_parent(struct _lv_obj_t * obj, struct _lv_obj_t * parent);
/**
* Move the object to the foreground.
* It will look like if it was created as the last child of its parent.
* It also means it can cover any of the siblings.
* @param obj pointer to an object
*/
void lv_obj_move_foreground(struct _lv_obj_t * obj);
/**
* Move the object to the background.
* It will look like if it was created as the first child of its parent.
* It also means any of the siblings can cover the object.
* @param obj pointer to an object
*/
void lv_obj_move_background(struct _lv_obj_t * obj);
/**
* Swap the positions of two objects.
* When used in listboxes, it can be used to sort the listbox items.
@ -110,18 +94,14 @@ void lv_obj_move_background(struct _lv_obj_t * obj);
void lv_obj_swap(struct _lv_obj_t* obj1, struct _lv_obj_t* obj2);
/**
* moves the object one position up in the hierarchy.
* moves the object to the given index in its parent.
* When used in listboxes, it can be used to sort the listbox items.
* @param obj pointer to the object to be moved upwards.
* @param obj pointer to the object to be moved.
* @param index new index in parent.
* @note to move to the foreground: lv_obj_move_to_index(obj, 0)
* @note to move forward (up): lv_obj_move_to_index(obj, lv_obj_get_index(obj) - 1)
*/
void lv_obj_move_up(struct _lv_obj_t* obj);
/**
* moves the object one position down in the hierarchy.
* When used in listboxes, it can be used to sort the listbox items.
* @param obj pointer to the object to be moved downwards.
*/
void lv_obj_move_down(struct _lv_obj_t* obj);
void lv_obj_move_to_index(struct _lv_obj_t* obj, int32_t index);
/**
* Get the screen of an object
@ -166,11 +146,11 @@ uint32_t lv_obj_get_child_cnt(const struct _lv_obj_t * obj);
/**
* Get the index of a child.
* @param obj pointer to an obejct
* @param obj pointer to an object
* @return the child index of the object.
* E.g. 0: the oldest (firstly created child)
*/
uint32_t lv_obj_get_child_id(const struct _lv_obj_t * obj);
uint32_t lv_obj_get_index(const struct _lv_obj_t* obj);
/**
* Iterate through all children of any object.

View File

@ -201,12 +201,12 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj)
#if LV_USE_WIN
/*Header*/
if(lv_obj_get_child_id(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
if(lv_obj_get_index(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
lv_obj_add_style(obj, &styles->light, 0);
return;
}
/*Content*/
else if(lv_obj_get_child_id(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
else if(lv_obj_get_index(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
lv_obj_add_style(obj, &styles->light, 0);
lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);
return;

View File

@ -630,13 +630,13 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj)
#if LV_USE_WIN
/*Header*/
if(lv_obj_get_child_id(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
if(lv_obj_get_index(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
lv_obj_add_style(obj, &styles->bg_color_grey, 0);
lv_obj_add_style(obj, &styles->pad_tiny, 0);
return;
}
/*Content*/
else if(lv_obj_get_child_id(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
else if(lv_obj_get_index(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
lv_obj_add_style(obj, &styles->scr, 0);
lv_obj_add_style(obj, &styles->pad_normal, 0);
lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);

View File

@ -219,13 +219,13 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj)
#if LV_USE_WIN
/*Header*/
if(lv_obj_get_child_id(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
if(lv_obj_get_index(obj) == 0 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
lv_obj_add_style(obj, &styles->card, 0);
lv_obj_add_style(obj, &styles->no_radius, 0);
return;
}
/*Content*/
else if(lv_obj_get_child_id(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
else if(lv_obj_get_index(obj) == 1 && lv_obj_check_type(lv_obj_get_parent(obj), &lv_win_class)) {
lv_obj_add_style(obj, &styles->card, 0);
lv_obj_add_style(obj, &styles->no_radius, 0);
lv_obj_add_style(obj, &styles->scrollbar, LV_PART_SCROLLBAR);

View File

@ -41,6 +41,46 @@ static inline LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_task_handler(void)
* MACROS
**********************/
/**********************
* INLINE FUNCTIONS
**********************/
/**
* Move the object to the foreground.
* It will look like if it was created as the last child of its parent.
* It also means it can cover any of the siblings.
* @param obj pointer to an object
*/
static inline void lv_obj_move_foreground(lv_obj_t* obj)
{
lv_obj_t* parent = lv_obj_get_parent(obj);
lv_obj_move_to_index(obj, lv_obj_get_child_cnt(parent) - 1);
}
/**
* Move the object to the background.
* It will look like if it was created as the first child of its parent.
* It also means any of the siblings can cover the object.
* @param obj pointer to an object
*/
static inline void lv_obj_move_background(lv_obj_t* obj)
{
lv_obj_move_to_index(obj, 0);
}
/**********************
* DEPRECATED FUNCTIONS
**********************/
static inline uint32_t lv_obj_get_child_id(const struct _lv_obj_t* obj)
{
LV_LOG_WARN("lv_obj_get_child_id(obj) is deprecated, please use lv_obj_get_index(obj).");
return lv_obj_get_index(obj);
}
#ifdef __cplusplus
} /*extern "C"*/
#endif