Fileman icon toolbar (#975)

* Add cut/copy/paste menu with icons
* Add icons, file limit to fileman
This commit is contained in:
Kyle Reed 2023-05-11 13:46:38 -07:00 committed by GitHub
parent f9fdeb3419
commit a832bd433d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 306 additions and 63 deletions

View file

@ -22,7 +22,6 @@
/* TODO:
* - Paging menu items
* - Copy/Move
*/
#include <algorithm>
@ -146,6 +145,22 @@ bool partner_file_prompt(
return true;
}
fs::path get_unique_filename(const fs::path& path, const fs::path& file) {
auto stem = file.stem();
auto ext = file.extension();
auto serial = 1;
fs::path new_path = file;
while (fs::file_exists(path / new_path)) {
new_path = stem;
new_path += fs::path{ u"_" };
new_path += to_string_dec_int(serial++);
new_path += ext;
}
return new_path;
}
}
namespace ui {
@ -198,6 +213,7 @@ FileManBaseView::FileManBaseView(
add_children({
&labels,
&text_current,
&text_info,
&button_exit
});
@ -287,8 +303,13 @@ void FileManBaseView::refresh_list() {
}
});
}
// HACK: Should page menu items instead of limiting the number.
if (menu_view.item_count() >= max_items_shown)
break;
}
text_info.set(menu_view.item_count() >= max_items_shown ? "Too many files!" : "");
menu_view.set_highlighted(prev_highlight);
}
@ -465,26 +486,30 @@ void FileManagerView::on_new_dir() {
});
}
void FileManagerView::on_new_file() {
name_buffer = "";
text_prompt(nav_, name_buffer, max_filename_length, [this](std::string& file_name) {
make_new_file(current_path / file_name);
reload_current();
});
}
void FileManagerView::on_paste() {
auto stem = copy_path.stem();
auto ext = copy_path.extension();
auto serial = 1;
fs::path new_path = copy_path.filename();
// Create a unique name.
while (fs::file_exists(current_path / new_path)) {
new_path = stem;
new_path += fs::path{ u"_" };
new_path += to_string_dec_int(serial++);
new_path += ext;
}
// TODO: handle partner file. Need to fix nav stack first.
auto result = copy_file(copy_path, current_path / new_path);
auto new_name = get_unique_filename(current_path, clipboard_path.filename());
fs::filesystem_error result;
if (clipboard_mode == ClipboardMode::Cut)
result = rename_file(clipboard_path, current_path / new_name);
else if (clipboard_mode == ClipboardMode::Copy)
result = copy_file(clipboard_path, current_path / new_name);
if (result.code() != FR_OK)
nav_.display_modal("Paste Failed", result.what());
copy_path = fs::path{ };
clipboard_path = fs::path{ };
clipboard_mode = ClipboardMode::None;
menu_view.focus();
reload_current();
}
@ -497,9 +522,12 @@ bool FileManagerView::selected_is_valid() const {
void FileManagerView::refresh_widgets(const bool v) {
button_rename.hidden(v);
button_delete.hidden(v);
button_new_dir.hidden(v);
button_cut.hidden(v);
button_copy.hidden(v);
button_paste.hidden(v);
button_new_dir.hidden(v);
button_new_file.hidden(v);
set_dirty();
}
@ -521,13 +549,14 @@ FileManagerView::FileManagerView(
&text_date,
&button_rename,
&button_delete,
&button_new_dir,
&button_cut,
&button_copy,
&button_paste
&button_paste,
&button_new_dir,
&button_new_file
});
menu_view.on_highlight = [this]() {
// TODO: enable/disable buttons.
if (selected_is_valid())
text_date.set(to_string_FAT_timestamp(file_created_date(get_selected_full_path())));
else
@ -544,32 +573,45 @@ FileManagerView::FileManagerView(
}
};
button_rename.on_select = [this](Button&) {
button_rename.on_select = [this]() {
if (selected_is_valid())
on_rename();
};
button_delete.on_select = [this](Button&) {
button_delete.on_select = [this]() {
if (selected_is_valid())
on_delete();
};
button_new_dir.on_select = [this](Button&) {
on_new_dir();
button_cut.on_select = [this]() {
if (selected_is_valid() && !get_selected_entry().is_directory) {
clipboard_path = get_selected_full_path();
clipboard_mode = ClipboardMode::Cut;
} else
nav_.display_modal("Cut", "Can't cut that.");
};
button_copy.on_select = [this](Button&) {
if (selected_is_valid() && !get_selected_entry().is_directory)
copy_path = get_selected_full_path();
else
button_copy.on_select = [this]() {
if (selected_is_valid() && !get_selected_entry().is_directory) {
clipboard_path = get_selected_full_path();
clipboard_mode = ClipboardMode::Copy;
} else
nav_.display_modal("Copy", "Can't copy that.");
};
button_paste.on_select = [this](Button&) {
if (!copy_path.empty())
button_paste.on_select = [this]() {
if (clipboard_mode != ClipboardMode::None)
on_paste();
else
nav_.display_modal("Paste", "Copy a file first.");
nav_.display_modal("Paste", "Cut or copy a file first.");
};
button_new_dir.on_select = [this]() {
on_new_dir();
};
button_new_file.on_select = [this]() {
on_new_file();
};
}