From 19ef348c80e027f4c9ea304bc96349c195e97fbb Mon Sep 17 00:00:00 2001 From: aanper Date: Thu, 15 Oct 2020 11:32:48 +0300 Subject: [PATCH] [wip] change gui/widget api --- applications/gui/gui.c | 72 ++++++++++++++++++--------------------- applications/gui/gui.h | 20 +++++++---- applications/gui/widget.c | 9 ----- applications/gui/widget.h | 10 +++++- 4 files changed, 55 insertions(+), 56 deletions(-) diff --git a/applications/gui/gui.c b/applications/gui/gui.c index ecc314d73..6724b97d2 100644 --- a/applications/gui/gui.c +++ b/applications/gui/gui.c @@ -22,48 +22,32 @@ struct GUI { WidgetArray_t widgets_dialog; }; -void gui_widget_status_bar_add(GUI* gui, Widget* widget) { +void gui_add_widget(GUI* gui, Widget* widget, WidgetLayer layer) { + WidgetArray_t* widget_array = NULL; + + switch(layer) { + case WidgetLayerStatusBar: + widget_array = &gui->widgets_status_bar; + break; + case WidgetLayerMain: + widget_array = &gui->widgets; + break; + case WidgetLayerFullscreen: + widget_array = &gui->widgets_fs; + break; + case WidgetLayerDialog: + widget_array = &gui->widgets_dialog; + break; + + default: + } + assert(gui); assert(widget); + assert(widget_array); gui_event_lock(gui->event); - WidgetArray_push_back(gui->widgets_status_bar, widget); - widget_gui_set(widget, gui); - gui_event_unlock(gui->event); - - gui_update(gui); -} - -void gui_widget_add(GUI* gui, Widget* widget) { - assert(gui); - assert(widget); - - gui_event_lock(gui->event); - WidgetArray_push_back(gui->widgets, widget); - widget_gui_set(widget, gui); - gui_event_unlock(gui->event); - - gui_update(gui); -} - -void gui_widget_fs_add(GUI* gui, Widget* widget) { - assert(gui); - assert(widget); - - gui_event_lock(gui->event); - WidgetArray_push_back(gui->widgets_fs, widget); - widget_gui_set(widget, gui); - gui_event_unlock(gui->event); - - gui_update(gui); -} - -void gui_widget_dialog_add(GUI* gui, Widget* widget) { - assert(gui); - assert(widget); - - gui_event_lock(gui->event); - WidgetArray_push_back(gui->widgets_dialog, widget); + WidgetArray_push_back(widget_array, widget); widget_gui_set(widget, gui); gui_event_unlock(gui->event); @@ -148,8 +132,10 @@ GUI* gui_alloc() { WidgetArray_init(gui->widgets); WidgetArray_init(gui->widgets_fs); WidgetArray_init(gui->widgets_dialog); + // Event dispatcher gui->event = gui_event_alloc(); + // Drawing canvas gui->canvas = canvas_alloc(); @@ -158,12 +144,20 @@ GUI* gui_alloc() { void gui_task(void* p) { GUI* gui = gui_alloc(); + + GuiApi gui_api = { + .add_widget = gui_add_widget, + .gui = gui, + }; + // Create FURI record - if(!furi_create_deprecated("gui", gui, sizeof(gui))) { + if(!furi_create("gui", &gui_api)) { printf("[gui_task] cannot create the gui record\n"); furiac_exit(NULL); } + furiac_ready(); + // Forever dispatch while(1) { GUIMessage message = gui_event_message_next(gui->event); diff --git a/applications/gui/gui.h b/applications/gui/gui.h index a95b802cc..b09e3bff2 100644 --- a/applications/gui/gui.h +++ b/applications/gui/gui.h @@ -1,12 +1,18 @@ #pragma once +#include "widget.h" + +typedef enum { + WidgetLayerStatusBar, + WidgetLayerMain, + WidgetLayerFullscreen, + WidgetLayerDialog +} WidgetLayer; + typedef struct Widget Widget; typedef struct GUI GUI; -void gui_widget_status_bar_add(GUI* gui, Widget* widget); - -void gui_widget_add(GUI* gui, Widget* widget); - -void gui_widget_fs_add(GUI* gui, Widget* widget); - -void gui_widget_dialog_add(GUI* gui, Widget* widget); +typedef struct { + void (*add_widget)(GUI* gui, Widget* widget, WidgetLayer layer); + GUI* gui; +} GuiApi; diff --git a/applications/gui/widget.c b/applications/gui/widget.c index bff2a4b92..1b6b6345a 100644 --- a/applications/gui/widget.c +++ b/applications/gui/widget.c @@ -7,15 +7,6 @@ #include "gui.h" #include "gui_i.h" -struct Widget { - void* gui; - bool is_enabled; - WidgetDrawCallback draw_callback; - void* draw_callback_context; - WidgetInputCallback input_callback; - void* input_callback_context; -}; - Widget* widget_alloc(WidgetDrawCallback callback, void* callback_context) { Widget* widget = furi_alloc(sizeof(Widget)); widget->is_enabled = true; diff --git a/applications/gui/widget.h b/applications/gui/widget.h index b29ade452..340e5310a 100644 --- a/applications/gui/widget.h +++ b/applications/gui/widget.h @@ -4,7 +4,15 @@ typedef struct GUI GUI; typedef struct Canvas Canvas; -typedef struct Widget Widget; + +typedef struct { + void* gui; + bool is_enabled; + WidgetDrawCallback draw_callback; + void* draw_callback_context; + WidgetInputCallback input_callback; + void* input_callback_context; +} Widget; typedef void (*WidgetDrawCallback)(Canvas* canvas, void* context); typedef void (*WidgetInputCallback)(InputEvent* event, void* context);