Add boiler plate Key Handling Into the UI code for UI elements

This commit is contained in:
MatthewColvin 2023-09-29 16:03:51 -05:00
parent 160114f54c
commit a44e92ea5d
8 changed files with 37 additions and 0 deletions

View file

@ -6,6 +6,13 @@ using namespace UI;
BasicUI::BasicUI(std::shared_ptr<HardwareAbstract> aHardware)
: UIBase(aHardware) {
aHardware->keys()->RegisterKeyPressHandler([](auto aKeyEvent) {
return Screen::Manager::getInstance().distributeKeyEvent(aKeyEvent);
// Could potentially add a check here and display that a key event was
// unused.
});
Screen::Manager::getInstance().pushScreen(
std::make_unique<Screen::HomeScreen>());
}

View file

@ -18,3 +18,8 @@ void Manager::pushScreen(Screen::Base::Ptr aScreen) {
mScreens.push(std::move(aScreen));
mScreens.top()->Show();
}
bool Manager::distributeKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) {
// Send Key Even to top Screen for handling
return mScreens.top()->KeyEvent(aKeyEvent);
}

View file

@ -13,6 +13,8 @@ public:
void pushScreen(Screen::Base::Ptr aScreen);
void pushScreen(Screen::Base::Ptr aScreen, lv_scr_load_anim_t aPushAnimation);
bool distributeKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);
private:
Manager();
static Manager mManager;

View file

@ -67,4 +67,8 @@ void UIElement::Hide() {
OnHide();
}
bool UIElement::KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) {
return OnKeyEvent(aKeyEvent);
}
} // namespace UI

View file

@ -3,6 +3,8 @@
#include "UIElementIds.hpp"
#include "lvgl.h"
#include "KeyPressAbstract.hpp"
namespace UI {
class UIElement {
@ -48,6 +50,14 @@ protected:
/// @brief Override in child class to run something after element is hidden
virtual void OnHide() = 0;
/// @brief Set KeyEvent to the UI element to see if it wants to handle it
virtual bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);
/// @brief Override to Handle KeyEvent for the specific element
/// @return true - Key event was used
/// fasle - Key event was unused
virtual bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) = 0;
private:
lv_obj_t *mLvglSelf;
const ID mId;

View file

@ -20,6 +20,9 @@ public:
protected:
void OnShow() override{};
void OnHide() override{};
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override {
return false;
};
private:
std::vector<Widget::Base::Ptr> mWidgets;

View file

@ -23,6 +23,9 @@ protected:
void Show() override;
void OnShow() override{};
void OnHide() override{};
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override {
return false;
};
private:
lv_scr_load_anim_t mPushAnimation = LV_SCR_LOAD_ANIM_NONE;

View file

@ -13,6 +13,9 @@ public:
protected:
void OnShow() override{};
void OnHide() override{};
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override {
return false;
};
private:
};