sort of working dictionary

This commit is contained in:
cottongin
2026-01-22 12:42:01 -05:00
parent ff22a82563
commit 9493fb1f18
24 changed files with 2887 additions and 11 deletions

View File

@@ -0,0 +1,56 @@
#pragma once
#include "CrossPointSettings.h"
#include "GfxRenderer.h"
/**
* Calculate content margins for dictionary activities that use button hints.
* Uses the same base margin pattern as EpubReaderActivity, then adds space
* for button hints at the correct logical edges based on orientation.
*
* Physical button locations (fixed on device):
* - Front buttons: physical X=760 (right edge of 800-pixel wide panel)
* - Side buttons: physical Y=44 (top area of 480-pixel tall panel)
*
* These map to different logical edges depending on orientation:
* - Portrait: Front=BOTTOM, Side=RIGHT
* - LandscapeCW: Front=LEFT, Side=BOTTOM
* - PortraitInverted: Front=TOP, Side=LEFT
* - LandscapeCCW: Front=RIGHT, Side=TOP
*/
inline void getDictionaryContentMargins(GfxRenderer& renderer, int* outTop, int* outRight, int* outBottom,
int* outLeft) {
// Start with same base margins as reader (getOrientedViewableTRBL + screenMargin)
renderer.getOrientedViewableTRBL(outTop, outRight, outBottom, outLeft);
*outTop += SETTINGS.screenMargin;
*outLeft += SETTINGS.screenMargin;
*outRight += SETTINGS.screenMargin;
*outBottom += SETTINGS.screenMargin;
// Add button hint space to the correct edges based on orientation
constexpr int FRONT_BUTTON_SPACE = 45; // 40px button height + 5px padding
constexpr int SIDE_BUTTON_SPACE = 50; // 45px button area + 5px padding
switch (renderer.getOrientation()) {
case GfxRenderer::Portrait:
// Front buttons at logical BOTTOM, Side buttons at logical RIGHT
*outBottom += FRONT_BUTTON_SPACE;
*outRight += SIDE_BUTTON_SPACE;
break;
case GfxRenderer::LandscapeClockwise:
// Front buttons at logical LEFT, Side buttons at logical BOTTOM
*outLeft += FRONT_BUTTON_SPACE;
*outBottom += SIDE_BUTTON_SPACE;
break;
case GfxRenderer::PortraitInverted:
// Front buttons at logical TOP, Side buttons at logical LEFT
*outTop += FRONT_BUTTON_SPACE;
*outLeft += SIDE_BUTTON_SPACE;
break;
case GfxRenderer::LandscapeCounterClockwise:
// Front buttons at logical RIGHT, Side buttons at logical TOP
*outRight += FRONT_BUTTON_SPACE;
*outTop += SIDE_BUTTON_SPACE;
break;
}
}

View File

