mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2024-12-13 11:44:31 +00:00
Remove expensive path.string() calls, UI changes
This commit is contained in:
parent
06643df6a5
commit
bf4ed416bd
@ -20,6 +20,7 @@
|
|||||||
* Boston, MA 02110-1301, USA.
|
* Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include "ui_fileman.hpp"
|
#include "ui_fileman.hpp"
|
||||||
#include "string_format.hpp"
|
#include "string_format.hpp"
|
||||||
#include "portapack.hpp"
|
#include "portapack.hpp"
|
||||||
@ -27,65 +28,92 @@
|
|||||||
|
|
||||||
using namespace portapack;
|
using namespace portapack;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
using namespace ui;
|
||||||
|
|
||||||
|
bool is_hidden_file(const std::filesystem::path& path) {
|
||||||
|
return !path.empty() && path.native()[0] == u'.';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gets a truncated name from a path for display.
|
||||||
|
std::string truncate(const std::filesystem::path& path, size_t max_length = 25) {
|
||||||
|
auto name = path.string();
|
||||||
|
return name.length() <= max_length ? name : name.substr(0, max_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case insensitive path equality on underlying "native" string.
|
||||||
|
bool iequal(
|
||||||
|
const std::filesystem::path& lhs,
|
||||||
|
const std::filesystem::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 (rhs.is_directory)
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return lhs.path < rhs.path;
|
||||||
|
});
|
||||||
|
|
||||||
|
entries.insert(it, std::move(entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
namespace ui {
|
namespace ui {
|
||||||
|
|
||||||
void FileManBaseView::load_directory_contents(const std::filesystem::path& dir_path) {
|
void FileManBaseView::load_directory_contents(const std::filesystem::path& dir_path) {
|
||||||
current_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();
|
entry_list.clear();
|
||||||
|
auto filtering = !extension_filter.empty();
|
||||||
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 });
|
|
||||||
|
|
||||||
|
text_current.set(dir_path.empty() ? "(sd root)" : truncate(dir_path));
|
||||||
|
|
||||||
for (const auto& entry : std::filesystem::directory_iterator(dir_path, u"*")) {
|
for (const auto& entry : std::filesystem::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 (std::filesystem::is_regular_file(entry.status())) {
|
||||||
if (entry.path().string().length() && entry.path().filename().string()[0] != '.') {
|
if (!filtering || iequal(entry.path().extension(), extension_filter))
|
||||||
if (std::filesystem::is_regular_file(entry.status())) {
|
insert_sorted(entry_list, { entry.path(), (uint32_t)entry.size(), false });
|
||||||
bool matched = true;
|
} else if (std::filesystem::is_directory(entry.status())) {
|
||||||
if (filtering) {
|
insert_sorted(entry_list, { entry.path(), 0, true });
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add "parent" directory if not at the root.
|
||||||
|
if (!dir_path.empty())
|
||||||
|
entry_list.insert(entry_list.begin(), { u"..", 0, true });
|
||||||
}
|
}
|
||||||
|
|
||||||
std::filesystem::path FileManBaseView::get_selected_path() {
|
std::filesystem::path FileManBaseView::get_selected_full_path() const {
|
||||||
auto selected_path_str = current_path.string();
|
if (get_selected_entry().path == std::filesystem::path(u".."))
|
||||||
auto entry_path = entry_list[menu_view.highlighted_index()].entry_path.string();
|
return current_path.parent_path();
|
||||||
|
|
||||||
if (entry_path == "..") {
|
return current_path / get_selected_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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::filesystem::path FileManBaseView::get_parent_dir() {
|
const fileman_entry& FileManBaseView::get_selected_entry() const {
|
||||||
auto current_path_str = current_path.string();
|
return entry_list[menu_view.highlighted_index()];
|
||||||
return current_path.string().substr(0, current_path_str.find_last_of('/'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FileManBaseView::FileManBaseView(
|
FileManBaseView::FileManBaseView(
|
||||||
@ -105,20 +133,21 @@ FileManBaseView::FileManBaseView(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!sdcIsCardInserted(&SDCD1)) {
|
if (!sdcIsCardInserted(&SDCD1)) {
|
||||||
empty_root=true;
|
empty_root = true;
|
||||||
text_current.set("NO SD CARD!");
|
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 {
|
} else {
|
||||||
load_directory_contents(current_path);
|
menu_view.on_left = [&nav, this]() {
|
||||||
if (!entry_list.size())
|
load_directory_contents(current_path.parent_path());
|
||||||
{
|
refresh_list();
|
||||||
empty_root = true;
|
};
|
||||||
text_current.set("EMPTY SD CARD!");
|
|
||||||
} else {
|
|
||||||
menu_view.on_left = [&nav, this]() {
|
|
||||||
load_directory_contents(get_parent_dir());
|
|
||||||
refresh_list();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,66 +160,67 @@ void FileManBaseView::focus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FileManBaseView::refresh_list() {
|
void FileManBaseView::refresh_list() {
|
||||||
|
// TODO: stash previous selected so scroll isn't reset.
|
||||||
if (on_refresh_widgets)
|
if (on_refresh_widgets)
|
||||||
on_refresh_widgets(false);
|
on_refresh_widgets(false);
|
||||||
|
|
||||||
menu_view.clear();
|
menu_view.clear();
|
||||||
|
|
||||||
for (size_t n = 0; n < entry_list.size(); n++) {
|
for (const auto& entry : entry_list) {
|
||||||
auto entry = &entry_list[n];
|
auto entry_name = truncate(entry.path, 20);
|
||||||
auto entry_name = entry->entry_path.filename().string().substr(0, 20);
|
|
||||||
|
if (entry.is_directory) {
|
||||||
if (entry->is_directory) {
|
|
||||||
|
|
||||||
menu_view.add_item({
|
menu_view.add_item({
|
||||||
entry_name,
|
entry_name,
|
||||||
ui::Color::yellow(),
|
ui::Color::yellow(),
|
||||||
&bitmap_icon_dir,
|
&bitmap_icon_dir,
|
||||||
[this](){
|
[this]() {
|
||||||
if (on_select_entry)
|
if (on_select_entry)
|
||||||
on_select_entry();
|
on_select_entry();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
auto file_size = entry.size;
|
||||||
auto file_size = entry->size;
|
|
||||||
size_t suffix_index = 0;
|
size_t suffix_index = 0;
|
||||||
|
|
||||||
while (file_size >= 1024) {
|
while (file_size >= 1024) {
|
||||||
file_size /= 1024;
|
file_size /= 1024;
|
||||||
suffix_index++;
|
suffix_index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (suffix_index > 4)
|
if (suffix_index > 4)
|
||||||
suffix_index = 4;
|
suffix_index = 4;
|
||||||
|
|
||||||
std::string size_str = to_string_dec_uint(file_size) + suffix[suffix_index];
|
std::string size_str = to_string_dec_uint(file_size) + suffix[suffix_index];
|
||||||
|
const auto& assoc = get_assoc(entry.path.extension());
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
menu_view.add_item({
|
menu_view.add_item({
|
||||||
entry_name + std::string(21 - entry_name.length(), ' ') + size_str,
|
entry_name + std::string(21 - entry_name.length(), ' ') + size_str,
|
||||||
file_types[c].color,
|
assoc.color,
|
||||||
file_types[c].icon,
|
assoc.icon,
|
||||||
[this](){
|
[this]() {
|
||||||
if (on_select_entry)
|
if (on_select_entry)
|
||||||
on_select_entry();
|
on_select_entry();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
menu_view.set_highlighted(0); // Refresh
|
menu_view.set_highlighted(0); // Refresh
|
||||||
|
}
|
||||||
|
|
||||||
|
const FileManBaseView::file_assoc_t& FileManBaseView::get_assoc(
|
||||||
|
const std::filesystem::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() {
|
/*void FileSaveView::on_save_name() {
|
||||||
@ -215,8 +245,7 @@ FileSaveView::FileSaveView(
|
|||||||
};
|
};
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
void FileLoadView::refresh_widgets(const bool v) {
|
void FileLoadView::refresh_widgets(const bool) {
|
||||||
(void)v; //avoid unused warning
|
|
||||||
set_dirty();
|
set_dirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,37 +268,39 @@ FileLoadView::FileLoadView(
|
|||||||
refresh_list();
|
refresh_list();
|
||||||
|
|
||||||
on_select_entry = [&nav, this]() {
|
on_select_entry = [&nav, this]() {
|
||||||
if (entry_list[menu_view.highlighted_index()].is_directory) {
|
if (get_selected_entry().is_directory) {
|
||||||
load_directory_contents(get_selected_path());
|
load_directory_contents(get_selected_full_path());
|
||||||
refresh_list();
|
refresh_list();
|
||||||
} else {
|
} else {
|
||||||
nav_.pop();
|
nav_.pop();
|
||||||
if (on_changed)
|
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) {
|
void FileManagerView::on_rename(NavigationView& nav) {
|
||||||
|
auto& entry = get_selected_entry();
|
||||||
|
|
||||||
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
||||||
std::string destination_path = current_path.string();
|
std::string destination_path = current_path.string();
|
||||||
if (destination_path.back() != '/')
|
if (destination_path.back() != '/')
|
||||||
destination_path += '/';
|
destination_path += '/';
|
||||||
destination_path = destination_path + buffer;
|
destination_path = destination_path + buffer;
|
||||||
rename_file(get_selected_path(), destination_path);
|
rename_file(get_selected_full_path(), destination_path);
|
||||||
load_directory_contents(current_path);
|
load_directory_contents(current_path);
|
||||||
refresh_list();
|
refresh_list();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileManagerView::on_refactor(NavigationView& nav) {
|
/*void FileManagerView::on_refactor(NavigationView& nav) {
|
||||||
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
|
||||||
|
|
||||||
std::string destination_path = current_path.string();
|
std::string destination_path = current_path.string();
|
||||||
if (destination_path.back() != '/')//if the path is not ended with '/', add '/'
|
if (destination_path.back() != '/')//if the path is not ended with '/', add '/'
|
||||||
destination_path += '/';
|
destination_path += '/';
|
||||||
|
|
||||||
auto selected_path = get_selected_path();
|
auto selected_path = get_selected_full_path();
|
||||||
auto extension = selected_path.extension().string();
|
auto extension = selected_path.extension().string();
|
||||||
|
|
||||||
if(extension.empty()){// Is Dir
|
if(extension.empty()){// Is Dir
|
||||||
@ -279,7 +310,7 @@ void FileManagerView::on_refactor(NavigationView& nav) {
|
|||||||
destination_path = destination_path + buffer + extension_buffer;
|
destination_path = destination_path + buffer + extension_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
rename_file(get_selected_path(), destination_path); //rename the selected file
|
rename_file(get_selected_full_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
|
if (!extension.empty() && selected_path.string().back() != '/' && extension.substr(1) == "C16") { //substr(1) is for ignore the dot
|
||||||
// Rename its partner ( C16 <-> TXT ) file.
|
// Rename its partner ( C16 <-> TXT ) file.
|
||||||
@ -298,19 +329,19 @@ void FileManagerView::on_refactor(NavigationView& nav) {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}*/
|
||||||
|
|
||||||
void FileManagerView::on_delete() {
|
void FileManagerView::on_delete() {
|
||||||
delete_file(get_selected_path());
|
delete_file(get_selected_full_path());
|
||||||
load_directory_contents(current_path);
|
load_directory_contents(current_path);
|
||||||
refresh_list();
|
refresh_list();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileManagerView::refresh_widgets(const bool v) {
|
void FileManagerView::refresh_widgets(const bool v) {
|
||||||
button_rename.hidden(v);
|
button_rename.hidden(v);
|
||||||
button_new_dir.hidden(v);
|
//button_refactor.hidden(v);
|
||||||
button_refactor.hidden(v);
|
|
||||||
button_delete.hidden(v);
|
button_delete.hidden(v);
|
||||||
|
button_new_dir.hidden(v);
|
||||||
set_dirty();
|
set_dirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,42 +363,33 @@ FileManagerView::FileManagerView(
|
|||||||
&labels,
|
&labels,
|
||||||
&text_date,
|
&text_date,
|
||||||
&button_rename,
|
&button_rename,
|
||||||
&button_refactor,
|
//&button_copy,
|
||||||
|
//&button_move,
|
||||||
|
&button_delete,
|
||||||
|
//&button_new_file,
|
||||||
&button_new_dir,
|
&button_new_dir,
|
||||||
&button_delete
|
|
||||||
});
|
});
|
||||||
|
|
||||||
menu_view.on_highlight = [this]() {
|
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();
|
refresh_list();
|
||||||
|
|
||||||
on_select_entry = [this]() {
|
on_select_entry = [this]() {
|
||||||
if (entry_list[menu_view.highlighted_index()].is_directory) {
|
if (get_selected_entry().is_directory) {
|
||||||
load_directory_contents(get_selected_path());
|
load_directory_contents(get_selected_full_path());
|
||||||
refresh_list();
|
refresh_list();
|
||||||
} else
|
} else
|
||||||
button_rename.focus();
|
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&) {
|
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);
|
on_rename(nav);
|
||||||
};
|
};
|
||||||
|
|
||||||
button_refactor.on_select = [this, &nav](Button&) {
|
/*button_refactor.on_select = [this, &nav](Button&) {
|
||||||
name_buffer = entry_list[menu_view.highlighted_index()].entry_path.filename().string().substr(0, max_filename_length);
|
name_buffer = entry_list[menu_view.highlighted_index()].path.filename().string().substr(0, max_filename_length);
|
||||||
size_t pos = name_buffer.find_last_of(".");
|
size_t pos = name_buffer.find_last_of(".");
|
||||||
|
|
||||||
if (pos != std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
@ -376,17 +398,27 @@ FileManagerView::FileManagerView(
|
|||||||
}
|
}
|
||||||
|
|
||||||
on_refactor(nav);
|
on_refactor(nav);
|
||||||
};
|
};*/
|
||||||
|
|
||||||
button_delete.on_select = [this, &nav](Button&) {
|
button_delete.on_select = [this, &nav](Button&) {
|
||||||
// Use display_modal ?
|
auto name = get_selected_entry().path.filename().string();
|
||||||
nav.push<ModalMessageView>("Delete", "Delete " + entry_list[menu_view.highlighted_index()].entry_path.filename().string() + "\nAre you sure?", YESNO,
|
nav.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO,
|
||||||
[this](bool choice) {
|
[this](bool choice) {
|
||||||
if (choice)
|
if (choice)
|
||||||
on_delete();
|
on_delete();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 / buffer);
|
||||||
|
load_directory_contents(current_path);
|
||||||
|
refresh_list();
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
namespace ui {
|
namespace ui {
|
||||||
|
|
||||||
struct fileman_entry {
|
struct fileman_entry {
|
||||||
std::filesystem::path entry_path { };
|
std::filesystem::path path { };
|
||||||
uint32_t size { };
|
uint32_t size { };
|
||||||
bool is_directory { };
|
bool is_directory { };
|
||||||
};
|
};
|
||||||
@ -46,47 +46,49 @@ public:
|
|||||||
void focus() override;
|
void focus() override;
|
||||||
|
|
||||||
void load_directory_contents(const std::filesystem::path& dir_path);
|
void load_directory_contents(const std::filesystem::path& dir_path);
|
||||||
std::filesystem::path get_selected_path();
|
std::filesystem::path get_selected_full_path() const;
|
||||||
|
const fileman_entry& get_selected_entry() const;
|
||||||
|
|
||||||
std::string title() const override { return "Fileman"; };
|
std::string title() const override { return "Fileman"; };
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
NavigationView& nav_;
|
static constexpr size_t max_filename_length = 64 - 2; // Necessary?
|
||||||
|
|
||||||
static constexpr size_t max_filename_length = 30 - 2;
|
|
||||||
|
|
||||||
const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
|
|
||||||
|
|
||||||
struct file_assoc_t {
|
struct file_assoc_t {
|
||||||
std::string extension;
|
std::filesystem::path extension;
|
||||||
const Bitmap* icon;
|
const Bitmap* icon;
|
||||||
ui::Color color;
|
ui::Color color;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
|
||||||
|
|
||||||
const std::vector<file_assoc_t> file_types = {
|
const std::vector<file_assoc_t> file_types = {
|
||||||
{ ".TXT", &bitmap_icon_file_text, ui::Color::white() },
|
{ u".TXT", &bitmap_icon_file_text, ui::Color::white() },
|
||||||
{ ".PNG", &bitmap_icon_file_image, ui::Color::green() },
|
{ u".PNG", &bitmap_icon_file_image, ui::Color::green() },
|
||||||
{ ".BMP", &bitmap_icon_file_image, ui::Color::green() },
|
{ u".BMP", &bitmap_icon_file_image, ui::Color::green() },
|
||||||
{ ".C8", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
{ u".C8", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
||||||
{ ".C16", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
{ u".C16", &bitmap_icon_file_iq, ui::Color::dark_cyan() },
|
||||||
{ ".WAV", &bitmap_icon_file_wav, ui::Color::dark_magenta() },
|
{ u".WAV", &bitmap_icon_file_wav, ui::Color::dark_magenta() },
|
||||||
{ "", &bitmap_icon_file, ui::Color::light_grey() }
|
{ u"", &bitmap_icon_file, ui::Color::light_grey() } // NB: Must be last.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void refresh_list();
|
||||||
|
const file_assoc_t& get_assoc(const std::filesystem::path& ext) const;
|
||||||
|
|
||||||
|
NavigationView& nav_;
|
||||||
|
|
||||||
bool empty_root { false };
|
bool empty_root { false };
|
||||||
std::function<void(void)> on_select_entry { nullptr };
|
std::function<void(void)> on_select_entry { nullptr };
|
||||||
std::function<void(bool)> on_refresh_widgets { nullptr };
|
std::function<void(bool)> on_refresh_widgets { nullptr };
|
||||||
|
|
||||||
std::vector<fileman_entry> entry_list { };
|
std::vector<fileman_entry> entry_list { };
|
||||||
std::filesystem::path current_path { u"" };
|
std::filesystem::path current_path { u"" };
|
||||||
std::string extension_filter { "" };
|
std::filesystem::path extension_filter { u"" };
|
||||||
|
|
||||||
void change_category(int32_t category_id);
|
|
||||||
std::filesystem::path get_parent_dir();
|
|
||||||
void refresh_list();
|
|
||||||
|
|
||||||
Labels labels {
|
Labels labels {
|
||||||
{ { 0, 0 }, "Path:", Color::light_grey() }
|
{ { 0, 0 }, "Path:", Color::light_grey() }
|
||||||
};
|
};
|
||||||
|
|
||||||
Text text_current {
|
Text text_current {
|
||||||
{ 6 * 8, 0 * 8, 24 * 8, 16 },
|
{ 6 * 8, 0 * 8, 24 * 8, 16 },
|
||||||
"",
|
"",
|
||||||
@ -147,7 +149,7 @@ private:
|
|||||||
|
|
||||||
void refresh_widgets(const bool v);
|
void refresh_widgets(const bool v);
|
||||||
void on_rename(NavigationView& nav);
|
void on_rename(NavigationView& nav);
|
||||||
void on_refactor(NavigationView& nav);
|
//void on_refactor(NavigationView& nav);
|
||||||
void on_delete();
|
void on_delete();
|
||||||
|
|
||||||
Labels labels {
|
Labels labels {
|
||||||
@ -164,9 +166,14 @@ private:
|
|||||||
"Rename"
|
"Rename"
|
||||||
};
|
};
|
||||||
|
|
||||||
Button button_refactor{
|
Button button_copy {
|
||||||
{ 10 * 8, 29 * 8, 10 * 8, 32 },
|
{ 10 * 8, 29 * 8, 10 * 8, 32 },
|
||||||
"Refactor"
|
"Copy"
|
||||||
|
};
|
||||||
|
|
||||||
|
Button button_move {
|
||||||
|
{ 10 * 8, 29 * 8, 10 * 8, 32 },
|
||||||
|
"Move"
|
||||||
};
|
};
|
||||||
|
|
||||||
Button button_delete {
|
Button button_delete {
|
||||||
@ -174,9 +181,14 @@ private:
|
|||||||
"Delete"
|
"Delete"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*Button button_new_file {
|
||||||
|
{ 0 * 8, 34 * 8, 14 * 8, 32 },
|
||||||
|
"New File"
|
||||||
|
};*/
|
||||||
|
|
||||||
Button button_new_dir {
|
Button button_new_dir {
|
||||||
{ 0 * 8, 34 * 8, 14 * 8, 32 },
|
{ 0 * 8, 34 * 8, 14 * 8, 32 },
|
||||||
"New dir"
|
"New Dir"
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -250,6 +250,16 @@ std::string filesystem_error::what() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path path::parent_path() const {
|
||||||
|
const auto t = filename().native();
|
||||||
|
const auto index = t.find_last_of(preferred_separator);
|
||||||
|
if( index == t.npos ) {
|
||||||
|
return *this;
|
||||||
|
} else {
|
||||||
|
return t.substr(0, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
path path::extension() const {
|
path path::extension() const {
|
||||||
const auto t = filename().native();
|
const auto t = filename().native();
|
||||||
const auto index = t.find_last_of(u'.');
|
const auto index = t.find_last_of(u'.');
|
||||||
@ -296,6 +306,10 @@ path& path::replace_extension(const path& replacement) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const path& lhs, const path& rhs) {
|
||||||
|
return lhs.native() == rhs.native();
|
||||||
|
}
|
||||||
|
|
||||||
bool operator<(const path& lhs, const path& rhs) {
|
bool operator<(const path& lhs, const path& rhs) {
|
||||||
return lhs.native() < rhs.native();
|
return lhs.native() < rhs.native();
|
||||||
}
|
}
|
||||||
@ -304,6 +318,12 @@ bool operator>(const path& lhs, const path& rhs) {
|
|||||||
return lhs.native() > rhs.native();
|
return lhs.native() > rhs.native();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path operator/(const path& lhs, const path& rhs) {
|
||||||
|
path result = lhs;
|
||||||
|
result /= rhs;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
directory_iterator::directory_iterator(
|
directory_iterator::directory_iterator(
|
||||||
std::filesystem::path path,
|
std::filesystem::path path,
|
||||||
std::filesystem::path wild
|
std::filesystem::path wild
|
||||||
|
@ -123,6 +123,7 @@ struct path {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path parent_path() const;
|
||||||
path extension() const;
|
path extension() const;
|
||||||
path filename() const;
|
path filename() const;
|
||||||
path stem() const;
|
path stem() const;
|
||||||
@ -151,14 +152,23 @@ struct path {
|
|||||||
return *this;
|
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());
|
path& replace_extension(const path& replacement = path());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string_type _s;
|
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);
|
||||||
bool operator>(const path& lhs, const path& rhs);
|
bool operator>(const path& lhs, const path& rhs);
|
||||||
|
path operator/(const path& lhs, const path& rhs);
|
||||||
|
|
||||||
using file_status = BYTE;
|
using file_status = BYTE;
|
||||||
|
|
||||||
|
@ -238,7 +238,7 @@ bool MenuView::set_highlighted(int32_t new_value) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t MenuView::highlighted_index() {
|
uint32_t MenuView::highlighted_index() const {
|
||||||
return highlighted_item;
|
return highlighted_item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ public:
|
|||||||
MenuItemView* item_view(size_t index) const;
|
MenuItemView* item_view(size_t index) const;
|
||||||
|
|
||||||
bool set_highlighted(int32_t new_value);
|
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 set_parent_rect(const Rect new_parent_rect) override;
|
||||||
void on_focus() override;
|
void on_focus() override;
|
||||||
|
@ -29,9 +29,25 @@ using namespace portapack;
|
|||||||
|
|
||||||
namespace ui {
|
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,
|
||||||
|
const size_t max_length,
|
||||||
|
const 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,
|
||||||
|
const size_t max_length,
|
||||||
|
const std::function<void(std::string&)> on_done
|
||||||
|
) {
|
||||||
//if (persistent_memory::ui_config_textentry() == 0) {
|
//if (persistent_memory::ui_config_textentry() == 0) {
|
||||||
auto te_view = nav.push<AlphanumView>(str, max_length);
|
auto te_view = nav.push<AlphanumView>(str, max_length);
|
||||||
|
te_view->set_cursor(cursor_pos);
|
||||||
te_view->on_changed = [on_done](std::string& value) {
|
te_view->on_changed = [on_done](std::string& value) {
|
||||||
if (on_done)
|
if (on_done)
|
||||||
on_done(value);
|
on_done(value);
|
||||||
@ -211,6 +227,10 @@ void TextEntryView::char_add(const char c) {
|
|||||||
text_input.char_add(c);
|
text_input.char_add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextEntryView::set_cursor(uint32_t pos) {
|
||||||
|
text_input.set_cursor(pos);
|
||||||
|
}
|
||||||
|
|
||||||
void TextEntryView::focus() {
|
void TextEntryView::focus() {
|
||||||
text_input.focus();
|
text_input.focus();
|
||||||
}
|
}
|
||||||
|
@ -82,6 +82,8 @@ public:
|
|||||||
|
|
||||||
void focus() override;
|
void focus() override;
|
||||||
std::string title() const override { return "Text entry"; };
|
std::string title() const override { return "Text entry"; };
|
||||||
|
|
||||||
|
void set_cursor(uint32_t pos);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TextEntryView(NavigationView& nav, std::string& str, size_t max_length);
|
TextEntryView(NavigationView& nav, std::string& str, size_t max_length);
|
||||||
@ -101,7 +103,18 @@ protected:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
void text_prompt(NavigationView& nav, std::string& str, size_t max_length, const std::function<void(std::string&)> on_done = nullptr);
|
void text_prompt(
|
||||||
|
NavigationView& nav,
|
||||||
|
std::string& str,
|
||||||
|
size_t max_length,
|
||||||
|
const std::function<void(std::string&)> on_done = nullptr);
|
||||||
|
|
||||||
|
void text_prompt(
|
||||||
|
NavigationView& nav,
|
||||||
|
std::string& str,
|
||||||
|
uint32_t cursor_pos,
|
||||||
|
size_t max_length,
|
||||||
|
const std::function<void(std::string&)> on_done = nullptr);
|
||||||
|
|
||||||
} /* namespace ui */
|
} /* namespace ui */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user