Add Getters and setters for X and Y values

had to make sure the getters properly calculated layout before retrieving values.

Add a Tab class to allow for better control of adding content into the tabs and managing the contents once its in there.

Use setting class to do a quick demo of the OnShow code abilities. This could be useful for things like settings pages that need to load up HW resources when they are shown but then can power them down when they are hidden.
This commit is contained in:
Matthew Colvin 2023-09-23 01:06:06 -05:00
parent 7c089e395a
commit 0796176a0e
8 changed files with 83 additions and 13 deletions

View file

@ -12,17 +12,35 @@ void UIElement::AddElement(UIElement *anUIElement) {
bool UIElement::IsVisible() { return lv_obj_is_visible(mLvglSelf); }
void UIElement::SetWidth(uint16_t aWidth) {
void UIElement::SetWidth(lv_coord_t aWidth) {
lv_obj_set_width(mLvglSelf, aWidth);
}
void UIElement::SetHeight(uint16_t aHeight) {
void UIElement::SetHeight(lv_coord_t aHeight) {
lv_obj_set_height(mLvglSelf, aHeight);
}
int16_t UIElement::GetHeight() { return lv_obj_get_height(mLvglSelf); };
lv_coord_t UIElement::GetHeight() {
lv_obj_update_layout(mLvglSelf);
return lv_obj_get_height(mLvglSelf);
};
int16_t UIElement::GetWidth() { return lv_obj_get_width(mLvglSelf); }
lv_coord_t UIElement::GetWidth() {
lv_obj_update_layout(mLvglSelf);
return lv_obj_get_width(mLvglSelf);
}
void UIElement::SetY(lv_coord_t aY) { lv_obj_set_y(mLvglSelf, aY); }
void UIElement::SetX(lv_coord_t aX) { lv_obj_set_x(mLvglSelf, aX); }
lv_coord_t UIElement::GetY() {
lv_obj_update_layout(mLvglSelf);
return lv_obj_get_y(mLvglSelf);
}
lv_coord_t UIElement::GetX() {
lv_obj_update_layout(mLvglSelf);
return lv_obj_get_x(mLvglSelf);
}
void UIElement::SetVisiblity(bool aVisible) {
if (aVisible == IsVisible()) {

View file

@ -16,11 +16,17 @@ public:
void SetVisiblity(bool aVisibility);
bool IsVisible();
virtual void SetWidth(uint16_t aWidth);
virtual void SetHeight(uint16_t aHeight);
virtual void SetWidth(lv_coord_t aWidth);
virtual void SetHeight(lv_coord_t aHeight);
int16_t GetWidth();
int16_t GetHeight();
lv_coord_t GetWidth();
lv_coord_t GetHeight();
virtual void SetY(lv_coord_t aY);
virtual void SetX(lv_coord_t aX);
lv_coord_t GetY();
lv_coord_t GetX();
virtual void AddElement(UIElement *anElement);

View file

@ -13,5 +13,6 @@ Base::Base(lv_obj_t *aLvglSelf, ID aID) : UIElement(aLvglSelf, aID) {
}
void Base::AddWidget(Widget::Base::Ptr aWidget) {
AddElement(aWidget.get());
mWidgets.push_back(std::move(aWidget));
}

View file

@ -15,6 +15,7 @@ public:
Base(lv_obj_t *aLvglSelf, ID aID);
void AddWidget(Widget::Base::Ptr aWidget);
size_t GetNumWidgets() { return mWidgets.size(); }
protected:
void OnShow() override{};

View file

@ -1,7 +1,20 @@
#include "BackgroundScreen.hpp"
#include "SettingsPage.hpp"
using namespace UI::Page;
SettingsPage::SettingsPage() : Base(ID::Pages::Settings) {
SetBgColor(lv_color_make(255, 0, 0));
}
void SettingsPage::OnShow() {
auto fakeSlider = std::make_unique<UI::Widget::Base>(
lv_slider_create(UI::Screen::BackgroundScreen::getLvInstance()));
fakeSlider->SetHeight(lv_pct(10));
fakeSlider->SetWidth(GetWidth());
auto sliderHeight = fakeSlider->GetHeight();
fakeSlider->SetY(sliderHeight * GetNumWidgets());
AddWidget(std::move(fakeSlider));
}

View file

@ -4,5 +4,8 @@ namespace UI::Page {
class SettingsPage : public Base {
public:
SettingsPage();
protected:
void OnShow() override;
};
} // namespace UI::Page

View file

@ -4,6 +4,15 @@
using namespace UI::Page;
Tab::Tab(lv_obj_t *aTab, ID aId) : Base(aTab, aId) {}
void Tab::GiveContent(Page::Base::Ptr aContent) {
AddElement(aContent.get());
mContent = std::move(aContent);
}
Base::Ptr Tab::TakeContent() { return std::move(mContent); }
TabView::TabView(ID aId)
: Base(lv_tabview_create(Screen::BackgroundScreen::getLvInstance(),
LV_DIR_TOP, 0),
@ -13,11 +22,10 @@ TabView::TabView(ID aId)
}
void TabView::AddTab(Page::Base::Ptr aPage, std::string aTitle) {
auto tab = std::make_unique<Base>(
auto tab = std::make_unique<Tab>(
lv_tabview_add_tab(LvglSelf(), aTitle.c_str()), aPage->GetID());
tab->AddElement(aPage.get());
tab->GiveContent(std::move(aPage));
mTabs.push_back(std::move(tab));
}
@ -35,7 +43,13 @@ void TabView::HandleTabChange() {
// Notify the page that it is now showing and the other that the are now
// hidden
for (int i = 0; i < mTabs.size(); i++) {
GetCurrentTabIdx() == i ? mTabs[i]->OnShow() : mTabs[i]->OnHide();
auto content = mTabs[i]->TakeContent();
if (GetCurrentTabIdx() == i) {
content->OnShow();
} else {
content->OnHide();
}
mTabs[i]->GiveContent(std::move(content));
}
}

View file

@ -1,7 +1,21 @@
#include "PageBase.hpp"
#include <functional>
namespace UI::Page {
class Tab : public Base {
public:
typedef std::unique_ptr<Tab> Ptr;
Tab(lv_obj_t *aTab, ID aId);
void GiveContent(Page::Base::Ptr aContent);
[[nodiscard]] Base::Ptr TakeContent();
private:
Base::Ptr mContent;
};
class TabView : public Base {
public:
TabView(ID aId);
@ -19,7 +33,7 @@ private:
void HandleTabChange();
static void HandleTabChangeImpl(lv_event_t *aTabChangeEvent);
std::vector<Page::Base::Ptr> mTabs;
std::vector<Page::Tab::Ptr> mTabs;
};
} // namespace UI::Page