From fe119239ad2cbafcd6a2037fb7ec68f8914b07b5 Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Thu, 29 Jan 2026 02:33:56 +0600 Subject: [PATCH] feat: `ButtonNavigator::onPressAndContinuous` helper function --- src/activities/util/KeyboardEntryActivity.cpp | 33 +++++-------------- src/util/ButtonNavigator.cpp | 6 ++++ src/util/ButtonNavigator.h | 17 +++++----- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/activities/util/KeyboardEntryActivity.cpp b/src/activities/util/KeyboardEntryActivity.cpp index 828faaae..301e67b0 100644 --- a/src/activities/util/KeyboardEntryActivity.cpp +++ b/src/activities/util/KeyboardEntryActivity.cpp @@ -139,23 +139,23 @@ void KeyboardEntryActivity::handleKeyPress() { void KeyboardEntryActivity::loop() { // Handle navigation - const auto upButtonCallback = [this] { + buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Up}, [this] { selectedRow = ButtonNavigator::previousIndex(selectedRow, NUM_ROWS); const int maxCol = getRowLength(selectedRow) - 1; if (selectedCol > maxCol) selectedCol = maxCol; updateRequired = true; - }; + }); - const auto downButtonCallback = [this] { + buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Down}, [this] { selectedRow = ButtonNavigator::nextIndex(selectedRow, NUM_ROWS); const int maxCol = getRowLength(selectedRow) - 1; if (selectedCol > maxCol) selectedCol = maxCol; updateRequired = true; - }; + }); - const auto leftButtonCallback = [this] { + buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Left}, [this] { const int maxCol = getRowLength(selectedRow) - 1; // Special bottom row case @@ -179,9 +179,9 @@ void KeyboardEntryActivity::loop() { } updateRequired = true; - }; + }); - const auto rightButtonCallback = [this] { + buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Right}, [this] { const int maxCol = getRowLength(selectedRow) - 1; // Special bottom row case @@ -204,24 +204,7 @@ void KeyboardEntryActivity::loop() { selectedCol = ButtonNavigator::nextIndex(selectedCol, maxCol + 1); } updateRequired = true; - }; - - constexpr auto upButton = MappedInputManager::Button::Up; - constexpr auto downButton = MappedInputManager::Button::Down; - constexpr auto leftButton = MappedInputManager::Button::Left; - constexpr auto rightButton = MappedInputManager::Button::Right; - - buttonNavigator.onPress({upButton}, upButtonCallback); - buttonNavigator.onContinuous({upButton}, upButtonCallback); - - buttonNavigator.onPress({downButton}, downButtonCallback); - buttonNavigator.onContinuous({downButton}, downButtonCallback); - - buttonNavigator.onPress({leftButton}, leftButtonCallback); - buttonNavigator.onContinuous({leftButton}, leftButtonCallback); - - buttonNavigator.onPress({rightButton}, rightButtonCallback); - buttonNavigator.onContinuous({rightButton}, rightButtonCallback); + }); // Selection if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) { diff --git a/src/util/ButtonNavigator.cpp b/src/util/ButtonNavigator.cpp index c041aa1d..f79229dd 100644 --- a/src/util/ButtonNavigator.cpp +++ b/src/util/ButtonNavigator.cpp @@ -6,11 +6,17 @@ void ButtonNavigator::onNext(const Callback& callback) { onNextPress(callback); onNextContinuous(callback); } + void ButtonNavigator::onPrevious(const Callback& callback) { onPreviousPress(callback); onPreviousContinuous(callback); } +void ButtonNavigator::onPressAndContinuous(const Buttons& buttons, const Callback& callback) { + onPress(buttons, callback); + onContinuous(buttons, callback); +} + void ButtonNavigator::onNextPress(const Callback& callback) { onPress(getNextButtons(), callback); } void ButtonNavigator::onPreviousPress(const Callback& callback) { onPress(getPreviousButtons(), callback); } diff --git a/src/util/ButtonNavigator.h b/src/util/ButtonNavigator.h index 3e88f315..2f9afbc1 100644 --- a/src/util/ButtonNavigator.h +++ b/src/util/ButtonNavigator.h @@ -12,19 +12,10 @@ class ButtonNavigator final { const uint16_t continuousStartMs; const uint16_t continuousIntervalMs; uint32_t lastContinuousNavTime = 0; - bool continuousNavHold = false; - static const MappedInputManager* mappedInput; [[nodiscard]] bool shouldNavigateContinuously() const; - [[nodiscard]] static Buttons getNextButtons() { - return {MappedInputManager::Button::Down, MappedInputManager::Button::Right}; - } - [[nodiscard]] static Buttons getPreviousButtons() { - return {MappedInputManager::Button::Up, MappedInputManager::Button::Left}; - } - public: explicit ButtonNavigator(const uint16_t continuousIntervalMs = 500, const uint16_t continuousStartMs = 500) : continuousStartMs(continuousStartMs), continuousIntervalMs(continuousIntervalMs) {} @@ -33,6 +24,7 @@ class ButtonNavigator final { void onNext(const Callback& callback); void onPrevious(const Callback& callback); + void onPressAndContinuous(const Buttons& buttons, const Callback& callback); void onNextPress(const Callback& callback); void onPreviousPress(const Callback& callback); @@ -51,4 +43,11 @@ class ButtonNavigator final { [[nodiscard]] static int nextPageIndex(int currentIndex, int totalItems, int itemsPerPage); [[nodiscard]] static int previousPageIndex(int currentIndex, int totalItems, int itemsPerPage); + + [[nodiscard]] static Buttons getNextButtons() { + return {MappedInputManager::Button::Down, MappedInputManager::Button::Right}; + } + [[nodiscard]] static Buttons getPreviousButtons() { + return {MappedInputManager::Button::Up, MappedInputManager::Button::Left}; + } }; \ No newline at end of file