diff options
author | xaowang96 <wangxuedong@xiaomi.com> | 2023-10-11 05:02:05 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-10 23:02:05 +0200 |
commit | f190d56f5d937eb957d085e7fcbe694473c6f2c1 (patch) | |
tree | 68f5bdb43c15d7cc6a65a11902e631ea7883b119 | |
parent | 14c0869e685b85a5dcd874598387e2f305024aad (diff) | |
download | lvgl-f190d56f5d937eb957d085e7fcbe694473c6f2c1.tar.gz lvgl-f190d56f5d937eb957d085e7fcbe694473c6f2c1.zip |
feat(checkbox): add RTL support (#4646)
Signed-off-by: wangxuedong <wangxuedong@xiaomi.com>
-rw-r--r-- | src/widgets/checkbox/lv_checkbox.c | 25 | ||||
-rw-r--r-- | tests/ref_imgs/checkbox_rtl_1.png | bin | 0 -> 6391 bytes | |||
-rw-r--r-- | tests/src/test_cases/widgets/test_checkbox.c | 23 |
3 files changed, 43 insertions, 5 deletions
diff --git a/src/widgets/checkbox/lv_checkbox.c b/src/widgets/checkbox/lv_checkbox.c index 15b66f99d..f5c1ccf9d 100644 --- a/src/widgets/checkbox/lv_checkbox.c +++ b/src/widgets/checkbox/lv_checkbox.c @@ -217,9 +217,12 @@ static void lv_checkbox_draw(lv_event_t * e) const lv_font_t * font = lv_obj_get_style_text_font(obj, LV_PART_MAIN); lv_coord_t font_h = lv_font_get_line_height(font); + const bool is_rtl = LV_BASE_DIR_RTL == lv_obj_get_style_base_dir(obj, LV_PART_MAIN); + lv_coord_t bg_border = lv_obj_get_style_border_width(obj, LV_PART_MAIN); lv_coord_t bg_topp = lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + bg_border; - lv_coord_t bg_leftp = lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + bg_border; + lv_coord_t bg_p = is_rtl ? lv_obj_get_style_pad_right(obj, LV_PART_MAIN) : lv_obj_get_style_pad_left(obj, + LV_PART_MAIN) + bg_border; lv_coord_t bg_colp = lv_obj_get_style_pad_column(obj, LV_PART_MAIN); lv_coord_t marker_leftp = lv_obj_get_style_pad_left(obj, LV_PART_INDICATOR); @@ -234,8 +237,14 @@ static void lv_checkbox_draw(lv_event_t * e) lv_draw_rect_dsc_init(&indic_dsc); lv_obj_init_draw_rect_dsc(obj, LV_PART_INDICATOR, &indic_dsc); lv_area_t marker_area; - marker_area.x1 = obj->coords.x1 + bg_leftp; - marker_area.x2 = marker_area.x1 + font_h + marker_leftp + marker_rightp - 1; + if(is_rtl) { + marker_area.x2 = obj->coords.x2 - bg_p; + marker_area.x1 = marker_area.x2 - font_h - marker_leftp - marker_rightp + 1; + } + else { + marker_area.x1 = obj->coords.x1 + bg_p; + marker_area.x2 = marker_area.x1 + font_h + marker_leftp + marker_rightp - 1; + } marker_area.y1 = obj->coords.y1 + bg_topp; marker_area.y2 = marker_area.y1 + font_h + marker_topp + marker_bottomp - 1; @@ -258,8 +267,14 @@ static void lv_checkbox_draw(lv_event_t * e) lv_coord_t y_ofs = (lv_area_get_height(&marker_area) - font_h) / 2; lv_area_t txt_area; - txt_area.x1 = marker_area.x2 + bg_colp; - txt_area.x2 = txt_area.x1 + txt_size.x; + if(is_rtl) { + txt_area.x2 = marker_area.x1 - bg_colp; + txt_area.x1 = txt_area.x2 - txt_size.x; + } + else { + txt_area.x1 = marker_area.x2 + bg_colp; + txt_area.x2 = txt_area.x1 + txt_size.x; + } txt_area.y1 = obj->coords.y1 + bg_topp + y_ofs; txt_area.y2 = txt_area.y1 + txt_size.y; diff --git a/tests/ref_imgs/checkbox_rtl_1.png b/tests/ref_imgs/checkbox_rtl_1.png Binary files differnew file mode 100644 index 000000000..a7919bfaf --- /dev/null +++ b/tests/ref_imgs/checkbox_rtl_1.png diff --git a/tests/src/test_cases/widgets/test_checkbox.c b/tests/src/test_cases/widgets/test_checkbox.c index 4800e0b64..e920243e4 100644 --- a/tests/src/test_cases/widgets/test_checkbox.c +++ b/tests/src/test_cases/widgets/test_checkbox.c @@ -93,4 +93,27 @@ void test_checkbox_should_allocate_memory_for_static_text(void) LV_HEAP_CHECK(TEST_ASSERT_LESS_THAN(initial_available_memory, m1.free_size)); } +void test_checkbox_rtl(void) +{ + const char * message = + "מעבד, או בשמו המלא יחידת עיבוד מרכזית (באנגלית: CPU - Central Processing Unit)."; + + lv_obj_t * screen = lv_obj_create(lv_scr_act()); + lv_obj_remove_style_all(screen); + lv_obj_set_size(screen, 800, 480); + lv_obj_center(screen); + lv_obj_set_style_bg_color(screen, lv_color_white(), 0); + lv_obj_set_style_bg_opa(screen, LV_OPA_100, 0); + lv_obj_set_style_pad_all(screen, 0, 0); + + lv_obj_t * test_checkbox = lv_checkbox_create(active_screen); + + lv_checkbox_set_text(test_checkbox, message); + lv_obj_set_style_text_font(test_checkbox, &lv_font_dejavu_16_persian_hebrew, 0); + lv_obj_center(test_checkbox); + lv_obj_set_style_base_dir(test_checkbox, LV_BASE_DIR_RTL, 0); + + TEST_ASSERT_EQUAL_SCREENSHOT("checkbox_rtl_1.png"); +} + #endif |