GUI: GuiApi isolation

This commit is contained in:
Aleksandr Kutuzov 2020-10-16 18:25:06 +03:00
parent 491343b683
commit f0800e579f
5 changed files with 22 additions and 29 deletions

View File

@ -15,6 +15,7 @@
ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST); ARRAY_DEF(WidgetArray, Widget*, M_PTR_OPLIST);
struct Gui { struct Gui {
GuiApi api;
GuiEvent* event; GuiEvent* event;
CanvasApi* canvas_api; CanvasApi* canvas_api;
WidgetArray_t widgets_status_bar; WidgetArray_t widgets_status_bar;
@ -25,39 +26,37 @@ struct Gui {
void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) { void gui_add_widget(GuiApi* gui_api, Widget* widget, WidgetLayer layer) {
assert(gui_api); assert(gui_api);
assert(gui_api->gui); assert(widget);
Gui* gui = (Gui*)gui_api;
// TODO add mutex on widget array // TODO add mutex on widget array
WidgetArray_t* widget_array = NULL; WidgetArray_t* widget_array = NULL;
switch(layer) { switch(layer) {
case WidgetLayerStatusBar: case WidgetLayerStatusBar:
widget_array = &gui_api->gui->widgets_status_bar; widget_array = &gui->widgets_status_bar;
break; break;
case WidgetLayerMain: case WidgetLayerMain:
widget_array = &gui_api->gui->widgets; widget_array = &gui->widgets;
break; break;
case WidgetLayerFullscreen: case WidgetLayerFullscreen:
widget_array = &gui_api->gui->widgets_fs; widget_array = &gui->widgets_fs;
break; break;
case WidgetLayerDialog: case WidgetLayerDialog:
widget_array = &gui_api->gui->widgets_dialog; widget_array = &gui->widgets_dialog;
break; break;
default: default:
break; break;
} }
assert(widget);
assert(widget_array); assert(widget_array);
gui_event_lock(gui_api->gui->event); gui_event_lock(gui->event);
WidgetArray_push_back((struct WidgetArray_s*)widget_array, widget); WidgetArray_push_back(*widget_array, widget);
widget_gui_set(widget, gui_api->gui); widget_gui_set(widget, gui);
gui_event_unlock(gui_api->gui->event); gui_event_unlock(gui->event);
gui_update(gui_api->gui); gui_update(gui);
} }
void gui_update(Gui* gui) { void gui_update(Gui* gui) {
@ -133,6 +132,7 @@ void gui_input(Gui* gui, InputEvent* input_event) {
Gui* gui_alloc() { Gui* gui_alloc() {
Gui* gui = furi_alloc(sizeof(Gui)); Gui* gui = furi_alloc(sizeof(Gui));
// Initialize widget arrays // Initialize widget arrays
WidgetArray_init(gui->widgets_status_bar); WidgetArray_init(gui->widgets_status_bar);
WidgetArray_init(gui->widgets); WidgetArray_init(gui->widgets);
@ -143,22 +143,17 @@ Gui* gui_alloc() {
gui->event = gui_event_alloc(); gui->event = gui_event_alloc();
// Drawing canvas api // Drawing canvas api
gui->canvas_api = canvas_api_init(); gui->canvas_api = canvas_api_init();
gui->api.add_widget = gui_add_widget;
return gui; return gui;
} }
void gui_task(void* p) { void gui_task(void* p) {
Gui* gui = gui_alloc(); Gui* gui = gui_alloc();
GuiApi gui_api = {
.add_widget = gui_add_widget,
.gui = gui,
};
// Create FURI record // Create FURI record
if(!furi_create("gui", &gui_api)) { if(!furi_create("gui", gui)) {
printf("[gui_task] cannot create the gui record\n"); printf("[gui_task] cannot create the gui record\n");
furiac_exit(NULL); furiac_exit(NULL);
} }

View File

@ -11,12 +11,8 @@ typedef enum {
} WidgetLayer; } WidgetLayer;
typedef struct Widget Widget; typedef struct Widget Widget;
typedef struct Gui Gui;
struct _GuiApi; typedef struct GuiApi GuiApi;
typedef struct _GuiApi GuiApi; struct GuiApi {
struct _GuiApi {
void (*add_widget)(GuiApi* gui_api, Widget* widget, WidgetLayer layer); void (*add_widget)(GuiApi* gui_api, Widget* widget, WidgetLayer layer);
Gui* gui;
}; };

View File

@ -1,3 +1,5 @@
#pragma once #pragma once
typedef struct Gui Gui;
void gui_update(Gui* gui); void gui_update(Gui* gui);

View File

@ -10,7 +10,7 @@
// TODO add mutex to widget ops // TODO add mutex to widget ops
struct Widget { struct Widget {
void* gui; Gui* gui;
bool is_enabled; bool is_enabled;
WidgetDrawCallback draw_callback; WidgetDrawCallback draw_callback;
void* draw_callback_context; void* draw_callback_context;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "gui.h" #include "gui_i.h"
void widget_gui_set(Widget* widget, Gui* gui); void widget_gui_set(Widget* widget, Gui* gui);