Implement DropDown core widget using

templates to simplify creation and handling of
of data related to dropdowns
This commit is contained in:
MatthewColvin 2023-10-12 21:13:04 -05:00
parent 6be699da64
commit 8d54d37978
5 changed files with 56 additions and 2 deletions

View file

@ -6,10 +6,21 @@ 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))) {
std::make_unique<Widget::BrightnessSlider>(mDisplay))),
mScreenTimeOutDropDown(AddElement<Widget::DropDown<int>>(
std::make_unique<Widget::DropDown<int>>([this](int aTimeout) {
}))) {
SetBgColor(Color::GREY);
mBrightnessSlider->SetWidth(GetContentWidth());
mBrightnessSlider->SetHeight(80);
mBrightnessSlider->AlignTo(this, LV_ALIGN_TOP_MID);
mScreenTimeOutDropDown->SetHeight(30);
mScreenTimeOutDropDown->SetWidth(GetContentWidth());
mScreenTimeOutDropDown->AddItem("10 Seconds", 10);
mScreenTimeOutDropDown->AddItem("15 Seconds", 15);
mScreenTimeOutDropDown->AddItem("20 Seconds", 20);
mScreenTimeOutDropDown->AlignTo(mBrightnessSlider, LV_ALIGN_OUT_BOTTOM_MID);
}

View file

@ -1,10 +1,11 @@
#pragma once
#include "DisplayAbstract.h"
#include "DropDown.hpp"
#include "PageBase.hpp"
namespace UI::Widget {
class BrightnessSlider;
}
} // namespace UI::Widget
namespace UI::Page {
class DisplaySettings : public Base {
@ -16,5 +17,6 @@ public:
private:
std::shared_ptr<DisplayAbstract> mDisplay;
Widget::BrightnessSlider *mBrightnessSlider;
Widget::DropDown<int> *mScreenTimeOutDropDown;
};
} // namespace UI::Page

View file

@ -18,6 +18,7 @@ public:
Button,
Label,
List,
DropDown,
BrightnessSlider,
INVALID_WIDGET_ID
};

View file

@ -1,3 +1,4 @@
#pragma once
#include "ScreenBase.hpp"
namespace UI::Screen {
/// @brief Due to the way LVGL utilizes screens we basically need a canvas to

View file

@ -0,0 +1,39 @@
#pragma once
#include "BackgroundScreen.hpp"
#include "WidgetBase.hpp"
#include <functional>
#include <string>
namespace UI::Widget {
template <typename T> class DropDown : public Base {
public:
DropDown(std::function<void(T)> aOnItemSelected)
: Base(lv_dropdown_create(UI::Screen::BackgroundScreen::getLvInstance()),
ID::Widgets::DropDown),
mSelectionHandler(aOnItemSelected) {
lv_dropdown_clear_options(LvglSelf());
}
void AddItem(std::string aOptionTitle, T aOptionData) {
lv_dropdown_add_option(LvglSelf(), aOptionTitle.c_str(),
LV_DROPDOWN_POS_LAST);
mOptionsData.push_back(aOptionData);
}
// TODO Could Implement a remove Item but need to make sure
// correct order is retained in data vector.
protected:
void OnLvglEvent(lv_event_t *anEvent) override {
if (anEvent->code == LV_EVENT_VALUE_CHANGED) {
auto idx = lv_dropdown_get_selected(LvglSelf());
mSelectionHandler(mOptionsData[idx]);
}
};
private:
std::function<void(T)> mSelectionHandler;
std::vector<T> mOptionsData;
};
} // namespace UI::Widget