@@ -0,0 +1,146 @@
#include "DictionaryMenuActivity.h"
#include <GfxRenderer.h>
#include "DictionaryMargins.h"
#include "MappedInputManager.h"
#include "fontIds.h"
namespace {
constexpr int MAX_MENU_ITEM_COUNT = 2;
const char* MENU_ITEMS[MAX_MENU_ITEM_COUNT] = {"Select from Screen", "Enter a Word"};
const char* MENU_DESCRIPTIONS[MAX_MENU_ITEM_COUNT] = {"Choose a word from the current page",
"Type a word to look up"};
} // namespace
void DictionaryMenuActivity::taskTrampoline(void* param) {
auto* self = static_cast<DictionaryMenuActivity*>(param);
self->displayTaskLoop();
}
void DictionaryMenuActivity::onEnter() {
Activity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
// Reset selection
selectedIndex = 0;
// Trigger first update
updateRequired = true;
xTaskCreate(&DictionaryMenuActivity::taskTrampoline, "DictMenuTask",
2048, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void DictionaryMenuActivity::onExit() {
Activity::onExit();
// Wait until not rendering to delete task
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void DictionaryMenuActivity::loop() {
const int menuItemCount = wordSelectionAvailable ? 2 : 1;
// Handle back button - cancel
// Use wasReleased to consume the full button event
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onCancel();
return;
}
// Handle confirm button - select current option
// Use wasReleased to consume the full button event
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const DictionaryMode mode =
(selectedIndex == 0) ? DictionaryMode::SELECT_FROM_SCREEN : DictionaryMode::ENTER_WORD;
onModeSelected(mode);
return;
}
// Handle navigation (only if multiple options available)
if (menuItemCount > 1) {
const bool prevPressed = mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Left);
const bool nextPressed = mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Right);
if (prevPressed) {
selectedIndex = (selectedIndex + menuItemCount - 1) % menuItemCount;
updateRequired = true;
} else if (nextPressed) {
selectedIndex = (selectedIndex + 1) % menuItemCount;
updateRequired = true;
}
}
}
void DictionaryMenuActivity::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
render();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void DictionaryMenuActivity::render() const {
renderer.clearScreen();
// Get margins using same pattern as reader + button hint space
int marginTop, marginRight, marginBottom, marginLeft;
getDictionaryContentMargins(renderer, &marginTop, &marginRight, &marginBottom, &marginLeft);
const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight();
const int menuItemCount = wordSelectionAvailable ? 2 : 1;
// Calculate usable content area
const int contentWidth = pageWidth - marginLeft - marginRight;
const int contentHeight = pageHeight - marginTop - marginBottom;
// Draw header with top margin
renderer.drawCenteredText(UI_12_FONT_ID, marginTop + 15, "Dictionary", true, EpdFontFamily::BOLD);
// Draw subtitle
renderer.drawCenteredText(UI_10_FONT_ID, marginTop + 50, "Look up a word");
// Draw menu items centered in content area
constexpr int itemHeight = 50; // Height for each menu item (including description)
const int startY = marginTop + (contentHeight - (menuItemCount * itemHeight)) / 2;
for (int i = 0; i < menuItemCount; i++) {
const int itemY = startY + i * itemHeight;
const bool isSelected = (i == selectedIndex);
// Draw selection highlight (black fill) for selected item
if (isSelected) {
renderer.fillRect(marginLeft + 10, itemY - 2, contentWidth - 20, itemHeight - 6);
}
// Draw text: black=false (white text) when selected (on black background)
// black=true (black text) when not selected (on white background)
renderer.drawText(UI_10_FONT_ID, marginLeft + 20, itemY, MENU_ITEMS[i], /*black=*/!isSelected);
renderer.drawText(SMALL_FONT_ID, marginLeft + 20, itemY + 22, MENU_DESCRIPTIONS[i], /*black=*/!isSelected);
}
// Draw help text at bottom
const auto labels = mappedInput.mapLabels("\xc2\xab Back", "Select", "", "");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
}

View File

