Fix for inputdialog

This commit is contained in:
Ken Van Hoeylandt 2026-09-04 08:59:09 +02:00
parent d42c68ce3a
commit 4db0d9496d
5 changed files with 71 additions and 39 deletions

View File

@ -1,8 +1,12 @@
#pragma once #pragma once
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <app/stream.h>
#include <tactility/concurrent/task_event_group.h>
/** /**
* Show a dialog with a title, a message and a text field. * Show a dialog with a title, a message and a text field.
*/ */
@ -10,19 +14,21 @@ namespace tt::app::inputdialog {
/** /**
* Show a dialog with the provided title, message and prefilled text, as a modal child of * Show a dialog with the provided title, message and prefilled text, as a modal child of
* @a callerAppInstanceId (a new-model app - see app/manager.h). The caller receives the result * @a callerAppInstanceId (see app_start_for_result_with_streams()). Result (0 = OK, 1 =
* as an APP_EVENT_RESULT in its own event loop: 0 = OK (call getLastText() for the entered * Cancelled or dismissed without a press) is delivered back via APP_EVENT_RESULT once this app's
* text), 1 = Cancelled or dismissed without a press. The caller is responsible for calling * thread exits. On result == 0, read the entered text with app_stream_read(&stream, ...) then
* app_manager_stop() on the returned instance id once it has handled the result. * app_stream_unsubscribe(&stream); on any other result, just app_stream_unsubscribe(&stream).
* The caller must call app_manager_stop() on the returned instance id once that event arrives,
* to fully reap this instance.
* @param[in,out] stream caller-owned storage bound to the started app's stdout; must stay valid
* until app_stream_unsubscribe() is called on it (see above).
* @param[in] buffer caller-owned backing storage for @a stream's ring buffer; must stay valid
* for the same duration as @a stream.
* @param[in] bufferCapacity size of @a buffer in bytes.
* @param[in] eventGroup the caller's own event group, reused for the stream's readiness bits
* (see app_stream_subscribe()). The caller isn't required to actually wait on them itself.
* @return the new dialog's app instance id * @return the new dialog's app instance id
*/ */
uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled = ""); uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled, AppStream& stream, void* buffer, size_t bufferCapacity, TaskEventGroup* eventGroup);
/**
* @return the text entered the last time any InputDialog instance was closed with OK. Only one
* dialog is expected to be open at a time - call this right after receiving its
* APP_EVENT_RESULT with result == 0.
*/
std::string getLastText();
} }

View File

