2021-07-23 16:09:34 +03:00
|
|
|
#include "widget.h"
|
2022-09-14 19:11:38 +03:00
|
|
|
#include "widget_elements/widget_element_i.h"
|
2023-02-07 19:33:05 +03:00
|
|
|
#include <furi.h>
|
|
|
|
#include <m-array.h>
|
2021-07-23 16:09:34 +03:00
|
|
|
|
2024-06-03 16:43:23 +03:00
|
|
|
ARRAY_DEF(ElementArray, WidgetElement*, M_PTR_OPLIST); // NOLINT
|
2021-07-23 16:09:34 +03:00
|
|
|
|
|
|
|
struct Widget {
|
|
|
|
View* view;
|
|
|
|
void* context;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ElementArray_t element;
|
|
|
|
} GuiWidgetModel;
|
|
|
|
|
|
|
|
static void gui_widget_view_draw_callback(Canvas* canvas, void* _model) {
|
|
|
|
GuiWidgetModel* model = _model;
|
|
|
|
canvas_clear(canvas);
|
|
|
|
|
|
|
|
// Draw all elements
|
|
|
|
ElementArray_it_t it;
|
|
|
|
ElementArray_it(it, model->element);
|
|
|
|
while(!ElementArray_end_p(it)) {
|
|
|
|
WidgetElement* element = *ElementArray_ref(it);
|
|
|
|
if(element->draw != NULL) {
|
|
|
|
element->draw(canvas, element);
|
|
|
|
}
|
|
|
|
ElementArray_next(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool gui_widget_view_input_callback(InputEvent* event, void* context) {
|
|
|
|
Widget* widget = context;
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
// Call all Widget Elements input handlers
|
|
|
|
with_view_model(
|
2022-10-08 20:38:29 +03:00
|
|
|
widget->view,
|
|
|
|
GuiWidgetModel * model,
|
|
|
|
{
|
2021-07-23 16:09:34 +03:00
|
|
|
ElementArray_it_t it;
|
|
|
|
ElementArray_it(it, model->element);
|
|
|
|
while(!ElementArray_end_p(it)) {
|
|
|
|
WidgetElement* element = *ElementArray_ref(it);
|
|
|
|
if(element->input != NULL) {
|
|
|
|
consumed |= element->input(event, element);
|
|
|
|
}
|
|
|
|
ElementArray_next(it);
|
|
|
|
}
|
2022-10-08 20:38:29 +03:00
|
|
|
},
|
|
|
|
true);
|
2021-07-23 16:09:34 +03:00
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:43:52 +03:00
|
|
|
Widget* widget_alloc(void) {
|
2022-02-18 22:53:46 +03:00
|
|
|
Widget* widget = malloc(sizeof(Widget));
|
2021-07-23 16:09:34 +03:00
|
|
|
widget->view = view_alloc();
|
|
|
|
view_set_context(widget->view, widget);
|
|
|
|
view_allocate_model(widget->view, ViewModelTypeLocking, sizeof(GuiWidgetModel));
|
|
|
|
view_set_draw_callback(widget->view, gui_widget_view_draw_callback);
|
|
|
|
view_set_input_callback(widget->view, gui_widget_view_input_callback);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 20:38:29 +03:00
|
|
|
widget->view, GuiWidgetModel * model, { ElementArray_init(model->element); }, true);
|
2021-07-23 16:09:34 +03:00
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:59:28 +03:00
|
|
|
void widget_reset(Widget* widget) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-07-23 16:09:34 +03:00
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 20:38:29 +03:00
|
|
|
widget->view,
|
|
|
|
GuiWidgetModel * model,
|
|
|
|
{
|
2021-07-23 16:09:34 +03:00
|
|
|
ElementArray_it_t it;
|
|
|
|
ElementArray_it(it, model->element);
|
|
|
|
while(!ElementArray_end_p(it)) {
|
|
|
|
WidgetElement* element = *ElementArray_ref(it);
|
|
|
|
furi_assert(element->free);
|
|
|
|
element->free(element);
|
|
|
|
ElementArray_next(it);
|
|
|
|
}
|
2021-11-15 20:09:40 +03:00
|
|
|
ElementArray_reset(model->element);
|
2022-10-08 20:38:29 +03:00
|
|
|
},
|
|
|
|
true);
|
2021-07-23 16:09:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void widget_free(Widget* widget) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-07-23 16:09:34 +03:00
|
|
|
// Free all elements
|
2022-02-02 22:59:28 +03:00
|
|
|
widget_reset(widget);
|
2021-07-23 16:09:34 +03:00
|
|
|
// Free elements container
|
|
|
|
with_view_model(
|
2022-10-08 20:38:29 +03:00
|
|
|
widget->view, GuiWidgetModel * model, { ElementArray_clear(model->element); }, true);
|
2021-07-23 16:09:34 +03:00
|
|
|
|
|
|
|
view_free(widget->view);
|
|
|
|
free(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
View* widget_get_view(Widget* widget) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-07-23 16:09:34 +03:00
|
|
|
return widget->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void widget_add_element(Widget* widget, WidgetElement* element) {
|
|
|
|
furi_assert(widget);
|
|
|
|
furi_assert(element);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 20:38:29 +03:00
|
|
|
widget->view,
|
|
|
|
GuiWidgetModel * model,
|
|
|
|
{
|
2021-07-23 16:09:34 +03:00
|
|
|
element->parent = widget;
|
|
|
|
ElementArray_push_back(model->element, element);
|
2022-10-08 20:38:29 +03:00
|
|
|
},
|
|
|
|
true);
|
2021-07-23 16:09:34 +03:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:24:19 +03:00
|
|
|
void widget_add_string_multiline_element(
|
2021-09-10 03:29:57 +03:00
|
|
|
Widget* widget,
|
|
|
|
uint8_t x,
|
|
|
|
uint8_t y,
|
|
|
|
Align horizontal,
|
|
|
|
Align vertical,
|
|
|
|
Font font,
|
|
|
|
const char* text) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-09-15 18:24:19 +03:00
|
|
|
WidgetElement* string_multiline_element =
|
|
|
|
widget_element_string_multiline_create(x, y, horizontal, vertical, font, text);
|
|
|
|
widget_add_element(widget, string_multiline_element);
|
2021-09-10 03:29:57 +03:00
|
|
|
}
|
|
|
|
|
2021-07-23 16:09:34 +03:00
|
|
|
void widget_add_string_element(
|
|
|
|
Widget* widget,
|
|
|
|
uint8_t x,
|
|
|
|
uint8_t y,
|
|
|
|
Align horizontal,
|
|
|
|
Align vertical,
|
|
|
|
Font font,
|
|
|
|
const char* text) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-07-23 16:09:34 +03:00
|
|
|
WidgetElement* string_element =
|
|
|
|
widget_element_string_create(x, y, horizontal, vertical, font, text);
|
|
|
|
widget_add_element(widget, string_element);
|
|
|
|
}
|
|
|
|
|
2021-10-27 21:55:03 +03:00
|
|
|
void widget_add_text_box_element(
|
|
|
|
Widget* widget,
|
|
|
|
uint8_t x,
|
|
|
|
uint8_t y,
|
|
|
|
uint8_t width,
|
|
|
|
uint8_t height,
|
|
|
|
Align horizontal,
|
|
|
|
Align vertical,
|
2022-04-19 22:23:50 +03:00
|
|
|
const char* text,
|
|
|
|
bool strip_to_dots) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2022-04-19 22:23:50 +03:00
|
|
|
WidgetElement* text_box_element = widget_element_text_box_create(
|
|
|
|
x, y, width, height, horizontal, vertical, text, strip_to_dots);
|
2021-10-27 21:55:03 +03:00
|
|
|
widget_add_element(widget, text_box_element);
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:08:13 +03:00
|
|
|
void widget_add_text_scroll_element(
|
|
|
|
Widget* widget,
|
|
|
|
uint8_t x,
|
|
|
|
uint8_t y,
|
|
|
|
uint8_t width,
|
|
|
|
uint8_t height,
|
|
|
|
const char* text) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2022-08-17 18:08:13 +03:00
|
|
|
WidgetElement* text_scroll_element =
|
|
|
|
widget_element_text_scroll_create(x, y, width, height, text);
|
|
|
|
widget_add_element(widget, text_scroll_element);
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:09:34 +03:00
|
|
|
void widget_add_button_element(
|
|
|
|
Widget* widget,
|
|
|
|
GuiButtonType button_type,
|
|
|
|
const char* text,
|
|
|
|
ButtonCallback callback,
|
|
|
|
void* context) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-07-23 16:09:34 +03:00
|
|
|
WidgetElement* button_element =
|
|
|
|
widget_element_button_create(button_type, text, callback, context);
|
|
|
|
widget_add_element(widget, button_element);
|
|
|
|
}
|
|
|
|
|
|
|
|
void widget_add_icon_element(Widget* widget, uint8_t x, uint8_t y, const Icon* icon) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
|
|
|
furi_check(icon);
|
2021-07-23 16:09:34 +03:00
|
|
|
WidgetElement* icon_element = widget_element_icon_create(x, y, icon);
|
|
|
|
widget_add_element(widget, icon_element);
|
|
|
|
}
|
2021-08-20 22:56:43 +03:00
|
|
|
|
|
|
|
void widget_add_frame_element(
|
|
|
|
Widget* widget,
|
|
|
|
uint8_t x,
|
|
|
|
uint8_t y,
|
|
|
|
uint8_t width,
|
|
|
|
uint8_t height,
|
|
|
|
uint8_t radius) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(widget);
|
2021-08-20 22:56:43 +03:00
|
|
|
WidgetElement* frame_element = widget_element_frame_create(x, y, width, height, radius);
|
|
|
|
widget_add_element(widget, frame_element);
|
|
|
|
}
|