Skip to content

Commit

Permalink
Refactor ItemSubMenu class to inherit from BaseItemZeroWidget and upd…
Browse files Browse the repository at this point in the history
…ate methods (#259)

- **New Features**
- Introduced a new method `handleCommit` for improved item selection
functionality.
- Added an inline function `ITEM_SUBMENU` for easier creation of
`ItemSubMenu` instances.

- **Improvements**
- Updated class inheritance for better integration with the application
framework.
	- Enhanced screen update process upon item selection.
  • Loading branch information
forntoh authored Oct 27, 2024
2 parents 3512977 + 32c3bb7 commit f2bdc8b
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/ItemSubMenu.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef ItemSubMenu_H
#define ItemSubMenu_H
#pragma once

#include "BaseItemZeroWidget.h"
#include "LcdMenu.h"
#include "MenuItem.h"
#include "MenuScreen.h"

/**
* @class ItemSubMenu
Expand All @@ -10,7 +11,7 @@
* This class extends the MenuItem class and provides functionality to navigate
* to a different screen when the item is selected.
*/
class ItemSubMenu : public MenuItem {
class ItemSubMenu : public BaseItemZeroWidget {
private:
MenuScreen*& screen;

Expand All @@ -19,25 +20,26 @@ class ItemSubMenu : public MenuItem {
* @param text text to display for the item
* @param screen the next screen to show
*/
ItemSubMenu(const char* text, MenuScreen*& screen) : MenuItem(text), screen(screen) {}
ItemSubMenu(const char* text, MenuScreen*& screen) : BaseItemZeroWidget(text), screen(screen) {}

protected:
bool process(LcdMenu* menu, const unsigned char command) {
switch (command) {
case ENTER:
changeScreen(menu);
return true;
default:
return false;
}
}
void changeScreen(LcdMenu* menu) {
void handleCommit(LcdMenu* menu) override {
LOG(F("ItemSubMenu::changeScreen"), text);
screen->setParent(menu->getScreen());
menu->setScreen(screen);
}
};

#define ITEM_SUBMENU(...) (new ItemSubMenu(__VA_ARGS__))

#endif
/**
* @brief Create a new submenu item.
*
* @param text The text to display for the item.
* @param screen The screen to navigate to when the item is selected.
* @return MenuItem* The created item. Caller takes ownership of the returned pointer.
*
* @example
* auto item = ITEM_SUBMENU("Settings", settingsScreen);
*/
inline MenuItem* ITEM_SUBMENU(const char* text, MenuScreen*& screen) {
return new ItemSubMenu(text, screen);
}

0 comments on commit f2bdc8b

Please sign in to comment.