feat(slider): add orientation support (based on bar)

This commit is contained in:
Gabor Kiss-Vamosi
2024-11-22 10:46:54 +01:00
parent ae0e884756
commit 70879191a1
5 changed files with 52 additions and 20 deletions

View File

@@ -11,9 +11,6 @@ Overview
The bar Widget has a background and an indicator. The length of the The bar Widget has a background and an indicator. The length of the
indicator against the background indicates the bar's current value. indicator against the background indicates the bar's current value.
Vertical bars can be created if the width of the Widget is smaller than
its height.
Both the start and end values of the bar can be set. Changing the start value to a Both the start and end values of the bar can be set. Changing the start value to a
value other than the minimum value in its range changes the start position of the indicator. value other than the minimum value in its range changes the start position of the indicator.
@@ -40,13 +37,13 @@ Usage
Orientation and size Orientation and size
-------------------- --------------------
- for orientation, width and height, simply set width and height style properties; - for orientation, width and height, simply set width and height properties;
- :cpp:expr:`lv_bar_set_orientation(slider, orientation)` to override orientation - :cpp:expr:`lv_bar_set_orientation(bar, orientation)` to override orientation
caused by ``width`` and ``height``. Valid values for ``orientation`` are: caused by ``width`` and ``height``. Valid values for ``orientation`` are:
- LV_BAR_ORIENTATION_AUTO, - :cpp:enumerator:`LV_BAR_ORIENTATION_AUTO`
- LV_BAR_ORIENTATION_HORIZONTAL, - :cpp:enumerator:`LV_BAR_ORIENTATION_HORIZONTAL`
- LV_BAR_ORIENTATION_VERTICAL. - :cpp:enumerator:`LV_BAR_ORIENTATION_VERTICAL`
Value and range Value and range
@@ -63,7 +60,6 @@ bottom to top in vertical mode. If the minimum value is greater than the maximum
The new value in :cpp:func:`lv_bar_set_value` can be set with or without an The new value in :cpp:func:`lv_bar_set_value` can be set with or without an
animation depending on the last parameter (``LV_ANIM_ON/OFF``). animation depending on the last parameter (``LV_ANIM_ON/OFF``).
Modes Modes
----- -----

View File