@@ -0,0 +1,45 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <functional>
#include "../Activity.h"
// Enum for dictionary mode selection
enum class DictionaryMode { ENTER_WORD, SELECT_FROM_SCREEN };
/**
* DictionaryMenuActivity presents the user with a choice:
* - "Enter a Word" - Manually type a word to look up
* - "Select from Screen" - Select a word from the current page
*
* The onModeSelected callback is called with the user's choice.
* The onCancel callback is called if the user presses back.
*/
class DictionaryMenuActivity final : public Activity {
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
int selectedIndex = 0;
bool updateRequired = false;
const std::function<void(DictionaryMode)> onModeSelected;
const std::function<void()> onCancel;
const bool wordSelectionAvailable; // True if we can select from screen (e.g., in EPUB reader)
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void render() const;
public:
explicit DictionaryMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::function<void(DictionaryMode)>& onModeSelected,
const std::function<void()>& onCancel, bool wordSelectionAvailable = true)
: Activity("DictionaryMenu", renderer, mappedInput),
onModeSelected(onModeSelected),
onCancel(onCancel),
wordSelectionAvailable(wordSelectionAvailable) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@@ -0,0 +1,199 @@
#include "DictionaryResultActivity.h"
#include <DictHtmlParser.h>
#include <GfxRenderer.h>
#include "DictionaryMargins.h"
#include "MappedInputManager.h"
#include "fontIds.h"
void DictionaryResultActivity::taskTrampoline(void* param) {
auto* self = static_cast<DictionaryResultActivity*>(param);
self->displayTaskLoop();
}
void DictionaryResultActivity::onEnter() {
Activity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
currentPage = 0;
// Process definition for display
if (!notFound) {
paginateDefinition();
}
updateRequired = true;
xTaskCreate(&DictionaryResultActivity::taskTrampoline, "DictResultTask",
4096, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void DictionaryResultActivity::onExit() {
Activity::onExit();
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void DictionaryResultActivity::loop() {
// Handle back button - use wasReleased to consume the full button event
// This prevents the release event from propagating to parent activities
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onBack();
return;
}
// Handle confirm button - search another word
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
onSearchAnother();
return;
}
// Handle page navigation - use orientation-aware PageBack/PageForward buttons
if (!notFound && pages.size() > 1) {
const bool prevPressed = mappedInput.wasPressed(MappedInputManager::Button::PageBack) ||
mappedInput.wasPressed(MappedInputManager::Button::Left);
const bool nextPressed = mappedInput.wasPressed(MappedInputManager::Button::PageForward) ||
mappedInput.wasPressed(MappedInputManager::Button::Right);
if (prevPressed && currentPage > 0) {
currentPage--;
updateRequired = true;
} else if (nextPressed && currentPage < static_cast<int>(pages.size()) - 1) {
currentPage++;
updateRequired = true;
}
}
}
void DictionaryResultActivity::paginateDefinition() {
pages.clear();
if (rawDefinition.empty()) {
notFound = true;
return;
}
// Get margins using same pattern as reader + button hint space
int marginTop, marginRight, marginBottom, marginLeft;
getDictionaryContentMargins(renderer, &marginTop, &marginRight, &marginBottom, &marginLeft);
const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight();
// Calculate available area for text (must match render() layout)
constexpr int headerHeight = 80; // Space for word and header (relative to marginTop)
constexpr int footerHeight = 30; // Space for page indicator
const int textMargin = marginLeft + 10;
const int textWidth = pageWidth - textMargin - marginRight - 10;
const int textHeight = pageHeight - marginTop - marginBottom - headerHeight - footerHeight;
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
// Collect all TextBlocks from the HTML parser
std::vector<std::shared_ptr<TextBlock>> allBlocks;
DictHtmlParser::parse(rawDefinition, UI_10_FONT_ID, renderer, textWidth,
[&allBlocks](std::shared_ptr<TextBlock> block) { allBlocks.push_back(block); });
if (allBlocks.empty()) {
notFound = true;
return;
}
// Paginate: group TextBlocks into pages based on available height
std::vector<std::shared_ptr<TextBlock>> currentPageBlocks;
int currentY = 0;
for (const auto& block : allBlocks) {
// Each TextBlock is one line of text
if (currentY + lineHeight > textHeight && !currentPageBlocks.empty()) {
// Page is full, start new page
pages.push_back(currentPageBlocks);
currentPageBlocks.clear();
currentY = 0;
}
currentPageBlocks.push_back(block);
currentY += lineHeight;
}
// Add remaining blocks as last page
if (!currentPageBlocks.empty()) {
pages.push_back(currentPageBlocks);
}
}
void DictionaryResultActivity::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
render();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void DictionaryResultActivity::render() const {
renderer.clearScreen();
// Get margins using same pattern as reader + button hint space
int marginTop, marginRight, marginBottom, marginLeft;
getDictionaryContentMargins(renderer, &marginTop, &marginRight, &marginBottom, &marginLeft);
const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight();
// Draw header with top margin
renderer.drawCenteredText(UI_12_FONT_ID, marginTop + 15, "Dictionary", true, EpdFontFamily::BOLD);
// Draw word being looked up (bold)
renderer.drawCenteredText(UI_12_FONT_ID, marginTop + 50, lookupWord.c_str(), true, EpdFontFamily::BOLD);
if (notFound) {
// Show not found message (centered in content area)
const int centerY = marginTop + (pageHeight - marginTop - marginBottom) / 2;
renderer.drawCenteredText(UI_10_FONT_ID, centerY, "Word not found");
} else if (!pages.empty()) {
// Draw definition text using TextBlocks with rich formatting
const int textStartY = marginTop + 80;
const int textMargin = marginLeft + 10;
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
const int bottomLimit = pageHeight - marginBottom - 25; // Leave space for page indicator
const auto& pageBlocks = pages[currentPage];
int y = textStartY;
// Render each TextBlock
for (const auto& block : pageBlocks) {
if (y >= bottomLimit) break;
block->render(renderer, UI_10_FONT_ID, textMargin, y);
y += lineHeight;
}
// Draw page indicator if multiple pages
if (pages.size() > 1) {
char pageIndicator[32];
snprintf(pageIndicator, sizeof(pageIndicator), "Page %d of %d", currentPage + 1, static_cast<int>(pages.size()));
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight - marginBottom - 5, pageIndicator);
}
}
// Draw button hints
const char* leftHint = (pages.size() > 1 && currentPage > 0) ? "< Prev" : "";
const char* rightHint = (pages.size() > 1 && currentPage < static_cast<int>(pages.size()) - 1) ? "Next >" : "";
const auto labels = mappedInput.mapLabels("\xc2\xab Back", "Search", leftHint, rightHint);
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
}

View File

