[FL-3025] IR button overflow fix (#2115)

* Allow for more than 255 button_menu items
* Allow more than 255 items in submenu
* Fix button_menu_reset

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Georgii Surkov 2022-12-10 21:05:52 +03:00 committed by GitHub
parent 31a9a3f5f3
commit 2954ec6d97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 37 deletions

View File

@ -30,7 +30,7 @@ struct ButtonMenu {
typedef struct { typedef struct {
ButtonMenuItemArray_t items; ButtonMenuItemArray_t items;
uint8_t position; size_t position;
const char* header; const char* header;
} ButtonMenuModel; } ButtonMenuModel;
@ -102,11 +102,9 @@ static void button_menu_view_draw_callback(Canvas* canvas, void* _model) {
ButtonMenuModel* model = (ButtonMenuModel*)_model; ButtonMenuModel* model = (ButtonMenuModel*)_model;
canvas_set_font(canvas, FontSecondary); canvas_set_font(canvas, FontSecondary);
uint8_t item_position = 0; const size_t active_screen = model->position / BUTTONS_PER_SCREEN;
int8_t active_screen = model->position / BUTTONS_PER_SCREEN; const size_t items_size = ButtonMenuItemArray_size(model->items);
size_t items_size = ButtonMenuItemArray_size(model->items); const size_t max_screen = items_size ? (items_size - 1) / BUTTONS_PER_SCREEN : 0;
int8_t max_screen = ((int16_t)items_size - 1) / BUTTONS_PER_SCREEN;
ButtonMenuItemArray_it_t it;
if(active_screen > 0) { if(active_screen > 0) {
canvas_draw_icon(canvas, 28, 1, &I_InfraredArrowUp_4x8); canvas_draw_icon(canvas, 28, 1, &I_InfraredArrowUp_4x8);
@ -125,6 +123,9 @@ static void button_menu_view_draw_callback(Canvas* canvas, void* _model) {
furi_string_free(disp_str); furi_string_free(disp_str);
} }
size_t item_position = 0;
ButtonMenuItemArray_it_t it;
for(ButtonMenuItemArray_it(it, model->items); !ButtonMenuItemArray_end_p(it); for(ButtonMenuItemArray_it(it, model->items); !ButtonMenuItemArray_end_p(it);
ButtonMenuItemArray_next(it), ++item_position) { ButtonMenuItemArray_next(it), ++item_position) {
if(active_screen == (item_position / BUTTONS_PER_SCREEN)) { if(active_screen == (item_position / BUTTONS_PER_SCREEN)) {
@ -195,14 +196,14 @@ static void button_menu_process_ok(ButtonMenu* button_menu, InputType type) {
if(item) { if(item) {
if(item->type == ButtonMenuItemTypeControl) { if(item->type == ButtonMenuItemTypeControl) {
if(type == InputTypeShort) { if(type == InputTypeShort) {
if(item && item->callback) { if(item->callback) {
item->callback(item->callback_context, item->index, type); item->callback(item->callback_context, item->index, type);
} }
} }
} }
if(item->type == ButtonMenuItemTypeCommon) { if(item->type == ButtonMenuItemTypeCommon) {
if((type == InputTypePress) || (type == InputTypeRelease)) { if((type == InputTypePress) || (type == InputTypeRelease)) {
if(item && item->callback) { if(item->callback) {
item->callback(item->callback_context, item->index, type); item->callback(item->callback_context, item->index, type);
} }
} }
@ -341,7 +342,7 @@ void button_menu_set_selected_item(ButtonMenu* button_menu, uint32_t index) {
button_menu->view, button_menu->view,
ButtonMenuModel * model, ButtonMenuModel * model,
{ {
uint8_t item_position = 0; size_t item_position = 0;
ButtonMenuItemArray_it_t it; ButtonMenuItemArray_it_t it;
for(ButtonMenuItemArray_it(it, model->items); !ButtonMenuItemArray_end_p(it); for(ButtonMenuItemArray_it(it, model->items); !ButtonMenuItemArray_end_p(it);
ButtonMenuItemArray_next(it), ++item_position) { ButtonMenuItemArray_next(it), ++item_position) {

View File

@ -20,8 +20,8 @@ ARRAY_DEF(SubmenuItemArray, SubmenuItem, M_POD_OPLIST);
typedef struct { typedef struct {
SubmenuItemArray_t items; SubmenuItemArray_t items;
const char* header; const char* header;
uint8_t position; size_t position;
uint8_t window_position; size_t window_position;
} SubmenuModel; } SubmenuModel;
static void submenu_process_up(Submenu* submenu); static void submenu_process_up(Submenu* submenu);
@ -36,19 +36,19 @@ static void submenu_view_draw_callback(Canvas* canvas, void* _model) {
canvas_clear(canvas); canvas_clear(canvas);
uint8_t position = 0;
SubmenuItemArray_it_t it;
if(model->header) { if(model->header) {
canvas_set_font(canvas, FontPrimary); canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 4, 11, model->header); canvas_draw_str(canvas, 4, 11, model->header);
} }
canvas_set_font(canvas, FontSecondary); canvas_set_font(canvas, FontSecondary);
size_t position = 0;
SubmenuItemArray_it_t it;
for(SubmenuItemArray_it(it, model->items); !SubmenuItemArray_end_p(it); for(SubmenuItemArray_it(it, model->items); !SubmenuItemArray_end_p(it);
SubmenuItemArray_next(it)) { SubmenuItemArray_next(it)) {
uint8_t item_position = position - model->window_position; const size_t item_position = position - model->window_position;
uint8_t items_on_screen = model->header ? 3 : 4; const size_t items_on_screen = model->header ? 3 : 4;
uint8_t y_offset = model->header ? 16 : 0; uint8_t y_offset = model->header ? 16 : 0;
if(item_position < items_on_screen) { if(item_position < items_on_screen) {
@ -198,7 +198,7 @@ void submenu_set_selected_item(Submenu* submenu, uint32_t index) {
submenu->view, submenu->view,
SubmenuModel * model, SubmenuModel * model,
{ {
uint32_t position = 0; size_t position = 0;
SubmenuItemArray_it_t it; SubmenuItemArray_it_t it;
for(SubmenuItemArray_it(it, model->items); !SubmenuItemArray_end_p(it); for(SubmenuItemArray_it(it, model->items); !SubmenuItemArray_end_p(it);
SubmenuItemArray_next(it)) { SubmenuItemArray_next(it)) {
@ -208,7 +208,9 @@ void submenu_set_selected_item(Submenu* submenu, uint32_t index) {
position++; position++;
} }
if(position >= SubmenuItemArray_size(model->items)) { const size_t items_size = SubmenuItemArray_size(model->items);
if(position >= items_size) {
position = 0; position = 0;
} }
@ -219,16 +221,12 @@ void submenu_set_selected_item(Submenu* submenu, uint32_t index) {
model->window_position -= 1; model->window_position -= 1;
} }
uint8_t items_on_screen = model->header ? 3 : 4; const size_t items_on_screen = model->header ? 3 : 4;
if(SubmenuItemArray_size(model->items) <= items_on_screen) { if(items_size <= items_on_screen) {
model->window_position = 0; model->window_position = 0;
} else { } else if(model->window_position >= items_size - items_on_screen) {
if(model->window_position >= model->window_position = items_size - items_on_screen;
(SubmenuItemArray_size(model->items) - items_on_screen)) {
model->window_position =
(SubmenuItemArray_size(model->items) - items_on_screen);
}
} }
}, },
true); true);
@ -239,16 +237,18 @@ void submenu_process_up(Submenu* submenu) {
submenu->view, submenu->view,
SubmenuModel * model, SubmenuModel * model,
{ {
uint8_t items_on_screen = model->header ? 3 : 4; const size_t items_on_screen = model->header ? 3 : 4;
const size_t items_size = SubmenuItemArray_size(model->items);
if(model->position > 0) { if(model->position > 0) {
model->position--; model->position--;
if(((model->position - model->window_position) < 1) && if((model->position - model->window_position < 1) &&
model->window_position > 0) { (model->window_position > 0)) {
model->window_position--; model->window_position--;
} }
} else { } else {
model->position = SubmenuItemArray_size(model->items) - 1; model->position = items_size - 1;
if(model->position > (items_on_screen - 1)) { if(model->position > items_on_screen - 1) {
model->window_position = model->position - (items_on_screen - 1); model->window_position = model->position - (items_on_screen - 1);
} }
} }
@ -261,12 +261,13 @@ void submenu_process_down(Submenu* submenu) {
submenu->view, submenu->view,
SubmenuModel * model, SubmenuModel * model,
{ {
uint8_t items_on_screen = model->header ? 3 : 4; const size_t items_on_screen = model->header ? 3 : 4;
if(model->position < (SubmenuItemArray_size(model->items) - 1)) { const size_t items_size = SubmenuItemArray_size(model->items);
if(model->position < items_size - 1) {
model->position++; model->position++;
if((model->position - model->window_position) > (items_on_screen - 2) && if((model->position - model->window_position > items_on_screen - 2) &&
model->window_position < (model->window_position < items_size - items_on_screen)) {
(SubmenuItemArray_size(model->items) - items_on_screen)) {
model->window_position++; model->window_position++;
} }
} else { } else {
@ -284,7 +285,8 @@ void submenu_process_ok(Submenu* submenu) {
submenu->view, submenu->view,
SubmenuModel * model, SubmenuModel * model,
{ {
if(model->position < (SubmenuItemArray_size(model->items))) { const size_t items_size = SubmenuItemArray_size(model->items);
if(model->position < items_size) {
item = SubmenuItemArray_get(model->items, model->position); item = SubmenuItemArray_get(model->items, model->position);
} }
}, },