Gui: change data ownership model in submenu, own text by default (#2217)

* GUI: Refactor submenu to internally use FuriStrings
* NFC: Rework DESFIRE dynamic info submenu
* GUI: don't mutate label when drawing submenu
* Get rid of submenu_add_item_cstr
* increase api version
* Get rid of submenu_set_header_cstr
* rollback api version
* Documentation
* RFID: use new submenu api

Co-authored-by: Sergey Gavrilov <who.just.the.doctor@gmail.com>
This commit is contained in:
Avery 2022-12-30 11:44:07 +08:00 committed by GitHub
parent b11b9f1b38
commit 06cf1bb868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 51 deletions

View File

@ -1,7 +1,6 @@
#include "../lfrfid_i.h"
typedef struct {
FuriString* menu_item_name[LFRFIDProtocolMax];
uint32_t line_sel;
} SaveTypeCtx;
@ -16,25 +15,27 @@ void lfrfid_scene_save_type_on_enter(void* context) {
Submenu* submenu = app->submenu;
SaveTypeCtx* state = malloc(sizeof(SaveTypeCtx));
FuriString* protocol_string = furi_string_alloc();
for(uint8_t i = 0; i < LFRFIDProtocolMax; i++) {
if(strcmp(
protocol_dict_get_manufacturer(app->dict, i),
protocol_dict_get_name(app->dict, i)) != 0) {
state->menu_item_name[i] = furi_string_alloc_printf(
furi_string_printf(
protocol_string,
"%s %s",
protocol_dict_get_manufacturer(app->dict, i),
protocol_dict_get_name(app->dict, i));
} else {
state->menu_item_name[i] =
furi_string_alloc_printf("%s", protocol_dict_get_name(app->dict, i));
furi_string_printf(protocol_string, "%s", protocol_dict_get_name(app->dict, i));
}
submenu_add_item(
submenu,
furi_string_get_cstr(state->menu_item_name[i]),
furi_string_get_cstr(protocol_string),
i,
lfrfid_scene_save_type_submenu_callback,
app);
}
furi_string_free(protocol_string);
submenu_set_selected_item(
submenu, scene_manager_get_scene_state(app->scene_manager, LfRfidSceneSaveType));
@ -73,13 +74,7 @@ void lfrfid_scene_save_type_on_exit(void* context) {
submenu_reset(app->submenu);
for(uint8_t i = 0; i < LFRFIDProtocolMax; i++) {
furi_string_free(state->menu_item_name[i]);
}
uint32_t line_sel = state->line_sel;
free(state);
scene_manager_set_scene_state(app->scene_manager, LfRfidSceneSaveType, line_sel);
}

View File

@ -51,23 +51,18 @@ void nfc_scene_mf_desfire_app_on_enter(void* context) {
nfc_scene_mf_desfire_app_submenu_callback,
nfc);
uint16_t cap = NFC_TEXT_STORE_SIZE;
char* buf = nfc->text_store;
FuriString* label = furi_string_alloc();
int idx = SubmenuIndexDynamic;
for(MifareDesfireFile* file = app->file_head; file; file = file->next) {
int size = snprintf(buf, cap, "File %d", file->id);
if(size < 0 || size >= cap) {
FURI_LOG_W(
TAG,
"Exceeded NFC_TEXT_STORE_SIZE when preparing file id strings; menu truncated");
break;
}
char* label = buf;
cap -= size + 1;
buf += size + 1;
furi_string_printf(label, "File %d", file->id);
submenu_add_item(
nfc->submenu, label, idx++, nfc_scene_mf_desfire_app_submenu_callback, nfc);
nfc->submenu,
furi_string_get_cstr(label),
idx++,
nfc_scene_mf_desfire_app_submenu_callback,
nfc);
}
furi_string_free(label);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
}

View File

@ -33,21 +33,18 @@ void nfc_scene_mf_desfire_data_on_enter(void* context) {
nfc_scene_mf_desfire_data_submenu_callback,
nfc);
uint16_t cap = NFC_TEXT_STORE_SIZE;
char* buf = nfc->text_store;
FuriString* label = furi_string_alloc();
int idx = SubmenuIndexDynamic;
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
int size = snprintf(buf, cap, "App %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
if(size < 0 || size >= cap) {
FURI_LOG_W(
TAG, "Exceeded NFC_TEXT_STORE_SIZE when preparing app id strings; menu truncated");
break;
}
char* label = buf;
cap -= size + 1;
buf += size + 1;
submenu_add_item(submenu, label, idx++, nfc_scene_mf_desfire_data_submenu_callback, nfc);
furi_string_printf(label, "App %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
submenu_add_item(
submenu,
furi_string_get_cstr(label),
idx++,
nfc_scene_mf_desfire_data_submenu_callback,
nfc);
}
furi_string_free(label);
if(state >= MifareDesfireDataStateItem) {
submenu_set_selected_item(

View File

@ -9,17 +9,48 @@ struct Submenu {
};
typedef struct {
const char* label;
FuriString* label;
uint32_t index;
SubmenuItemCallback callback;
void* callback_context;
} SubmenuItem;
ARRAY_DEF(SubmenuItemArray, SubmenuItem, M_POD_OPLIST);
static void SubmenuItem_init(SubmenuItem* item) {
item->label = furi_string_alloc();
item->index = 0;
item->callback = NULL;
item->callback_context = NULL;
}
static void SubmenuItem_init_set(SubmenuItem* item, const SubmenuItem* src) {
item->label = furi_string_alloc_set(src->label);
item->index = src->index;
item->callback = src->callback;
item->callback_context = src->callback_context;
}
static void SubmenuItem_set(SubmenuItem* item, const SubmenuItem* src) {
furi_string_set(item->label, src->label);
item->index = src->index;
item->callback = src->callback;
item->callback_context = src->callback_context;
}
static void SubmenuItem_clear(SubmenuItem* item) {
furi_string_free(item->label);
}
ARRAY_DEF(
SubmenuItemArray,
SubmenuItem,
(INIT(API_2(SubmenuItem_init)),
SET(API_6(SubmenuItem_set)),
INIT_SET(API_6(SubmenuItem_init_set)),
CLEAR(API_2(SubmenuItem_clear))))
typedef struct {
SubmenuItemArray_t items;
const char* header;
FuriString* header;
size_t position;
size_t window_position;
} SubmenuModel;
@ -36,9 +67,9 @@ static void submenu_view_draw_callback(Canvas* canvas, void* _model) {
canvas_clear(canvas);
if(model->header) {
if(!furi_string_empty(model->header)) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 4, 11, model->header);
canvas_draw_str(canvas, 4, 11, furi_string_get_cstr(model->header));
}
canvas_set_font(canvas, FontSecondary);
@ -48,8 +79,8 @@ static void submenu_view_draw_callback(Canvas* canvas, void* _model) {
for(SubmenuItemArray_it(it, model->items); !SubmenuItemArray_end_p(it);
SubmenuItemArray_next(it)) {
const size_t item_position = position - model->window_position;
const size_t items_on_screen = model->header ? 3 : 4;
uint8_t y_offset = model->header ? 16 : 0;
const size_t items_on_screen = furi_string_empty(model->header) ? 4 : 3;
uint8_t y_offset = furi_string_empty(model->header) ? 0 : 16;
if(item_position < items_on_screen) {
if(position == model->position) {
@ -134,7 +165,7 @@ Submenu* submenu_alloc() {
SubmenuItemArray_init(model->items);
model->position = 0;
model->window_position = 0;
model->header = NULL;
model->header = furi_string_alloc();
},
true);
@ -145,7 +176,13 @@ void submenu_free(Submenu* submenu) {
furi_assert(submenu);
with_view_model(
submenu->view, SubmenuModel * model, { SubmenuItemArray_clear(model->items); }, true);
submenu->view,
SubmenuModel * model,
{
furi_string_free(model->header);
SubmenuItemArray_clear(model->items);
},
true);
view_free(submenu->view);
free(submenu);
}
@ -170,7 +207,7 @@ void submenu_add_item(
SubmenuModel * model,
{
item = SubmenuItemArray_push_new(model->items);
item->label = label;
furi_string_set_str(item->label, label);
item->index = index;
item->callback = callback;
item->callback_context = callback_context;
@ -188,7 +225,7 @@ void submenu_reset(Submenu* submenu) {
SubmenuItemArray_reset(model->items);
model->position = 0;
model->window_position = 0;
model->header = NULL;
furi_string_reset(model->header);
},
true);
}
@ -221,7 +258,7 @@ void submenu_set_selected_item(Submenu* submenu, uint32_t index) {
model->window_position -= 1;
}
const size_t items_on_screen = model->header ? 3 : 4;
const size_t items_on_screen = furi_string_empty(model->header) ? 4 : 3;
if(items_size <= items_on_screen) {
model->window_position = 0;
@ -240,7 +277,7 @@ void submenu_process_up(Submenu* submenu) {
submenu->view,
SubmenuModel * model,
{
const size_t items_on_screen = model->header ? 3 : 4;
const size_t items_on_screen = furi_string_empty(model->header) ? 4 : 3;
const size_t items_size = SubmenuItemArray_size(model->items);
if(model->position > 0) {
@ -263,7 +300,7 @@ void submenu_process_down(Submenu* submenu) {
submenu->view,
SubmenuModel * model,
{
const size_t items_on_screen = model->header ? 3 : 4;
const size_t items_on_screen = furi_string_empty(model->header) ? 4 : 3;
const size_t items_size = SubmenuItemArray_size(model->items);
if(model->position < items_size - 1) {
@ -303,5 +340,14 @@ void submenu_set_header(Submenu* submenu, const char* header) {
furi_assert(submenu);
with_view_model(
submenu->view, SubmenuModel * model, { model->header = header; }, true);
submenu->view,
SubmenuModel * model,
{
if(header == NULL) {
furi_string_reset(model->header);
} else {
furi_string_set_str(model->header, header);
}
},
true);
}