Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor item command class to use item widget #251

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 27 additions & 52 deletions src/ItemCommand.h
Original file line number Diff line number Diff line change
@@ -1,66 +1,41 @@
#ifndef ItemCommand_H
#define ItemCommand_H
#pragma once

#include "MenuItem.h"
#include "BaseItemZeroWidget.h"

/**
* @class ItemCommand
* @brief Represents a menu item that executes a command when selected.
* @brief A menu item that executes a callback function when selected.
*
* The ItemCommand class inherits from MenuItem and allows for the execution
* of a callback function when the item is entered. This is useful for creating
* interactive menu items in an LCD menu system.
* This class extends the BaseItemZeroWidget class and provides a menu item
* that executes a callback function when selected. The callback function is
* provided as a function pointer during construction.
*
* @note The callback function should match the signature defined by the fptr type.
* As a BaseItemZeroWidget, this item responds to selection events in the menu
* system. When the user confirms the selection, handleCommit is triggered,
* which executes the provided callback.
*/
class ItemCommand : public MenuItem {
class ItemCommand : public BaseItemZeroWidget {
private:
// Declare a function pointer for the command callback.
fptr callback;
void (*callback)();

public:
/**
* Construct a new ItemCommand object.
*
* @param text The text of the item.
* @param callback A reference to the callback function to be invoked when
* the item is entered.
*/
ItemCommand(const char* text, fptr callback) : MenuItem(text), callback(callback) {}

/**
* Get the callback function for this item.
*
* @return The function pointer to the callback function.
*/
fptr getCallback() { return callback; }

/**
* Set the callback function for this item.
*
* @param callback A reference to the new callback function to be invoked
* when the item is entered.
*/
void setCallBack(fptr callback) { this->callback = callback; };
ItemCommand(const char* text, void (*callback)()) : BaseItemZeroWidget(text), callback(callback) {}

protected:
bool process(LcdMenu* menu, const unsigned char command) override {
switch (command) {
case ENTER:
executeCommand();
return true;
default:
return false;
}
}
void executeCommand() {
if (callback != NULL) {
callback();
}
LOG(F("ItemCommand::enter"), text);
void handleCommit(LcdMenu* menu) override {
if (callback) callback();
}
};

#define ITEM_COMMAND(...) (new ItemCommand(__VA_ARGS__))

#endif // ITEM_COMMAND_H
/**
* @brief Create a new command item.
*
* @param text The text to display for the item.
* @param callback The function to call when the item is selected.
* @return MenuItem* The created item. Caller takes ownership of the returned pointer.
*
* @example
* auto item = ITEM_COMMAND("Save", []() { save_data(); });
*/
inline MenuItem* ITEM_COMMAND(const char* text, void (*callback)()) {
return new ItemCommand(text, callback);
}
Loading