From 549172e807962208b5914964bd7d9608a0a9eece Mon Sep 17 00:00:00 2001 From: Sieren Date: Sat, 22 May 2021 12:07:52 +0200 Subject: [PATCH] Move Styles into separate class --- main/AppNewScreen.cpp | 18 ++++----------- main/AppNewScreen.h | 2 -- main/CMakeLists.txt | 1 + main/ui/Styles.cpp | 39 ++++++++++++++++++++++++++++++++ main/ui/Styles.h | 22 ++++++++++++++++++ main/ui/UIMosaicButton.cpp | 18 ++++----------- main/ui/UISensorComboButton.cpp | 16 +++---------- main/ui/UISwitchDeviceButton.cpp | 16 +++---------- 8 files changed, 77 insertions(+), 55 deletions(-) create mode 100644 main/ui/Styles.cpp create mode 100644 main/ui/Styles.h diff --git a/main/AppNewScreen.cpp b/main/AppNewScreen.cpp index 81b9421..4bb5d05 100644 --- a/main/AppNewScreen.cpp +++ b/main/AppNewScreen.cpp @@ -1,4 +1,5 @@ #include "AppNewScreen.h" +#include #include #include /// Additional LVGL Custom Drivers @@ -68,15 +69,6 @@ namespace gfx ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer)); ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, LV_TICK_PERIOD_MS * 1000)); - lv_style_init(&mainStyle); - lv_style_set_border_width(&mainStyle, LV_STATE_DEFAULT, 0); - lv_style_set_pad_inner(&mainStyle, LV_STATE_DEFAULT, 0); - lv_style_set_pad_left(&mainStyle, LV_STATE_DEFAULT, 0); - lv_style_set_pad_right(&mainStyle, LV_STATE_DEFAULT, 0); - lv_style_set_bg_color(&mainStyle, LV_STATE_DEFAULT, LV_COLOR_BLACK); - lv_style_set_text_color(&mainStyle, LV_STATE_DEFAULT, LV_COLOR_WHITE); - lv_obj_report_style_mod(&mainStyle); - using namespace std::placeholders; mpAppContext->registerStateCallback(std::bind(&AppNewScreen::appContextChanged, this, _1)); Serial.println("Done setting up the GUI"); @@ -88,7 +80,7 @@ namespace gfx lv_cont_set_layout(screen, LV_LAYOUT_COLUMN_MID); lv_obj_set_style_local_bg_color (screen, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK); lv_obj_set_style_local_bg_opa( screen, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER); - lv_obj_add_style(screen, LV_OBJ_PART_MAIN, &mainStyle); + lv_obj_add_style(screen, LV_OBJ_PART_MAIN, &Styles::getInstance().mainStyle); if (!mpStatusBar) { mpStatusBar = std::make_shared( @@ -191,7 +183,7 @@ namespace gfx lv_obj_t* AppNewScreen::createTabOverview(lv_obj_t* pParent) { lv_obj_t* tabView = lv_tabview_create(pParent, NULL); - lv_obj_add_style(tabView, LV_TABVIEW_PART_BG, &mainStyle); + lv_obj_add_style(tabView, LV_TABVIEW_PART_BG, &Styles::getInstance().mainStyle); lv_tabview_set_btns_pos(tabView, LV_TABVIEW_TAB_POS_NONE); return tabView; } @@ -203,7 +195,7 @@ namespace gfx lv_obj_set_style_local_pad_right(tab, LV_TABVIEW_PART_BG_SCROLLABLE , LV_STATE_DEFAULT, 0); lv_obj_set_style_local_pad_top(tab, LV_TABVIEW_PART_BG_SCROLLABLE , LV_STATE_DEFAULT, 0); lv_obj_set_style_local_pad_bottom(tab, LV_TABVIEW_PART_BG_SCROLLABLE , LV_STATE_DEFAULT, 0); - lv_obj_add_style(tab, LV_PAGE_PART_SCROLLABLE, &mainStyle); + lv_obj_add_style(tab, LV_PAGE_PART_SCROLLABLE, &Styles::getInstance().mainStyle); return tab; } @@ -214,7 +206,7 @@ namespace gfx lv_obj_set_size(pButtonCont, lv_obj_get_width(pParent), lv_obj_get_height(pParent)); lv_obj_set_auto_realign(pButtonCont, true); lv_cont_set_fit2(pButtonCont, LV_FIT_MAX, LV_FIT_NONE); - lv_obj_add_style(pButtonCont, LV_OBJ_PART_MAIN, &mainStyle); + lv_obj_add_style(pButtonCont, LV_OBJ_PART_MAIN, &Styles::getInstance().mainStyle); lv_page_glue_obj(pButtonCont, true); lv_obj_set_style_local_pad_top(pButtonCont, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, 0); return pButtonCont; diff --git a/main/AppNewScreen.h b/main/AppNewScreen.h index acbbfc6..233f14e 100644 --- a/main/AppNewScreen.h +++ b/main/AppNewScreen.h @@ -11,8 +11,6 @@ #include -static lv_style_t mainStyle; - namespace gfx { class AppNewScreen : public std::enable_shared_from_this diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 78c559a..347f0fa 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -37,6 +37,7 @@ file(GLOB UI_SRC "${UI_SRC_PATH}/UISensorComboButton.cpp" "${UI_SRC_PATH}/UISwitchDeviceButton.cpp" "${UI_SRC_PATH}/UIStatusBar.cpp" + "${UI_SRC_PATH}/Styles.cpp" ) set(UIINCLUDES diff --git a/main/ui/Styles.cpp b/main/ui/Styles.cpp new file mode 100644 index 0000000..671c83f --- /dev/null +++ b/main/ui/Styles.cpp @@ -0,0 +1,39 @@ +#include "Styles.h" + + +namespace gfx +{ + Styles::Styles() + { + lv_style_init(&mainStyle); + lv_style_set_border_width(&mainStyle, LV_STATE_DEFAULT, 0); + lv_style_set_pad_inner(&mainStyle, LV_STATE_DEFAULT, 0); + lv_style_set_pad_left(&mainStyle, LV_STATE_DEFAULT, 0); + lv_style_set_pad_right(&mainStyle, LV_STATE_DEFAULT, 0); + lv_style_set_bg_color(&mainStyle, LV_STATE_DEFAULT, LV_COLOR_BLACK); + lv_style_set_text_color(&mainStyle, LV_STATE_DEFAULT, LV_COLOR_WHITE); + lv_obj_report_style_mod(&mainStyle); + + lv_style_copy(&switchBtnContStyle, &mainStyle); + lv_style_set_bg_color(&switchBtnContStyle, LV_STATE_DEFAULT, LV_COLOR_BLACK); + lv_style_set_bg_color(&switchBtnContStyle, LV_STATE_PRESSED, LV_COLOR_GRAY); + lv_style_set_bg_color(&switchBtnContStyle, LV_STATE_FOCUSED, LV_COLOR_RED); + lv_style_set_bg_color(&switchBtnContStyle, LV_STATE_FOCUSED | LV_STATE_PRESSED, lv_color_hex(0xf88)); + lv_style_set_border_width(&switchBtnContStyle, LV_STATE_DEFAULT, 0); + lv_style_set_text_font(&switchBtnContStyle, LV_STATE_DEFAULT, &lv_font_montserrat_8); + lv_style_set_pad_inner(&switchBtnContStyle, LV_STATE_DEFAULT, 2); + lv_style_set_pad_top(&switchBtnContStyle, LV_STATE_DEFAULT, 20); + lv_style_set_pad_bottom(&switchBtnContStyle, LV_STATE_DEFAULT, 5); + + lv_style_copy(&sensorBtnContStyle, &mainStyle); + lv_style_set_bg_color(&sensorBtnContStyle, LV_STATE_DEFAULT, LV_COLOR_BLACK); + lv_style_set_bg_color(&sensorBtnContStyle, LV_STATE_PRESSED, LV_COLOR_GRAY); + lv_style_set_bg_color(&sensorBtnContStyle, LV_STATE_FOCUSED, LV_COLOR_RED); + lv_style_set_bg_color(&sensorBtnContStyle, LV_STATE_FOCUSED | LV_STATE_PRESSED, lv_color_hex(0xf88)); + lv_style_set_border_width(&sensorBtnContStyle, LV_STATE_DEFAULT, 0); + lv_style_set_text_font(&sensorBtnContStyle, LV_STATE_DEFAULT, &lv_font_montserrat_12); + lv_style_set_pad_inner(&sensorBtnContStyle, LV_STATE_DEFAULT, 2); + lv_style_set_pad_top(&sensorBtnContStyle, LV_STATE_DEFAULT, 18); + lv_style_set_pad_bottom(&sensorBtnContStyle, LV_STATE_DEFAULT, 5); + } +} \ No newline at end of file diff --git a/main/ui/Styles.h b/main/ui/Styles.h new file mode 100644 index 0000000..993f19c --- /dev/null +++ b/main/ui/Styles.h @@ -0,0 +1,22 @@ +#pragma once +#include "lvgl.h" + +namespace gfx +{ + class Styles + { + public: + static Styles &getInstance() + { + static Styles instance; + return instance; + }; + static void init(); + Styles(); + lv_style_t mainStyle; + lv_style_t switchBtnContStyle; + lv_style_t sensorBtnContStyle; + + private: + }; +} // namespace gfx diff --git a/main/ui/UIMosaicButton.cpp b/main/ui/UIMosaicButton.cpp index e49a5ee..86ecf78 100644 --- a/main/ui/UIMosaicButton.cpp +++ b/main/ui/UIMosaicButton.cpp @@ -2,6 +2,7 @@ #include "AppNewScreen.h" #include +#include #include #include #include @@ -28,26 +29,15 @@ namespace gfx lv_cont_set_layout(mpContHeader, LV_LAYOUT_COLUMN_MID); lv_obj_align(mpContHeader, mpParent, LV_ALIGN_CENTER, 0, 0); - static lv_style_t lv_style1; - lv_style_set_bg_color(&lv_style1, LV_STATE_DEFAULT, LV_COLOR_BLACK); - lv_style_set_bg_color(&lv_style1, LV_STATE_PRESSED, LV_COLOR_GRAY); - lv_style_set_bg_color(&lv_style1, LV_STATE_FOCUSED, LV_COLOR_RED); - lv_style_set_bg_color(&lv_style1, LV_STATE_FOCUSED | LV_STATE_PRESSED, lv_color_hex(0xf88)); - lv_style_set_border_width(&lv_style1, LV_STATE_DEFAULT, 0); - lv_style_set_text_font(&lv_style1, LV_STATE_DEFAULT, &lv_font_montserrat_12); - lv_style_set_pad_inner(&lv_style1, LV_STATE_DEFAULT, 2); - lv_style_set_pad_top(&lv_style1, LV_STATE_DEFAULT, 20); - lv_style_set_pad_bottom(&lv_style1, LV_STATE_DEFAULT, 5); - static lv_style_t innerStyle; - lv_style_copy(&innerStyle, &mainStyle); + lv_style_copy(&innerStyle, &Styles::getInstance().mainStyle); lv_style_set_pad_bottom(&innerStyle, LV_STATE_DEFAULT, 1); - lv_obj_add_style(mpContHeader, LV_OBJ_PART_MAIN, &lv_style1); + lv_obj_add_style(mpContHeader, LV_OBJ_PART_MAIN, &Styles::getInstance().switchBtnContStyle); mpImage = lv_img_create(mpContHeader, NULL); lv_page_glue_obj(mpContHeader, true); lv_obj_set_user_data(mpContHeader, this); - lv_obj_add_style(mpImage, LV_OBJ_PART_MAIN, &lv_style1); + lv_obj_add_style(mpImage, LV_OBJ_PART_MAIN, &Styles::getInstance().switchBtnContStyle); mpLabelText = lv_label_create(mpContHeader, NULL); static lv_style_t style_status_label; diff --git a/main/ui/UISensorComboButton.cpp b/main/ui/UISensorComboButton.cpp index 706f45c..627e598 100644 --- a/main/ui/UISensorComboButton.cpp +++ b/main/ui/UISensorComboButton.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace gfx @@ -26,28 +27,17 @@ namespace gfx lv_cont_set_layout(mpContHeader, LV_LAYOUT_COLUMN_MID); lv_obj_align(mpContHeader, UIMosaicButton::mpParent, LV_ALIGN_IN_TOP_LEFT, 0, 0); - static lv_style_t lv_style1; - lv_style_copy(&lv_style1, &mainStyle); - lv_style_set_bg_color(&lv_style1, LV_STATE_DEFAULT, LV_COLOR_BLACK); - lv_style_set_bg_color(&lv_style1, LV_STATE_PRESSED, LV_COLOR_GRAY); - lv_style_set_bg_color(&lv_style1, LV_STATE_FOCUSED, LV_COLOR_RED); - lv_style_set_bg_color(&lv_style1, LV_STATE_FOCUSED | LV_STATE_PRESSED, lv_color_hex(0xf88)); - lv_style_set_border_width(&lv_style1, LV_STATE_DEFAULT, 0); - lv_style_set_text_font(&lv_style1, LV_STATE_DEFAULT, &lv_font_montserrat_12); - lv_style_set_pad_inner(&lv_style1, LV_STATE_DEFAULT, 2); - lv_style_set_pad_top(&lv_style1, LV_STATE_DEFAULT, 18); - lv_style_set_pad_bottom(&lv_style1, LV_STATE_DEFAULT, 5); lv_page_glue_obj(mpContHeader, true); static lv_style_t innerStyle; - lv_style_copy(&innerStyle, &mainStyle); + lv_style_copy(&innerStyle, &Styles::getInstance().mainStyle); lv_style_set_pad_top(&innerStyle, LV_STATE_DEFAULT, 1); lv_style_set_pad_bottom(&innerStyle, LV_STATE_DEFAULT, 1); lv_style_set_pad_left(&innerStyle, LV_STATE_DEFAULT, 4); lv_style_set_pad_right(&innerStyle, LV_STATE_DEFAULT, 0); lv_style_set_border_width(&innerStyle, LV_STATE_DEFAULT, 0); lv_style_set_bg_color(&innerStyle, LV_STATE_DEFAULT, LV_COLOR_BLACK); - lv_obj_add_style(mpContHeader, LV_OBJ_PART_MAIN, &lv_style1); + lv_obj_add_style(mpContHeader, LV_OBJ_PART_MAIN, &Styles::getInstance().sensorBtnContStyle); lv_obj_t* firstCont = lv_cont_create(mpContHeader, NULL); lv_cont_set_fit2(firstCont, LV_FIT_TIGHT, LV_FIT_TIGHT); diff --git a/main/ui/UISwitchDeviceButton.cpp b/main/ui/UISwitchDeviceButton.cpp index 35ed9b7..570b3b8 100644 --- a/main/ui/UISwitchDeviceButton.cpp +++ b/main/ui/UISwitchDeviceButton.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace gfx @@ -25,23 +26,12 @@ namespace gfx lv_cont_set_layout(mpContHeader, LV_LAYOUT_COLUMN_MID); lv_obj_align(mpContHeader, UIMosaicButton::mpParent, LV_ALIGN_IN_TOP_LEFT, 0, 0); - static lv_style_t lv_style1; - lv_style_copy(&lv_style1, &mainStyle); - lv_style_set_bg_color(&lv_style1, LV_STATE_DEFAULT, LV_COLOR_BLACK); - lv_style_set_bg_color(&lv_style1, LV_STATE_PRESSED, LV_COLOR_GRAY); - lv_style_set_bg_color(&lv_style1, LV_STATE_FOCUSED, LV_COLOR_RED); - lv_style_set_bg_color(&lv_style1, LV_STATE_FOCUSED | LV_STATE_PRESSED, lv_color_hex(0xf88)); - lv_style_set_border_width(&lv_style1, LV_STATE_DEFAULT, 0); - lv_style_set_text_font(&lv_style1, LV_STATE_DEFAULT, &lv_font_montserrat_8); - lv_style_set_pad_inner(&lv_style1, LV_STATE_DEFAULT, 2); - lv_style_set_pad_top(&lv_style1, LV_STATE_DEFAULT, 20); - lv_style_set_pad_bottom(&lv_style1, LV_STATE_DEFAULT, 5); lv_page_glue_obj(mpContHeader, true); static lv_style_t innerStyle; - lv_style_copy(&innerStyle, &mainStyle); + lv_style_copy(&innerStyle, &Styles::getInstance().mainStyle); lv_style_set_pad_bottom(&innerStyle, LV_STATE_DEFAULT, 1); - lv_obj_add_style(mpContHeader, LV_OBJ_PART_MAIN, &lv_style1); + lv_obj_add_style(mpContHeader, LV_OBJ_PART_MAIN, &Styles::getInstance().switchBtnContStyle); mpImage = lv_img_create(mpContHeader, NULL); lv_obj_set_user_data(mpContHeader, this);