Skip to content

Commit

Permalink
Move Styles into separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
sieren committed May 22, 2021
1 parent ca6a4bd commit 549172e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 55 deletions.
18 changes: 5 additions & 13 deletions main/AppNewScreen.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "AppNewScreen.h"
#include <ui/Styles.h>
#include <config/PlatformInject.hpp>
#include <util/varianthelper.hpp>
/// Additional LVGL Custom Drivers
Expand Down Expand Up @@ -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");
Expand All @@ -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<UIStatusBar>(
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions main/AppNewScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#include <lvgl.h>

static lv_style_t mainStyle;

namespace gfx
{
class AppNewScreen : public std::enable_shared_from_this<AppNewScreen>
Expand Down
1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions main/ui/Styles.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 22 additions & 0 deletions main/ui/Styles.h
Original file line number Diff line number Diff line change
@@ -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
18 changes: 4 additions & 14 deletions main/ui/UIMosaicButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "AppNewScreen.h"
#include <ui/Icons.hpp>
#include <ui/Styles.h>
#include <config/Config.h>
#include <config/Icon.hpp>
#include <ntp/NTPSync.h>
Expand All @@ -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;
Expand Down
16 changes: 3 additions & 13 deletions main/ui/UISensorComboButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <config/Config.h>
#include <config/Icon.hpp>
#include <ui/Icons.hpp>
#include <ui/Styles.h>
#include <functional>

namespace gfx
Expand All @@ -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);
Expand Down
16 changes: 3 additions & 13 deletions main/ui/UISwitchDeviceButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <config/Config.h>
#include <config/Icon.hpp>
#include <ntp/NTPSync.h>
#include <ui/Styles.h>
#include <functional>

namespace gfx
Expand All @@ -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);
Expand Down

0 comments on commit 549172e

Please sign in to comment.