@@ -0,0 +1,63 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <Epub/blocks/TextBlock.h>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "../Activity.h"
/**
* DictionaryResultActivity displays a word definition with pagination.
* Supports multi-page definitions with navigation and rich text formatting.
*/
class DictionaryResultActivity final : public Activity {
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
bool updateRequired = false;
const std::string lookupWord; // Named to avoid Arduino's 'word' macro
const std::string rawDefinition;
const std::function<void()> onBack;
const std::function<void()> onSearchAnother;
// Pagination - each page contains TextBlocks with styled text
std::vector<std::vector<std::shared_ptr<TextBlock>>> pages;
int currentPage = 0;
bool notFound = false;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void render() const;
void paginateDefinition();
public:
/**
* Constructor
* @param renderer Graphics renderer
* @param mappedInput Input manager
* @param wordToLookup The word that was looked up
* @param definition The definition text (HTML will be stripped). Empty = not found.
* @param onBack Callback when user wants to go back to book
* @param onSearchAnother Callback when user wants to search another word
*/
explicit DictionaryResultActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::string& wordToLookup, const std::string& definition,
const std::function<void()>& onBack,
const std::function<void()>& onSearchAnother)
: Activity("DictionaryResult", renderer, mappedInput),
lookupWord(wordToLookup),
rawDefinition(definition),
onBack(onBack),
onSearchAnother(onSearchAnother),
notFound(definition.empty()) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@@ -0,0 +1,261 @@
#include "DictionarySearchActivity.h"
#include <GfxRenderer.h>
#include <HardwareSerial.h>
#include <StarDict.h>
#include "DictionaryMargins.h"
#include "DictionaryResultActivity.h"
#include "MappedInputManager.h"
#include "activities/util/KeyboardEntryActivity.h"
#include "fontIds.h"
namespace {
// Dictionary path on SD card
constexpr const char* DICT_BASE_PATH = "/dictionaries/dict-data";
// Global dictionary instance (lazy initialized)
StarDict* g_dictionary = nullptr;
StarDict& getDictionary() {
if (!g_dictionary) {
g_dictionary = new StarDict(DICT_BASE_PATH);
if (!g_dictionary->begin()) {
Serial.printf("[%lu] [DICT] Failed to initialize dictionary\n", millis());
}
}
return *g_dictionary;
}
} // namespace
void DictionarySearchActivity::taskTrampoline(void* param) {
auto* self = static_cast<DictionarySearchActivity*>(param);
self->displayTaskLoop();
}
void DictionarySearchActivity::onEnter() {
ActivityWithSubactivity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
isSearching = false;
keyboardShown = false;
searchStatus = "";
updateRequired = true;
xTaskCreate(&DictionarySearchActivity::taskTrampoline, "DictSearchTask",
4096, // Stack size (needs more for dictionary operations)
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
// If no initial word provided, show keyboard
if (searchWord.empty()) {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
keyboardShown = true;
enterNewActivity(new KeyboardEntryActivity(
renderer, mappedInput, "Enter Word", "", 10,
64, // maxLength
false, // not password
[this](const std::string& word) {
// User entered a word
exitActivity();
searchWord = word;
keyboardShown = false;
if (!word.empty()) {
performSearch(word);
} else {
onBack();
}
},
[this]() {
// User cancelled keyboard
exitActivity();
keyboardShown = false;
onBack();
}));
xSemaphoreGive(renderingMutex);
} else {
// Perform search with provided word
performSearch(searchWord);
}
}
void DictionarySearchActivity::onExit() {
ActivityWithSubactivity::onExit();
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void DictionarySearchActivity::loop() {
if (subActivity) {
subActivity->loop();
return;
}
// Handle back button - use wasReleased to consume the full button event
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onBack();
return;
}
}
void DictionarySearchActivity::performSearch(const std::string& word) {
isSearching = true;
searchStatus = "Searching...";
updateRequired = true;
// Small delay to allow render
vTaskDelay(50 / portTICK_PERIOD_MS);
// Initialize dictionary if needed
StarDict& dict = getDictionary();
if (!dict.isReady()) {
searchStatus = "Dictionary not found";
isSearching = false;
updateRequired = true;
return;
}
// Perform lookup
const auto result = dict.lookup(word);
if (result.found) {
showResult(result.word, result.definition);
} else {
showNotFound(word);
}
}
void DictionarySearchActivity::showResult(const std::string& word, const std::string& definition) {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
isSearching = false;
exitActivity();
enterNewActivity(new DictionaryResultActivity(
renderer, mappedInput, word, definition,
[this]() {
// Back from result
exitActivity();
onBack();
},
[this]() {
// Search another word
exitActivity();
searchWord = "";
keyboardShown = true;
enterNewActivity(new KeyboardEntryActivity(
renderer, mappedInput, "Enter Word", "", 10, 64, false,
[this](const std::string& newWord) {
exitActivity();
keyboardShown = false;
if (!newWord.empty()) {
performSearch(newWord);
} else {
onBack();
}
},
[this]() {
exitActivity();
keyboardShown = false;
onBack();
}));
}));
xSemaphoreGive(renderingMutex);
}
void DictionarySearchActivity::showNotFound(const std::string& word) {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
isSearching = false;
exitActivity();
enterNewActivity(new DictionaryResultActivity(
renderer, mappedInput, word, "", // Empty definition = not found
[this]() {
// Back from result
exitActivity();
onBack();
},
[this]() {
// Search another word
exitActivity();
searchWord = "";
keyboardShown = true;
enterNewActivity(new KeyboardEntryActivity(
renderer, mappedInput, "Enter Word", "", 10, 64, false,
[this](const std::string& newWord) {
exitActivity();
keyboardShown = false;
if (!newWord.empty()) {
performSearch(newWord);
} else {
onBack();
}
},
[this]() {
exitActivity();
keyboardShown = false;
onBack();
}));
}));
xSemaphoreGive(renderingMutex);
}
void DictionarySearchActivity::displayTaskLoop() {
int animationCounter = 0;
constexpr int ANIMATION_INTERVAL = 30; // ~300ms at 10ms per tick
while (true) {
// Handle animation updates when searching
if (isSearching && !subActivity) {
animationCounter++;
if (animationCounter >= ANIMATION_INTERVAL) {
animationCounter = 0;
animationFrame = (animationFrame + 1) % 3;
updateRequired = true;
}
}
if (updateRequired && !subActivity) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
render();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void DictionarySearchActivity::render() const {
renderer.clearScreen();
// Get margins using same pattern as reader + button hint space
int marginTop, marginRight, marginBottom, marginLeft;
getDictionaryContentMargins(renderer, &marginTop, &marginRight, &marginBottom, &marginLeft);
const auto pageHeight = renderer.getScreenHeight();
// Draw header with top margin
renderer.drawCenteredText(UI_12_FONT_ID, marginTop + 15, "Dictionary", true, EpdFontFamily::BOLD);
if (isSearching) {
// Show searching status with word and animated ellipsis
// Center in content area (accounting for margins)
const int centerY = marginTop + (pageHeight - marginTop - marginBottom) / 2;
// Build animated ellipsis
const char* dots = (animationFrame == 0) ? "." : (animationFrame == 1) ? ".." : "...";
// Show "Searching for 'word'..."
char statusText[128];
snprintf(statusText, sizeof(statusText), "Searching for '%s'%s", searchWord.c_str(), dots);
renderer.drawCenteredText(UI_10_FONT_ID, centerY, statusText);
}
renderer.displayBuffer();
}

View File

@@ -0,0 +1,51 @@
#pragma once
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <functional>
#include <string>
#include "../ActivityWithSubactivity.h"
/**
* DictionarySearchActivity handles the dictionary lookup flow:
* - If no word is provided, shows keyboard for entry
* - Performs StarDict lookup
* - Shows result in DictionaryResultActivity
*/
class DictionarySearchActivity final : public ActivityWithSubactivity {
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
bool updateRequired = false;
const std::function<void()> onBack;
std::string searchWord;
bool keyboardShown = false;
bool isSearching = false;
std::string searchStatus;
int animationFrame = 0; // For ellipsis animation (0-2)
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void render() const;
void performSearch(const std::string& word);
void showResult(const std::string& word, const std::string& definition);
void showNotFound(const std::string& word);
public:
/**
* Constructor
* @param renderer Graphics renderer
* @param mappedInput Input manager
* @param onBack Callback when user wants to go back
* @param initialWord Optional word to look up immediately (if empty, shows keyboard)
*/
explicit DictionarySearchActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::function<void()>& onBack, const std::string& initialWord = "")
: ActivityWithSubactivity("DictionarySearch", renderer, mappedInput), onBack(onBack), searchWord(initialWord) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@@ -0,0 +1,263 @@
#include "EpubWordSelectionActivity.h"
#include <EInkDisplay.h>
#include <GfxRenderer.h>
#include <algorithm>
#include <cctype>
#include "DictionaryMargins.h"
#include "MappedInputManager.h"
#include "fontIds.h"
void EpubWordSelectionActivity::taskTrampoline(void* param) {
auto* self = static_cast<EpubWordSelectionActivity*>(param);
self->displayTaskLoop();
}
void EpubWordSelectionActivity::onEnter() {
Activity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
selectedWordIndex = 0;
currentLineIndex = 0;
// Build list of all words on the page
buildWordList();
updateRequired = true;
xTaskCreate(&EpubWordSelectionActivity::taskTrampoline, "WordSelectTask",
4096, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void EpubWordSelectionActivity::onExit() {
Activity::onExit();
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
}
void EpubWordSelectionActivity::buildWordList() {
allWords.clear();
if (!page) return;
const int lineHeight = renderer.getLineHeight(fontId);
for (const auto& element : page->elements) {
// All page elements are PageLine (only type in PageElementTag enum)
const auto* pageLine = static_cast<PageLine*>(element.get());
if (!pageLine) continue;
const auto& textBlock = pageLine->getTextBlock();
if (!textBlock || textBlock->getWordCount() == 0) {
continue;
}
const auto& words = textBlock->getWords();
const auto& xPositions = textBlock->getWordXPositions();
const auto& styles = textBlock->getWordStyles();
auto wordIt = words.begin();
auto xPosIt = xPositions.begin();
auto styleIt = styles.begin();
while (wordIt != words.end() && xPosIt != xPositions.end() && styleIt != styles.end()) {
// Skip whitespace-only words
const std::string& wordText = *wordIt;
bool hasAlpha = false;
for (char c : wordText) {
if (std::isalpha(static_cast<unsigned char>(c))) {
hasAlpha = true;
break;
}
}
if (hasAlpha) {
WordInfo info;
info.text = wordText;
info.x = *xPosIt + pageLine->xPos + xOffset;
info.y = pageLine->yPos + yOffset;
info.width = renderer.getTextWidth(fontId, wordText.c_str(), *styleIt);
info.height = lineHeight;
info.style = *styleIt;
allWords.push_back(info);
}
++wordIt;
++xPosIt;
++styleIt;
}
}
}
int EpubWordSelectionActivity::findLineForWordIndex(int wordIndex) const {
if (wordIndex < 0 || wordIndex >= static_cast<int>(allWords.size())) return 0;
const int targetY = allWords[wordIndex].y;
int lineIdx = 0;
int lastY = -1;
for (size_t i = 0; i <= static_cast<size_t>(wordIndex); i++) {
if (allWords[i].y != lastY) {
if (lastY >= 0) lineIdx++;
lastY = allWords[i].y;
}
}
return lineIdx;
}
int EpubWordSelectionActivity::findWordIndexForLine(int lineIndex) const {
if (allWords.empty()) return 0;
int currentLine = 0;
int lastY = allWords[0].y;
for (size_t i = 0; i < allWords.size(); i++) {
if (allWords[i].y != lastY) {
currentLine++;
lastY = allWords[i].y;
}
if (currentLine == lineIndex) {
return static_cast<int>(i);
}
}
// If line not found, return last word
return static_cast<int>(allWords.size()) - 1;
}
void EpubWordSelectionActivity::loop() {
if (allWords.empty()) {
onCancel();
return;
}
// Handle back button - cancel
// Use wasReleased to consume the full button event
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onCancel();
return;
}
// Handle confirm button - select current word
// Use wasReleased to consume the full button event
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
// Clean up the word (remove leading/trailing punctuation)
std::string selectedWord = allWords[selectedWordIndex].text;
// Strip em-space prefix if present
if (selectedWord.size() >= 3 && static_cast<uint8_t>(selectedWord[0]) == 0xE2 &&
static_cast<uint8_t>(selectedWord[1]) == 0x80 && static_cast<uint8_t>(selectedWord[2]) == 0x83) {
selectedWord = selectedWord.substr(3);
}
// Strip leading/trailing non-alpha characters
while (!selectedWord.empty() && !std::isalpha(static_cast<unsigned char>(selectedWord.front()))) {
selectedWord.erase(0, 1);
}
while (!selectedWord.empty() && !std::isalpha(static_cast<unsigned char>(selectedWord.back()))) {
selectedWord.pop_back();
}
if (!selectedWord.empty()) {
onWordSelected(selectedWord);
} else {
onCancel();
}
return;
}
// Handle navigation
const bool leftPressed = mappedInput.wasPressed(MappedInputManager::Button::Left);
const bool rightPressed = mappedInput.wasPressed(MappedInputManager::Button::Right);
const bool upPressed = mappedInput.wasPressed(MappedInputManager::Button::Up);
const bool downPressed = mappedInput.wasPressed(MappedInputManager::Button::Down);
if (leftPressed && selectedWordIndex > 0) {
selectedWordIndex--;
currentLineIndex = findLineForWordIndex(selectedWordIndex);
updateRequired = true;
} else if (rightPressed && selectedWordIndex < static_cast<int>(allWords.size()) - 1) {
selectedWordIndex++;
currentLineIndex = findLineForWordIndex(selectedWordIndex);
updateRequired = true;
} else if (upPressed) {
// Move to previous line
if (currentLineIndex > 0) {
currentLineIndex--;
selectedWordIndex = findWordIndexForLine(currentLineIndex);
updateRequired = true;
}
} else if (downPressed) {
// Move to next line
const int lastLine = findLineForWordIndex(static_cast<int>(allWords.size()) - 1);
if (currentLineIndex < lastLine) {
currentLineIndex++;
selectedWordIndex = findWordIndexForLine(currentLineIndex);
updateRequired = true;
}
}
}
void EpubWordSelectionActivity::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
render();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void EpubWordSelectionActivity::render() const {
renderer.clearScreen();
// Get margins using same pattern as reader + button hint space
int marginTop, marginRight, marginBottom, marginLeft;
getDictionaryContentMargins(renderer, &marginTop, &marginRight, &marginBottom, &marginLeft);
// Draw the page content (uses pre-calculated offsets from reader)
// The page already has proper offsets, so render as-is
if (page) {
page->render(renderer, fontId, xOffset, yOffset);
}
// Highlight the selected word with an inverted rectangle
if (!allWords.empty() && selectedWordIndex >= 0 && selectedWordIndex < static_cast<int>(allWords.size())) {
const WordInfo& selected = allWords[selectedWordIndex];
// Draw selection box (inverted colors)
constexpr int padding = 2;
renderer.fillRect(selected.x - padding, selected.y - padding, selected.width + padding * 2,
selected.height + padding * 2);
// Redraw the word in white on black
renderer.drawText(fontId, selected.x, selected.y, selected.text.c_str(), false, selected.style);
}
// Draw instruction text - position it just above the front button area
const auto screenHeight = renderer.getScreenHeight();
renderer.drawCenteredText(SMALL_FONT_ID, screenHeight - marginBottom - 10, "Navigate with arrows, select with confirm");
// Draw button hints
const auto labels = mappedInput.mapLabels("\xc2\xab Cancel", "Select", "< >", "");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer(EInkDisplay::FAST_REFRESH);
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include <Epub/Page.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "../Activity.h"
/**
* EpubWordSelectionActivity allows selecting a word from the current EPUB page.
* Displays the page with a cursor that can navigate between words.
*/
class EpubWordSelectionActivity final : public Activity {
// Word info for selection
struct WordInfo {
std::string text; // Named 'text' to avoid Arduino's 'word' macro
int16_t x;
int16_t y;
int16_t width;
int16_t height;
EpdFontFamily::Style style;
};
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
bool updateRequired = false;
const std::unique_ptr<Page> page;
const int fontId;
const int xOffset;
const int yOffset;
const std::function<void(const std::string&)> onWordSelected;
const std::function<void()> onCancel;
// Word navigation state
std::vector<WordInfo> allWords;
int selectedWordIndex = 0;
int currentLineIndex = 0;
static void taskTrampoline(void* param);
[[noreturn]] void displayTaskLoop();
void render() const;
void buildWordList();
int findWordIndexForLine(int lineIndex) const;
int findLineForWordIndex(int wordIndex) const;
public:
/**
* Constructor
* @param renderer Graphics renderer
* @param mappedInput Input manager
* @param page The current page to select words from (ownership transferred)
* @param fontId Font ID used for rendering
* @param xOffset X offset for rendering
* @param yOffset Y offset for rendering
* @param onWordSelected Callback when a word is selected
* @param onCancel Callback when selection is cancelled
*/
explicit EpubWordSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Page> page,
int fontId, int xOffset, int yOffset,
const std::function<void(const std::string&)>& onWordSelected,
const std::function<void()>& onCancel)
: Activity("EpubWordSelection", renderer, mappedInput),
page(std::move(page)),
fontId(fontId),
xOffset(xOffset),
yOffset(yOffset),
onWordSelected(onWordSelected),
onCancel(onCancel) {}
void onEnter() override;
void onExit() override;
void loop() override;
};

View File

@@ -11,6 +11,9 @@
#include "MappedInputManager.h"
#include "RecentBooksStore.h"
#include "ScreenComponents.h"
#include "activities/dictionary/DictionaryMenuActivity.h"
#include "activities/dictionary/DictionarySearchActivity.h"
#include "activities/dictionary/EpubWordSelectionActivity.h"
#include "fontIds.h"
namespace {
@@ -163,6 +166,89 @@ void EpubReaderActivity::loop() {
return;
}
// Dictionary power button press
if (SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::DICTIONARY &&
mappedInput.wasReleased(MappedInputManager::Button::Power)) {
xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity();
enterNewActivity(new DictionaryMenuActivity(
renderer, mappedInput,
[this](DictionaryMode mode) {
// CRITICAL: Cache all needed values BEFORE exitActivity() destroys the lambda's owner
// The lambda is stored in DictionaryMenuActivity, so exitActivity() destroys it
GfxRenderer& cachedRenderer = renderer;
MappedInputManager& cachedMappedInput = mappedInput;
Section* cachedSection = section.get();
SemaphoreHandle_t cachedMutex = renderingMutex;
EpubReaderActivity* self = this;
// Handle dictionary mode selection - exitActivity deletes DictionaryMenuActivity
exitActivity();
if (mode == DictionaryMode::ENTER_WORD) {
// Enter word mode - show keyboard and search
self->enterNewActivity(new DictionarySearchActivity(cachedRenderer, cachedMappedInput,
[self]() {
// On back from dictionary
self->exitActivity();
self->updateRequired = true;
},
"")); // Empty string = show keyboard
} else {
// Select from screen mode - show word selection on current page
if (cachedSection) {
xSemaphoreTake(cachedMutex, portMAX_DELAY);
auto page = cachedSection->loadPageFromSectionFile();
if (page) {
// Get margins for word selection positioning
int orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft;
cachedRenderer.getOrientedViewableTRBL(&orientedMarginTop, &orientedMarginRight, &orientedMarginBottom,
&orientedMarginLeft);
orientedMarginTop += SETTINGS.screenMargin;
orientedMarginLeft += SETTINGS.screenMargin;
// Cache the font ID before creating activity
const int cachedFontId = SETTINGS.getReaderFontId();
self->enterNewActivity(new EpubWordSelectionActivity(
cachedRenderer, cachedMappedInput, std::move(page), cachedFontId, orientedMarginLeft,
orientedMarginTop,
[self](const std::string& selectedWord) {
// Word selected - look it up
self->exitActivity();
self->enterNewActivity(new DictionarySearchActivity(self->renderer, self->mappedInput,
[self]() {
self->exitActivity();
self->updateRequired = true;
},
selectedWord));
},
[self]() {
// Cancelled word selection
self->exitActivity();
self->updateRequired = true;
}));
xSemaphoreGive(cachedMutex);
} else {
xSemaphoreGive(cachedMutex);
self->updateRequired = true;
}
} else {
self->updateRequired = true;
}
}
},
[this]() {
// Cancelled dictionary menu - cache self before exitActivity destroys the lambda
EpubReaderActivity* self = this;
exitActivity();
self->updateRequired = true;
},
section != nullptr)); // Word selection only available if section is loaded
xSemaphoreGive(renderingMutex);
return;
}
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::PageForward) ||

View File

@@ -42,7 +42,8 @@ const SettingInfo controlsSettings[controlsSettingsCount] = {
SettingInfo::Enum("Side Button Layout (reader)", &CrossPointSettings::sideButtonLayout,
{"Prev, Next", "Next, Prev"}),
SettingInfo::Toggle("Long-press Chapter Skip", &CrossPointSettings::longPressChapterSkip),
SettingInfo::Enum("Short Power Button Click", &CrossPointSettings::shortPwrBtn, {"Ignore", "Sleep", "Page Turn"})};
SettingInfo::Enum("Short Power Button Click", &CrossPointSettings::shortPwrBtn,
{"Ignore", "Sleep", "Page Turn", "Dictionary"})};
constexpr int systemSettingsCount = 5;
const SettingInfo systemSettings[systemSettingsCount] = {

View File

@@ -1,5 +1,6 @@
#include "KeyboardEntryActivity.h"
#include "activities/dictionary/DictionaryMargins.h"
#include "MappedInputManager.h"
#include "fontIds.h"
@@ -39,7 +40,7 @@ void KeyboardEntryActivity::onEnter() {
updateRequired = true;
xTaskCreate(&KeyboardEntryActivity::taskTrampoline, "KeyboardEntryActivity",
2048, // Stack size
4096, // Stack size (increased from 2048)
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
@@ -238,22 +239,25 @@ void KeyboardEntryActivity::loop() {
updateRequired = true;
}
// Cancel
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
// Cancel - use wasReleased to consume the full button event and prevent propagation
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
if (onCancel) {
onCancel();
}
updateRequired = true;
}
}
void KeyboardEntryActivity::render() const {
// Get margins using same pattern as reader + button hint space
int marginTop, marginRight, marginBottom, marginLeft;
getDictionaryContentMargins(renderer, &marginTop, &marginRight, &marginBottom, &marginLeft);
const auto pageWidth = renderer.getScreenWidth();
renderer.clearScreen();
// Draw title
renderer.drawCenteredText(UI_10_FONT_ID, startY, title.c_str());
// Draw title with top margin
renderer.drawCenteredText(UI_10_FONT_ID, marginTop + startY, title.c_str());
// Draw input field
const int inputY = startY + 22;