mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-21 05:17:37 +00:00
Fileman icon toolbar (#975)
* Add cut/copy/paste menu with icons * Add icons, file limit to fileman
This commit is contained in:
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -42,6 +42,12 @@ enum class EmptyReason : uint8_t {
|
||||
NoSDC
|
||||
};
|
||||
|
||||
enum class ClipboardMode : uint8_t {
|
||||
None,
|
||||
Cut,
|
||||
Copy
|
||||
};
|
||||
|
||||
class FileManBaseView : public View {
|
||||
public:
|
||||
FileManBaseView(
|
||||
@@ -56,6 +62,7 @@ public:
|
||||
|
||||
protected:
|
||||
static constexpr size_t max_filename_length = 64;
|
||||
static constexpr size_t max_items_shown = 128;
|
||||
|
||||
struct file_assoc_t {
|
||||
std::filesystem::path extension;
|
||||
@@ -109,6 +116,12 @@ protected:
|
||||
{ 0, 2 * 8, 240, 26 * 8 },
|
||||
true
|
||||
};
|
||||
|
||||
// HACK: for item count limit.
|
||||
Text text_info {
|
||||
{ 1 * 8, 35 * 8, 15 * 8, 16 },
|
||||
""
|
||||
};
|
||||
|
||||
Button button_exit {
|
||||
{ 21 * 8, 34 * 8, 9 * 8, 32 },
|
||||
@@ -194,13 +207,15 @@ public:
|
||||
private:
|
||||
// Passed by ref to other views needing lifetime extension.
|
||||
std::string name_buffer { };
|
||||
std::filesystem::path copy_path { };
|
||||
std::filesystem::path clipboard_path { };
|
||||
ClipboardMode clipboard_mode { ClipboardMode::None };
|
||||
|
||||
void refresh_widgets(const bool v);
|
||||
void on_rename();
|
||||
void on_delete();
|
||||
void on_new_dir();
|
||||
void on_paste();
|
||||
void on_new_dir();
|
||||
void on_new_file();
|
||||
|
||||
// True if the selected entry is a real file item.
|
||||
bool selected_is_valid() const;
|
||||
@@ -214,29 +229,53 @@ private:
|
||||
""
|
||||
};
|
||||
|
||||
Button button_rename {
|
||||
{ 0 * 8, 29 * 8, 9 * 8, 32 },
|
||||
"Rename"
|
||||
NewButton button_rename {
|
||||
{ 0 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_rename,
|
||||
Color::dark_blue()
|
||||
};
|
||||
|
||||
Button button_delete {
|
||||
{ 21 * 4, 29 * 8, 9 * 8, 32 },
|
||||
"Delete"
|
||||
NewButton button_delete {
|
||||
{ 4 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_trash,
|
||||
Color::red()
|
||||
};
|
||||
|
||||
Button button_new_dir {
|
||||
{ 21 * 8, 29 * 8, 9 * 8, 32 },
|
||||
"New Dir"
|
||||
NewButton button_cut {
|
||||
{ 9 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_cut,
|
||||
Color::dark_grey()
|
||||
};
|
||||
|
||||
Button button_copy {
|
||||
{ 0 * 8, 34 * 8, 9 * 8, 32 },
|
||||
"Copy"
|
||||
NewButton button_copy {
|
||||
{ 13 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_copy,
|
||||
Color::dark_grey()
|
||||
};
|
||||
|
||||
Button button_paste {
|
||||
{ 21 * 4, 34 * 8, 9 * 8, 32 },
|
||||
"Paste"
|
||||
NewButton button_paste {
|
||||
{ 17 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_paste,
|
||||
Color::dark_grey()
|
||||
};
|
||||
|
||||
NewButton button_new_dir {
|
||||
{ 22 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_new_dir,
|
||||
Color::green()
|
||||
};
|
||||
|
||||
NewButton button_new_file {
|
||||
{ 26 * 8, 29 * 8, 4 * 8, 32 },
|
||||
{ },
|
||||
&bitmap_icon_new_file,
|
||||
Color::green()
|
||||
};
|
||||
};
|
||||
|
||||
|
@@ -446,7 +446,7 @@ namespace ui {
|
||||
else
|
||||
{
|
||||
auto result = delete_file( pmem_flag_file );
|
||||
if( result != 0 )
|
||||
if( result.code() != FR_OK )
|
||||
{
|
||||
text_pmem_status.set("!err. deleting pmem flagfile!");
|
||||
}
|
||||
|
Reference in New Issue
Block a user