Skip to content

Commit

Permalink
feat(calendar): Set a custom year list for calendar header
Browse files Browse the repository at this point in the history
  • Loading branch information
C47D committed Jan 11, 2024
1 parent f2c1032 commit 33456e3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/extra/widgets/calendar/lv_calendar_header_dropdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ lv_obj_t * lv_calendar_header_dropdown_create(lv_obj_t * parent)
return obj;
}

void lv_calendar_header_dropdown_set_year_list(lv_obj_t * parent, const char * years_list)
{
bool child_found = false;
uint32_t idx = 0;
lv_obj_t * child = NULL;

const uint32_t calendar_child_count = lv_obj_get_child_count(parent);

/* Search for the header dropdown */
for(idx = 0; idx < calendar_child_count; idx++) {
child = lv_obj_get_child(parent, idx);
if(lv_obj_check_type(child, &lv_calendar_header_dropdown_class)) {
child_found = true;
break;
}
}

if(!child_found) return;

child_found = false;
const uint32_t header_child_count = lv_obj_get_child_count(child);

/* Search for the year dropdown */
for(idx = 0; idx < header_child_count; idx++) {
child = lv_obj_get_child(child, idx);
if(lv_obj_check_type(child, &lv_dropdown_class)) {
child_found = true;
break;
}
}

if(!child_found) return;

lv_dropdown_clear_options(child);
lv_dropdown_set_options(child, years_list);

lv_obj_invalidate(parent);
}

/**********************
* STATIC FUNCTIONS
**********************/
Expand Down Expand Up @@ -120,7 +159,14 @@ static void year_event_cb(lv_event_t * e)
const lv_calendar_date_t * d;
d = lv_calendar_get_showed_date(calendar);
lv_calendar_date_t newd = *d;
newd.year = 2023 - sel;

/* Get the first year on the options list
* NOTE: Assumes the first 4 digits in the option list are numbers */
const char * year_p = lv_dropdown_get_options(dropdown);
const uint32_t year = (year_p[0] - '0') * 1000 + (year_p[1] - '0') * 100 + (year_p[2] - '0') * 10 +
(year_p[3] - '0');

newd.year = year - sel;

lv_calendar_set_showed_date(calendar, newd.year, newd.month);
}
Expand All @@ -132,7 +178,14 @@ static void value_changed_event_cb(lv_event_t * e)
const lv_calendar_date_t * cur_date = lv_calendar_get_showed_date(calendar);

lv_obj_t * year_dd = lv_obj_get_child(header, 0);
lv_dropdown_set_selected(year_dd, 2023 - cur_date->year);

/* Get the first year on the options list
* NOTE: Assumes the first 4 digits in the option list are numbers */
const char * year_p = lv_dropdown_get_options(year_dd);
const uint32_t year = (year_p[0] - '0') * 1000 + (year_p[1] - '0') * 100 + (year_p[2] - '0') * 10 +
(year_p[3] - '0');

lv_dropdown_set_selected(year_dd, year - cur_date->year);

lv_obj_t * month_dd = lv_obj_get_child(header, 1);
lv_dropdown_set_selected(month_dd, cur_date->month - 1);
Expand Down
9 changes: 9 additions & 0 deletions src/extra/widgets/calendar/lv_calendar_header_dropdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ extern const lv_obj_class_t lv_calendar_header_dropdown_class;
*/
lv_obj_t * lv_calendar_header_dropdown_create(lv_obj_t * parent);

/**
* Sets a custom calendar year list
* @param parent pointer to a calendar object
* @param years_list pointer to an const char array with the years list, see lv_dropdown set_options for more information.
* E.g. `const char * years = "2023\n2022\n2021\n2020\n2019"
* Only the pointer will be saved so this variable can't be local which will be destroyed later.
*/
void lv_calendar_header_dropdown_set_year_list(lv_obj_t * parent, const char * years_list);

/**********************
* MACROS
**********************/
Expand Down

0 comments on commit 33456e3

Please sign in to comment.