Implemented LaunchId and FileSelection, updated Notes (#281)

- Implemented `LaunchId` to keep track of the apps that are started
- Implemented `FileSelection` app to select existing and/or new files.
- Moved some re-usable file functionality to `tt::file::`
- Renamed `Files` app to `FileBrowser`
- Updated `Notes` app to use new `FileSelection` functionality, and cleaned it up a bit.
- General code cleanliness improvements
This commit is contained in:
Ken Van Hoeylandt
2025-05-25 22:11:50 +02:00
committed by GitHub
parent 74eb830870
commit 2691dbb014
38 changed files with 971 additions and 508 deletions
-91
View File
@@ -1,91 +0,0 @@
#include "Tactility/app/files/FileUtils.h"
#include <Tactility/StringUtils.h>
#include <Tactility/TactilityCore.h>
#include <cstring>
namespace tt::app::files {
#define TAG "file_utils"
std::string getChildPath(const std::string& basePath, const std::string& childPath) {
// Postfix with "/" when the current path isn't "/"
if (basePath.length() != 1) {
return basePath + "/" + childPath;
} else {
return "/" + childPath;
}
}
int dirent_filter_dot_entries(const struct dirent* entry) {
return (strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, ".") == 0) ? -1 : 0;
}
bool dirent_sort_alpha_and_type(const struct dirent& left, const struct dirent& right) {
bool left_is_dir = left.d_type == TT_DT_DIR || left.d_type == TT_DT_CHR;
bool right_is_dir = right.d_type == TT_DT_DIR || right.d_type == TT_DT_CHR;
if (left_is_dir == right_is_dir) {
return strcmp(left.d_name, right.d_name) < 0;
} else {
return left_is_dir > right_is_dir;
}
}
int scandir(
const std::string& path,
std::vector<dirent>& outList,
ScandirFilter _Nullable filterMethod,
ScandirSort _Nullable sortMethod
) {
TT_LOG_I(TAG, "scandir start");
DIR* dir = opendir(path.c_str());
if (dir == nullptr) {
TT_LOG_E(TAG, "Failed to open dir %s", path.c_str());
return -1;
}
struct dirent* current_entry;
while ((current_entry = readdir(dir)) != nullptr) {
if (filterMethod(current_entry) == 0) {
outList.push_back(*current_entry);
}
}
closedir(dir);
if (sortMethod != nullptr) {
sort(outList.begin(), outList.end(), sortMethod);
}
TT_LOG_I(TAG, "scandir finish");
return (int)outList.size();
};
bool isSupportedExecutableFile(const std::string& filename) {
#ifdef ESP_PLATFORM
// Currently only the PNG library is built into Tactility
return filename.ends_with(".elf");
#else
return false;
#endif
}
bool isSupportedImageFile(const std::string& filename) {
// Currently only the PNG library is built into Tactility
return string::lowercase(filename).ends_with(".png");
}
bool isSupportedTextFile(const std::string& filename) {
std::string filename_lower = string::lowercase(filename);
return filename_lower.ends_with(".txt") ||
filename_lower.ends_with(".ini") ||
filename_lower.ends_with(".json") ||
filename_lower.ends_with(".yaml") ||
filename_lower.ends_with(".yml") ||
filename_lower.ends_with(".lua") ||
filename_lower.ends_with(".js") ||
filename_lower.ends_with(".properties");
}
} // namespace tt::app::files
-47
View File
@@ -1,47 +0,0 @@
#include "Tactility/app/files/View.h"
#include "Tactility/app/files/State.h"
#include "Tactility/app/AppContext.h"
#include <Tactility/Assets.h>
#include <Tactility/service/loader/Loader.h>
#include <memory>
namespace tt::app::files {
#define TAG "files_app"
extern const AppManifest manifest;
class FilesApp : public App {
std::unique_ptr<View> view;
std::shared_ptr<State> state;
public:
FilesApp() {
state = std::make_shared<State>();
view = std::make_unique<View>(state);
}
void onShow(AppContext& appContext, lv_obj_t* parent) override {
view->init(parent);
}
void onResult(AppContext& appContext, Result result, std::unique_ptr<Bundle> bundle) override {
view->onResult(result, std::move(bundle));
}
};
extern const AppManifest manifest = {
.id = "Files",
.name = "Files",
.icon = TT_ASSETS_APP_ICON_FILES,
.type = Type::Hidden,
.createApp = create<FilesApp>
};
void start() {
service::loader::startApp(manifest.id);
}
} // namespace
-119
View File
@@ -1,119 +0,0 @@
#include "Tactility/app/files/State.h"
#include "Tactility/app/files/FileUtils.h"
#include "Tactility/hal/sdcard/SdCardDevice.h"
#include <Tactility/Log.h>
#include <Tactility/Partitions.h>
#include <Tactility/kernel/Kernel.h>
#include <cstring>
#include <unistd.h>
#include <vector>
#include <dirent.h>
#define TAG "files_app"
namespace tt::app::files {
State::State() {
if (kernel::getPlatform() == kernel::PlatformSimulator) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != nullptr) {
setEntriesForPath(cwd);
} else {
TT_LOG_E(TAG, "Failed to get current work directory files");
setEntriesForPath("/");
}
} else {
setEntriesForPath("/");
}
}
std::string State::getSelectedChildPath() const {
return getChildPath(current_path, selected_child_entry);
}
bool State::setEntriesForPath(const std::string& path) {
auto lock = mutex.asScopedLock();
if (!lock.lock(100)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "setEntriesForPath");
return false;
}
TT_LOG_I(TAG, "Changing path: %s -> %s", current_path.c_str(), path.c_str());
/**
* ESP32 does not have a root directory, so we have to create it manually.
* We'll add the NVS Flash partitions and the binding for the sdcard.
*/
bool show_custom_root = (kernel::getPlatform() == kernel::PlatformEsp) && (path == "/");
if (show_custom_root) {
TT_LOG_I(TAG, "Setting custom root");
dir_entries.clear();
dir_entries.push_back(dirent{
.d_ino = 0,
.d_type = TT_DT_DIR,
.d_name = SYSTEM_PARTITION_NAME
});
dir_entries.push_back(dirent{
.d_ino = 1,
.d_type = TT_DT_DIR,
.d_name = DATA_PARTITION_NAME
});
auto sdcards = tt::hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
for (auto& sdcard : sdcards) {
auto state = sdcard->getState();
if (state == hal::sdcard::SdCardDevice::State::Mounted) {
auto mount_name = sdcard->getMountPath().substr(1);
auto dir_entry = dirent {
.d_ino = 2,
.d_type = TT_DT_DIR,
.d_name = { 0 }
};
assert(mount_name.length() < sizeof(dirent::d_name));
strcpy(dir_entry.d_name, mount_name.c_str());
dir_entries.push_back(dir_entry);
}
}
current_path = path;
selected_child_entry = "";
action = ActionNone;
return true;
} else {
dir_entries.clear();
int count = tt::app::files::scandir(path, dir_entries, &dirent_filter_dot_entries, dirent_sort_alpha_and_type);
if (count >= 0) {
TT_LOG_I(TAG, "%s has %u entries", path.c_str(), count);
current_path = path;
selected_child_entry = "";
action = ActionNone;
return true;
} else {
TT_LOG_E(TAG, "Failed to fetch entries for %s", path.c_str());
return false;
}
}
}
bool State::setEntriesForChildPath(const std::string& child_path) {
auto path = getChildPath(current_path, child_path);
TT_LOG_I(TAG, "Navigating from %s to %s", current_path.c_str(), path.c_str());
return setEntriesForPath(path);
}
bool State::getDirent(uint32_t index, dirent& dirent) {
auto lock = mutex.asScopedLock();
if (!lock.lock(50 / portTICK_PERIOD_MS)) {
return false;
}
if (index < dir_entries.size()) {
dirent = dir_entries[index];
return true;
} else {
return false;
}
}
}
-334
View File
@@ -1,334 +0,0 @@
#include "Tactility/app/files/FileUtils.h"
#include "Tactility/app/files/View.h"
#include "Tactility/app/alertdialog/AlertDialog.h"
#include "Tactility/app/imageviewer/ImageViewer.h"
#include "Tactility/app/inputdialog/InputDialog.h"
#include "Tactility/app/textviewer/TextViewer.h"
#include "Tactility/app/ElfApp.h"
#include "Tactility/lvgl/Toolbar.h"
#include "Tactility/lvgl/LvglSync.h"
#include <Tactility/Tactility.h>
#include <Tactility/StringUtils.h>
#include <cstring>
#include <unistd.h>
#ifdef ESP_PLATFORM
#include "Tactility/service/loader/Loader.h"
#endif
#define TAG "files_app"
namespace tt::app::files {
// region Callbacks
static void dirEntryListScrollBeginCallback(lv_event_t* event) {
auto* view = (View*)lv_event_get_user_data(event);
view->onDirEntryListScrollBegin();
}
static void onDirEntryPressedCallback(lv_event_t* event) {
auto* view = (View*)lv_event_get_user_data(event);
auto* button = lv_event_get_target_obj(event);
auto index = lv_obj_get_index(button);
view->onDirEntryPressed(index);
}
static void onDirEntryLongPressedCallback(lv_event_t* event) {
auto* view = (View*)lv_event_get_user_data(event);
auto* button = lv_event_get_target_obj(event);
auto index = lv_obj_get_index(button);
view->onDirEntryLongPressed(index);
}
static void onRenamePressedCallback(lv_event_t* event) {
auto* view = (View*)lv_event_get_user_data(event);
view->onRenamePressed();
}
static void onDeletePressedCallback(lv_event_t* event) {
auto* view = (View*)lv_event_get_user_data(event);
view->onDeletePressed();
}
static void onNavigateUpPressedCallback(TT_UNUSED lv_event_t* event) {
auto* view = (View*)lv_event_get_user_data(event);
view->onNavigateUpPressed();
}
// endregion
void View::viewFile(const std::string& path, const std::string& filename) {
std::string file_path = path + "/" + filename;
// For PC we need to make the path relative to the current work directory,
// because that's how LVGL maps its 'drive letter' to the file system.
std::string processed_filepath;
if (kernel::getPlatform() == kernel::PlatformSimulator) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == nullptr) {
TT_LOG_E(TAG, "Failed to get current working directory");
return;
}
if (!file_path.starts_with(cwd)) {
TT_LOG_E(TAG, "Can only work with files in working directory %s", cwd);
return;
}
processed_filepath = file_path.substr(strlen(cwd));
} else {
processed_filepath = file_path;
}
TT_LOG_I(TAG, "Clicked %s", file_path.c_str());
if (isSupportedExecutableFile(filename)) {
#ifdef ESP_PLATFORM
app::registerElfApp(processed_filepath);
auto app_id = app::getElfAppId(processed_filepath);
service::loader::startApp(app_id);
#endif
} else if (isSupportedImageFile(filename)) {
app::imageviewer::start(processed_filepath);
} else if (isSupportedTextFile(filename)) {
if (kernel::getPlatform() == kernel::PlatformEsp) {
app::textviewer::start(processed_filepath);
} else {
// Remove forward slash, because we need a relative path
app::textviewer::start(processed_filepath.substr(1));
}
} else {
TT_LOG_W(TAG, "opening files of this type is not supported");
}
onNavigate();
}
void View::onDirEntryPressed(uint32_t index) {
dirent dir_entry;
if (state->getDirent(index, dir_entry)) {
TT_LOG_I(TAG, "Pressed %s %d", dir_entry.d_name, dir_entry.d_type);
state->setSelectedChildEntry(dir_entry.d_name);
switch (dir_entry.d_type) {
case TT_DT_DIR:
case TT_DT_CHR:
state->setEntriesForChildPath(dir_entry.d_name);
onNavigate();
update();
break;
case TT_DT_LNK:
TT_LOG_W(TAG, "opening links is not supported");
break;
case TT_DT_REG:
viewFile(state->getCurrentPath(), dir_entry.d_name);
onNavigate();
break;
default:
// Assume it's a file
// TODO: Find a better way to identify a file
viewFile(state->getCurrentPath(), dir_entry.d_name);
onNavigate();
break;
}
}
}
void View::onDirEntryLongPressed(int32_t index) {
dirent dir_entry;
if (state->getDirent(index, dir_entry)) {
TT_LOG_I(TAG, "Pressed %s %d", dir_entry.d_name, dir_entry.d_type);
state->setSelectedChildEntry(dir_entry.d_name);
switch (dir_entry.d_type) {
case TT_DT_DIR:
case TT_DT_CHR:
showActionsForDirectory();
break;
case TT_DT_LNK:
TT_LOG_W(TAG, "opening links is not supported");
break;
case TT_DT_REG:
showActionsForFile();
break;
default:
// Assume it's a file
// TODO: Find a better way to identify a file
showActionsForFile();
break;
}
}
}
void View::createDirEntryWidget(lv_obj_t* parent, struct dirent& dir_entry) {
tt_check(parent);
auto* list = (lv_obj_t*)parent;
const char* symbol;
if (dir_entry.d_type == TT_DT_DIR || dir_entry.d_type == TT_DT_CHR) {
symbol = LV_SYMBOL_DIRECTORY;
} else if (isSupportedImageFile(dir_entry.d_name)) {
symbol = LV_SYMBOL_IMAGE;
} else if (dir_entry.d_type == TT_DT_LNK) {
symbol = LV_SYMBOL_LOOP;
} else {
symbol = LV_SYMBOL_FILE;
}
lv_obj_t* button = lv_list_add_button(list, symbol, dir_entry.d_name);
lv_obj_add_event_cb(button, &onDirEntryPressedCallback, LV_EVENT_SHORT_CLICKED, this);
lv_obj_add_event_cb(button, &onDirEntryLongPressedCallback, LV_EVENT_LONG_PRESSED, this);
}
void View::onNavigateUpPressed() {
if (state->getCurrentPath() != "/") {
TT_LOG_I(TAG, "Navigating upwards");
std::string new_absolute_path;
if (string::getPathParent(state->getCurrentPath(), new_absolute_path)) {
state->setEntriesForPath(new_absolute_path);
}
onNavigate();
update();
}
}
void View::onRenamePressed() {
std::string entry_name = state->getSelectedChildEntry();
TT_LOG_I(TAG, "Pending rename %s", entry_name.c_str());
state->setPendingAction(State::ActionRename);
app::inputdialog::start("Rename", "", entry_name);
}
void View::onDeletePressed() {
std::string file_path = state->getSelectedChildPath();
TT_LOG_I(TAG, "Pending delete %s", file_path.c_str());
state->setPendingAction(State::ActionDelete);
std::string message = "Do you want to delete this?\n" + file_path;
const std::vector<std::string> choices = { "Yes", "No" };
app::alertdialog::start("Are you sure?", message, choices);
}
void View::showActionsForDirectory() {
lv_obj_clean(action_list);
auto* rename_button = lv_list_add_button(action_list, LV_SYMBOL_EDIT, "Rename");
lv_obj_add_event_cb(rename_button, onRenamePressedCallback, LV_EVENT_SHORT_CLICKED, this);
lv_obj_remove_flag(action_list, LV_OBJ_FLAG_HIDDEN);
}
void View::showActionsForFile() {
lv_obj_clean(action_list);
auto* rename_button = lv_list_add_button(action_list, LV_SYMBOL_EDIT, "Rename");
lv_obj_add_event_cb(rename_button, onRenamePressedCallback, LV_EVENT_SHORT_CLICKED, this);
auto* delete_button = lv_list_add_button(action_list, LV_SYMBOL_TRASH, "Delete");
lv_obj_add_event_cb(delete_button, onDeletePressedCallback, LV_EVENT_SHORT_CLICKED, this);
lv_obj_remove_flag(action_list, LV_OBJ_FLAG_HIDDEN);
}
void View::update() {
auto scoped_lockable = lvgl::getSyncLock()->asScopedLock();
if (scoped_lockable.lock(lvgl::defaultLockTime)) {
lv_obj_clean(dir_entry_list);
state->withEntries([this](const std::vector<dirent>& entries) {
for (auto entry : entries) {
TT_LOG_D(TAG, "Entry: %s %d", entry.d_name, entry.d_type);
createDirEntryWidget(dir_entry_list, entry);
}
});
if (state->getCurrentPath() == "/") {
lv_obj_add_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_remove_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN);
}
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "lvgl");
}
}
void View::init(lv_obj_t* parent) {
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
auto* toolbar = lvgl::toolbar_create(parent, "Files");
navigate_up_button = lvgl::toolbar_add_button_action(toolbar, LV_SYMBOL_UP, &onNavigateUpPressedCallback, this);
auto* wrapper = lv_obj_create(parent);
lv_obj_set_width(wrapper, LV_PCT(100));
lv_obj_set_style_border_width(wrapper, 0, 0);
lv_obj_set_style_pad_all(wrapper, 0, 0);
lv_obj_set_flex_grow(wrapper, 1);
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_ROW);
dir_entry_list = lv_list_create(wrapper);
lv_obj_set_height(dir_entry_list, LV_PCT(100));
lv_obj_set_flex_grow(dir_entry_list, 1);
lv_obj_add_event_cb(dir_entry_list, dirEntryListScrollBeginCallback, LV_EVENT_SCROLL_BEGIN, this);
action_list = lv_list_create(wrapper);
lv_obj_set_height(action_list, LV_PCT(100));
lv_obj_set_flex_grow(action_list, 1);
lv_obj_add_flag(action_list, LV_OBJ_FLAG_HIDDEN);
update();
}
void View::onDirEntryListScrollBegin() {
auto scoped_lockable = lvgl::getSyncLock()->asScopedLock();
if (scoped_lockable.lock(lvgl::defaultLockTime)) {
lv_obj_add_flag(action_list, LV_OBJ_FLAG_HIDDEN);
}
}
void View::onNavigate() {
auto scoped_lockable = lvgl::getSyncLock()->asScopedLock();
if (scoped_lockable.lock(lvgl::defaultLockTime)) {
lv_obj_add_flag(action_list, LV_OBJ_FLAG_HIDDEN);
}
}
void View::onResult(Result result, std::unique_ptr<Bundle> bundle) {
if (result != Result::Ok || bundle == nullptr) {
return;
}
std::string filepath = state->getSelectedChildPath();
TT_LOG_I(TAG, "Result for %s", filepath.c_str());
switch (state->getPendingAction()) {
case State::ActionDelete: {
if (alertdialog::getResultIndex(*bundle) == 0) {
int delete_count = (int)remove(filepath.c_str());
if (delete_count > 0) {
TT_LOG_I(TAG, "Deleted %d items", delete_count);
} else {
TT_LOG_W(TAG, "Failed to delete %s", filepath.c_str());
}
state->setEntriesForPath(state->getCurrentPath());
update();
}
break;
}
case State::ActionRename: {
auto new_name = app::inputdialog::getResult(*bundle);
if (!new_name.empty() && new_name != state->getSelectedChildEntry()) {
std::string rename_to = getChildPath(state->getCurrentPath(), new_name);
if (rename(filepath.c_str(), rename_to.c_str())) {
TT_LOG_I(TAG, "Renamed \"%s\" to \"%s\"", filepath.c_str(), rename_to.c_str());
} else {
TT_LOG_E(TAG, "Failed to rename \"%s\" to \"%s\"", filepath.c_str(), rename_to.c_str());
}
state->setEntriesForPath(state->getCurrentPath());
update();
}
break;
}
default:
break;
}
}
}