mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-10 06:54:19 +00:00
Gui: change dialog_ex text ownership model (#3831)
* Gui: change dialog_ex text ownership model * Gui: change text ownership model part 2 * Examples: fix DialogEx usage in number input * Gui: fix nullptr dereference in DialogEx, proper reset procedure Co-authored-by: hedger <hedger@users.noreply.github.com>
This commit is contained in:
parent
5272eb7550
commit
f353e5708d
2 changed files with 69 additions and 53 deletions
|
@ -13,8 +13,7 @@ static void example_number_input_scene_update_view(void* context) {
|
|||
|
||||
dialog_ex_set_header(dialog_ex, "The number is", 64, 0, AlignCenter, AlignTop);
|
||||
|
||||
static char buffer[12]; //needs static for extended lifetime
|
||||
|
||||
char buffer[12] = {};
|
||||
snprintf(buffer, sizeof(buffer), "%ld", app->current_number);
|
||||
dialog_ex_set_text(dialog_ex, buffer, 64, 29, AlignCenter, AlignCenter);
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ struct DialogEx {
|
|||
};
|
||||
|
||||
typedef struct {
|
||||
const char* text;
|
||||
FuriString* text;
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
Align horizontal;
|
||||
|
@ -28,16 +28,15 @@ typedef struct {
|
|||
TextElement text;
|
||||
IconElement icon;
|
||||
|
||||
const char* left_text;
|
||||
const char* center_text;
|
||||
const char* right_text;
|
||||
FuriString* left_text;
|
||||
FuriString* center_text;
|
||||
FuriString* right_text;
|
||||
} DialogExModel;
|
||||
|
||||
static void dialog_ex_view_draw_callback(Canvas* canvas, void* _model) {
|
||||
DialogExModel* model = _model;
|
||||
|
||||
// Prepare canvas
|
||||
canvas_clear(canvas);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
|
||||
if(model->icon.icon != NULL) {
|
||||
|
@ -46,94 +45,94 @@ static void dialog_ex_view_draw_callback(Canvas* canvas, void* _model) {
|
|||
|
||||
// Draw header
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
if(model->header.text != NULL) {
|
||||
if(furi_string_size(model->header.text)) {
|
||||
elements_multiline_text_aligned(
|
||||
canvas,
|
||||
model->header.x,
|
||||
model->header.y,
|
||||
model->header.horizontal,
|
||||
model->header.vertical,
|
||||
model->header.text);
|
||||
furi_string_get_cstr(model->header.text));
|
||||
}
|
||||
|
||||
// Draw text
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
if(model->text.text != NULL) {
|
||||
if(furi_string_size(model->text.text)) {
|
||||
elements_multiline_text_aligned(
|
||||
canvas,
|
||||
model->text.x,
|
||||
model->text.y,
|
||||
model->text.horizontal,
|
||||
model->text.vertical,
|
||||
model->text.text);
|
||||
furi_string_get_cstr(model->text.text));
|
||||
}
|
||||
|
||||
// Draw buttons
|
||||
if(model->left_text != NULL) {
|
||||
elements_button_left(canvas, model->left_text);
|
||||
if(furi_string_size(model->left_text)) {
|
||||
elements_button_left(canvas, furi_string_get_cstr(model->left_text));
|
||||
}
|
||||
|
||||
if(model->center_text != NULL) {
|
||||
elements_button_center(canvas, model->center_text);
|
||||
if(furi_string_size(model->center_text)) {
|
||||
elements_button_center(canvas, furi_string_get_cstr(model->center_text));
|
||||
}
|
||||
|
||||
if(model->right_text != NULL) {
|
||||
elements_button_right(canvas, model->right_text);
|
||||
if(furi_string_size(model->right_text)) {
|
||||
elements_button_right(canvas, furi_string_get_cstr(model->right_text));
|
||||
}
|
||||
}
|
||||
|
||||
static bool dialog_ex_view_input_callback(InputEvent* event, void* context) {
|
||||
DialogEx* dialog_ex = context;
|
||||
bool consumed = false;
|
||||
const char* left_text = NULL;
|
||||
const char* center_text = NULL;
|
||||
const char* right_text = NULL;
|
||||
bool left_text_present = false;
|
||||
bool center_text_present = false;
|
||||
bool right_text_present = false;
|
||||
|
||||
with_view_model(
|
||||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{
|
||||
left_text = model->left_text;
|
||||
center_text = model->center_text;
|
||||
right_text = model->right_text;
|
||||
left_text_present = furi_string_size(model->left_text);
|
||||
center_text_present = furi_string_size(model->center_text);
|
||||
right_text_present = furi_string_size(model->right_text);
|
||||
},
|
||||
true);
|
||||
false);
|
||||
|
||||
if(dialog_ex->callback) {
|
||||
if(event->type == InputTypeShort) {
|
||||
if(event->key == InputKeyLeft && left_text != NULL) {
|
||||
if(event->key == InputKeyLeft && left_text_present) {
|
||||
dialog_ex->callback(DialogExResultLeft, dialog_ex->context);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyOk && center_text != NULL) {
|
||||
} else if(event->key == InputKeyOk && center_text_present) {
|
||||
dialog_ex->callback(DialogExResultCenter, dialog_ex->context);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyRight && right_text != NULL) {
|
||||
} else if(event->key == InputKeyRight && right_text_present) {
|
||||
dialog_ex->callback(DialogExResultRight, dialog_ex->context);
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(event->type == InputTypePress && dialog_ex->enable_extended_events) {
|
||||
if(event->key == InputKeyLeft && left_text != NULL) {
|
||||
if(event->key == InputKeyLeft && left_text_present) {
|
||||
dialog_ex->callback(DialogExPressLeft, dialog_ex->context);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyOk && center_text != NULL) {
|
||||
} else if(event->key == InputKeyOk && center_text_present) {
|
||||
dialog_ex->callback(DialogExPressCenter, dialog_ex->context);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyRight && right_text != NULL) {
|
||||
} else if(event->key == InputKeyRight && right_text_present) {
|
||||
dialog_ex->callback(DialogExPressRight, dialog_ex->context);
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(event->type == InputTypeRelease && dialog_ex->enable_extended_events) {
|
||||
if(event->key == InputKeyLeft && left_text != NULL) {
|
||||
if(event->key == InputKeyLeft && left_text_present) {
|
||||
dialog_ex->callback(DialogExReleaseLeft, dialog_ex->context);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyOk && center_text != NULL) {
|
||||
} else if(event->key == InputKeyOk && center_text_present) {
|
||||
dialog_ex->callback(DialogExReleaseCenter, dialog_ex->context);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyRight && right_text != NULL) {
|
||||
} else if(event->key == InputKeyRight && right_text_present) {
|
||||
dialog_ex->callback(DialogExReleaseRight, dialog_ex->context);
|
||||
consumed = true;
|
||||
}
|
||||
|
@ -154,13 +153,13 @@ DialogEx* dialog_ex_alloc(void) {
|
|||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{
|
||||
model->header.text = NULL;
|
||||
model->header.text = furi_string_alloc();
|
||||
model->header.x = 0;
|
||||
model->header.y = 0;
|
||||
model->header.horizontal = AlignLeft;
|
||||
model->header.vertical = AlignBottom;
|
||||
|
||||
model->text.text = NULL;
|
||||
model->text.text = furi_string_alloc();
|
||||
model->text.x = 0;
|
||||
model->text.y = 0;
|
||||
model->text.horizontal = AlignLeft;
|
||||
|
@ -170,17 +169,28 @@ DialogEx* dialog_ex_alloc(void) {
|
|||
model->icon.y = 0;
|
||||
model->icon.icon = NULL;
|
||||
|
||||
model->left_text = NULL;
|
||||
model->center_text = NULL;
|
||||
model->right_text = NULL;
|
||||
model->left_text = furi_string_alloc();
|
||||
model->center_text = furi_string_alloc();
|
||||
model->right_text = furi_string_alloc();
|
||||
},
|
||||
true);
|
||||
false);
|
||||
dialog_ex->enable_extended_events = false;
|
||||
return dialog_ex;
|
||||
}
|
||||
|
||||
void dialog_ex_free(DialogEx* dialog_ex) {
|
||||
furi_check(dialog_ex);
|
||||
with_view_model(
|
||||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{
|
||||
furi_string_free(model->header.text);
|
||||
furi_string_free(model->text.text);
|
||||
furi_string_free(model->left_text);
|
||||
furi_string_free(model->center_text);
|
||||
furi_string_free(model->right_text);
|
||||
},
|
||||
false);
|
||||
view_free(dialog_ex->view);
|
||||
free(dialog_ex);
|
||||
}
|
||||
|
@ -212,7 +222,7 @@ void dialog_ex_set_header(
|
|||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{
|
||||
model->header.text = text;
|
||||
furi_string_set(model->header.text, text);
|
||||
model->header.x = x;
|
||||
model->header.y = y;
|
||||
model->header.horizontal = horizontal;
|
||||
|
@ -233,7 +243,7 @@ void dialog_ex_set_text(
|
|||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{
|
||||
model->text.text = text;
|
||||
furi_string_set(model->text.text, text);
|
||||
model->text.x = x;
|
||||
model->text.y = y;
|
||||
model->text.horizontal = horizontal;
|
||||
|
@ -257,34 +267,41 @@ void dialog_ex_set_icon(DialogEx* dialog_ex, uint8_t x, uint8_t y, const Icon* i
|
|||
|
||||
void dialog_ex_set_left_button_text(DialogEx* dialog_ex, const char* text) {
|
||||
furi_check(dialog_ex);
|
||||
with_view_model(dialog_ex->view, DialogExModel * model, { model->left_text = text; }, true);
|
||||
with_view_model(
|
||||
dialog_ex->view, DialogExModel * model, { furi_string_set(model->left_text, text); }, true);
|
||||
}
|
||||
|
||||
void dialog_ex_set_center_button_text(DialogEx* dialog_ex, const char* text) {
|
||||
furi_check(dialog_ex);
|
||||
with_view_model(dialog_ex->view, DialogExModel * model, { model->center_text = text; }, true);
|
||||
with_view_model(
|
||||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{ furi_string_set(model->center_text, text); },
|
||||
true);
|
||||
}
|
||||
|
||||
void dialog_ex_set_right_button_text(DialogEx* dialog_ex, const char* text) {
|
||||
furi_check(dialog_ex);
|
||||
with_view_model(dialog_ex->view, DialogExModel * model, { model->right_text = text; }, true);
|
||||
with_view_model(
|
||||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{ furi_string_set(model->right_text, text); },
|
||||
true);
|
||||
}
|
||||
|
||||
void dialog_ex_reset(DialogEx* dialog_ex) {
|
||||
furi_check(dialog_ex);
|
||||
TextElement clean_text_el = {
|
||||
.text = NULL, .x = 0, .y = 0, .horizontal = AlignLeft, .vertical = AlignLeft};
|
||||
IconElement clean_icon_el = {.icon = NULL, .x = 0, .y = 0};
|
||||
with_view_model(
|
||||
dialog_ex->view,
|
||||
DialogExModel * model,
|
||||
{
|
||||
model->header = clean_text_el;
|
||||
model->text = clean_text_el;
|
||||
model->icon = clean_icon_el;
|
||||
model->left_text = NULL;
|
||||
model->center_text = NULL;
|
||||
model->right_text = NULL;
|
||||
model->icon.icon = NULL;
|
||||
furi_string_reset(model->header.text);
|
||||
furi_string_reset(model->text.text);
|
||||
|
||||
furi_string_reset(model->left_text);
|
||||
furi_string_reset(model->center_text);
|
||||
furi_string_reset(model->right_text);
|
||||
},
|
||||
true);
|
||||
dialog_ex->context = NULL;
|
||||
|
|
Loading…
Reference in a new issue