mirror of
https://github.com/eried/portapack-mayhem.git
synced 2024-10-01 01:26:06 -04:00
Merge pull request #941 from kallanreed/fileman_ux
Fileman UI and Perf Fixes
This commit is contained in:
commit
2f343adf21
@ -206,7 +206,7 @@ void SoundBoardView::refresh_list() {
|
||||
file_list[n].string().substr(0, 30),
|
||||
ui::Color::white(),
|
||||
nullptr,
|
||||
[this](){
|
||||
[this](KeyEvent){
|
||||
on_select_entry();
|
||||
}
|
||||
});
|
||||
|
@ -20,72 +20,167 @@
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/* TODO:
|
||||
* - Paging menu items
|
||||
* - UI with empty SD card
|
||||
* - Copy/Move
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include "ui_fileman.hpp"
|
||||
#include "string_format.hpp"
|
||||
#include "portapack.hpp"
|
||||
#include "event_m0.hpp"
|
||||
|
||||
using namespace portapack;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
using namespace ui;
|
||||
|
||||
bool is_hidden_file(const fs::path& path) {
|
||||
return !path.empty() && path.native()[0] == u'.';
|
||||
}
|
||||
|
||||
// Gets a truncated name from a path for display.
|
||||
std::string truncate(const fs::path& path, size_t max_length) {
|
||||
auto name = path.string();
|
||||
return name.length() <= max_length ? name : name.substr(0, max_length);
|
||||
}
|
||||
|
||||
// Gets a human readable file size string.
|
||||
std::string get_pretty_size(uint32_t file_size) {
|
||||
static const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
|
||||
size_t suffix_index = 0;
|
||||
|
||||
while (file_size >= 1024) {
|
||||
file_size /= 1024;
|
||||
suffix_index++;
|
||||
}
|
||||
|
||||
if (suffix_index > 4)
|
||||
suffix_index = 4;
|
||||
|
||||
return to_string_dec_uint(file_size) + suffix[suffix_index];
|
||||
}
|
||||
|
||||
// Case insensitive path equality on underlying "native" string.
|
||||
bool iequal(
|
||||
const fs::path& lhs,
|
||||
const fs::path& rhs
|
||||
) {
|
||||
const auto& lhs_str = lhs.native();
|
||||
const auto& rhs_str = rhs.native();
|
||||
|
||||
// NB: Not correct for Unicode/locales.
|
||||
if (lhs_str.length() == rhs_str.length()) {
|
||||
for (size_t i = 0; i < lhs_str.length(); ++i)
|
||||
if (towupper(lhs_str[i]) != towupper(rhs_str[i]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Inserts the entry into the entry list sorted directories first then by file name.
|
||||
void insert_sorted(std::vector<fileman_entry>& entries, fileman_entry&& entry) {
|
||||
auto it = std::lower_bound(std::begin(entries), std::end(entries), entry,
|
||||
[](const fileman_entry& lhs, const fileman_entry& rhs) {
|
||||
if (lhs.is_directory && !rhs.is_directory)
|
||||
return true;
|
||||
else if (!lhs.is_directory && rhs.is_directory)
|
||||
return false;
|
||||
else
|
||||
return lhs.path < rhs.path;
|
||||
});
|
||||
|
||||
entries.insert(it, std::move(entry));
|
||||
}
|
||||
|
||||
// Returns the partner file path or an empty path if no partner is found.
|
||||
fs::path get_partner_file(fs::path path) {
|
||||
const fs::path txt_path{ u".TXT" };
|
||||
const fs::path c16_path{ u".C16" };
|
||||
auto ext = path.extension();
|
||||
|
||||
if (iequal(ext, txt_path))
|
||||
ext = c16_path;
|
||||
else if (iequal(ext, c16_path))
|
||||
ext = txt_path;
|
||||
else
|
||||
return { };
|
||||
|
||||
path.replace_extension(ext);
|
||||
return file_exists(path) ? path : fs::path{ };
|
||||
}
|
||||
|
||||
// Modal prompt to update the partner file if it exists.
|
||||
// Runs continuation on_partner_action to update the partner file.
|
||||
// Returns true is a partner is found, otherwise false.
|
||||
bool partner_file_prompt(
|
||||
NavigationView& nav,
|
||||
const fs::path& path,
|
||||
std::string action_name,
|
||||
std::function<void(const fs::path&, bool)> on_partner_action
|
||||
) {
|
||||
auto partner = get_partner_file(path);
|
||||
|
||||
if (partner.empty())
|
||||
return false;
|
||||
|
||||
nav.push_under_current<ModalMessageView>(
|
||||
"Partner File",
|
||||
partner.filename().string() + "\n" + action_name + " this file too?",
|
||||
YESNO,
|
||||
[&nav, partner, on_partner_action](bool choice) {
|
||||
if (on_partner_action)
|
||||
on_partner_action(partner, choice);
|
||||
}
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace ui {
|
||||
|
||||
void FileManBaseView::load_directory_contents(const std::filesystem::path& dir_path) {
|
||||
void FileManBaseView::load_directory_contents(const fs::path& dir_path) {
|
||||
current_path = dir_path;
|
||||
|
||||
text_current.set(dir_path.string().length()? dir_path.string().substr(0, 30 - 6):"(sd root)");
|
||||
|
||||
entry_list.clear();
|
||||
|
||||
auto filtering = (bool)extension_filter.size();
|
||||
|
||||
// List directories and files, put directories up top
|
||||
if (dir_path.string().length())
|
||||
entry_list.push_back({ u"..", 0, true });
|
||||
auto filtering = !extension_filter.empty();
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(dir_path, u"*")) {
|
||||
text_current.set(dir_path.empty() ? "(sd root)" : truncate(dir_path, 24));
|
||||
|
||||
for (const auto& entry : fs::directory_iterator(dir_path, u"*")) {
|
||||
// Hide files starting with '.' (hidden / tmp).
|
||||
if (is_hidden_file(entry.path()))
|
||||
continue;
|
||||
|
||||
// do not display dir / files starting with '.' (hidden / tmp)
|
||||
if (entry.path().string().length() && entry.path().filename().string()[0] != '.') {
|
||||
if (std::filesystem::is_regular_file(entry.status())) {
|
||||
bool matched = true;
|
||||
if (filtering) {
|
||||
auto entry_extension = entry.path().extension().string();
|
||||
|
||||
for (auto &c: entry_extension)
|
||||
c = toupper(c);
|
||||
|
||||
if (entry_extension != extension_filter)
|
||||
matched = false;
|
||||
}
|
||||
|
||||
if (matched)
|
||||
entry_list.push_back({ entry.path(), (uint32_t)entry.size(), false });
|
||||
} else if (std::filesystem::is_directory(entry.status())) {
|
||||
entry_list.insert(entry_list.begin(), { entry.path(), 0, true });
|
||||
}
|
||||
if (fs::is_regular_file(entry.status())) {
|
||||
if (!filtering || iequal(entry.path().extension(), extension_filter))
|
||||
insert_sorted(entry_list, { entry.path(), (uint32_t)entry.size(), false });
|
||||
} else if (fs::is_directory(entry.status())) {
|
||||
insert_sorted(entry_list, { entry.path(), 0, true });
|
||||
}
|
||||
}
|
||||
|
||||
// Add "parent" directory if not at the root.
|
||||
if (!dir_path.empty())
|
||||
entry_list.insert(entry_list.begin(), { parent_dir_path, 0, true });
|
||||
}
|
||||
|
||||
std::filesystem::path FileManBaseView::get_selected_path() {
|
||||
auto selected_path_str = current_path.string();
|
||||
auto entry_path = entry_list[menu_view.highlighted_index()].entry_path.string();
|
||||
|
||||
if (entry_path == "..") {
|
||||
selected_path_str = get_parent_dir().string();
|
||||
} else {
|
||||
if (selected_path_str.back() != '/')
|
||||
selected_path_str += '/';
|
||||
|
||||
selected_path_str += entry_path;
|
||||
}
|
||||
|
||||
return selected_path_str;
|
||||
fs::path FileManBaseView::get_selected_full_path() const {
|
||||
if (get_selected_entry().path == parent_dir_path)
|
||||
return current_path.parent_path();
|
||||
|
||||
return current_path / get_selected_entry().path;
|
||||
}
|
||||
|
||||
std::filesystem::path FileManBaseView::get_parent_dir() {
|
||||
auto current_path_str = current_path.string();
|
||||
return current_path.string().substr(0, current_path_str.find_last_of('/'));
|
||||
const fileman_entry& FileManBaseView::get_selected_entry() const {
|
||||
return entry_list[menu_view.highlighted_index()];
|
||||
}
|
||||
|
||||
FileManBaseView::FileManBaseView(
|
||||
@ -105,20 +200,21 @@ FileManBaseView::FileManBaseView(
|
||||
};
|
||||
|
||||
if (!sdcIsCardInserted(&SDCD1)) {
|
||||
empty_root=true;
|
||||
empty_root = true;
|
||||
text_current.set("NO SD CARD!");
|
||||
return;
|
||||
}
|
||||
|
||||
load_directory_contents(current_path);
|
||||
|
||||
if (!entry_list.size()) {
|
||||
empty_root = true;
|
||||
text_current.set("EMPTY SD CARD!");
|
||||
} else {
|
||||
load_directory_contents(current_path);
|
||||
if (!entry_list.size())
|
||||
{
|
||||
empty_root = true;
|
||||
text_current.set("EMPTY SD CARD!");
|
||||
} else {
|
||||
menu_view.on_left = [&nav, this]() {
|
||||
load_directory_contents(get_parent_dir());
|
||||
refresh_list();
|
||||
};
|
||||
}
|
||||
menu_view.on_left = [this]() {
|
||||
current_path = current_path.parent_path();
|
||||
reload_current();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,61 +232,55 @@ void FileManBaseView::refresh_list() {
|
||||
|
||||
menu_view.clear();
|
||||
|
||||
for (size_t n = 0; n < entry_list.size(); n++) {
|
||||
auto entry = &entry_list[n];
|
||||
auto entry_name = entry->entry_path.filename().string().substr(0, 20);
|
||||
|
||||
if (entry->is_directory) {
|
||||
|
||||
for (const auto& entry : entry_list) {
|
||||
auto entry_name = truncate(entry.path, 20);
|
||||
|
||||
if (entry.is_directory) {
|
||||
menu_view.add_item({
|
||||
entry_name,
|
||||
ui::Color::yellow(),
|
||||
&bitmap_icon_dir,
|
||||
[this](){
|
||||
[this](KeyEvent key) {
|
||||
if (on_select_entry)
|
||||
on_select_entry();
|
||||
on_select_entry(key);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
auto file_size = entry->size;
|
||||
size_t suffix_index = 0;
|
||||
|
||||
while (file_size >= 1024) {
|
||||
file_size /= 1024;
|
||||
suffix_index++;
|
||||
}
|
||||
if (suffix_index > 4)
|
||||
suffix_index = 4;
|
||||
|
||||
std::string size_str = to_string_dec_uint(file_size) + suffix[suffix_index];
|
||||
|
||||
auto entry_extension = entry->entry_path.extension().string();
|
||||
for (auto &c: entry_extension)
|
||||
c = toupper(c);
|
||||
|
||||
// Associate extension to icon and color
|
||||
size_t c;
|
||||
for (c = 0; c < file_types.size() - 1; c++) {
|
||||
if (entry_extension == file_types[c].extension)
|
||||
break;
|
||||
}
|
||||
const auto& assoc = get_assoc(entry.path.extension());
|
||||
auto size_str = get_pretty_size(entry.size);
|
||||
|
||||
menu_view.add_item({
|
||||
entry_name + std::string(21 - entry_name.length(), ' ') + size_str,
|
||||
file_types[c].color,
|
||||
file_types[c].icon,
|
||||
[this](){
|
||||
assoc.color,
|
||||
assoc.icon,
|
||||
[this](KeyEvent key) {
|
||||
if (on_select_entry)
|
||||
on_select_entry();
|
||||
on_select_entry(key);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
menu_view.set_highlighted(0); // Refresh
|
||||
menu_view.set_highlighted(0); // Refresh
|
||||
}
|
||||
|
||||
void FileManBaseView::reload_current() {
|
||||
load_directory_contents(current_path);
|
||||
refresh_list();
|
||||
}
|
||||
|
||||
const FileManBaseView::file_assoc_t& FileManBaseView::get_assoc(
|
||||
const fs::path& ext) const
|
||||
{
|
||||
size_t index = 0;
|
||||
|
||||
for (; index < file_types.size() - 1; ++index)
|
||||
if (iequal(ext, file_types[index].extension))
|
||||
return file_types[index];
|
||||
|
||||
// Default to last entry in the list.
|
||||
return file_types[index];
|
||||
}
|
||||
|
||||
/*void FileSaveView::on_save_name() {
|
||||
@ -215,8 +305,7 @@ FileSaveView::FileSaveView(
|
||||
};
|
||||
}*/
|
||||
|
||||
void FileLoadView::refresh_widgets(const bool v) {
|
||||
(void)v; //avoid unused warning
|
||||
void FileLoadView::refresh_widgets(const bool) {
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
@ -238,84 +327,96 @@ FileLoadView::FileLoadView(
|
||||
|
||||
refresh_list();
|
||||
|
||||
on_select_entry = [&nav, this]() {
|
||||
if (entry_list[menu_view.highlighted_index()].is_directory) {
|
||||
load_directory_contents(get_selected_path());
|
||||
refresh_list();
|
||||
on_select_entry = [this](KeyEvent) {
|
||||
if (get_selected_entry().is_directory) {
|
||||
current_path = get_selected_full_path();
|
||||
reload_current();
|
||||
} else {
|
||||
nav_.pop();
|
||||
if (on_changed)
|
||||
on_changed(current_path.string() + '/' + entry_list[menu_view.highlighted_index()].entry_path.string());
|
||||
on_changed(get_selected_full_path());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void FileManagerView::on_rename(NavigationView& nav) {
|
||||
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
||||
std::string destination_path = current_path.string();
|
||||
if (destination_path.back() != '/')
|
||||
destination_path += '/';
|
||||
destination_path = destination_path + buffer;
|
||||
rename_file(get_selected_path(), destination_path);
|
||||
load_directory_contents(current_path);
|
||||
refresh_list();
|
||||
});
|
||||
}
|
||||
void FileManagerView::on_rename() {
|
||||
auto& entry = get_selected_entry();
|
||||
|
||||
void FileManagerView::on_refactor(NavigationView& nav) {
|
||||
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
||||
// Don't rename ".."
|
||||
if (entry.path == parent_dir_path)
|
||||
return;
|
||||
|
||||
std::string destination_path = current_path.string();
|
||||
if (destination_path.back() != '/')//if the path is not ended with '/', add '/'
|
||||
destination_path += '/';
|
||||
name_buffer = entry.path.filename().string();
|
||||
|
||||
auto selected_path = get_selected_path();
|
||||
auto extension = selected_path.extension().string();
|
||||
uint32_t cursor_pos = (uint32_t)name_buffer.length();
|
||||
if (auto pos = name_buffer.find_last_of("."); pos != name_buffer.npos)
|
||||
cursor_pos = pos;
|
||||
|
||||
if(extension.empty()){// Is Dir
|
||||
destination_path = destination_path + buffer;
|
||||
extension_buffer = "";
|
||||
}else{//is File
|
||||
destination_path = destination_path + buffer + extension_buffer;
|
||||
}
|
||||
text_prompt(nav_, name_buffer, cursor_pos, max_filename_length,
|
||||
[this, &entry](std::string& renamed) {
|
||||
auto renamed_path = fs::path{ renamed };
|
||||
rename_file(get_selected_full_path(), current_path / renamed_path);
|
||||
|
||||
rename_file(get_selected_path(), destination_path); //rename the selected file
|
||||
|
||||
if (!extension.empty() && selected_path.string().back() != '/' && extension.substr(1) == "C16") { //substr(1) is for ignore the dot
|
||||
// Rename its partner ( C16 <-> TXT ) file.
|
||||
auto partner_file_path = selected_path.string().substr(0, selected_path.string().size() - 4) + ".TXT";
|
||||
destination_path = destination_path.substr(0, destination_path.size() - 4) + ".TXT";
|
||||
rename_file(partner_file_path, destination_path);
|
||||
} else if (!extension.empty() && selected_path.string().back() != '/' && extension.substr(1) == "TXT") {
|
||||
// If the file user choose is a TXT file.
|
||||
auto partner_file_path = selected_path.string().substr(0, selected_path.string().size() - 4) + ".C16";
|
||||
destination_path = destination_path.substr(0, destination_path.size() - 4) + ".C16";
|
||||
rename_file(partner_file_path, destination_path);
|
||||
}
|
||||
|
||||
load_directory_contents(current_path);
|
||||
refresh_list();
|
||||
|
||||
});
|
||||
auto has_partner = partner_file_prompt(nav_, entry.path, "Rename",
|
||||
[this, renamed_path](const fs::path& partner, bool should_rename) mutable {
|
||||
if (should_rename) {
|
||||
auto new_name = renamed_path.replace_extension(partner.extension());
|
||||
rename_file(current_path / partner, current_path / new_name);
|
||||
}
|
||||
reload_current();
|
||||
}
|
||||
);
|
||||
|
||||
if (!has_partner)
|
||||
reload_current();
|
||||
});
|
||||
}
|
||||
|
||||
void FileManagerView::on_delete() {
|
||||
delete_file(get_selected_path());
|
||||
load_directory_contents(current_path);
|
||||
refresh_list();
|
||||
auto& entry = get_selected_entry();
|
||||
|
||||
// Don't delete ".."
|
||||
if (entry.path == parent_dir_path)
|
||||
return;
|
||||
|
||||
auto name = entry.path.filename().string();
|
||||
nav_.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO,
|
||||
[this, &entry](bool choice) {
|
||||
if (choice) {
|
||||
delete_file(get_selected_full_path());
|
||||
|
||||
auto has_partner = partner_file_prompt(
|
||||
nav_, entry.path, "Delete",
|
||||
[this](const fs::path& partner, bool should_delete) {
|
||||
if (should_delete)
|
||||
delete_file(current_path / partner);
|
||||
reload_current();
|
||||
}
|
||||
);
|
||||
|
||||
if (!has_partner)
|
||||
reload_current();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void FileManagerView::on_new_dir() {
|
||||
name_buffer = "";
|
||||
text_prompt(nav_, name_buffer, max_filename_length, [this](std::string& dir_name) {
|
||||
make_new_directory(current_path / dir_name);
|
||||
reload_current();
|
||||
});
|
||||
}
|
||||
|
||||
void FileManagerView::refresh_widgets(const bool v) {
|
||||
button_rename.hidden(v);
|
||||
button_new_dir.hidden(v);
|
||||
button_refactor.hidden(v);
|
||||
button_delete.hidden(v);
|
||||
button_new_dir.hidden(v);
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
FileManagerView::~FileManagerView() {
|
||||
// Flush ?
|
||||
}
|
||||
|
||||
FileManagerView::FileManagerView(
|
||||
@ -332,60 +433,35 @@ FileManagerView::FileManagerView(
|
||||
&labels,
|
||||
&text_date,
|
||||
&button_rename,
|
||||
&button_refactor,
|
||||
&button_delete,
|
||||
&button_new_dir,
|
||||
&button_delete
|
||||
});
|
||||
|
||||
menu_view.on_highlight = [this]() {
|
||||
text_date.set(to_string_FAT_timestamp(file_created_date(get_selected_path())));
|
||||
text_date.set(to_string_FAT_timestamp(file_created_date(get_selected_full_path())));
|
||||
};
|
||||
|
||||
refresh_list();
|
||||
|
||||
on_select_entry = [this]() {
|
||||
if (entry_list[menu_view.highlighted_index()].is_directory) {
|
||||
load_directory_contents(get_selected_path());
|
||||
on_select_entry = [this](KeyEvent key) {
|
||||
if (key == KeyEvent::Select && get_selected_entry().is_directory) {
|
||||
load_directory_contents(get_selected_full_path());
|
||||
refresh_list();
|
||||
} else
|
||||
} else {
|
||||
button_rename.focus();
|
||||
};
|
||||
|
||||
button_new_dir.on_select = [this, &nav](Button&) {
|
||||
name_buffer.clear();
|
||||
|
||||
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
||||
make_new_directory(current_path.string() + '/' + buffer);
|
||||
load_directory_contents(current_path);
|
||||
refresh_list();
|
||||
});
|
||||
};
|
||||
|
||||
button_rename.on_select = [this, &nav](Button&) {
|
||||
name_buffer = entry_list[menu_view.highlighted_index()].entry_path.filename().string().substr(0, max_filename_length);
|
||||
on_rename(nav);
|
||||
};
|
||||
|
||||
button_refactor.on_select = [this, &nav](Button&) {
|
||||
name_buffer = entry_list[menu_view.highlighted_index()].entry_path.filename().string().substr(0, max_filename_length);
|
||||
size_t pos = name_buffer.find_last_of(".");
|
||||
|
||||
if (pos != std::string::npos) {
|
||||
extension_buffer = name_buffer.substr(pos);
|
||||
name_buffer = name_buffer.substr(0, pos);
|
||||
}
|
||||
|
||||
on_refactor(nav);
|
||||
};
|
||||
|
||||
button_rename.on_select = [this](Button&) {
|
||||
on_rename();
|
||||
};
|
||||
|
||||
button_delete.on_select = [this, &nav](Button&) {
|
||||
// Use display_modal ?
|
||||
nav.push<ModalMessageView>("Delete", "Delete " + entry_list[menu_view.highlighted_index()].entry_path.filename().string() + "\nAre you sure?", YESNO,
|
||||
[this](bool choice) {
|
||||
if (choice)
|
||||
on_delete();
|
||||
}
|
||||
);
|
||||
button_delete.on_select = [this](Button&) {
|
||||
on_delete();
|
||||
};
|
||||
|
||||
button_new_dir.on_select = [this](Button&) {
|
||||
on_new_dir();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@
|
||||
namespace ui {
|
||||
|
||||
struct fileman_entry {
|
||||
std::filesystem::path entry_path { };
|
||||
std::filesystem::path path { };
|
||||
uint32_t size { };
|
||||
bool is_directory { };
|
||||
};
|
||||
@ -43,50 +43,53 @@ public:
|
||||
std::string filter
|
||||
);
|
||||
|
||||
void focus() override;
|
||||
|
||||
void load_directory_contents(const std::filesystem::path& dir_path);
|
||||
std::filesystem::path get_selected_path();
|
||||
|
||||
void focus() override;
|
||||
std::string title() const override { return "Fileman"; };
|
||||
|
||||
protected:
|
||||
NavigationView& nav_;
|
||||
|
||||
static constexpr size_t max_filename_length = 30 - 2;
|
||||
|
||||
const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
|
||||
|
||||
static constexpr size_t max_filename_length = 50;
|
||||
|
||||
struct file_assoc_t {
|
||||
std::string extension;
|
||||
std::filesystem::path extension;
|
||||
const Bitmap* icon;
|
||||
ui::Color color;
|
||||
};
|
||||
|
||||
const std::vector<file_assoc_t> file_types = {
|
||||
{ ".TXT", &bitmap_icon_file_text, ui::Color::white() },
|
||||
{ ".PNG", &bitmap_icon_file_image, ui::Color::green() },
|
||||
{ ".BMP", &bitmap_icon_file_image, ui::Color::green() },
|
||||
{ ".C8", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
||||
{ ".C16", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
||||
{ ".WAV", &bitmap_icon_file_wav, ui::Color::dark_magenta() },
|
||||
{ "", &bitmap_icon_file, ui::Color::light_grey() }
|
||||
{ u".TXT", &bitmap_icon_file_text, ui::Color::white() },
|
||||
{ u".PNG", &bitmap_icon_file_image, ui::Color::green() },
|
||||
{ u".BMP", &bitmap_icon_file_image, ui::Color::green() },
|
||||
{ u".C8", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
||||
{ u".C16", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
||||
{ u".WAV", &bitmap_icon_file_wav, ui::Color::dark_magenta() },
|
||||
{ u"", &bitmap_icon_file, ui::Color::light_grey() } // NB: Must be last.
|
||||
};
|
||||
|
||||
bool empty_root { false };
|
||||
std::function<void(void)> on_select_entry { nullptr };
|
||||
std::function<void(bool)> on_refresh_widgets { nullptr };
|
||||
std::vector<fileman_entry> entry_list { };
|
||||
std::filesystem::path current_path { u"" };
|
||||
std::string extension_filter { "" };
|
||||
|
||||
void change_category(int32_t category_id);
|
||||
std::filesystem::path get_parent_dir();
|
||||
|
||||
|
||||
std::filesystem::path get_selected_full_path() const;
|
||||
const fileman_entry& get_selected_entry() const;
|
||||
|
||||
void refresh_list();
|
||||
void reload_current();
|
||||
void load_directory_contents(const std::filesystem::path& dir_path);
|
||||
const file_assoc_t& get_assoc(const std::filesystem::path& ext) const;
|
||||
|
||||
NavigationView& nav_;
|
||||
|
||||
bool empty_root { false };
|
||||
std::function<void(KeyEvent)> on_select_entry { nullptr };
|
||||
std::function<void(bool)> on_refresh_widgets { nullptr };
|
||||
|
||||
const std::filesystem::path parent_dir_path { u".." };
|
||||
std::filesystem::path current_path { u"" };
|
||||
std::filesystem::path extension_filter { u"" };
|
||||
|
||||
std::vector<fileman_entry> entry_list { };
|
||||
|
||||
Labels labels {
|
||||
{ { 0, 0 }, "Path:", Color::light_grey() }
|
||||
};
|
||||
|
||||
Text text_current {
|
||||
{ 6 * 8, 0 * 8, 24 * 8, 16 },
|
||||
"",
|
||||
@ -142,13 +145,13 @@ public:
|
||||
~FileManagerView();
|
||||
|
||||
private:
|
||||
// Passed by ref to other views needing lifetime extension.
|
||||
std::string name_buffer { };
|
||||
std::string extension_buffer { };
|
||||
|
||||
void refresh_widgets(const bool v);
|
||||
void on_rename(NavigationView& nav);
|
||||
void on_refactor(NavigationView& nav);
|
||||
void on_rename();
|
||||
void on_delete();
|
||||
void on_new_dir();
|
||||
|
||||
Labels labels {
|
||||
{ { 0, 26 * 8 }, "Created ", Color::light_grey() }
|
||||
@ -160,25 +163,19 @@ private:
|
||||
};
|
||||
|
||||
Button button_rename {
|
||||
{ 0 * 8, 29 * 8, 9 * 8, 32 },
|
||||
{ 0 * 8, 29 * 8, 14 * 8, 32 },
|
||||
"Rename"
|
||||
};
|
||||
|
||||
Button button_refactor{
|
||||
{ 10 * 8, 29 * 8, 10 * 8, 32 },
|
||||
"Refactor"
|
||||
};
|
||||
|
||||
Button button_delete {
|
||||
{ 21 * 8, 29 * 8, 9 * 8, 32 },
|
||||
{ 16 * 8, 29 * 8, 14 * 8, 32 },
|
||||
"Delete"
|
||||
};
|
||||
|
||||
|
||||
Button button_new_dir {
|
||||
{ 0 * 8, 34 * 8, 14 * 8, 32 },
|
||||
"New dir"
|
||||
"New Dir"
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
} /* namespace ui */
|
||||
|
@ -44,7 +44,7 @@ FlashUtilityView::FlashUtilityView(NavigationView& nav) : nav_ (nav) {
|
||||
filename.string().substr(0, max_filename_length),
|
||||
ui::Color::red(),
|
||||
&bitmap_icon_temperature,
|
||||
[this, path]() {
|
||||
[this, path](KeyEvent) {
|
||||
this->firmware_selected(path);
|
||||
}
|
||||
});
|
||||
|
@ -120,7 +120,7 @@ void FreqManBaseView::refresh_list() {
|
||||
freqman_item_string(database[n], 30),
|
||||
ui::Color::white(),
|
||||
nullptr,
|
||||
[this](){
|
||||
[this](KeyEvent){
|
||||
if (on_select_frequency)
|
||||
on_select_frequency();
|
||||
}
|
||||
|
@ -197,6 +197,13 @@ std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::
|
||||
return directory_list;
|
||||
}
|
||||
|
||||
bool file_exists(const std::filesystem::path& file_path) {
|
||||
FILINFO filinfo;
|
||||
auto fr = f_stat(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
|
||||
|
||||
return fr == FR_OK;
|
||||
}
|
||||
|
||||
uint32_t delete_file(const std::filesystem::path& file_path) {
|
||||
return f_unlink(reinterpret_cast<const TCHAR*>(file_path.c_str()));
|
||||
}
|
||||
@ -250,6 +257,15 @@ std::string filesystem_error::what() const {
|
||||
}
|
||||
}
|
||||
|
||||
path path::parent_path() const {
|
||||
const auto index = _s.find_last_of(preferred_separator);
|
||||
if( index == _s.npos ) {
|
||||
return { }; // NB: Deviation from STL.
|
||||
} else {
|
||||
return _s.substr(0, index);
|
||||
}
|
||||
}
|
||||
|
||||
path path::extension() const {
|
||||
const auto t = filename().native();
|
||||
const auto index = t.find_last_of(u'.');
|
||||
@ -296,6 +312,10 @@ path& path::replace_extension(const path& replacement) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const path& lhs, const path& rhs) {
|
||||
return lhs.native() == rhs.native();
|
||||
}
|
||||
|
||||
bool operator<(const path& lhs, const path& rhs) {
|
||||
return lhs.native() < rhs.native();
|
||||
}
|
||||
@ -304,6 +324,18 @@ bool operator>(const path& lhs, const path& rhs) {
|
||||
return lhs.native() > rhs.native();
|
||||
}
|
||||
|
||||
path operator+(const path& lhs, const path& rhs) {
|
||||
path result = lhs;
|
||||
result += rhs;
|
||||
return result;
|
||||
}
|
||||
|
||||
path operator/(const path& lhs, const path& rhs) {
|
||||
path result = lhs;
|
||||
result /= rhs;
|
||||
return result;
|
||||
}
|
||||
|
||||
directory_iterator::directory_iterator(
|
||||
std::filesystem::path path,
|
||||
std::filesystem::path wild
|
||||
|
@ -123,6 +123,7 @@ struct path {
|
||||
return *this;
|
||||
}
|
||||
|
||||
path parent_path() const;
|
||||
path extension() const;
|
||||
path filename() const;
|
||||
path stem() const;
|
||||
@ -151,14 +152,24 @@ struct path {
|
||||
return *this;
|
||||
}
|
||||
|
||||
path& operator/=(const path& p) {
|
||||
if (_s.back() != preferred_separator)
|
||||
_s += preferred_separator;
|
||||
_s += p._s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
path& replace_extension(const path& replacement = path());
|
||||
|
||||
private:
|
||||
string_type _s;
|
||||
};
|
||||
|
||||
bool operator==(const path& lhs, const path& rhs);
|
||||
bool operator<(const path& lhs, const path& rhs);
|
||||
bool operator>(const path& lhs, const path& rhs);
|
||||
path operator+(const path& lhs, const path& rhs);
|
||||
path operator/(const path& lhs, const path& rhs);
|
||||
|
||||
using file_status = BYTE;
|
||||
|
||||
@ -238,6 +249,7 @@ struct FATTimestamp {
|
||||
uint16_t FAT_time;
|
||||
};
|
||||
|
||||
bool file_exists(const std::filesystem::path& file_path);
|
||||
uint32_t delete_file(const std::filesystem::path& file_path);
|
||||
uint32_t rename_file(const std::filesystem::path& file_path, const std::filesystem::path& new_name);
|
||||
FATTimestamp file_created_date(const std::filesystem::path& file_path);
|
||||
@ -245,6 +257,8 @@ uint32_t make_new_directory(const std::filesystem::path& dir_path);
|
||||
|
||||
std::vector<std::filesystem::path> scan_root_files(const std::filesystem::path& directory, const std::filesystem::path& extension);
|
||||
std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::path& directory);
|
||||
|
||||
/* Gets an auto incrementing filename. */
|
||||
std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_stem_pattern);
|
||||
|
||||
/* Values added to FatFs FRESULT enum, values outside the FRESULT data type */
|
||||
|
@ -238,7 +238,7 @@ bool MenuView::set_highlighted(int32_t new_value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t MenuView::highlighted_index() {
|
||||
uint32_t MenuView::highlighted_index() const {
|
||||
return highlighted_item;
|
||||
}
|
||||
|
||||
@ -262,7 +262,7 @@ bool MenuView::on_key(const KeyEvent key) {
|
||||
case KeyEvent::Select:
|
||||
case KeyEvent::Right:
|
||||
if( menu_items[highlighted_item].on_select ) {
|
||||
menu_items[highlighted_item].on_select();
|
||||
menu_items[highlighted_item].on_select(key);
|
||||
}
|
||||
return true;
|
||||
|
||||
|
@ -39,7 +39,7 @@ struct MenuItem {
|
||||
std::string text;
|
||||
ui::Color color;
|
||||
const Bitmap* bitmap;
|
||||
std::function<void(void)> on_select;
|
||||
std::function<void(KeyEvent)> on_select;
|
||||
|
||||
// TODO: Prevent default-constructed MenuItems.
|
||||
// I managed to construct a menu with three extra, unspecified menu items
|
||||
@ -87,7 +87,7 @@ public:
|
||||
MenuItemView* item_view(size_t index) const;
|
||||
|
||||
bool set_highlighted(int32_t new_value);
|
||||
uint32_t highlighted_index();
|
||||
uint32_t highlighted_index() const;
|
||||
|
||||
void set_parent_rect(const Rect new_parent_rect) override;
|
||||
void on_focus() override;
|
||||
|
@ -29,9 +29,25 @@ using namespace portapack;
|
||||
|
||||
namespace ui {
|
||||
|
||||
void text_prompt(NavigationView& nav, std::string& str, const size_t max_length, const std::function<void(std::string&)> on_done) {
|
||||
void text_prompt(
|
||||
NavigationView& nav,
|
||||
std::string& str,
|
||||
size_t max_length,
|
||||
std::function<void(std::string&)> on_done
|
||||
) {
|
||||
text_prompt(nav, str, str.length(), max_length, on_done);
|
||||
}
|
||||
|
||||
void text_prompt(
|
||||
NavigationView& nav,
|
||||
std::string& str,
|
||||
uint32_t cursor_pos,
|
||||
size_t max_length,
|
||||
std::function<void(std::string&)> on_done
|
||||
) {
|
||||
//if (persistent_memory::ui_config_textentry() == 0) {
|
||||
auto te_view = nav.push<AlphanumView>(str, max_length);
|
||||
te_view->set_cursor(cursor_pos);
|
||||
te_view->on_changed = [on_done](std::string& value) {
|
||||
if (on_done)
|
||||
on_done(value);
|
||||
@ -54,7 +70,7 @@ TextField::TextField(
|
||||
uint32_t length
|
||||
) : Widget{ { position, { 8 * static_cast<int>(length), 16 } } },
|
||||
text_{ str },
|
||||
max_length_{ std::max<size_t>(max_length, 1) },
|
||||
max_length_{ std::max<size_t>(max_length, str.length()) },
|
||||
char_count_{ std::max<uint32_t>(length, 1) },
|
||||
cursor_pos_{ text_.length() },
|
||||
insert_mode_{ true }
|
||||
@ -66,36 +82,11 @@ const std::string& TextField::value() const {
|
||||
return text_;
|
||||
}
|
||||
|
||||
void TextField::set(const std::string& str) {
|
||||
// Assume that setting the string implies we want the whole thing.
|
||||
max_length_ = std::max(max_length_, str.length());
|
||||
|
||||
text_ = str;
|
||||
cursor_pos_ = str.length();
|
||||
set_cursor(str.length());
|
||||
}
|
||||
|
||||
void TextField::set_cursor(uint32_t pos) {
|
||||
cursor_pos_ = std::min<size_t>(pos, text_.length());
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
void TextField::set_max_length(size_t max_length) {
|
||||
// Doesn't make sense, ignore.
|
||||
if (max_length == 0)
|
||||
return;
|
||||
|
||||
if (max_length < text_.length()) {
|
||||
text_.erase(max_length - 1);
|
||||
text_.shrink_to_fit();
|
||||
} else {
|
||||
text_.reserve(max_length);
|
||||
}
|
||||
|
||||
max_length_ = max_length;
|
||||
set_cursor(cursor_pos_);
|
||||
}
|
||||
|
||||
void TextField::set_insert_mode() {
|
||||
insert_mode_ = true;
|
||||
}
|
||||
@ -211,6 +202,10 @@ void TextEntryView::char_add(const char c) {
|
||||
text_input.char_add(c);
|
||||
}
|
||||
|
||||
void TextEntryView::set_cursor(uint32_t pos) {
|
||||
text_input.set_cursor(pos);
|
||||
}
|
||||
|
||||
void TextEntryView::focus() {
|
||||
text_input.focus();
|
||||
}
|
||||
@ -227,7 +222,6 @@ TextEntryView::TextEntryView(
|
||||
});
|
||||
|
||||
button_ok.on_select = [this, &str, &nav](Button&) {
|
||||
str.shrink_to_fit(); // NB: str is the TextField string.
|
||||
if (on_changed)
|
||||
on_changed(str);
|
||||
nav.pop();
|
||||
|
@ -53,9 +53,7 @@ public:
|
||||
|
||||
const std::string& value() const;
|
||||
|
||||
void set(const std::string& str);
|
||||
void set_cursor(uint32_t pos);
|
||||
void set_max_length(size_t max_length);
|
||||
void set_insert_mode();
|
||||
void set_overwrite_mode();
|
||||
|
||||
@ -82,6 +80,8 @@ public:
|
||||
|
||||
void focus() override;
|
||||
std::string title() const override { return "Text entry"; };
|
||||
|
||||
void set_cursor(uint32_t pos);
|
||||
|
||||
protected:
|
||||
TextEntryView(NavigationView& nav, std::string& str, size_t max_length);
|
||||
@ -101,7 +101,22 @@ protected:
|
||||
};
|
||||
};
|
||||
|
||||
void text_prompt(NavigationView& nav, std::string& str, size_t max_length, const std::function<void(std::string&)> on_done = nullptr);
|
||||
// Show the TextEntry view to receive keyboard input.
|
||||
// NB: This function returns immediately. 'str' is taken
|
||||
// by reference and its lifetime must be ensured by the
|
||||
// caller until the TextEntry view is dismissed.
|
||||
void text_prompt(
|
||||
NavigationView& nav,
|
||||
std::string& str,
|
||||
size_t max_length,
|
||||
std::function<void(std::string&)> on_done = nullptr);
|
||||
|
||||
void text_prompt(
|
||||
NavigationView& nav,
|
||||
std::string& str,
|
||||
uint32_t cursor_pos,
|
||||
size_t max_length,
|
||||
std::function<void(std::string&)> on_done = nullptr);
|
||||
|
||||
} /* namespace ui */
|
||||
|
||||
|
@ -76,6 +76,15 @@ namespace ui
|
||||
{
|
||||
return reinterpret_cast<T *>(push_view(std::unique_ptr<View>(new T(*this, std::forward<Args>(args)...))));
|
||||
}
|
||||
|
||||
// Pushes a new view under the current on the stack so the current view returns into this new one.
|
||||
template <class T, class... Args>
|
||||
void push_under_current(Args &&...args)
|
||||
{
|
||||
auto new_view = std::unique_ptr<View>(new T(*this, std::forward<Args>(args)...));
|
||||
view_stack.insert(view_stack.end() - 1, std::move(new_view));
|
||||
}
|
||||
|
||||
template <class T, class... Args>
|
||||
T *replace(Args &&...args)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user