Create Base Implementation of OnShow and OnHide that notify all owned elements of their shown status
Update OnShow and OnHide to be aware of current set visibility.
This commit is contained in:
parent
7d68cce6ad
commit
c7f9482e0a
11 changed files with 103 additions and 85 deletions
|
@ -1,22 +1,15 @@
|
|||
#include "BrightnessSlider.hpp"
|
||||
#include "DisplaySettings.hpp"
|
||||
#include "BrightnessSlider.hpp"
|
||||
|
||||
using namespace UI::Page;
|
||||
|
||||
DisplaySettings::DisplaySettings(std::shared_ptr<DisplayAbstract> aDisplay)
|
||||
: Base(UI::ID::Pages::DisplaySettings), mDisplay(aDisplay),
|
||||
mBrightnessSlider(AddElement<Widget::BrightnessSlider>(std::make_unique<Widget::BrightnessSlider>(mDisplay))) {
|
||||
mBrightnessSlider(AddElement<Widget::BrightnessSlider>(
|
||||
std::make_unique<Widget::BrightnessSlider>(mDisplay))) {
|
||||
SetBgColor(Color::GREY);
|
||||
|
||||
mBrightnessSlider->SetWidth(GetContentWidth());
|
||||
mBrightnessSlider->SetHeight(80);
|
||||
mBrightnessSlider->AlignTo(this, LV_ALIGN_TOP_MID);
|
||||
}
|
||||
|
||||
void DisplaySettings::OnShow() {
|
||||
|
||||
};
|
||||
|
||||
void DisplaySettings::OnHide(){
|
||||
|
||||
};
|
|
@ -11,9 +11,6 @@ class DisplaySettings : public Base {
|
|||
public:
|
||||
DisplaySettings(std::shared_ptr<DisplayAbstract> aDisplay);
|
||||
|
||||
void OnShow() override;
|
||||
void OnHide() override;
|
||||
|
||||
std::string GetTitle() override { return "Display Settings"; };
|
||||
|
||||
private:
|
||||
|
|
|
@ -14,8 +14,6 @@ public:
|
|||
lv_style_selector_t selector = LV_PART_MAIN) override;
|
||||
|
||||
protected:
|
||||
void OnShow() override{};
|
||||
void OnHide() override{};
|
||||
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -15,10 +15,12 @@ BrightnessSlider::BrightnessSlider(std::shared_ptr<DisplayAbstract> aDisplay)
|
|||
mLabel->AlignTo(this, LV_ALIGN_TOP_MID);
|
||||
mSlider->AlignTo(mLabel, LV_ALIGN_OUT_BOTTOM_MID);
|
||||
mSlider->SetWidth(GetContentWidth() - GetContentWidth() * 0.25f);
|
||||
SetVisiblity(false);
|
||||
}
|
||||
|
||||
void BrightnessSlider::OnShow() {
|
||||
mSlider->SetValue(mDisplay->getBrightness());
|
||||
Base::OnShow();
|
||||
}
|
||||
|
||||
void BrightnessSlider::SetHeight(lv_coord_t aHeight) {
|
||||
|
@ -30,4 +32,14 @@ void BrightnessSlider::SetHeight(lv_coord_t aHeight) {
|
|||
mLabel->AlignTo(this, LV_ALIGN_TOP_MID);
|
||||
mSlider->AlignTo(mLabel, LV_ALIGN_OUT_BOTTOM_MID, 0,
|
||||
mLabel->GetContentHeight());
|
||||
}
|
||||
|
||||
bool BrightnessSlider::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) {
|
||||
using ID = KeyPressAbstract::KeyId;
|
||||
using Type = KeyPressAbstract::KeyEvent::Type;
|
||||
if (aKeyEvent.mId == ID::Aux1 && aKeyEvent.mType == Type::Press) {
|
||||
SetVisiblity(!IsSetVisible());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
|
@ -1,21 +1,25 @@
|
|||
#pragma once
|
||||
#include "WidgetBase.hpp"
|
||||
#include "DisplayAbstract.h"
|
||||
#include "WidgetBase.hpp"
|
||||
|
||||
namespace UI::Widget{
|
||||
namespace UI::Widget {
|
||||
class Slider;
|
||||
class Label;
|
||||
|
||||
class BrightnessSlider : public Base{
|
||||
class BrightnessSlider : public Base {
|
||||
public:
|
||||
BrightnessSlider(std::shared_ptr<DisplayAbstract> aDisplay);
|
||||
BrightnessSlider(std::shared_ptr<DisplayAbstract> aDisplay);
|
||||
|
||||
void SetHeight(lv_coord_t aHeight) override;
|
||||
|
||||
protected:
|
||||
void OnShow() override;
|
||||
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override;
|
||||
|
||||
void OnShow() override;
|
||||
void SetHeight(lv_coord_t aHeight) override;
|
||||
private:
|
||||
std::shared_ptr<DisplayAbstract> mDisplay;
|
||||
Slider* mSlider;
|
||||
Label* mLabel;
|
||||
std::shared_ptr<DisplayAbstract> mDisplay;
|
||||
Slider *mSlider;
|
||||
Label *mLabel;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace UI::Widget
|
|
@ -62,6 +62,11 @@ bool UIElement::IsVisible() {
|
|||
return lv_obj_is_visible(mLvglSelf);
|
||||
}
|
||||
|
||||
bool UIElement::IsSetVisible() {
|
||||
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||
return !lv_obj_has_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
void UIElement::SetWidth(lv_coord_t aWidth) {
|
||||
LvglResourceManager::GetInstance().AttemptNow(
|
||||
[this, aWidth] { lv_obj_set_width(mLvglSelf, aWidth); });
|
||||
|
@ -227,7 +232,7 @@ void UIElement::AlignTo(UIElement *anElementToAlignTo, lv_align_t anAlignment,
|
|||
}
|
||||
|
||||
void UIElement::SetVisiblity(bool aVisible) {
|
||||
if (aVisible == IsVisible()) {
|
||||
if (aVisible == !lv_obj_has_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN)) {
|
||||
return;
|
||||
}
|
||||
if (aVisible) {
|
||||
|
@ -249,26 +254,27 @@ void UIElement::SetBgOpacity(lv_opa_t aOpacity, lv_style_selector_t aStyle) {
|
|||
});
|
||||
}
|
||||
|
||||
void UIElement::StartLvglEventHandler(){
|
||||
if(mIsHandlingLvglEvents){ return; }
|
||||
void UIElement::StartLvglEventHandler() {
|
||||
if (mIsHandlingLvglEvents) {
|
||||
return;
|
||||
}
|
||||
lv_obj_add_event_cb(mLvglSelf, UIElement::LvglEventHandler, LV_EVENT_ALL,
|
||||
this);
|
||||
mIsHandlingLvglEvents = true;
|
||||
}
|
||||
void UIElement::StopLvglEventHandler(){
|
||||
if(!mIsHandlingLvglEvents){return;}
|
||||
lv_obj_remove_event_cb_with_user_data(mLvglSelf,UIElement::LvglEventHandler,
|
||||
void UIElement::StopLvglEventHandler() {
|
||||
if (!mIsHandlingLvglEvents) {
|
||||
return;
|
||||
}
|
||||
lv_obj_remove_event_cb_with_user_data(mLvglSelf, UIElement::LvglEventHandler,
|
||||
this);
|
||||
mIsHandlingLvglEvents = false;
|
||||
}
|
||||
|
||||
void UIElement::Show() {
|
||||
if (IsVisible()) {
|
||||
if (IsSetVisible()) {
|
||||
return;
|
||||
}
|
||||
for (auto &elem : mContainedElements) {
|
||||
elem->OnShow();
|
||||
}
|
||||
{
|
||||
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||
lv_obj_clear_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN);
|
||||
|
@ -277,12 +283,9 @@ void UIElement::Show() {
|
|||
}
|
||||
|
||||
void UIElement::Hide() {
|
||||
if (!IsVisible()) {
|
||||
if (!IsSetVisible()) {
|
||||
return;
|
||||
}
|
||||
for (auto &elem : mContainedElements) {
|
||||
elem->OnHide();
|
||||
}
|
||||
{
|
||||
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||
lv_obj_add_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN);
|
||||
|
@ -290,6 +293,22 @@ void UIElement::Hide() {
|
|||
OnHide();
|
||||
}
|
||||
|
||||
void UIElement::OnHide() {
|
||||
for (auto &elem : mContainedElements) {
|
||||
if (!IsSetVisible()) {
|
||||
elem->OnHide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UIElement::OnShow() {
|
||||
for (auto &elem : mContainedElements) {
|
||||
if (!lv_obj_has_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN)) {
|
||||
elem->OnShow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////// Statics //////////////////////////
|
||||
|
||||
void UIElement::LvglEventHandler(lv_event_t *anEvent) {
|
||||
|
|
|
@ -26,6 +26,7 @@ public:
|
|||
|
||||
void SetVisiblity(bool aVisibility);
|
||||
bool IsVisible();
|
||||
bool IsSetVisible();
|
||||
|
||||
virtual void SetWidth(lv_coord_t aWidth);
|
||||
virtual void SetHeight(lv_coord_t aHeight);
|
||||
|
@ -87,7 +88,6 @@ public:
|
|||
mLvglKeepAliveTime = aTimeToKeepLvglObj;
|
||||
}
|
||||
|
||||
|
||||
void StartLvglEventHandler();
|
||||
void StopLvglEventHandler();
|
||||
|
||||
|
@ -101,9 +101,9 @@ protected:
|
|||
/// @brief Hide Element
|
||||
virtual void Hide();
|
||||
/// @brief Override in child class to run something after element is shown
|
||||
virtual void OnShow() = 0;
|
||||
virtual void OnShow();
|
||||
/// @brief Override in child class to run something after element is hidden
|
||||
virtual void OnHide() = 0;
|
||||
virtual void OnHide();
|
||||
|
||||
// Override in object to handle LVGL events for that object
|
||||
virtual void OnLvglEvent(lv_event_t *anEvent){};
|
||||
|
|
|
@ -24,8 +24,6 @@ public:
|
|||
|
||||
protected:
|
||||
void Show() override;
|
||||
void OnShow() override{};
|
||||
void OnHide() override{};
|
||||
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -3,30 +3,27 @@
|
|||
using namespace UI;
|
||||
using namespace UI::Widget;
|
||||
|
||||
ListItem::ListItem(lv_obj_t *aListItem, std::function<void()> onItemSelected):
|
||||
UIElement(aListItem,ID()),
|
||||
mSelectedHandler(std::move(onItemSelected)){}
|
||||
ListItem::ListItem(lv_obj_t *aListItem, std::function<void()> onItemSelected)
|
||||
: UIElement(aListItem, ID()), mSelectedHandler(std::move(onItemSelected)) {}
|
||||
|
||||
void ListItem::OnLvglEvent(lv_event_t* anEvent){
|
||||
if(anEvent->code == LV_EVENT_CLICKED){
|
||||
if(mSelectedHandler){
|
||||
mSelectedHandler();
|
||||
}
|
||||
void ListItem::OnLvglEvent(lv_event_t *anEvent) {
|
||||
if (anEvent->code == LV_EVENT_CLICKED) {
|
||||
if (mSelectedHandler) {
|
||||
mSelectedHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List::List():Base(lv_list_create(Screen::BackgroundScreen::getLvInstance()),ID::Widgets::List){
|
||||
StopLvglEventHandler();
|
||||
List::List()
|
||||
: Base(lv_list_create(Screen::BackgroundScreen::getLvInstance()),
|
||||
ID::Widgets::List) {
|
||||
StopLvglEventHandler();
|
||||
}
|
||||
|
||||
void List::AddItem(std::string aTitle, const char* aSymbol, std::function<void()> onItemSelected){
|
||||
auto lvListItem = lv_list_add_btn(LvglSelf(),aSymbol,aTitle.c_str());
|
||||
mListItems.push_back(std::make_unique<ListItem>(lvListItem, std::move(onItemSelected)));
|
||||
mListItems.back()->SetHeight(lv_pct(20));
|
||||
void List::AddItem(std::string aTitle, const char *aSymbol,
|
||||
std::function<void()> onItemSelected) {
|
||||
auto lvListItem = lv_list_add_btn(LvglSelf(), aSymbol, aTitle.c_str());
|
||||
mListItems.push_back(
|
||||
std::make_unique<ListItem>(lvListItem, std::move(onItemSelected)));
|
||||
mListItems.back()->SetHeight(lv_pct(20));
|
||||
}
|
||||
|
||||
void List::OnLvglEvent(lv_event_t* anEvent){
|
||||
if(anEvent->code == LV_EVENT_CLICKED){
|
||||
auto tgt = anEvent->target;
|
||||
}
|
||||
}
|
|
@ -1,30 +1,32 @@
|
|||
#pragma once
|
||||
#include "WidgetBase.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace UI::Widget{
|
||||
namespace UI::Widget {
|
||||
|
||||
class ListItem : public UIElement{
|
||||
class ListItem : public UIElement {
|
||||
public:
|
||||
ListItem(lv_obj_t* aListItem, std::function<void()> onItemSelected);
|
||||
protected:
|
||||
void OnLvglEvent(lv_event_t* anEvent) override;
|
||||
bool OnKeyEvent(KeyPressAbstract::KeyEvent anEvent)override{return false;};
|
||||
void OnShow()override{};
|
||||
void OnHide()override{};
|
||||
private:
|
||||
std::function<void()> mSelectedHandler;
|
||||
};
|
||||
|
||||
class List : public Base{
|
||||
public:
|
||||
List();
|
||||
void AddItem(std::string aTitle, const char* aSymbol, std::function<void()> onItemSelected);
|
||||
ListItem(lv_obj_t *aListItem, std::function<void()> onItemSelected);
|
||||
|
||||
protected:
|
||||
void OnLvglEvent(lv_event_t* anEvent)override;
|
||||
void OnLvglEvent(lv_event_t *anEvent) override;
|
||||
bool OnKeyEvent(KeyPressAbstract::KeyEvent anEvent) override {
|
||||
return false;
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<UIElement::Ptr> mListItems;
|
||||
std::function<void()> mSelectedHandler;
|
||||
};
|
||||
|
||||
}
|
||||
class List : public Base {
|
||||
public:
|
||||
List();
|
||||
void AddItem(std::string aTitle, const char *aSymbol,
|
||||
std::function<void()> onItemSelected);
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::vector<UIElement::Ptr> mListItems;
|
||||
};
|
||||
|
||||
} // namespace UI::Widget
|
|
@ -24,8 +24,6 @@ public:
|
|||
virtual ~Base() override = default;
|
||||
|
||||
protected:
|
||||
void OnShow() override{};
|
||||
void OnHide() override{};
|
||||
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override {
|
||||
return false;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue