Introduce mui_backForm feature

Signed-off-by: qianfan Zhao <qianfanguijin@163.com>
This commit is contained in:
qianfan Zhao 2024-07-22 17:29:30 +08:00
parent dc9fc73ddf
commit 03ac62023a
3 changed files with 68 additions and 16 deletions

View File

@ -811,6 +811,57 @@ void mui_RestoreForm(mui_t *ui)
mui_GotoForm(ui, ui->last_form_id, ui->last_form_cursor_focus_position);
}
int mui_pushFormStack(mui_t *ui, uint8_t form_id, uint8_t cursor_position)
{
if (ui->menu_form_stack_counts >= MUI_MENU_CACHE_CNT)
return -1;
MUI_DEBUG("mui_pushFormStack: [%d] %d:%d\n", ui->menu_form_stack_counts, form_id, cursor_position);
ui->menu_form_id[ui->menu_form_stack_counts] = form_id;
ui->menu_form_cursor_focus_position[ui->menu_form_stack_counts] = cursor_position;
ui->menu_form_stack_counts++;
return 0;
}
int mui_popFormStack(mui_t *ui, uint8_t *form_id, uint8_t *cursor_position)
{
if (ui->menu_form_stack_counts == 0 || ui->menu_form_stack_counts > MUI_MENU_CACHE_CNT)
return -1;
ui->menu_form_stack_counts--;
*form_id = ui->menu_form_id[ui->menu_form_stack_counts];
*cursor_position = ui->menu_form_cursor_focus_position[ui->menu_form_stack_counts];
MUI_DEBUG("mui_popFormStack: [%d] %d:%d\n", ui->menu_form_stack_counts, *form_id, *cursor_position);
return 0;
}
int mui_popFormStackAt(mui_t *ui, uint8_t form_id, uint8_t *cursor_position)
{
if (ui->menu_form_stack_counts == 0 || ui->menu_form_stack_counts > MUI_MENU_CACHE_CNT)
return -1;
for (uint8_t i = ui->menu_form_stack_counts - 1; i > 0; i--) {
if (ui->menu_form_id[i] == form_id) {
/* found it and reset stack size */
*cursor_position = ui->menu_form_cursor_focus_position[i];
ui->menu_form_stack_counts = i;
MUI_DEBUG("mui_popFormStackAt: [%d] %d:%d\n", i, form_id, *cursor_position);
return 0;
}
}
if (ui->menu_form_id[0] == form_id) {
*cursor_position = ui->menu_form_cursor_focus_position[0];
ui->menu_form_stack_counts = 0;
MUI_DEBUG("mui_popFormStackAt: [%d] %d:%d\n", 0, form_id, *cursor_position);
return 0;
}
return -1;
}
/*
Save a cursor position for mui_GotoFormAutoCursorPosition command
Two such positions is stored.
@ -819,16 +870,7 @@ void mui_SaveCursorPosition(mui_t *ui, uint8_t cursor_position)
{
uint8_t form_id = mui_get_fds_char(ui->current_form_fds+1);
MUI_DEBUG("mui_SaveCursorPosition form_id=%d cursor_position=%d\n", form_id, cursor_position);
if ( form_id == ui->menu_form_id[0] )
ui->menu_form_last_added = 0;
else if ( form_id == ui->menu_form_id[1] )
ui->menu_form_last_added = 1;
else
ui->menu_form_last_added ^= 1;
ui->menu_form_id[ui->menu_form_last_added] = form_id;
ui->menu_form_cursor_focus_position[ui->menu_form_last_added] = cursor_position;
MUI_DEBUG("mui_SaveCursorPosition ui->menu_form_last_added=%d \n", ui->menu_form_last_added);
mui_pushFormStack(ui, form_id, cursor_position);
}
/*
@ -837,10 +879,7 @@ void mui_SaveCursorPosition(mui_t *ui, uint8_t cursor_position)
uint8_t mui_GotoFormAutoCursorPosition(mui_t *ui, uint8_t form_id)
{
uint8_t cursor_position = 0;
if ( form_id == ui->menu_form_id[0] )
cursor_position = ui->menu_form_cursor_focus_position[0];
if ( form_id == ui->menu_form_id[1] )
cursor_position = ui->menu_form_cursor_focus_position[1];
mui_popFormStackAt(ui, form_id, &cursor_position);
MUI_DEBUG("mui_GotoFormAutoCursorPosition form_id=%d cursor_position=%d\n", form_id, cursor_position);
return mui_GotoForm(ui, form_id, cursor_position);
}
@ -903,6 +942,14 @@ void mui_SendSelect(mui_t *ui)
mui_send_cursor_msg(ui, MUIF_MSG_CURSOR_SELECT);
}
void mui_backForm(mui_t *ui)
{
uint8_t form_id, cursor_position;
if (mui_popFormStack(ui, &form_id, &cursor_position) == 0)
mui_GotoForm(ui, form_id, cursor_position);
}
/*
Same as mui_SendSelect(), but will try to find a field, which is marked as "execute on select" (MUIF_EXECUTE_ON_SELECT_BUTTON).
If such a field exists, then this field is executed, otherwise the current field will receive the select message.

View File

@ -223,7 +223,7 @@ struct muif_struct
#define MUI_MAX_TEXT_LEN 41
#endif
#define MUI_MENU_CACHE_CNT 2
#define MUI_MENU_CACHE_CNT 32
struct mui_struct
{
@ -277,7 +277,7 @@ struct mui_struct
/* menu cursor position backup */
uint8_t menu_form_id[MUI_MENU_CACHE_CNT];
uint8_t menu_form_cursor_focus_position[MUI_MENU_CACHE_CNT];
uint8_t menu_form_last_added;
uint8_t menu_form_stack_counts;
} ;
#define mui_IsCursorFocus(mui) ((mui)->dflags & MUIF_DFLAG_IS_CURSOR_FOCUS)
@ -592,6 +592,7 @@ int mui_GetCurrentFormId(mui_t *ui); /* form id or -1 if the menu system is i
void mui_NextField(mui_t *ui);
void mui_PrevField(mui_t *ui);
void mui_SendSelect(mui_t *ui);
void mui_backForm(mui_t *ui);
void mui_SendSelectWithExecuteOnSelectFieldSearch(mui_t *ui); /* use this if MUIF_EXECUTE_ON_SELECT_BUTTON is used */
void mui_SendValueIncrement(mui_t *ui);

View File

@ -1190,6 +1190,10 @@ int main(void)
{
mui_SendSelect(&ui);
}
if (k == 'b')
{
mui_backForm(&ui);
}
if ( k == 't' )
{