Skip to content

Commit

Permalink
Fileman copy/paste support (#970)
Browse files Browse the repository at this point in the history
* Add copy/paste UI instead of file save
  • Loading branch information
kallanreed committed May 10, 2023
1 parent 9a22a76 commit 8cae998
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 289 deletions.
231 changes: 154 additions & 77 deletions firmware/application/apps/ui_fileman.cpp
Expand Up @@ -192,8 +192,8 @@ const fileman_entry& FileManBaseView::get_selected_entry() const {
FileManBaseView::FileManBaseView(
NavigationView& nav,
std::string filter
) : nav_ (nav),
extension_filter { filter }
) : nav_{ nav },
extension_filter{ filter }
{
add_children({
&labels,
Expand All @@ -206,15 +206,15 @@ FileManBaseView::FileManBaseView(
};

if (!sdcIsCardInserted(&SDCD1)) {
empty_root = true;
empty_ = EmptyReason::NoSDC;
text_current.set("NO SD CARD!");
return;
}

load_directory_contents(current_path);

if (!entry_list.size()) {
empty_root = true;
empty_ = EmptyReason::NoFiles;
text_current.set("EMPTY SD CARD!");
} else {
menu_view.on_left = [this]() {
Expand All @@ -224,7 +224,7 @@ FileManBaseView::FileManBaseView(
}

void FileManBaseView::focus() {
if (empty_root) {
if (empty_ != EmptyReason::NotEmpty) {
button_exit.focus();
} else {
menu_view.focus();
Expand Down Expand Up @@ -310,34 +310,8 @@ const FileManBaseView::file_assoc_t& FileManBaseView::get_assoc(
return file_types[index];
}

/*void FileSaveView::on_save_name() {
text_prompt(nav_, &filename_buffer, 8, [this](std::string * buffer) {
nav_.pop();
});
}
FileSaveView::FileSaveView(
NavigationView& nav
) : FileManBaseView(nav)
{
name_buffer.clear();
add_children({
&text_save,
&button_save_name,
&live_timestamp
});
button_save_name.on_select = [this, &nav](Button&) {
on_save_name();
};
}*/

/* FileLoadView **************************************************************/

void FileLoadView::refresh_widgets(const bool) {
set_dirty();
}

FileLoadView::FileLoadView(
NavigationView& nav,
std::string filter
Expand Down Expand Up @@ -367,6 +341,68 @@ FileLoadView::FileLoadView(
};
}

void FileLoadView::refresh_widgets(const bool) {
set_dirty();
}

/* FileSaveView **************************************************************/
/*
FileSaveView::FileSaveView(
NavigationView& nav,
const fs::path& path,
const fs::path& file
) : nav_{ nav },
path_{ path },
file_{ file }
{
add_children({
&labels,
&text_path,
&button_edit_path,
&text_name,
&button_edit_name,
&button_save,
&button_cancel,
});
button_edit_path.on_select = [this](Button&) {
buffer_ = path_.string();
text_prompt(nav_, buffer_, max_filename_length,
[this](std::string&) {
path_ = buffer_;
refresh_widgets();
});
};
button_edit_name.on_select = [this](Button&) {
buffer_ = file_.string();
text_prompt(nav_, buffer_, max_filename_length,
[this](std::string&) {
file_ = buffer_;
refresh_widgets();
});
};
button_save.on_select = [this](Button&) {
if (on_save)
on_save(path_ / file_);
else
nav_.pop();
};
button_cancel.on_select = [this](Button&) {
nav_.pop();
};
refresh_widgets();
}
void FileSaveView::refresh_widgets() {
text_path.set(truncate(path_, 30));
text_name.set(truncate(file_, 30));
set_dirty();
}
*/
/* FileManagerView ***********************************************************/

void FileManagerView::on_rename() {
Expand Down Expand Up @@ -429,6 +465,30 @@ void FileManagerView::on_new_dir() {
});
}

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);
if (result.code() != FR_OK)
nav_.display_modal("Paste Failed", result.what());

copy_path = fs::path{ };
menu_view.focus();
reload_current();
}

bool FileManagerView::selected_is_valid() const {
return !entry_list.empty() &&
get_selected_entry().path != parent_dir_path;
Expand All @@ -438,62 +498,79 @@ void FileManagerView::refresh_widgets(const bool v) {
button_rename.hidden(v);
button_delete.hidden(v);
button_new_dir.hidden(v);
button_copy.hidden(v);
button_paste.hidden(v);
set_dirty();
}

FileManagerView::~FileManagerView() {
}

FileManagerView::FileManagerView(
NavigationView& nav
) : FileManBaseView(nav, "")
{
if (!empty_root) {
on_refresh_widgets = [this](bool v) {
refresh_widgets(v);
};

add_children({
&menu_view,
&labels,
&text_date,
&button_rename,
&button_delete,
&button_new_dir,
});

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
text_date.set("");
};

refresh_list();
// Don't bother with the UI in the case of no SDC.
if (empty_ == EmptyReason::NoSDC)
return;

on_refresh_widgets = [this](bool v) {
refresh_widgets(v);
};

on_select_entry = [this](KeyEvent key) {
if (key == KeyEvent::Select && get_selected_entry().is_directory) {
push_dir(get_selected_entry().path);
} else {
button_rename.focus();
}
};

button_rename.on_select = [this](Button&) {
if (selected_is_valid())
on_rename();
};
add_children({
&menu_view,
&labels,
&text_date,
&button_rename,
&button_delete,
&button_new_dir,
&button_copy,
&button_paste
});

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
text_date.set("");
};

refresh_list();

button_delete.on_select = [this](Button&) {
if (selected_is_valid())
on_delete();
};
on_select_entry = [this](KeyEvent key) {
if (key == KeyEvent::Select && get_selected_entry().is_directory) {
push_dir(get_selected_entry().path);
} else {
button_rename.focus();
}
};

button_rename.on_select = [this](Button&) {
if (selected_is_valid())
on_rename();
};

button_new_dir.on_select = [this](Button&) {
on_new_dir();
};
}
button_delete.on_select = [this](Button&) {
if (selected_is_valid())
on_delete();
};

button_new_dir.on_select = [this](Button&) {
on_new_dir();
};

button_copy.on_select = [this](Button&) {
if (selected_is_valid() && !get_selected_entry().is_directory)
copy_path = get_selected_full_path();
else
nav_.display_modal("Copy", "Can't copy that.");
};

button_paste.on_select = [this](Button&) {
if (!copy_path.empty())
on_paste();
else
nav_.display_modal("Paste", "Copy a file first.");
};
}

}

0 comments on commit 8cae998

Please sign in to comment.