2021-07-23 16:09:34 +03:00
|
|
|
#include "widget_element_i.h"
|
2021-07-22 09:05:07 +03:00
|
|
|
#include <gui/elements.h>
|
|
|
|
#include <m-string.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GuiButtonType button_type;
|
|
|
|
string_t text;
|
|
|
|
ButtonCallback callback;
|
|
|
|
void* context;
|
|
|
|
} GuiButtonModel;
|
|
|
|
|
2021-07-23 16:09:34 +03:00
|
|
|
static void gui_button_draw(Canvas* canvas, WidgetElement* element) {
|
2021-07-22 09:05:07 +03: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) {
|
|
|
|
elements_button_left(canvas, string_get_cstr(model->text));
|
|
|
|
} else if(model->button_type == GuiButtonTypeRight) {
|
|
|
|
elements_button_right(canvas, string_get_cstr(model->text));
|
|
|
|
} else if(model->button_type == GuiButtonTypeCenter) {
|
|
|
|
elements_button_center(canvas, string_get_cstr(model->text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:09:34 +03:00
|
|
|
static bool gui_button_input(InputEvent* event, WidgetElement* element) {
|
2021-07-22 09:05:07 +03:00
|
|
|
GuiButtonModel* model = element->model;
|
|
|
|
bool consumed = false;
|
|
|
|
|
2021-09-10 03:29:57 +03:00
|
|
|
if(model->callback == NULL) return consumed;
|
|
|
|
|
2021-09-15 18:24:19 +03:00
|
|
|
if((model->button_type == GuiButtonTypeLeft) && (event->key == InputKeyLeft)) {
|
|
|
|
model->callback(model->button_type, event->type, model->context);
|
2021-09-10 03:29:57 +03:00
|
|
|
consumed = true;
|
2021-09-15 18:24:19 +03: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 03:29:57 +03:00
|
|
|
consumed = true;
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:09:34 +03:00
|
|
|
static void gui_button_free(WidgetElement* gui_button) {
|
2021-07-22 09:05:07 +03:00
|
|
|
furi_assert(gui_button);
|
|
|
|
|
|
|
|
GuiButtonModel* model = gui_button->model;
|
|
|
|
string_clear(model->text);
|
|
|
|
free(gui_button->model);
|
|
|
|
free(gui_button);
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:09:34 +03:00
|
|
|
WidgetElement* widget_element_button_create(
|
2021-07-22 09:05:07 +03:00
|
|
|
GuiButtonType button_type,
|
|
|
|
const char* text,
|
|
|
|
ButtonCallback callback,
|
|
|
|
void* context) {
|
|
|
|
// Allocate and init model
|
2022-02-18 22:53:46 +03:00
|
|
|
GuiButtonModel* model = malloc(sizeof(GuiButtonModel));
|
2021-07-22 09:05:07 +03:00
|
|
|
model->button_type = button_type;
|
|
|
|
model->callback = callback;
|
|
|
|
model->context = context;
|
|
|
|
string_init_set_str(model->text, text);
|
|
|
|
|
|
|
|
// Allocate and init Element
|
2022-02-18 22:53:46 +03:00
|
|
|
WidgetElement* gui_button = malloc(sizeof(WidgetElement));
|
2021-07-22 09:05:07 +03: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;
|
|
|
|
}
|