From 6094b9c3f20506bb06281c658938ea3b98963284 Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Wed, 27 Nov 2024 21:13:07 +0100 Subject: [PATCH] Implemented app result and SelectionDialog (#96) --- Documentation/ideas.md | 5 +- Tactility/Private/app/AppInstance.h | 20 ++++ Tactility/Source/Tactility.cpp | 2 + Tactility/Source/app/App.h | 1 + Tactility/Source/app/AppInstance.cpp | 5 + Tactility/Source/app/Manifest.h | 16 ++- .../app/selectiondialog/SelectionDialog.cpp | 109 ++++++++++++++++++ .../app/selectiondialog/SelectionDialog.h | 25 ++++ Tactility/Source/lvgl/Toolbar.cpp | 6 +- Tactility/Source/service/loader/Loader.cpp | 36 +++++- TactilityCore/Source/StringUtils.cpp | 45 ++++++++ TactilityCore/Source/StringUtils.h | 21 ++++ Tests/TactilityCore/StringTest.cpp | 66 +++++++++++ 13 files changed, 345 insertions(+), 12 deletions(-) create mode 100644 Tactility/Source/app/selectiondialog/SelectionDialog.cpp create mode 100644 Tactility/Source/app/selectiondialog/SelectionDialog.h create mode 100644 Tests/TactilityCore/StringTest.cpp diff --git a/Documentation/ideas.md b/Documentation/ideas.md index 3a845a50..2f8df48e 100644 --- a/Documentation/ideas.md +++ b/Documentation/ideas.md @@ -15,15 +15,14 @@ - Bug: in LVGL9 with M5Core2, crash when bottom item is clicked without scrolling first - Publish firmwares with upload tool - De-duplicate WiFi SSIDs. -- Refactor hardware configuration init methods to return esp_err_t instead of bool - Replace M5Unified and M5GFX with custom drivers (so we can fix the Core2 SD card mounting bug, and so we regain some firmware space) # Core Ideas - Support for displays with different DPI. Consider the layer-based system like on Android. - If present, use LED to show boot status - 2 wire speaker support -- tt::startApp() and similar functions as proxies for Loader app start/stop/etc. -- tt::setAppResult() for apps that need to return data to other apps (e.g. file selection) +- tt::app::start() and similar functions as proxies for Loader app start/stop/etc. +- App.setResult() for apps that need to return data to other apps (e.g. file selection) - Wi-Fi using dispatcher to dispatch its main functionality to the dedicated Wi-Fi CPU core (to avoid main loop hack) # App Ideas diff --git a/Tactility/Private/app/AppInstance.h b/Tactility/Private/app/AppInstance.h index 7cfc59fd..0c0d1f62 100644 --- a/Tactility/Private/app/AppInstance.h +++ b/Tactility/Private/app/AppInstance.h @@ -4,6 +4,7 @@ #include "app/Manifest.h" #include "Bundle.h" #include "Mutex.h" +#include namespace tt::app { @@ -15,6 +16,20 @@ typedef enum { StateStopped // App is not in memory } State; +struct ResultHolder { + ResultHolder(Result result, const Bundle& resultData) { + this->result = result; + this->resultData = new Bundle(resultData); + } + + ~ResultHolder() { + delete resultData; + } + + Result result; + Bundle* _Nullable resultData; +}; + /** * Thread-safe app instance. */ @@ -37,6 +52,7 @@ private: * These manifest methods can optionally allocate/free data that is attached here. */ void* _Nullable data = nullptr; + std::unique_ptr resultHolder; public: @@ -61,6 +77,10 @@ public: void setData(void* data); const Bundle& getParameters() const; + + void setResult(Result result, const Bundle& bundle); + bool hasResult() const; + std::unique_ptr& getResult() { return resultHolder; } }; } // namespace diff --git a/Tactility/Source/Tactility.cpp b/Tactility/Source/Tactility.cpp index a70f4455..ec372fa3 100644 --- a/Tactility/Source/Tactility.cpp +++ b/Tactility/Source/Tactility.cpp @@ -44,6 +44,7 @@ namespace app { namespace display { extern const Manifest manifest; } namespace i2csettings { extern const Manifest manifest; } namespace power { extern const Manifest manifest; } + namespace selectiondialog { extern const Manifest manifest; } namespace systeminfo { extern const Manifest manifest; } namespace textviewer { extern const Manifest manifest; } namespace wificonnect { extern const Manifest manifest; } @@ -62,6 +63,7 @@ static const std::vector system_apps = { &app::i2csettings::manifest, &app::imageviewer::manifest, &app::settings::manifest, + &app::selectiondialog::manifest, &app::systeminfo::manifest, &app::textviewer::manifest, &app::wificonnect::manifest, diff --git a/Tactility/Source/app/App.h b/Tactility/Source/app/App.h index 80125514..96546bea 100644 --- a/Tactility/Source/app/App.h +++ b/Tactility/Source/app/App.h @@ -24,6 +24,7 @@ public: virtual void setData(void* data) = 0; virtual const Bundle& getParameters() const = 0; virtual Flags getFlags() const = 0; + virtual void setResult(Result result, const Bundle& bundle = Bundle()) = 0; }; } diff --git a/Tactility/Source/app/AppInstance.cpp b/Tactility/Source/app/AppInstance.cpp index a55f2793..a0c3ace9 100644 --- a/Tactility/Source/app/AppInstance.cpp +++ b/Tactility/Source/app/AppInstance.cpp @@ -57,4 +57,9 @@ const Bundle& AppInstance::getParameters() const { return parameters; } +void AppInstance::setResult(Result result, const Bundle& bundle) { + std::unique_ptr new_holder(new ResultHolder(result, bundle)); + resultHolder = std::move(new_holder); +} + } // namespace diff --git a/Tactility/Source/app/Manifest.h b/Tactility/Source/app/Manifest.h index ad2585be..cb7d6afb 100644 --- a/Tactility/Source/app/Manifest.h +++ b/Tactility/Source/app/Manifest.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "CoreDefines.h" // Forward declarations @@ -23,11 +24,17 @@ typedef enum { TypeUser } Type; +typedef enum { + ResultOk, + ResultCancelled, + ResultError +} Result; typedef void (*AppOnStart)(App& app); typedef void (*AppOnStop)(App& app); typedef void (*AppOnShow)(App& app, lv_obj_t* parent); typedef void (*AppOnHide)(App& app); +typedef void (*AppOnResult)(App& app, Result result, const Bundle& resultData); typedef struct Manifest { /** @@ -43,7 +50,7 @@ typedef struct Manifest { /** * Optional icon. */ - std::string icon = {}; + std::string icon; /** * App type affects launch behaviour. @@ -69,7 +76,12 @@ typedef struct Manifest { * Non-blocking method, called before gui is destroyed */ const AppOnHide _Nullable onHide = nullptr; -} AppManifest; + + /** + * Handle the result for apps that are launched + */ + const AppOnResult _Nullable onResult = nullptr; +} Manifest; struct { bool operator()(const Manifest* left, const Manifest* right) const { return left->name < right->name; } diff --git a/Tactility/Source/app/selectiondialog/SelectionDialog.cpp b/Tactility/Source/app/selectiondialog/SelectionDialog.cpp new file mode 100644 index 00000000..3e624449 --- /dev/null +++ b/Tactility/Source/app/selectiondialog/SelectionDialog.cpp @@ -0,0 +1,109 @@ +#include "SelectionDialog.h" +#include "Log.h" +#include "lvgl.h" +#include "lvgl/Toolbar.h" +#include "service/loader/Loader.h" +#include + +namespace tt::app::selectiondialog { + +#define PARAMETER_BUNDLE_KEY_TITLE "title" +#define PARAMETER_BUNDLE_KEY_ITEMS "items" +#define RESULT_BUNDLE_KEY_INDEX "index" + +#define PARAMETER_ITEM_CONCATENATION_TOKEN ";;" +#define DEFAULT_TITLE "Select..." + +#define TAG "selection_dialog" + +void setItemsParameter(Bundle& bundle, const std::vector& items) { + std::string result = string_join(items, PARAMETER_ITEM_CONCATENATION_TOKEN); + bundle.putString(PARAMETER_BUNDLE_KEY_ITEMS, result); +} + +int32_t getResultIndex(const Bundle& bundle) { + int32_t index = -1; + bundle.optInt32(RESULT_BUNDLE_KEY_INDEX, index); + return index; +} + +void setResultIndex(Bundle& bundle, int32_t index) { + bundle.putInt32(RESULT_BUNDLE_KEY_INDEX, index); +} + +void setTitleParameter(Bundle& bundle, const std::string& title) { + bundle.putString(PARAMETER_BUNDLE_KEY_TITLE, title); +} + +static std::string getTitleParameter(const Bundle& bundle) { + std::string result; + if (bundle.optString(PARAMETER_BUNDLE_KEY_TITLE, result)) { + return result; + } else { + return DEFAULT_TITLE; + } +} + +static void onListItemSelected(lv_event_t* e) { + lv_event_code_t code = lv_event_get_code(e); + if (code == LV_EVENT_CLICKED) { + size_t index = (size_t)(e->user_data); + TT_LOG_I(TAG, "Selected item at index %d", index); + tt::app::App* app = service::loader::get_current_app(); + Bundle bundle; + setResultIndex(bundle, (int32_t)index); + app->setResult(app::ResultOk, bundle); + service::loader::stop_app(); + } +} + +static void createChoiceItem(void* parent, const std::string& title, size_t index) { + auto* list = static_cast(parent); + lv_obj_t* btn = lv_list_add_button(list, nullptr, title.c_str()); + lv_obj_add_event_cb(btn, &onListItemSelected, LV_EVENT_CLICKED, (void*)index); +} + +static void onShow(App& app, lv_obj_t* parent) { + lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN); + std::string title = getTitleParameter(app.getParameters()); + lvgl::toolbar_create(parent, title); + + lv_obj_t* list = lv_list_create(parent); + lv_obj_set_width(list, LV_PCT(100)); + lv_obj_set_flex_grow(list, 1); + + const Bundle& parameters = app.getParameters(); + std::string items_concatenated; + if (parameters.optString(PARAMETER_BUNDLE_KEY_ITEMS, items_concatenated)) { + std::vector items = string_split(items_concatenated, PARAMETER_ITEM_CONCATENATION_TOKEN); + if (items.empty() || items.front().empty()) { + TT_LOG_E(TAG, "No items provided"); + app.setResult(ResultError); + service::loader::stop_app(); + } else if (items.size() == 1) { + Bundle result_bundle; + setResultIndex(result_bundle, 0); + app.setResult(ResultOk, result_bundle); + service::loader::stop_app(); + TT_LOG_W(TAG, "Auto-selecting single item"); + } else { + size_t index = 0; + for (const auto& item: items) { + createChoiceItem(list, item, index++); + } + } + } else { + TT_LOG_E(TAG, "No items provided"); + app.setResult(ResultError); + service::loader::stop_app(); + } +} + +extern const Manifest manifest = { + .id = "SelectionDialog", + .name = "Selection Dialog", + .type = TypeHidden, + .onShow = &onShow +}; + +} diff --git a/Tactility/Source/app/selectiondialog/SelectionDialog.h b/Tactility/Source/app/selectiondialog/SelectionDialog.h new file mode 100644 index 00000000..b189e4c8 --- /dev/null +++ b/Tactility/Source/app/selectiondialog/SelectionDialog.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include "Bundle.h" + +/** + * Start the app by its ID and provide: + * - an optional title + * - 2 or more items + * + * If you provide 0 items, the app will auto-close. + * If you provide 1 item, the app will auto-close with result index 0 + */ +namespace tt::app::selectiondialog { + + /** App startup parameters */ + + void setTitleParameter(Bundle& bundle, const std::string& title); + void setItemsParameter(Bundle& bundle, const std::vector& items); + + /** App result data */ + + int32_t getResultIndex(const Bundle& bundle); +} \ No newline at end of file diff --git a/Tactility/Source/lvgl/Toolbar.cpp b/Tactility/Source/lvgl/Toolbar.cpp index 08b43226..0da01f78 100644 --- a/Tactility/Source/lvgl/Toolbar.cpp +++ b/Tactility/Source/lvgl/Toolbar.cpp @@ -83,13 +83,13 @@ lv_obj_t* toolbar_create(lv_obj_t* parent, const std::string& title) { lv_obj_set_style_pad_all(toolbar->action_container, 0, 0); lv_obj_set_style_border_width(toolbar->action_container, 0, 0); + toolbar_set_nav_action(obj, LV_SYMBOL_CLOSE, &stop_app, nullptr); + return obj; } lv_obj_t* toolbar_create(lv_obj_t* parent, const app::App& app) { - lv_obj_t* toolbar = toolbar_create(parent, app.getManifest().name); - toolbar_set_nav_action(toolbar, LV_SYMBOL_CLOSE, &stop_app, nullptr); - return toolbar; + return toolbar_create(parent, app.getManifest().name); } void toolbar_set_title(lv_obj_t* obj, const std::string& title) { diff --git a/Tactility/Source/service/loader/Loader.cpp b/Tactility/Source/service/loader/Loader.cpp index ac54cc7c..7739ef84 100644 --- a/Tactility/Source/service/loader/Loader.cpp +++ b/Tactility/Source/service/loader/Loader.cpp @@ -66,6 +66,7 @@ static void loader_unlock() { } LoaderStatus start_app(const std::string& id, bool blocking, const Bundle& bundle) { + TT_LOG_I(TAG, "Start app %s", id.c_str()); tt_assert(loader_singleton); LoaderMessageLoaderStatusResult result = { @@ -94,6 +95,7 @@ LoaderStatus start_app(const std::string& id, bool blocking, const Bundle& bundl } void stop_app() { + TT_LOG_I(TAG, "Stop app"); tt_check(loader_singleton); LoaderMessage message(LoaderMessageTypeAppStop); loader_singleton->queue.put(&message, TtWaitForever); @@ -257,6 +259,8 @@ static void do_stop_app() { // Stop current app app::AppInstance* app_to_stop = loader_singleton->app_stack.top(); + std::unique_ptr result_holder = std::move(app_to_stop->getResult()); + const app::Manifest& manifest = app_to_stop->getManifest(); app_transition_to_state(*app_to_stop, app::StateHiding); app_transition_to_state(*app_to_stop, app::StateStopped); @@ -268,14 +272,38 @@ static void do_stop_app() { TT_LOG_I(TAG, "Free heap: %zu", heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); #endif - // Resume previous app - if (original_stack_size > 1) { - - } app::AppInstance* app_to_resume = loader_singleton->app_stack.top(); tt_assert(app_to_resume); app_transition_to_state(*app_to_resume, app::StateShowing); + auto on_result = app_to_resume->getManifest().onResult; + if (on_result != nullptr) { + if (result_holder != nullptr) { + Bundle* result_bundle = result_holder->resultData; + if (result_bundle != nullptr) { + on_result( + *app_to_resume, + result_holder->result, + *result_bundle + ); + } else { + const Bundle empty_bundle; + on_result( + *app_to_resume, + result_holder->result, + empty_bundle + ); + } + } else { + const Bundle empty_bundle; + on_result( + *app_to_resume, + app::ResultCancelled, + empty_bundle + ); + } + } + loader_unlock(); LoaderEventInternal event_internal = {.type = LoaderEventTypeApplicationStopped}; diff --git a/TactilityCore/Source/StringUtils.cpp b/TactilityCore/Source/StringUtils.cpp index e2285d46..daeb85ad 100644 --- a/TactilityCore/Source/StringUtils.cpp +++ b/TactilityCore/Source/StringUtils.cpp @@ -1,5 +1,7 @@ #include "StringUtils.h" #include +#include +#include namespace tt { @@ -27,4 +29,47 @@ bool string_get_path_parent(const char* path, char* output) { } } +std::vector string_split(const std::string&input, const std::string&delimiter) { + size_t token_index = 0; + size_t delimiter_index; + const size_t delimiter_length = delimiter.length(); + std::string token; + std::vector result; + + while ((delimiter_index = input.find(delimiter, token_index)) != std::string::npos) { + token = input.substr(token_index, delimiter_index - token_index); + token_index = delimiter_index + delimiter_length; + result.push_back(token); + } + + auto end_token = input.substr(token_index); + if (!end_token.empty()) { + result.push_back(end_token); + } + + return result; +} + +std::string string_join(const std::vector& input, const std::string& delimiter) { + std::stringstream stream; + size_t size = input.size(); + + if (size == 0) { + return ""; + } else if (size == 1) { + return input.front(); + } else { + auto iterator = input.begin(); + while (iterator != input.end()) { + stream << *iterator; + iterator++; + if (iterator != input.end()) { + stream << delimiter; + } + } + } + + return stream.str(); +} + } // namespace diff --git a/TactilityCore/Source/StringUtils.h b/TactilityCore/Source/StringUtils.h index 0818feef..d51a937c 100644 --- a/TactilityCore/Source/StringUtils.h +++ b/TactilityCore/Source/StringUtils.h @@ -1,6 +1,8 @@ #pragma once #include +#include +#include namespace tt { @@ -21,4 +23,23 @@ int string_find_last_index(const char* text, size_t from_index, char find); */ bool string_get_path_parent(const char* path, char* output); +/** + * Splits the provided input into separate pieces with delimiter as separator text. + * When the input string is empty, the output list will be empty too. + * + * @param input the input to split up + * @param delimiter a non-empty string to recognize as separator + */ +std::vector string_split(const std::string& input, const std::string& delimiter); + +/** + * Join a set of tokens into a single string, given a delimiter (separator). + * If the input is an empty list, the result will be an empty string. + * The delimeter is only placed inbetween tokens and not appended at the end of the resulting string. + * + * @param input the tokens to join together + * @param delimiter the separator to join with + */ +std::string string_join(const std::vector& input, const std::string& delimiter); + } // namespace diff --git a/Tests/TactilityCore/StringTest.cpp b/Tests/TactilityCore/StringTest.cpp new file mode 100644 index 00000000..de19bba9 --- /dev/null +++ b/Tests/TactilityCore/StringTest.cpp @@ -0,0 +1,66 @@ +#include "doctest.h" +#include "StringUtils.h" + +using namespace tt; +using namespace std; + +// region string_split + +TEST_CASE("splitting an empty string results in an empty vector") { + auto result = string_split("", "."); + CHECK_EQ(result.empty(), true); +} + +TEST_CASE("splitting a string with a single token results in a vector with that token") { + auto result = string_split("token", "."); + CHECK_EQ(result.size(), 1); + CHECK_EQ(result.front(), "token"); +} + +TEST_CASE("splitting a string with multiple tokens results in a vector with those tokens") { + auto result = string_split("token1;token2;token3;", ";"); + CHECK_EQ(result.size(), 3); + CHECK_EQ(result[0], "token1"); + CHECK_EQ(result[1], "token2"); + CHECK_EQ(result[2], "token3"); +} + +// endregion string_split + +// region string_join + +TEST_CASE("joining an empty vector results in an empty string") { + vector tokens = {}; + auto result = string_join(tokens, "."); + CHECK_EQ(result, ""); +} + +TEST_CASE("joining a single token results in a string with that value") { + vector tokens = { + "token" + }; + auto result = string_join(tokens, "."); + CHECK_EQ(result, "token"); +} + +TEST_CASE("joining multiple tokens results in a string with all the tokens and the delimiter") { + vector tokens = { + "token1", + "token2", + "token3", + }; + auto result = string_join(tokens, "."); + CHECK_EQ(result, "token1.token2.token3"); +} + +TEST_CASE("joining with empty tokens leads to an extra delimiter") { + vector tokens = { + "token1", + "", + "token2", + }; + auto result = string_join(tokens, "."); + CHECK_EQ(result, "token1..token2"); +} + +// endregion string_join