@ -2,6 +2,9 @@
#include "./State.h" #include "./State.h"
#include <app/stream.h>
#include <tactility/concurrent/task_event_group.h>
#include <cstdint> #include <cstdint>
#include <lvgl.h> #include <lvgl.h>
#include <memory> #include <memory>
@ -10,8 +13,12 @@ namespace tt::app::files {
class View final { class View final {
std::shared_ptr<State> state; std::shared_ptr<State> state;
TaskEventGroup* eventGroup = nullptr;
uint32_t appInstanceId = 0; uint32_t appInstanceId = 0;
AppStream inputDialogStream {};
uint8_t inputDialogBuffer[256] {};
size_t current_start_index = 0; size_t current_start_index = 0;
size_t last_loaded_index = 0; size_t last_loaded_index = 0;
const size_t MAX_BATCH = 50; const size_t MAX_BATCH = 50;
@ -39,7 +46,7 @@ class View final {
public: public:
explicit View(const std::shared_ptr<State>& state) : state(state) {} View(const std::shared_ptr<State>& state, TaskEventGroup* eventGroup) : state(state), eventGroup(eventGroup) {}
void init(uint32_t appInstanceId, lv_obj_t* parent); void init(uint32_t appInstanceId, lv_obj_t* parent);
void update(size_t start_index = 0); void update(size_t start_index = 0);

View File

@ -31,12 +31,13 @@ void createWidgets(lv_obj_t* parent, void* userData) {
int32_t appMain(int argc, char* argv[]) { int32_t appMain(int argc, char* argv[]) {
uint32_t appInstanceId = app_scheduler_current_app_id(); uint32_t appInstanceId = app_scheduler_current_app_id();
auto state = std::make_shared<State>(); auto state = std::make_shared<State>();
View view(state);
CreateContext createContext { &view, appInstanceId };
TaskEventGroup event_group {}; TaskEventGroup event_group {};
task_event_group_construct(&event_group); task_event_group_construct(&event_group);
View view(state, &event_group);
CreateContext createContext { &view, appInstanceId };
AppEventSubscription sub {}; AppEventSubscription sub {};
check(app_event_subscribe(&sub, &event_group) == ERROR_NONE); check(app_event_subscribe(&sub, &event_group) == ERROR_NONE);

View File

@ -1,6 +1,7 @@
#include <app/event.h> #include <app/event.h>
#include <app/execute.h> #include <app/execute.h>
#include <app/install.h> #include <app/install.h>
#include <app/stream.h>
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
#include <lvgl/widgets/toolbar.h> #include <lvgl/widgets/toolbar.h>
@ -378,7 +379,7 @@ void View::onRenamePressed() {
std::string entry_name = state->getSelectedChildEntry(); std::string entry_name = state->getSelectedChildEntry();
LOG_I(TAG, "Pending rename %s", entry_name.c_str()); LOG_I(TAG, "Pending rename %s", entry_name.c_str());
state->setPendingAction(State::ActionRename); state->setPendingAction(State::ActionRename);
inputdialog::start(appInstanceId, "Rename", "", entry_name); inputdialog::start(appInstanceId, "Rename", "", entry_name, inputDialogStream, inputDialogBuffer, sizeof(inputDialogBuffer), eventGroup);
} }
void View::onDeletePressed() { void View::onDeletePressed() {
@ -393,13 +394,13 @@ void View::onDeletePressed() {
void View::onNewFilePressed() { void View::onNewFilePressed() {
LOG_I(TAG, "Creating new file"); LOG_I(TAG, "Creating new file");
state->setPendingAction(State::ActionCreateFile); state->setPendingAction(State::ActionCreateFile);
inputdialog::start(appInstanceId, "New File", "Enter filename:", ""); inputdialog::start(appInstanceId, "New File", "Enter filename:", "", inputDialogStream, inputDialogBuffer, sizeof(inputDialogBuffer), eventGroup);
} }
void View::onNewFolderPressed() { void View::onNewFolderPressed() {
LOG_I(TAG, "Creating new folder"); LOG_I(TAG, "Creating new folder");
state->setPendingAction(State::ActionCreateFolder); state->setPendingAction(State::ActionCreateFolder);
inputdialog::start(appInstanceId, "New Folder", "Enter folder name:", ""); inputdialog::start(appInstanceId, "New Folder", "Enter folder name:", "", inputDialogStream, inputDialogBuffer, sizeof(inputDialogBuffer), eventGroup);
} }
void View::addCommonFileActions() { void View::addCommonFileActions() {
@ -597,10 +598,21 @@ void View::onResult(uint32_t launchId, int32_t result) {
std::string filepath = state->getSelectedChildPath(); std::string filepath = state->getSelectedChildPath();
LOG_I(TAG, "Result for %s", filepath.c_str()); LOG_I(TAG, "Result for %s", filepath.c_str());
// Text-entry result (rename/new file/new folder); empty for Cancel, or for a dialog that // Text-entry result (rename/new file/new folder), read from the AppStream bound to that
// doesn't produce text (delete/paste confirmations) - those switch cases below only look at // dialog's stdout. Empty for Cancel. Other pending actions (delete/paste confirmations) never
// `result`, not this. // bound this stream; their switch cases below only look at `result`, not `resultText`.
std::string resultText = (result == 0) ? inputdialog::getLastText() : std::string(); bool isTextEntryAction = state->getPendingAction() == State::ActionRename ||
state->getPendingAction() == State::ActionCreateFile ||
state->getPendingAction() == State::ActionCreateFolder;
std::string resultText;
if (isTextEntryAction) {
if (result == 0) {
char buffer[sizeof(inputDialogBuffer)];
size_t length = app_stream_read(&inputDialogStream, buffer, sizeof(buffer));
resultText = std::string(buffer, length);
}
app_stream_unsubscribe(&inputDialogStream);
}
switch (state->getPendingAction()) { switch (state->getPendingAction()) {
case State::ActionDelete: { case State::ActionDelete: {

View File

@ -5,6 +5,7 @@
#include <app/start.h> #include <app/start.h>
#include <app/manifest.h> #include <app/manifest.h>
#include <app/scheduler.h> #include <app/scheduler.h>
#include <app/stream.h>
#include <lvgl_window_manager/window_manager.h> #include <lvgl_window_manager/window_manager.h>
@ -14,6 +15,8 @@
#include <lvgl.h> #include <lvgl.h>
#include <unistd.h>
namespace tt::app::inputdialog { namespace tt::app::inputdialog {
constexpr auto* TAG = "InputDialog"; constexpr auto* TAG = "InputDialog";
@ -31,7 +34,8 @@ struct Context {
// The eventual appMain() return value - see AlertDialog.cpp's Context::result for why this // The eventual appMain() return value - see AlertDialog.cpp's Context::result for why this
// is a plain (non-atomic) field safely shared between the LVGL thread (writer, before // is a plain (non-atomic) field safely shared between the LVGL thread (writer, before
// emitting APP_EVENT_CLOSE) and this dialog's own thread (reader, after waking from it). // emitting APP_EVENT_CLOSE) and this dialog's own thread (reader, after waking from it).
int32_t result = 1; // Cancelled - safety-net default if closed without pressing a button int32_t resultCode = 1; // Cancelled - safety-net default if closed without pressing a button
std::string resultText;
}; };
struct ButtonContext { struct ButtonContext {
@ -40,12 +44,6 @@ struct ButtonContext {
lv_obj_t* textarea; lv_obj_t* textarea;
}; };
// The last text entered via OK. Static rather than per-instance: simple, and in practice only
// one InputDialog is ever open at a time. Written on the LVGL thread (onButtonPressed(), before
// emitting APP_EVENT_CLOSE); read by the parent via getLastText() after receiving that event -
// safe without a lock for the same reason Context::result is (see AlertDialog.cpp).
std::string lastText;
void onButtonDeleted(lv_event_t* e) { void onButtonDeleted(lv_event_t* e) {
delete static_cast<ButtonContext*>(lv_event_get_user_data(e)); delete static_cast<ButtonContext*>(lv_event_get_user_data(e));
} }
@ -54,11 +52,11 @@ void onButtonPressed(lv_event_t* e) {
auto* btnCtx = static_cast<ButtonContext*>(lv_event_get_user_data(e)); auto* btnCtx = static_cast<ButtonContext*>(lv_event_get_user_data(e));
if (btnCtx->textarea != nullptr) { if (btnCtx->textarea != nullptr) {
LOG_I(TAG, "OK pressed"); LOG_I(TAG, "OK pressed");
lastText = lv_textarea_get_text(btnCtx->textarea); btnCtx->ctx->resultText = lv_textarea_get_text(btnCtx->textarea);
btnCtx->ctx->result = 0; btnCtx->ctx->resultCode = 0;
} else { } else {
LOG_I(TAG, "Cancel pressed"); LOG_I(TAG, "Cancel pressed");
btnCtx->ctx->result = 1; btnCtx->ctx->resultCode = 1;
} }
app_event_emit_close(btnCtx->ctx->appInstanceId); app_event_emit_close(btnCtx->ctx->appInstanceId);
} }
@ -138,22 +136,30 @@ int32_t appMain(int argc, char* argv[]) {
check(app_event_unsubscribe(&sub) == ERROR_NONE); check(app_event_unsubscribe(&sub) == ERROR_NONE);
task_event_group_destruct(&event_group); task_event_group_destruct(&event_group);
return ctx.result; if (ctx.resultCode == 0) {
// The caller captures this via an AppStream bound to our stdout (see start()); see
// AppStdioWrap.cpp for how printf() itself gets routed there on POSIX.
printf("%s", ctx.resultText.c_str());
}
return ctx.resultCode;
} }
} // namespace } // namespace
uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled) { uint32_t start(uint32_t callerAppInstanceId, const std::string& title, const std::string& message, const std::string& prefilled, AppStream& stream, void* buffer, size_t bufferCapacity, TaskEventGroup* eventGroup) {
const char* argv[] = { title.c_str(), message.c_str(), prefilled.c_str() }; const char* argv[] = { title.c_str(), message.c_str(), prefilled.c_str() };
AppStreamBinding binding = {
.producer_fd = STDOUT_FILENO,
.stream = &stream,
.buffer = buffer,
.buffer_capacity = bufferCapacity,
.event_group = eventGroup,
};
uint32_t instanceId = 0; uint32_t instanceId = 0;
app_start_for_result(manifest.id, 3, argv, callerAppInstanceId, &instanceId); app_start_for_result_with_streams(manifest.id, 3, argv, &binding, 1, callerAppInstanceId, &instanceId);
return instanceId; return instanceId;
} }
std::string getLastText() {
return lastText;
}
extern const ::AppManifest manifest = { extern const ::AppManifest manifest = {
.id = "tactility.inputdialog", .id = "tactility.inputdialog",
.name = "Input Dialog", .name = "Input Dialog",