Files App - Copy, Cut, Paste Actions (#510)

This commit is contained in:
Shadowtrance 2026-02-27 05:11:05 +10:00 committed by GitHub
parent d2048e01b6
commit 33caf09856
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 320 additions and 17 deletions

View File

@ -85,7 +85,6 @@
- T-Deck: Use trackball as input device (with optional mouse functionality for LVGL)
- Show a warning screen if firmware encryption or secure boot are off when saving WiFi credentials.
- Remove flex_flow from app_container in Gui.cpp
- Files app: copy/cut/paste actions
- ElfAppManifest: change name (remove "manifest" as it's confusing), remove icon and title, publish snapshot SDK on CDN
- Bug: CYD 2432S032C screen rotation fails due to touch driver issue
- Calculator app should show regular text input field on non-touch devices that have a keyboard (Cardputer, T-Lora Pager)

View File

@ -155,6 +155,8 @@ const struct ModuleSymbol lvgl_module_symbols[] = {
DEFINE_MODULE_SYMBOL(lv_obj_class_init_obj),
DEFINE_MODULE_SYMBOL(lv_obj_move_foreground),
DEFINE_MODULE_SYMBOL(lv_obj_move_to_index),
DEFINE_MODULE_SYMBOL(lv_obj_set_style_min_height),
DEFINE_MODULE_SYMBOL(lv_obj_set_style_max_height),
// lv_font
DEFINE_MODULE_SYMBOL(lv_font_get_default),
// lv_theme
@ -385,6 +387,7 @@ const struct ModuleSymbol lvgl_module_symbols[] = {
DEFINE_MODULE_SYMBOL(lv_group_set_editing),
DEFINE_MODULE_SYMBOL(lv_group_create),
DEFINE_MODULE_SYMBOL(lv_group_delete),
DEFINE_MODULE_SYMBOL(lv_group_get_editing),
// lv_mem
DEFINE_MODULE_SYMBOL(lv_free),
DEFINE_MODULE_SYMBOL(lv_malloc),
@ -407,5 +410,9 @@ const struct ModuleSymbol lvgl_module_symbols[] = {
DEFINE_MODULE_SYMBOL(lv_anim_start),
DEFINE_MODULE_SYMBOL(lv_anim_path_ease_in_out),
DEFINE_MODULE_SYMBOL(lv_anim_path_linear),
DEFINE_MODULE_SYMBOL(lv_anim_path_ease_in),
DEFINE_MODULE_SYMBOL(lv_anim_path_ease_out),
// lv_async
DEFINE_MODULE_SYMBOL(lv_async_call),
MODULE_SYMBOL_TERMINATOR
};

View File

@ -2,7 +2,9 @@
#include <Tactility/RecursiveMutex.h>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <dirent.h>
@ -17,7 +19,8 @@ public:
ActionDelete,
ActionRename,
ActionCreateFile,
ActionCreateFolder
ActionCreateFolder,
ActionPaste
};
private:
@ -27,6 +30,10 @@ private:
std::string current_path;
std::string selected_child_entry;
PendingAction action = ActionNone;
std::string pending_paste_dst;
std::string clipboard_path;
bool clipboard_is_cut = false;
bool clipboard_active = false;
public:
@ -66,6 +73,46 @@ public:
PendingAction getPendingAction() const { return action; }
void setPendingAction(PendingAction newAction) { action = newAction; }
// These accessors intentionally omit mutex locking: both are only called
// from the UI thread (onPastePressed → onResult), so no concurrent access
// is possible. If that threading assumption changes, add mutex guards here
// to match the clipboard accessors above.
std::string getPendingPasteDst() const { return pending_paste_dst; }
void setPendingPasteDst(const std::string& dst) { pending_paste_dst = dst; }
void setClipboard(const std::string& path, bool is_cut) {
mutex.withLock([&] {
clipboard_path = path;
clipboard_is_cut = is_cut;
clipboard_active = true;
});
}
bool hasClipboard() const {
bool result = false;
mutex.withLock([&] { result = clipboard_active; });
return result;
}
/** Returns {path, is_cut} atomically, or nullopt if clipboard is empty. */
std::optional<std::pair<std::string, bool>> getClipboard() const {
std::optional<std::pair<std::string, bool>> result;
mutex.withLock([&] {
if (clipboard_active) {
result = { clipboard_path, clipboard_is_cut };
}
});
return result;
}
void clearClipboard() {
mutex.withLock([&] {
clipboard_active = false;
clipboard_path.clear();
clipboard_is_cut = false;
});
}
};
}

View File

@ -21,10 +21,12 @@ class View final {
lv_obj_t* navigate_up_button = nullptr;
lv_obj_t* new_file_button = nullptr;
lv_obj_t* new_folder_button = nullptr;
lv_obj_t* paste_button = nullptr;
std::string installAppPath = { 0 };
LaunchId installAppLaunchId = 0;
void showActions();
void showActionsForDirectory();
void showActionsForFile();
@ -46,6 +48,9 @@ public:
void onDeletePressed();
void onNewFilePressed();
void onNewFolderPressed();
void onCopyPressed();
void onCutPressed();
void onPastePressed();
void onDirEntryListScrollBegin();
void onResult(LaunchId launchId, Result result, std::unique_ptr<Bundle> bundle);
void deinit(const AppContext& appContext);
@ -53,6 +58,7 @@ public:
private:
bool resolveDirentFromListIndex(int32_t list_index, dirent& out_entry);
void doPaste(const std::string& src, bool is_cut, const std::string& dst);
};
}

