diff --git a/src/activities/network/WifiScreen.cpp b/src/activities/network/WifiScreen.cpp index d3a2efd..3b67896 100644 --- a/src/activities/network/WifiScreen.cpp +++ b/src/activities/network/WifiScreen.cpp @@ -199,11 +199,11 @@ void WifiScreen::selectNetwork(int index) { if (selectedRequiresPassword) { // Show password entry state = WifiScreenState::PASSWORD_ENTRY; - keyboard.reset(new OnScreenKeyboard(renderer, inputManager, "Enter WiFi Password", - "", // No initial text - 64, // Max password length - false // Show password by default (hard keyboard to use) - )); + keyboard.reset(new KeyboardEntryActivity(renderer, inputManager, "Enter WiFi Password", + "", // No initial text + 64, // Max password length + false // Show password by default (hard keyboard to use) + )); updateRequired = true; } else { // Connect directly for open networks diff --git a/src/activities/network/WifiScreen.h b/src/activities/network/WifiScreen.h index a57c793..173b51f 100644 --- a/src/activities/network/WifiScreen.h +++ b/src/activities/network/WifiScreen.h @@ -10,7 +10,7 @@ #include #include "../Activity.h" -#include "OnScreenKeyboard.h" +#include "../util/KeyboardEntryActivity.h" // Structure to hold WiFi network information struct WifiNetworkInfo { @@ -46,7 +46,7 @@ class WifiScreen final : public Activity { bool selectedRequiresPassword = false; // On-screen keyboard for password entry - std::unique_ptr keyboard; + std::unique_ptr keyboard; // Connection result std::string connectedIP; diff --git a/src/activities/network/OnScreenKeyboard.cpp b/src/activities/util/KeyboardEntryActivity.cpp similarity index 86% rename from src/activities/network/OnScreenKeyboard.cpp rename to src/activities/util/KeyboardEntryActivity.cpp index fcbbbba..8db7b94 100644 --- a/src/activities/network/OnScreenKeyboard.cpp +++ b/src/activities/util/KeyboardEntryActivity.cpp @@ -1,34 +1,33 @@ -#include "OnScreenKeyboard.h" +#include "KeyboardEntryActivity.h" -#include "config.h" +#include "../../config.h" // Keyboard layouts - lowercase -const char* const OnScreenKeyboard::keyboard[NUM_ROWS] = { +const char* const KeyboardEntryActivity::keyboard[NUM_ROWS] = { "`1234567890-=", "qwertyuiop[]\\", "asdfghjkl;'", "zxcvbnm,./", "^ _____?", "^ _____?", "^ _____ 0 && text.length() > maxLength) { text = text.substr(0, maxLength); } } -void OnScreenKeyboard::reset(const std::string& newTitle, const std::string& newInitialText) { +void KeyboardEntryActivity::reset(const std::string& newTitle, const std::string& newInitialText) { if (!newTitle.empty()) { title = newTitle; } @@ -40,7 +39,22 @@ void OnScreenKeyboard::reset(const std::string& newTitle, const std::string& new cancelled = false; } -int OnScreenKeyboard::getRowLength(int row) const { +void KeyboardEntryActivity::onEnter() { + // Reset state when entering the activity + complete = false; + cancelled = false; +} + +void KeyboardEntryActivity::onExit() { + // Clean up if needed +} + +void KeyboardEntryActivity::loop() { + handleInput(); + render(10); +} + +int KeyboardEntryActivity::getRowLength(int row) const { if (row < 0 || row >= NUM_ROWS) return 0; // Return actual length of each row based on keyboard layout @@ -60,7 +74,7 @@ int OnScreenKeyboard::getRowLength(int row) const { } } -char OnScreenKeyboard::getSelectedChar() const { +char KeyboardEntryActivity::getSelectedChar() const { const char* const* layout = shiftActive ? keyboardShift : keyboard; if (selectedRow < 0 || selectedRow >= NUM_ROWS) return '\0'; @@ -69,7 +83,7 @@ char OnScreenKeyboard::getSelectedChar() const { return layout[selectedRow][selectedCol]; } -void OnScreenKeyboard::handleKeyPress() { +void KeyboardEntryActivity::handleKeyPress() { // Handle special row (bottom row with shift, space, backspace, done) if (selectedRow == SHIFT_ROW) { if (selectedCol == SHIFT_COL) { @@ -117,7 +131,7 @@ void OnScreenKeyboard::handleKeyPress() { } } -bool OnScreenKeyboard::handleInput() { +bool KeyboardEntryActivity::handleInput() { if (complete || cancelled) { return false; } @@ -179,7 +193,7 @@ bool OnScreenKeyboard::handleInput() { return handled; } -void OnScreenKeyboard::render(int startY) const { +void KeyboardEntryActivity::render(int startY) const { const auto pageWidth = GfxRenderer::getScreenWidth(); // Draw title diff --git a/src/activities/network/OnScreenKeyboard.h b/src/activities/util/KeyboardEntryActivity.h similarity index 74% rename from src/activities/network/OnScreenKeyboard.h rename to src/activities/util/KeyboardEntryActivity.h index e1511cd..af6d9b4 100644 --- a/src/activities/network/OnScreenKeyboard.h +++ b/src/activities/util/KeyboardEntryActivity.h @@ -5,18 +5,20 @@ #include #include +#include "../Activity.h" + /** - * Reusable on-screen keyboard component for text input. - * Can be embedded in any screen that needs text entry. + * Reusable keyboard entry activity for text input. + * Can be started from any activity that needs text entry. * * Usage: - * 1. Create an OnScreenKeyboard instance - * 2. Call render() to draw the keyboard - * 3. Call handleInput() to process button presses - * 4. When isComplete() returns true, get the result from getText() - * 5. Call isCancelled() to check if user cancelled input + * 1. Create a KeyboardEntryActivity instance + * 2. Set callbacks with setOnComplete() and setOnCancel() + * 3. Call onEnter() to start the activity + * 4. Call loop() in your main loop + * 5. When complete or cancelled, callbacks will be invoked */ -class OnScreenKeyboard { +class KeyboardEntryActivity : public Activity { public: // Callback types using OnCompleteCallback = std::function; @@ -31,20 +33,20 @@ class OnScreenKeyboard { * @param maxLength Maximum length of input text (0 for unlimited) * @param isPassword If true, display asterisks instead of actual characters */ - OnScreenKeyboard(GfxRenderer& renderer, InputManager& inputManager, const std::string& title = "Enter Text", - const std::string& initialText = "", size_t maxLength = 0, bool isPassword = false); + KeyboardEntryActivity(GfxRenderer& renderer, InputManager& inputManager, const std::string& title = "Enter Text", + const std::string& initialText = "", size_t maxLength = 0, bool isPassword = false); /** - * Handle button input. Call this in your screen's handleInput(). + * Handle button input. Call this in your main loop. * @return true if input was handled, false otherwise */ bool handleInput(); /** * Render the keyboard at the specified Y position. - * @param startY Y-coordinate where keyboard rendering starts + * @param startY Y-coordinate where keyboard rendering starts (default 10) */ - void render(int startY) const; + void render(int startY = 10) const; /** * Get the current text entered by the user. @@ -81,10 +83,12 @@ class OnScreenKeyboard { */ void setOnCancel(OnCancelCallback callback) { onCancel = callback; } - private: - GfxRenderer& renderer; - InputManager& inputManager; + // Activity overrides + void onEnter() override; + void onExit() override; + void loop() override; + private: std::string title; std::string text; size_t maxLength;