flipperzero-firmware/applications/gui/modules/dialog_ex.c
SG 274c12fc56
[FL-2274] Inventing streams and moving FFF to them (#981)
* Streams: string stream
* String stream: updated insert/delete api
* Streams: generic stream interface and string stream implementation
* Streams: helpers for insert and delete_and_insert
* FFF: now compatible with streams
* MinUnit: introduced tests with arguments
* FFF: stream access violation
* Streams: copy data between streams
* Streams: file stream
* FFF: documentation
* FFStream: documentation
* FFF: alloc as file
* MinUnit: support for nested tests
* Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout.
* FFF: simplified file open function
* Streams: unit tests
* FFF: tests
* Streams: declare cache_size constant as define, to allow variable modified arrays
* FFF: lib moved to a separate folder
* iButton: new FFF
* RFID: new FFF
* Animations: new FFF
* IR: new FFF
* NFC: new FFF
* Flipper file format: delete lib
* U2F: new FFF
* Subghz: new FFF and streams
* Streams: read line
* Streams: split
* FuriCore: implement memset with extra asserts
* FuriCore: implement extra heap asserts without inventing memset
* Scene manager: protected access to the scene id stack with a size check
* NFC worker: dirty fix for issue where hal_nfc was busy on app start
* Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc.
* FuriCore: cleanup memmgr code.
* Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console.
* Memmgr: added ability to track allocations and deallocations through console.
* FFStream: some speedup
* Streams, FF: minor fixes
* Tests: restore
* File stream: a slightly more thread-safe version of file_stream_delete_and_insert

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 22:53:46 +03:00

265 lines
7.2 KiB
C
Executable File

#include "dialog_ex.h"
#include <gui/elements.h>
#include <furi.h>
struct DialogEx {
View* view;
void* context;
DialogExResultCallback callback;
};
typedef struct {
const char* text;
uint8_t x;
uint8_t y;
Align horizontal;
Align vertical;
} TextElement;
typedef struct {
int8_t x;
int8_t y;
const Icon* icon;
} IconElement;
typedef struct {
TextElement header;
TextElement text;
IconElement icon;
const char* left_text;
const char* center_text;
const char* 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) {
canvas_draw_icon(canvas, model->icon.x, model->icon.y, model->icon.icon);
}
// Draw header
if(model->header.text != NULL) {
canvas_set_font(canvas, FontPrimary);
elements_multiline_text_aligned(
canvas,
model->header.x,
model->header.y,
model->header.horizontal,
model->header.vertical,
model->header.text);
}
// Draw text
if(model->text.text != NULL) {
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas,
model->text.x,
model->text.y,
model->text.horizontal,
model->text.vertical,
model->text.text);
}
// Draw buttons
if(model->left_text != NULL) {
elements_button_left(canvas, model->left_text);
}
if(model->center_text != NULL) {
elements_button_center(canvas, model->center_text);
}
if(model->right_text != NULL) {
elements_button_right(canvas, 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;
with_view_model(
dialog_ex->view, (DialogExModel * model) {
left_text = model->left_text;
center_text = model->center_text;
right_text = model->right_text;
return true;
});
// Process key presses only
if(event->type == InputTypeShort && dialog_ex->callback) {
if(event->key == InputKeyLeft && left_text != NULL) {
dialog_ex->callback(DialogExResultLeft, dialog_ex->context);
consumed = true;
} else if(event->key == InputKeyOk && center_text != NULL) {
dialog_ex->callback(DialogExResultCenter, dialog_ex->context);
consumed = true;
} else if(event->key == InputKeyRight && right_text != NULL) {
dialog_ex->callback(DialogExResultRight, dialog_ex->context);
consumed = true;
}
}
return consumed;
}
DialogEx* dialog_ex_alloc() {
DialogEx* dialog_ex = malloc(sizeof(DialogEx));
dialog_ex->view = view_alloc();
view_set_context(dialog_ex->view, dialog_ex);
view_allocate_model(dialog_ex->view, ViewModelTypeLockFree, sizeof(DialogExModel));
view_set_draw_callback(dialog_ex->view, dialog_ex_view_draw_callback);
view_set_input_callback(dialog_ex->view, dialog_ex_view_input_callback);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->header.text = NULL;
model->header.x = 0;
model->header.y = 0;
model->header.horizontal = AlignLeft;
model->header.vertical = AlignBottom;
model->text.text = NULL;
model->text.x = 0;
model->text.y = 0;
model->text.horizontal = AlignLeft;
model->text.vertical = AlignBottom;
model->icon.x = 0;
model->icon.y = 0;
model->icon.icon = NULL;
model->left_text = NULL;
model->center_text = NULL;
model->right_text = NULL;
return true;
});
return dialog_ex;
}
void dialog_ex_free(DialogEx* dialog_ex) {
furi_assert(dialog_ex);
view_free(dialog_ex->view);
free(dialog_ex);
}
View* dialog_ex_get_view(DialogEx* dialog_ex) {
furi_assert(dialog_ex);
return dialog_ex->view;
}
void dialog_ex_set_result_callback(DialogEx* dialog_ex, DialogExResultCallback callback) {
furi_assert(dialog_ex);
dialog_ex->callback = callback;
}
void dialog_ex_set_context(DialogEx* dialog_ex, void* context) {
furi_assert(dialog_ex);
dialog_ex->context = context;
}
void dialog_ex_set_header(
DialogEx* dialog_ex,
const char* text,
uint8_t x,
uint8_t y,
Align horizontal,
Align vertical) {
furi_assert(dialog_ex);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->header.text = text;
model->header.x = x;
model->header.y = y;
model->header.horizontal = horizontal;
model->header.vertical = vertical;
return true;
});
}
void dialog_ex_set_text(
DialogEx* dialog_ex,
const char* text,
uint8_t x,
uint8_t y,
Align horizontal,
Align vertical) {
furi_assert(dialog_ex);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->text.text = text;
model->text.x = x;
model->text.y = y;
model->text.horizontal = horizontal;
model->text.vertical = vertical;
return true;
});
}
void dialog_ex_set_icon(DialogEx* dialog_ex, uint8_t x, uint8_t y, const Icon* icon) {
furi_assert(dialog_ex);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->icon.x = x;
model->icon.y = y;
model->icon.icon = icon;
return true;
});
}
void dialog_ex_set_left_button_text(DialogEx* dialog_ex, const char* text) {
furi_assert(dialog_ex);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->left_text = text;
return true;
});
}
void dialog_ex_set_center_button_text(DialogEx* dialog_ex, const char* text) {
furi_assert(dialog_ex);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->center_text = text;
return true;
});
}
void dialog_ex_set_right_button_text(DialogEx* dialog_ex, const char* text) {
furi_assert(dialog_ex);
with_view_model(
dialog_ex->view, (DialogExModel * model) {
model->right_text = text;
return true;
});
}
void dialog_ex_reset(DialogEx* dialog_ex) {
furi_assert(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;
return true;
});
dialog_ex->context = NULL;
dialog_ex->callback = NULL;
}