2021-07-23 13:09:34 +00:00
|
|
|
#include "widget_element_i.h"
|
2021-07-22 06:05:07 +00:00
|
|
|
#include <gui/elements.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GuiButtonType button_type;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* text;
|
2021-07-22 06:05:07 +00:00
|
|
|
ButtonCallback callback;
|
|
|
|
void* context;
|
|
|
|
} GuiButtonModel;
|
|
|
|
|
2021-07-23 13:09:34 +00:00
|
|
|
static void gui_button_draw(Canvas* canvas, WidgetElement* element) {
|
2021-07-22 06:05:07 +00:00
|
|
|
furi_assert(canvas);
|
|
|
|
furi_assert(element);
|
|
|
|
GuiButtonModel* model = element->model;
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
|
|
|
|
if(model->button_type == GuiButtonTypeLeft) {
|
2022-10-05 15:15:23 +00:00
|
|
|
elements_button_left(canvas, furi_string_get_cstr(model->text));
|
2021-07-22 06:05:07 +00:00
|
|
|
} else if(model->button_type == GuiButtonTypeRight) {
|
2022-10-05 15:15:23 +00:00
|
|
|
elements_button_right(canvas, furi_string_get_cstr(model->text));
|
2021-07-22 06:05:07 +00:00
|
|
|
} else if(model->button_type == GuiButtonTypeCenter) {
|
2022-10-05 15:15:23 +00:00
|
|
|
elements_button_center(canvas, furi_string_get_cstr(model->text));
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 13:09:34 +00:00
|
|
|
static bool gui_button_input(InputEvent* event, WidgetElement* element) {
|
2021-07-22 06:05:07 +00:00
|
|
|
GuiButtonModel* model = element->model;
|
|
|
|
bool consumed = false;
|
|
|
|
|
2021-09-10 00:29:57 +00:00
|
|
|
if(model->callback == NULL) return consumed;
|
|
|
|
|
2021-09-15 15:24:19 +00:00
|
|
|
if((model->button_type == GuiButtonTypeLeft) && (event->key == InputKeyLeft)) {
|
|
|
|
model->callback(model->button_type, event->type, model->context);
|
2021-09-10 00:29:57 +00:00
|
|
|
consumed = true;
|
2021-09-15 15:24:19 +00:00
|
|
|
} else if((model->button_type == GuiButtonTypeRight) && (event->key == InputKeyRight)) {
|
|
|
|
model->callback(model->button_type, event->type, model->context);
|
|
|
|
consumed = true;
|
|
|
|
} else if((model->button_type == GuiButtonTypeCenter) && (event->key == InputKeyOk)) {
|
|
|
|
model->callback(model->button_type, event->type, model->context);
|
2021-09-10 00:29:57 +00:00
|
|
|
consumed = true;
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2021-07-23 13:09:34 +00:00
|
|
|
static void gui_button_free(WidgetElement* gui_button) {
|
2021-07-22 06:05:07 +00:00
|
|
|
furi_assert(gui_button);
|
|
|
|
|
|
|
|
GuiButtonModel* model = gui_button->model;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(model->text);
|
2021-07-22 06:05:07 +00:00
|
|
|
free(gui_button->model);
|
|
|
|
free(gui_button);
|
|
|
|
}
|
|
|
|
|
2021-07-23 13:09:34 +00:00
|
|
|
WidgetElement* widget_element_button_create(
|
2021-07-22 06:05:07 +00:00
|
|
|
GuiButtonType button_type,
|
|
|
|
const char* text,
|
|
|
|
ButtonCallback callback,
|
|
|
|
void* context) {
|
|
|
|
// Allocate and init model
|
2022-11-05 15:07:24 +00:00
|
|
|
GuiButtonModel* model = malloc(sizeof(GuiButtonModel)); //-V773
|
2021-07-22 06:05:07 +00:00
|
|
|
model->button_type = button_type;
|
|
|
|
model->callback = callback;
|
|
|
|
model->context = context;
|
2022-10-05 15:15:23 +00:00
|
|
|
model->text = furi_string_alloc_set(text);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
|
|
|
// Allocate and init Element
|
2022-02-18 19:53:46 +00:00
|
|
|
WidgetElement* gui_button = malloc(sizeof(WidgetElement));
|
2021-07-22 06:05:07 +00:00
|
|
|
gui_button->parent = NULL;
|
|
|
|
gui_button->input = gui_button_input;
|
|
|
|
gui_button->draw = gui_button_draw;
|
|
|
|
gui_button->free = gui_button_free;
|
|
|
|
gui_button->model = model;
|
|
|
|
|
|
|
|
return gui_button;
|
|
|
|
}
|