@@ -54,13 +54,13 @@ To set a different value use:
- :cpp:expr:`lv_slider_set_value(slider, new_value, LV_ANIM_ON/OFF)` (animation time - :cpp:expr:`lv_slider_set_value(slider, new_value, LV_ANIM_ON/OFF)` (animation time
is set by the styles' ``anim_time`` property); is set by the styles' ``anim_time`` property);
- :cpp:expr:`lv_slider_set_range(slider, min , max)`; and - :cpp:expr:`lv_slider_set_range(slider, min , max)`; and
- for orientation, width and height, simply set width and height style properties; - for orientation, width and height, simply set width and height properties;
- :cpp:expr:`lv_bar_set_orientation(slider, orientation)` to override orientation - :cpp:expr:`lv_slider_set_orientation(slider, orientation)` to override orientation
caused by ``width`` and ``height``. Valid values for ``orientation`` are: caused by ``width`` and ``height``. Valid values for ``orientation`` are:
- LV_BAR_ORIENTATION_AUTO, - :cpp:enumerator:`LV_SLIDER_ORIENTATION_AUTO`
- LV_BAR_ORIENTATION_HORIZONTAL, - :cpp:enumerator:`LV_SLIDER_ORIENTATION_HORIZONTAL`
- LV_BAR_ORIENTATION_VERTICAL. - :cpp:enumerator:`LV_SLIDER_ORIENTATION_VERTICAL`
The default drawing direction is from left to right in horizontal orientation and The default drawing direction is from left to right in horizontal orientation and
bottom to top in vertical orientation. If the minimum value is set to be greater bottom to top in vertical orientation. If the minimum value is set to be greater

View File

@@ -85,7 +85,7 @@ void lv_bar_set_range(lv_obj_t * obj, int32_t min, int32_t max);
/** /**
* Set the type of bar. * Set the type of bar.
* @param obj pointer to bar object * @param obj pointer to bar object
* @param mode bar type from ::lv_bar_mode_t * @param mode bar type from `lv_bar_mode_t`
*/ */
void lv_bar_set_mode(lv_obj_t * obj, lv_bar_mode_t mode); void lv_bar_set_mode(lv_obj_t * obj, lv_bar_mode_t mode);
@@ -131,14 +131,14 @@ int32_t lv_bar_get_max_value(const lv_obj_t * obj);
/** /**
* Get the type of bar. * Get the type of bar.
* @param obj pointer to bar object * @param obj pointer to bar object
* @return bar type from ::lv_bar_mode_t * @return bar type from `lv_bar_mode_t`
*/ */
lv_bar_mode_t lv_bar_get_mode(lv_obj_t * obj); lv_bar_mode_t lv_bar_get_mode(lv_obj_t * obj);
/** /**
* Get the orientation of bar. * Get the orientation of bar.
* @param obj pointer to bar object * @param obj pointer to bar object
* @return bar orientation from ::lv_bar_orientation_t * @return bar orientation from `lv_bar_orientation_t`
*/ */
lv_bar_orientation_t lv_bar_get_orientation(lv_obj_t * obj); lv_bar_orientation_t lv_bar_get_orientation(lv_obj_t * obj);

View File

@@ -103,6 +103,11 @@ void lv_slider_set_mode(lv_obj_t * obj, lv_slider_mode_t mode)
lv_bar_set_mode(obj, (lv_bar_mode_t)mode); lv_bar_set_mode(obj, (lv_bar_mode_t)mode);
} }
void lv_slider_set_orientation(lv_obj_t * obj, lv_slider_orientation_t orientation)
{
lv_bar_set_orientation(obj, (lv_bar_orientation_t)orientation);
}
int32_t lv_slider_get_value(const lv_obj_t * obj) int32_t lv_slider_get_value(const lv_obj_t * obj)
{ {
return lv_bar_get_value(obj); return lv_bar_get_value(obj);
@@ -131,6 +136,14 @@ lv_slider_mode_t lv_slider_get_mode(lv_obj_t * slider)
else return LV_SLIDER_MODE_NORMAL; else return LV_SLIDER_MODE_NORMAL;
} }
lv_slider_orientation_t lv_slider_get_orientation(lv_obj_t * slider)
{
lv_bar_orientation_t ori = lv_bar_get_orientation(slider);
if(ori == LV_BAR_ORIENTATION_HORIZONTAL) return LV_SLIDER_ORIENTATION_HORIZONTAL;
else if(ori == LV_BAR_ORIENTATION_VERTICAL) return LV_SLIDER_ORIENTATION_VERTICAL;
else return LV_SLIDER_ORIENTATION_AUTO;
}
bool lv_slider_is_symmetrical(lv_obj_t * obj) bool lv_slider_is_symmetrical(lv_obj_t * obj)
{ {
return lv_bar_is_symmetrical(obj); return lv_bar_is_symmetrical(obj);
@@ -388,7 +401,10 @@ static void position_knob(lv_obj_t * obj, lv_area_t * knob_area, const int32_t k
static bool is_slider_horizontal(lv_obj_t * obj) static bool is_slider_horizontal(lv_obj_t * obj)
{ {
return lv_obj_get_width(obj) >= lv_obj_get_height(obj); lv_slider_t * slider = (lv_slider_t *)obj;
if(slider->bar.orientation == LV_BAR_ORIENTATION_AUTO) return lv_obj_get_width(obj) >= lv_obj_get_height(obj);
else if(slider->bar.orientation == LV_BAR_ORIENTATION_HORIZONTAL) return true;
else return false;
} }
static void drag_start(lv_obj_t * obj) static void drag_start(lv_obj_t * obj)

View File

@@ -35,6 +35,12 @@ typedef enum {
LV_SLIDER_MODE_RANGE = LV_BAR_MODE_RANGE LV_SLIDER_MODE_RANGE = LV_BAR_MODE_RANGE
} lv_slider_mode_t; } lv_slider_mode_t;
typedef enum {
LV_SLIDER_ORIENTATION_AUTO = LV_BAR_ORIENTATION_AUTO,
LV_SLIDER_ORIENTATION_HORIZONTAL = LV_BAR_ORIENTATION_HORIZONTAL,
LV_SLIDER_ORIENTATION_VERTICAL = LV_BAR_ORIENTATION_VERTICAL
} lv_slider_orientation_t;
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_slider_class; LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_slider_class;
/********************** /**********************
@@ -79,10 +85,17 @@ void lv_slider_set_range(lv_obj_t * obj, int32_t min, int32_t max);
/** /**
* Set the mode of slider. * Set the mode of slider.
* @param obj pointer to a slider object * @param obj pointer to a slider object
* @param mode the mode of the slider. See ::lv_slider_mode_t * @param mode the mode of the slider. See `lv_slider_mode_t`
*/ */
void lv_slider_set_mode(lv_obj_t * obj, lv_slider_mode_t mode); void lv_slider_set_mode(lv_obj_t * obj, lv_slider_mode_t mode);
/**
* Set the orientation of slider.
* @param obj pointer to a slider object
* @param orientation slider orientation from `lv_slider_orientation_t`
*/
void lv_slider_set_orientation(lv_obj_t * obj, lv_slider_orientation_t orientation);
/*===================== /*=====================
* Getter functions * Getter functions
*====================*/ *====================*/
@@ -125,10 +138,17 @@ bool lv_slider_is_dragged(const lv_obj_t * obj);
/** /**
* Get the mode of the slider. * Get the mode of the slider.
* @param slider pointer to a slider object * @param slider pointer to a slider object
* @return see ::lv_slider_mode_t * @return see `lv_slider_mode_t`
*/ */
lv_slider_mode_t lv_slider_get_mode(lv_obj_t * slider); lv_slider_mode_t lv_slider_get_mode(lv_obj_t * slider);
/**
* Get the orientation of slider.
* @param obj pointer to a slider object
* @return slider orientation from `lv_slider_orientation_t`
*/
lv_slider_orientation_t lv_slider_get_orientation(lv_obj_t * slider);
/** /**
* Give the slider is in symmetrical mode or not * Give the slider is in symmetrical mode or not
* @param obj pointer to slider object * @param obj pointer to slider object