widget_element_button.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "widget_element_i.h"
  2. #include <gui/elements.h>
  3. #include <m-string.h>
  4. typedef struct {
  5. GuiButtonType button_type;
  6. string_t text;
  7. ButtonCallback callback;
  8. void* context;
  9. } GuiButtonModel;
  10. static void gui_button_draw(Canvas* canvas, WidgetElement* element) {
  11. furi_assert(canvas);
  12. furi_assert(element);
  13. GuiButtonModel* model = element->model;
  14. canvas_set_color(canvas, ColorBlack);
  15. canvas_set_font(canvas, FontSecondary);
  16. if(model->button_type == GuiButtonTypeLeft) {
  17. elements_button_left(canvas, string_get_cstr(model->text));
  18. } else if(model->button_type == GuiButtonTypeRight) {
  19. elements_button_right(canvas, string_get_cstr(model->text));
  20. } else if(model->button_type == GuiButtonTypeCenter) {
  21. elements_button_center(canvas, string_get_cstr(model->text));
  22. }
  23. }
  24. static bool gui_button_input(InputEvent* event, WidgetElement* element) {
  25. GuiButtonModel* model = element->model;
  26. bool consumed = false;
  27. if(model->callback == NULL) return consumed;
  28. if(event->key == InputKeyOk && event->type == InputTypePress &&
  29. model->button_type == GuiButtonTypeCenter) {
  30. model->callback(GuiButtonTypeCenterPress, model->context);
  31. consumed = true;
  32. } else if(
  33. event->key == InputKeyOk && event->type == InputTypeRelease &&
  34. model->button_type == GuiButtonTypeCenter) {
  35. model->callback(GuiButtonTypeCenterRelease, model->context);
  36. consumed = true;
  37. } else if(event->type == InputTypeShort) {
  38. if((model->button_type == GuiButtonTypeLeft) && (event->key == InputKeyLeft)) {
  39. model->callback(model->button_type, model->context);
  40. consumed = true;
  41. } else if((model->button_type == GuiButtonTypeRight) && (event->key == InputKeyRight)) {
  42. model->callback(model->button_type, model->context);
  43. consumed = true;
  44. } else if((model->button_type == GuiButtonTypeCenter) && (event->key == InputKeyOk)) {
  45. model->callback(model->button_type, model->context);
  46. consumed = true;
  47. }
  48. }
  49. return consumed;
  50. }
  51. static void gui_button_free(WidgetElement* gui_button) {
  52. furi_assert(gui_button);
  53. GuiButtonModel* model = gui_button->model;
  54. string_clear(model->text);
  55. free(gui_button->model);
  56. free(gui_button);
  57. }
  58. WidgetElement* widget_element_button_create(
  59. GuiButtonType button_type,
  60. const char* text,
  61. ButtonCallback callback,
  62. void* context) {
  63. // Allocate and init model
  64. GuiButtonModel* model = furi_alloc(sizeof(GuiButtonModel));
  65. model->button_type = button_type;
  66. model->callback = callback;
  67. model->context = context;
  68. string_init_set_str(model->text, text);
  69. // Allocate and init Element
  70. WidgetElement* gui_button = furi_alloc(sizeof(WidgetElement));
  71. gui_button->parent = NULL;
  72. gui_button->input = gui_button_input;
  73. gui_button->draw = gui_button_draw;
  74. gui_button->free = gui_button_free;
  75. gui_button->model = model;
  76. return gui_button;
  77. }