Skip to content

Commit

Permalink
feat(bar): add bar orientation (lvgl#6212)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabor Kiss-Vamosi <[email protected]>
  • Loading branch information
TridentTD and kisvegabor authored Jun 24, 2024
1 parent 1601ab9 commit fd9e901
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/widgets/bar/lv_bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ void lv_bar_set_mode(lv_obj_t * obj, lv_bar_mode_t mode)
lv_obj_invalidate(obj);
}

void lv_bar_set_orientation(lv_obj_t * obj, lv_bar_orientation_t orientation)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_bar_t * bar = (lv_bar_t *)obj;

bar->orientation = orientation;
lv_obj_invalidate(obj);
}

/*=====================
* Getter functions
*====================*/
Expand Down Expand Up @@ -209,6 +218,14 @@ lv_bar_mode_t lv_bar_get_mode(lv_obj_t * obj)
return bar->mode;
}

lv_bar_orientation_t lv_bar_get_orientation(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_bar_t * bar = (lv_bar_t *)obj;

return bar->orientation;
}

bool lv_bar_is_symmetrical(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
Expand Down Expand Up @@ -237,6 +254,7 @@ static void lv_bar_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
bar->indic_area.y1 = 0;
bar->indic_area.y2 = 0;
bar->mode = LV_BAR_MODE_NORMAL;
bar->orientation = LV_BAR_ORIENTATION_AUTO;
bar->val_reversed = false;

lv_bar_init_anim(obj, &bar->cur_value_anim);
Expand Down Expand Up @@ -280,7 +298,20 @@ static void draw_indic(lv_event_t * e)
range = 1;
}

bool hor = barw >= barh;
bool hor = false;
switch(bar->orientation) {
case LV_BAR_ORIENTATION_HORIZONTAL:
hor = true;
break;
case LV_BAR_ORIENTATION_VERTICAL:
hor = false;
break;
case LV_BAR_ORIENTATION_AUTO:
default:
hor = (barw >= barh);
break;
}

bool sym = lv_bar_is_symmetrical(obj);

/*Calculate the indicator area*/
Expand All @@ -304,7 +335,6 @@ static void draw_indic(lv_event_t * e)
bar->indic_area.x1 = obj->coords.x1 + (barw / 2) - (LV_BAR_SIZE_MIN / 2);
bar->indic_area.x2 = bar->indic_area.x1 + LV_BAR_SIZE_MIN;
}

int32_t indic_max_w = lv_area_get_width(&bar->indic_area);
int32_t indic_max_h = lv_area_get_height(&bar->indic_area);

Expand Down
27 changes: 26 additions & 1 deletion src/widgets/bar/lv_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,23 @@ enum _lv_bar_mode_t {
LV_BAR_MODE_SYMMETRICAL,
LV_BAR_MODE_RANGE
};

#ifdef DOXYGEN
typedef _lv_bar_mode_t lv_bar_mode_t;
#else
typedef uint8_t lv_bar_mode_t;
#endif /*DOXYGEN*/

enum _lv_bar_orientation_t {
LV_BAR_ORIENTATION_AUTO,
LV_BAR_ORIENTATION_HORIZONTAL,
LV_BAR_ORIENTATION_VERTICAL
};
#ifdef DOXYGEN
typedef _lv_bar_orientation_t lv_bar_orientation_t;
#else
typedef uint8_t lv_bar_orientation_t;
#endif /*DOXYGEN*/

typedef struct {
lv_obj_t * bar;
int32_t anim_start;
Expand All @@ -58,6 +68,7 @@ typedef struct {
_lv_bar_anim_t cur_value_anim;
_lv_bar_anim_t start_value_anim;
lv_bar_mode_t mode : 2; /**< Type of bar*/
lv_bar_orientation_t orientation : 2; /**< Orientation of bar*/
} lv_bar_t;

LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_bar_class;
Expand Down Expand Up @@ -109,6 +120,13 @@ void lv_bar_set_range(lv_obj_t * obj, int32_t min, int32_t max);
*/
void lv_bar_set_mode(lv_obj_t * obj, lv_bar_mode_t mode);

/**
* Set the orientation of bar.
* @param obj pointer to bar object
* @param orientation bar orientation from `lv_bar_orientation_t`
*/
void lv_bar_set_orientation(lv_obj_t * obj, lv_bar_orientation_t orientation);

/*=====================
* Getter functions
*====================*/
Expand Down Expand Up @@ -148,6 +166,13 @@ int32_t lv_bar_get_max_value(const lv_obj_t * obj);
*/
lv_bar_mode_t lv_bar_get_mode(lv_obj_t * obj);

/**
* Get the orientation of bar.
* @param obj pointer to bar object
* @return bar orientation from ::lv_bar_orientation_t
*/
lv_bar_orientation_t lv_bar_get_orientation(lv_obj_t * obj);

/**
* Give the bar is in symmetrical mode or not
* @param obj pointer to bar object
Expand Down
Binary file added tests/ref_imgs/widgets/bar_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions tests/src/test_cases/widgets/test_bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void setUp(void)

void tearDown(void)
{
lv_obj_clean(g_active_screen);
}

void test_bar_should_have_valid_default_attributes(void)
Expand Down Expand Up @@ -399,4 +400,51 @@ void test_bar_render_corner(void)
render_test_screen_create(true, LV_GRAD_DIR_VER, "widgets/bar_corner_6.png");
}


static lv_obj_t * bar_create_orientation(lv_bar_orientation_t orientation, int32_t w, int32_t h)
{
lv_obj_t * bar = lv_bar_create(g_active_screen);
lv_bar_set_orientation(bar, orientation);
lv_obj_set_size(bar, w, h);
lv_bar_set_value(bar, 30, LV_ANIM_OFF);

return bar;

}

void test_bar_orientation(void)
{
lv_obj_clean(g_active_screen);

lv_obj_set_flex_flow(g_active_screen, LV_FLEX_FLOW_ROW_WRAP);

lv_obj_t * label;

label = lv_label_create(g_active_screen);
lv_label_set_text(label, "Auto");
lv_obj_set_width(label, lv_pct(100));

bar_create_orientation(LV_BAR_ORIENTATION_AUTO, 100, 20);
bar_create_orientation(LV_BAR_ORIENTATION_AUTO, 20, 100);
bar_create_orientation(LV_BAR_ORIENTATION_AUTO, 100, 100);

label = lv_label_create(g_active_screen);
lv_label_set_text(label, "Vertical");
lv_obj_set_width(label, lv_pct(100));

bar_create_orientation(LV_BAR_ORIENTATION_VERTICAL, 100, 20);
bar_create_orientation(LV_BAR_ORIENTATION_VERTICAL, 20, 100);
bar_create_orientation(LV_BAR_ORIENTATION_VERTICAL, 100, 100);

label = lv_label_create(g_active_screen);
lv_label_set_text(label, "Horizontal");
lv_obj_set_width(label, lv_pct(100));

bar_create_orientation(LV_BAR_ORIENTATION_HORIZONTAL, 100, 20);
bar_create_orientation(LV_BAR_ORIENTATION_HORIZONTAL, 20, 100);
bar_create_orientation(LV_BAR_ORIENTATION_HORIZONTAL, 100, 100);

TEST_ASSERT_EQUAL_SCREENSHOT("widgets/bar_2.png");
}

#endif

0 comments on commit fd9e901

Please sign in to comment.