View File

@ -74,6 +74,114 @@ static void onNewFolderPressedCallback(lv_event_t* event) {
view->onNewFolderPressed();
}
static void onCopyPressedCallback(lv_event_t* event) {
auto* view = static_cast<View*>(lv_event_get_user_data(event));
view->onCopyPressed();
}
static void onCutPressedCallback(lv_event_t* event) {
auto* view = static_cast<View*>(lv_event_get_user_data(event));
view->onCutPressed();
}
static void onPastePressedCallback(lv_event_t* event) {
auto* view = static_cast<View*>(lv_event_get_user_data(event));
view->onPastePressed();
}
// endregion
// region File helpers
static bool copyFileContents(const std::string& src, const std::string& dst) {
auto src_lock = file::getLock(src);
auto dst_lock = file::getLock(dst);
const bool same_lock = (src_lock.get() == dst_lock.get());
auto unlock_all = [&] {
if (!same_lock) dst_lock->unlock();
src_lock->unlock();
};
src_lock->lock();
if (!same_lock) dst_lock->lock();
FILE* in = fopen(src.c_str(), "rb");
if (in == nullptr) {
unlock_all();
return false;
}
FILE* out = fopen(dst.c_str(), "wb");
if (out == nullptr) {
fclose(in);
unlock_all();
return false;
}
uint8_t buf[512];
bool success = true;
size_t n;
while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
if (fwrite(buf, 1, n, out) != n) {
success = false;
break;
}
}
if (ferror(in)) {
success = false;
}
fclose(in);
if (fclose(out) != 0) {
success = false;
}
if (!success) {
remove(dst.c_str());
}
unlock_all();
return success;
}
static bool copyRecursive(const std::string& src, const std::string& dst) {
if (file::isDirectory(src)) {
if (!file::findOrCreateDirectory(dst, 0755)) {
return false;
}
// Process one entry at a time: release the device lock between iterations
// so other SPI bus users aren't starved, and stop immediately on failure.
auto lock = file::getLock(src);
lock->lock();
DIR* dir = opendir(src.c_str());
if (!dir) {
lock->unlock();
file::deleteRecursively(dst);
return false;
}
bool success = true;
while (success) {
struct dirent* entry = readdir(dir);
if (!entry) break;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;
std::string name = entry->d_name; // copy before releasing lock
lock->unlock();
success = copyRecursive(file::getChildPath(src, name), file::getChildPath(dst, name));
lock->lock();
}
closedir(dir);
lock->unlock();
if (!success) {
file::deleteRecursively(dst);
}
return success;
} else {
return copyFileContents(src, dst);
}
}
// endregion
void View::viewFile(const std::string& path, const std::string& filename) {
@ -280,9 +388,13 @@ void View::onNewFolderPressed() {
inputdialog::start("New Folder", "Enter folder name:", "");
}
void View::showActionsForDirectory() {
void View::showActions() {
lv_obj_clean(action_list);
auto* copy_button = lv_list_add_button(action_list, LV_SYMBOL_COPY, "Copy");
lv_obj_add_event_cb(copy_button, onCopyPressedCallback, LV_EVENT_SHORT_CLICKED, this);
auto* cut_button = lv_list_add_button(action_list, LV_SYMBOL_CUT, "Cut");
lv_obj_add_event_cb(cut_button, onCutPressedCallback, LV_EVENT_SHORT_CLICKED, this);
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");
@ -291,16 +403,8 @@ void View::showActionsForDirectory() {
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::showActionsForDirectory() { showActions(); }
void View::showActionsForFile() { showActions(); }
void View::update(size_t start_index) {
const bool is_root = (state->getCurrentPath() == "/");
@ -360,9 +464,15 @@ void View::update(size_t start_index) {
});
if (is_root) {
lv_obj_add_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(lv_obj_get_parent(navigate_up_button), LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_remove_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN);
lv_obj_remove_flag(lv_obj_get_parent(navigate_up_button), LV_OBJ_FLAG_HIDDEN);
}
if (state->hasClipboard() && !is_root) {
lv_obj_remove_flag(lv_obj_get_parent(paste_button), LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag(lv_obj_get_parent(paste_button), LV_OBJ_FLAG_HIDDEN);
}
}
@ -374,6 +484,8 @@ void View::init(const AppContext& appContext, lv_obj_t* parent) {
navigate_up_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_UP, &onNavigateUpPressedCallback, this);
new_file_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_FILE, &onNewFilePressedCallback, this);
new_folder_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_DIRECTORY, &onNewFolderPressedCallback, this);
paste_button = lvgl::toolbar_add_image_button_action(toolbar, LV_SYMBOL_PASTE, &onPastePressedCallback, this);
lv_obj_add_flag(lv_obj_get_parent(paste_button), LV_OBJ_FLAG_HIDDEN);
auto* wrapper = lv_obj_create(parent);
lv_obj_set_width(wrapper, LV_PCT(100));
@ -454,7 +566,15 @@ void View::onResult(LaunchId launchId, Result result, std::unique_ptr<Bundle> bu
auto lock = file::getLock(filepath);
lock->lock();
std::string rename_to = file::getChildPath(state->getCurrentPath(), new_name);
if (rename(filepath.c_str(), rename_to.c_str())) {
struct stat st;
if (stat(rename_to.c_str(), &st) == 0) {
LOGGER.warn("Rename: destination already exists: \"{}\"", rename_to);
lock->unlock();
state->setPendingAction(State::ActionNone);
alertdialog::start("Rename failed", "\"" + new_name + "\" already exists.");
break;
}
if (rename(filepath.c_str(), rename_to.c_str()) == 0) {
LOGGER.info("Renamed \"{}\" to \"{}\"", filepath, rename_to);
} else {
LOGGER.error("Failed to rename \"{}\" to \"{}\"", filepath, rename_to);
@ -522,11 +642,134 @@ void View::onResult(LaunchId launchId, Result result, std::unique_ptr<Bundle> bu
}
break;
}
case State::ActionPaste: {
if (alertdialog::getResultIndex(*bundle) == 0) {
auto clipboard = state->getClipboard();
if (clipboard.has_value()) {
std::string dst = state->getPendingPasteDst();
// Trade-off: dst is removed before the copy attempt. If doPaste
// subsequently fails (e.g. source read error, out of space), the
// original dst data is unrecoverable. Acceptable for an embedded
// file manager; a safer approach would rename dst to a temp path
// first and roll back on failure.
if (file::deleteRecursively(dst)) {
doPaste(clipboard->first, clipboard->second, dst);
} else {
LOGGER.error("Overwrite: failed to remove existing destination: \"{}\"", dst);
state->setPendingAction(State::ActionNone);
alertdialog::start(
"Overwrite failed",
"Could not remove \"" + file::getLastPathSegment(dst) + "\" before overwriting."
);
}
}
}
break;
}
default:
break;
}
}
void View::onCopyPressed() {
std::string path = state->getSelectedChildPath();
state->setClipboard(path, false);
LOGGER.info("Copied to clipboard: {}", path);
onNavigate();
update();
}
void View::onCutPressed() {
std::string path = state->getSelectedChildPath();
state->setClipboard(path, true);
LOGGER.info("Cut to clipboard: {}", path);
onNavigate();
update();
}
void View::onPastePressed() {
auto clipboard = state->getClipboard();
if (!clipboard.has_value()) return;
std::string src = clipboard->first;
bool is_cut = clipboard->second;
std::string entry_name = file::getLastPathSegment(src);
std::string dst = file::getChildPath(state->getCurrentPath(), entry_name);
// Note: getLock(src) guards the source path; the existence check below is
// against dst, so there is a TOCTOU gap — another writer could create dst
// between this check and the write inside doPaste. Acceptable on a
// single-user embedded device; locking dst instead would be more correct.
if (src == dst) {
LOGGER.info("Paste: source and destination are the same path, skipping");
return;
}
auto lock = file::getLock(src);
lock->lock();
struct stat st;
bool dst_exists = (stat(dst.c_str(), &st) == 0);
lock->unlock();
if (dst_exists) {
state->setPendingPasteDst(dst);
state->setPendingAction(State::ActionPaste);
const std::vector<std::string> choices = {"Overwrite", "Cancel"};
alertdialog::start("File exists", "Overwrite \"" + entry_name + "\"?", choices);
return;
}
doPaste(src, is_cut, dst);
}
void View::doPaste(const std::string& src, bool is_cut, const std::string& dst) {
bool success = false;
bool src_delete_failed = false;
if (is_cut) {
auto lock = file::getLock(src);
lock->lock();
success = (rename(src.c_str(), dst.c_str()) == 0);
lock->unlock();
if (!success) {
// Fallback for cross-filesystem moves: copy then delete.
// Only mark success if both halves succeed — if the source removal
// fails we leave success=false so the clipboard is preserved and
// the error is surfaced; the user must remove the source manually.
if (copyRecursive(src, dst)) {
if (file::deleteRecursively(src)) {
success = true;
} else {
src_delete_failed = true;
LOGGER.error("Cut: copied \"{}\" to \"{}\" but failed to remove source — manual cleanup required", src, dst);
}
}
}
} else {
success = copyRecursive(src, dst);
}
const std::string filename = file::getLastPathSegment(src);
if (success) {
LOGGER.info("{} \"{}\" to \"{}\"", is_cut ? "Moved" : "Copied", src, dst);
if (is_cut) {
state->clearClipboard();
}
} else if (src_delete_failed) {
state->setPendingAction(State::ActionNone); // prevent re-trigger on dialog dismiss
alertdialog::start("Move incomplete", "\"" + filename + "\" was copied but the original could not be removed.\nPlease delete it manually.");
} else {
LOGGER.error("Failed to {} \"{}\" to \"{}\"", is_cut ? "move" : "copy", src, dst);
state->setPendingAction(State::ActionNone); // prevent re-trigger on dialog dismiss
alertdialog::start(
std::string("Failed to ") + (is_cut ? "move" : "copy"),
"\"" + filename + "\" could not be " + (is_cut ? "moved." : "copied.")
);
}
state->setEntriesForPath(state->getCurrentPath());
update();
}
void View::deinit(const AppContext& appContext) {
lv_obj_remove_event_cb(dir_entry_list, dirEntryListScrollBeginCallback);
}

View File

@ -1,6 +1,5 @@
#include <private/elf_symbol.h>
#include <cstddef>
#include <cstdlib>
#include <symbols/mbedtls.h>

View File

@ -87,6 +87,8 @@ const esp_elfsym main_symbols[] {
ESP_ELFSYM_EXPORT(unlink),
// strings.h
ESP_ELFSYM_EXPORT(explicit_bzero),
ESP_ELFSYM_EXPORT(strcasecmp),
ESP_ELFSYM_EXPORT(strncasecmp),
// time.h
ESP_ELFSYM_EXPORT(clock_gettime),
ESP_ELFSYM_EXPORT(strftime),