Modal dialog for partner file action, fix lifetime

This commit is contained in:
Kyle Reed 2023-05-02 09:38:08 -07:00
parent 2cba96ff36
commit 11f4edc892
10 changed files with 138 additions and 139 deletions

View File

@ -206,7 +206,7 @@ void SoundBoardView::refresh_list() {
file_list[n].string().substr(0, 30), file_list[n].string().substr(0, 30),
ui::Color::white(), ui::Color::white(),
nullptr, nullptr,
[this](){ [this](KeyEvent){
on_select_entry(); on_select_entry();
} }
}); });

View File

@ -20,6 +20,11 @@
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
*/ */
/* BUGS:
* - OOM/paging menu items
* - Using UI with empty SD card
*/
#include <algorithm> #include <algorithm>
#include "ui_fileman.hpp" #include "ui_fileman.hpp"
#include "string_format.hpp" #include "string_format.hpp"
@ -27,21 +32,22 @@
#include "event_m0.hpp" #include "event_m0.hpp"
using namespace portapack; using namespace portapack;
namespace fs = std::filesystem;
namespace { namespace {
using namespace ui; using namespace ui;
bool is_hidden_file(const std::filesystem::path& path) { bool is_hidden_file(const fs::path& path) {
return !path.empty() && path.native()[0] == u'.'; return !path.empty() && path.native()[0] == u'.';
} }
// Gets a truncated name from a path for display. // Gets a truncated name from a path for display.
std::string truncate(const std::filesystem::path& path, size_t max_length = 25) { std::string truncate(const fs::path& path, size_t max_length) {
auto name = path.string(); auto name = path.string();
return name.length() <= max_length ? name : name.substr(0, max_length); return name.length() <= max_length ? name : name.substr(0, max_length);
} }
// Gets a human readable size string. // Gets a human readable file size string.
std::string get_pretty_size(uint32_t file_size) { std::string get_pretty_size(uint32_t file_size) {
static const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" }; static const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
size_t suffix_index = 0; size_t suffix_index = 0;
@ -59,8 +65,8 @@ std::string get_pretty_size(uint32_t file_size) {
// Case insensitive path equality on underlying "native" string. // Case insensitive path equality on underlying "native" string.
bool iequal( bool iequal(
const std::filesystem::path& lhs, const fs::path& lhs,
const std::filesystem::path& rhs const fs::path& rhs
) { ) {
const auto& lhs_str = lhs.native(); const auto& lhs_str = lhs.native();
const auto& rhs_str = rhs.native(); const auto& rhs_str = rhs.native();
@ -93,9 +99,9 @@ void insert_sorted(std::vector<fileman_entry>& entries, fileman_entry&& entry) {
} }
// Returns the partner file path or an empty path if no partner is found. // Returns the partner file path or an empty path if no partner is found.
std::filesystem::path get_partner_file(std::filesystem::path path) { fs::path get_partner_file(fs::path path) {
const auto txt_path = std::filesystem::path{ u".TXT" }; const fs::path txt_path{ u".TXT" };
const auto c16_path = std::filesystem::path{ u".C16" }; const fs::path c16_path{ u".C16" };
auto ext = path.extension(); auto ext = path.extension();
if (iequal(ext, txt_path)) if (iequal(ext, txt_path))
@ -106,47 +112,56 @@ std::filesystem::path get_partner_file(std::filesystem::path path) {
return { }; return { };
path.replace_extension(ext); path.replace_extension(ext);
return file_exists(path) ? path : std::filesystem::path{ }; return file_exists(path) ? path : fs::path{ };
} }
// Modal prompt to update the partner file. // Modal prompt to update the partner file if it exists.
// Returns true if user wants to update the partner file too. // Runs continuation on_partner_action to update the partner file.
bool partner_file_prompt(NavigationView& nav, const std::filesystem::path& partner) { // Returns true is a partner is found, otherwise false.
bool result = 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()) if (partner.empty())
return result; return false;
nav.push<ModalMessageView>( nav.push_under_current<ModalMessageView>(
"Partner File Exists", "Partner File",
partner.filename().string() + "\nUpdate this file too?", partner.filename().string() + "\n" + action_name + " this file too?",
YESNO, YESNO,
[&result](bool choice) { result = choice; } [&nav, partner, on_partner_action](bool choice) {
if (on_partner_action)
on_partner_action(partner, choice);
}
); );
return result; return true;
} }
} }
namespace ui { 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; current_path = dir_path;
entry_list.clear(); entry_list.clear();
auto filtering = !extension_filter.empty(); auto filtering = !extension_filter.empty();
text_current.set(dir_path.empty() ? "(sd root)" : truncate(dir_path)); text_current.set(dir_path.empty() ? "(sd root)" : truncate(dir_path, 24));
for (const auto& entry : std::filesystem::directory_iterator(dir_path, u"*")) { for (const auto& entry : fs::directory_iterator(dir_path, u"*")) {
// Hide files starting with '.' (hidden / tmp). // Hide files starting with '.' (hidden / tmp).
if (is_hidden_file(entry.path())) if (is_hidden_file(entry.path()))
continue; continue;
if (std::filesystem::is_regular_file(entry.status())) { if (fs::is_regular_file(entry.status())) {
if (!filtering || iequal(entry.path().extension(), extension_filter)) if (!filtering || iequal(entry.path().extension(), extension_filter))
insert_sorted(entry_list, { entry.path(), (uint32_t)entry.size(), false }); insert_sorted(entry_list, { entry.path(), (uint32_t)entry.size(), false });
} else if (std::filesystem::is_directory(entry.status())) { } else if (fs::is_directory(entry.status())) {
insert_sorted(entry_list, { entry.path(), 0, true }); insert_sorted(entry_list, { entry.path(), 0, true });
} }
} }
@ -156,7 +171,7 @@ void FileManBaseView::load_directory_contents(const std::filesystem::path& dir_p
entry_list.insert(entry_list.begin(), { parent_dir_path, 0, true }); entry_list.insert(entry_list.begin(), { parent_dir_path, 0, true });
} }
std::filesystem::path FileManBaseView::get_selected_full_path() const { fs::path FileManBaseView::get_selected_full_path() const {
if (get_selected_entry().path == parent_dir_path) if (get_selected_entry().path == parent_dir_path)
return current_path.parent_path(); return current_path.parent_path();
@ -195,9 +210,9 @@ FileManBaseView::FileManBaseView(
empty_root = true; empty_root = true;
text_current.set("EMPTY SD CARD!"); text_current.set("EMPTY SD CARD!");
} else { } else {
menu_view.on_left = [&nav, this]() { menu_view.on_left = [this]() {
load_directory_contents(current_path.parent_path()); current_path = current_path.parent_path();
refresh_list(); reload_current();
}; };
} }
} }
@ -224,9 +239,9 @@ void FileManBaseView::refresh_list() {
entry_name, entry_name,
ui::Color::yellow(), ui::Color::yellow(),
&bitmap_icon_dir, &bitmap_icon_dir,
[this]() { [this](KeyEvent key) {
if (on_select_entry) if (on_select_entry)
on_select_entry(); on_select_entry(key);
} }
}); });
@ -238,9 +253,9 @@ void FileManBaseView::refresh_list() {
entry_name + std::string(21 - entry_name.length(), ' ') + size_str, entry_name + std::string(21 - entry_name.length(), ' ') + size_str,
assoc.color, assoc.color,
assoc.icon, assoc.icon,
[this]() { [this](KeyEvent key) {
if (on_select_entry) if (on_select_entry)
on_select_entry(); on_select_entry(key);
} }
}); });
} }
@ -249,8 +264,13 @@ void FileManBaseView::refresh_list() {
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 FileManBaseView::file_assoc_t& FileManBaseView::get_assoc(
const std::filesystem::path& ext) const const fs::path& ext) const
{ {
size_t index = 0; size_t index = 0;
@ -306,10 +326,10 @@ FileLoadView::FileLoadView(
refresh_list(); refresh_list();
on_select_entry = [this]() { on_select_entry = [this](KeyEvent) {
if (get_selected_entry().is_directory) { if (get_selected_entry().is_directory) {
load_directory_contents(get_selected_full_path()); current_path /= get_selected_entry().path;
refresh_list(); reload_current();
} else { } else {
nav_.pop(); nav_.pop();
if (on_changed) if (on_changed)
@ -319,65 +339,75 @@ FileLoadView::FileLoadView(
} }
void FileManagerView::on_rename() { void FileManagerView::on_rename() {
// Don't allow rename of ".." auto& entry = get_selected_entry();
if (get_selected_entry().path == parent_dir_path)
// Don't rename ".."
if (entry.path == parent_dir_path)
return; return;
auto& entry = get_selected_entry(); name_buffer = entry.path.filename().string();
auto name = entry.path.filename().string();
auto orig_ext = entry.path.extension();
uint32_t cursor_pos = (uint32_t)name.length(); uint32_t cursor_pos = (uint32_t)name_buffer.length();
if (auto pos = name.find_last_of("."); pos != name.npos) if (auto pos = name_buffer.find_last_of("."); pos != name_buffer.npos)
cursor_pos = pos; cursor_pos = pos;
text_prompt(nav_, name, cursor_pos, max_filename_length, text_prompt(nav_, name_buffer, cursor_pos, max_filename_length,
[this, &entry, &orig_ext](std::string& renamed) { [this, &entry](std::string& renamed) {
auto renamed_path = std::filesystem::path{ renamed }; auto renamed_path = fs::path{ renamed };
bool has_partner = false;
rename_file(get_selected_full_path(), current_path / renamed_path); rename_file(get_selected_full_path(), current_path / renamed_path);
if (iequal(renamed_path.extension(), orig_ext)) { if (iequal(renamed_path.extension(), entry.path.extension())) {
auto partner = get_partner_file(current_path / entry.path); has_partner = partner_file_prompt(nav_, entry.path, "Rename",
if (partner_file_prompt(nav_, partner)) { [this, renamed_path](const fs::path& partner, bool should_rename) mutable {
auto new_name = renamed_path.replace_extension(partner.extension()); if (should_rename) {
rename_file(partner, current_path / new_name); auto new_name = renamed_path.replace_extension(partner.extension());
} rename_file(current_path / partner, current_path / new_name);
}
reload_current();
}
);
} }
load_directory_contents(current_path); if (!has_partner)
refresh_list(); reload_current();
}); });
} }
void FileManagerView::on_delete() { void FileManagerView::on_delete() {
// Don't allow delete of ".." auto& entry = get_selected_entry();
if (get_selected_entry().path == parent_dir_path)
// Don't delete ".."
if (entry.path == parent_dir_path)
return; return;
auto& entry = get_selected_entry();
auto name = entry.path.filename().string(); auto name = entry.path.filename().string();
nav_.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO, nav_.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO,
[this, &entry](bool choice) { [this, &entry](bool choice) {
if (choice) { if (choice) {
delete_file(get_selected_full_path()); delete_file(get_selected_full_path());
auto partner = get_partner_file(current_path / entry.path); auto has_partner = partner_file_prompt(
if (partner_file_prompt(nav_, partner)) nav_, entry.path, "Delete",
delete_file(partner); [this](const fs::path& partner, bool should_delete) {
if (should_delete)
delete_file(current_path / partner);
reload_current();
}
);
load_directory_contents(current_path); if (!has_partner)
refresh_list(); reload_current();
} }
} }
); );
} }
void FileManagerView::on_new_dir() { void FileManagerView::on_new_dir() {
std::string name; name_buffer = "";
text_prompt(nav_, name, max_filename_length, [this](std::string& dir_name) { text_prompt(nav_, name_buffer, max_filename_length, [this](std::string& dir_name) {
make_new_directory(current_path / dir_name); make_new_directory(current_path / dir_name);
load_directory_contents(current_path); reload_current();
refresh_list();
}); });
} }
@ -389,7 +419,6 @@ void FileManagerView::refresh_widgets(const bool v) {
} }
FileManagerView::~FileManagerView() { FileManagerView::~FileManagerView() {
// Flush ?
} }
FileManagerView::FileManagerView( FileManagerView::FileManagerView(
@ -406,10 +435,7 @@ FileManagerView::FileManagerView(
&labels, &labels,
&text_date, &text_date,
&button_rename, &button_rename,
//&button_copy,
//&button_move,
&button_delete, &button_delete,
//&button_new_file,
&button_new_dir, &button_new_dir,
}); });
@ -419,12 +445,13 @@ FileManagerView::FileManagerView(
refresh_list(); refresh_list();
on_select_entry = [this]() { on_select_entry = [this](KeyEvent key) {
if (get_selected_entry().is_directory) { if (key == KeyEvent::Select && get_selected_entry().is_directory) {
load_directory_contents(get_selected_full_path()); load_directory_contents(get_selected_full_path());
refresh_list(); refresh_list();
} else } else {
button_rename.focus(); button_rename.focus();
}
}; };
button_rename.on_select = [this](Button&) { button_rename.on_select = [this](Button&) {

View File

@ -44,15 +44,10 @@ public:
); );
void focus() override; void focus() override;
void load_directory_contents(const std::filesystem::path& dir_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:
static constexpr size_t max_filename_length = 64; // Necessary? static constexpr size_t max_filename_length = 50;
struct file_assoc_t { struct file_assoc_t {
std::filesystem::path extension; std::filesystem::path extension;
@ -70,20 +65,27 @@ protected:
{ u"", &bitmap_icon_file, ui::Color::light_grey() } // NB: Must be last. { u"", &bitmap_icon_file, ui::Color::light_grey() } // NB: Must be last.
}; };
std::filesystem::path get_selected_full_path() const;
const fileman_entry& get_selected_entry() const;
void refresh_list(); 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; const file_assoc_t& get_assoc(const std::filesystem::path& ext) const;
NavigationView& nav_; NavigationView& nav_;
bool empty_root { false }; bool empty_root { false };
std::function<void(void)> on_select_entry { nullptr }; std::function<void(KeyEvent)> 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 { };
const std::filesystem::path parent_dir_path { u".." }; const std::filesystem::path parent_dir_path { u".." };
std::filesystem::path current_path { u"" }; std::filesystem::path current_path { u"" };
std::filesystem::path extension_filter { u"" }; std::filesystem::path extension_filter { u"" };
std::vector<fileman_entry> entry_list { };
Labels labels { Labels labels {
{ { 0, 0 }, "Path:", Color::light_grey() } { { 0, 0 }, "Path:", Color::light_grey() }
}; };
@ -143,8 +145,8 @@ public:
~FileManagerView(); ~FileManagerView();
private: private:
// Passed by ref to other views needing lifetime extension.
std::string name_buffer { }; std::string name_buffer { };
std::string extension_buffer { };
void refresh_widgets(const bool v); void refresh_widgets(const bool v);
void on_rename(); void on_rename();
@ -165,26 +167,11 @@ private:
"Rename" "Rename"
}; };
/*Button button_copy {
{ 10 * 8, 29 * 8, 10 * 8, 32 },
"Copy"
};
Button button_move {
{ 10 * 8, 29 * 8, 10 * 8, 32 },
"Move"
};*/
Button button_delete { Button button_delete {
{ 21 * 8, 29 * 8, 9 * 8, 32 }, { 21 * 8, 29 * 8, 9 * 8, 32 },
"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"

View File

@ -44,7 +44,7 @@ FlashUtilityView::FlashUtilityView(NavigationView& nav) : nav_ (nav) {
filename.string().substr(0, max_filename_length), filename.string().substr(0, max_filename_length),
ui::Color::red(), ui::Color::red(),
&bitmap_icon_temperature, &bitmap_icon_temperature,
[this, path]() { [this, path](KeyEvent) {
this->firmware_selected(path); this->firmware_selected(path);
} }
}); });

View File

@ -120,7 +120,7 @@ void FreqManBaseView::refresh_list() {
freqman_item_string(database[n], 30), freqman_item_string(database[n], 30),
ui::Color::white(), ui::Color::white(),
nullptr, nullptr,
[this](){ [this](KeyEvent){
if (on_select_frequency) if (on_select_frequency)
on_select_frequency(); on_select_frequency();
} }

View File

@ -262,7 +262,7 @@ bool MenuView::on_key(const KeyEvent key) {
case KeyEvent::Select: case KeyEvent::Select:
case KeyEvent::Right: case KeyEvent::Right:
if( menu_items[highlighted_item].on_select ) { if( menu_items[highlighted_item].on_select ) {
menu_items[highlighted_item].on_select(); menu_items[highlighted_item].on_select(key);
} }
return true; return true;

View File

@ -39,7 +39,7 @@ struct MenuItem {
std::string text; std::string text;
ui::Color color; ui::Color color;
const Bitmap* bitmap; const Bitmap* bitmap;
std::function<void(void)> on_select; std::function<void(KeyEvent)> on_select;
// TODO: Prevent default-constructed MenuItems. // TODO: Prevent default-constructed MenuItems.
// I managed to construct a menu with three extra, unspecified menu items // I managed to construct a menu with three extra, unspecified menu items

View File

@ -32,8 +32,8 @@ namespace ui {
void text_prompt( void text_prompt(
NavigationView& nav, NavigationView& nav,
std::string& str, std::string& str,
const size_t max_length, size_t max_length,
const std::function<void(std::string&)> on_done std::function<void(std::string&)> on_done
) { ) {
text_prompt(nav, str, str.length(), max_length, on_done); text_prompt(nav, str, str.length(), max_length, on_done);
} }
@ -42,8 +42,8 @@ void text_prompt(
NavigationView& nav, NavigationView& nav,
std::string& str, std::string& str,
uint32_t cursor_pos, uint32_t cursor_pos,
const size_t max_length, size_t max_length,
const std::function<void(std::string&)> on_done 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);
@ -70,7 +70,7 @@ TextField::TextField(
uint32_t length uint32_t length
) : Widget{ { position, { 8 * static_cast<int>(length), 16 } } }, ) : Widget{ { position, { 8 * static_cast<int>(length), 16 } } },
text_{ str }, 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) }, char_count_{ std::max<uint32_t>(length, 1) },
cursor_pos_{ text_.length() }, cursor_pos_{ text_.length() },
insert_mode_{ true } insert_mode_{ true }
@ -82,36 +82,11 @@ const std::string& TextField::value() const {
return text_; 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) { void TextField::set_cursor(uint32_t pos) {
cursor_pos_ = std::min<size_t>(pos, text_.length()); cursor_pos_ = std::min<size_t>(pos, text_.length());
set_dirty(); 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() { void TextField::set_insert_mode() {
insert_mode_ = true; insert_mode_ = true;
} }
@ -247,7 +222,6 @@ TextEntryView::TextEntryView(
}); });
button_ok.on_select = [this, &str, &nav](Button&) { button_ok.on_select = [this, &str, &nav](Button&) {
str.shrink_to_fit(); // NB: str is the TextField string.
if (on_changed) if (on_changed)
on_changed(str); on_changed(str);
nav.pop(); nav.pop();

View File

@ -53,9 +53,7 @@ public:
const std::string& value() const; const std::string& value() const;
void set(const std::string& str);
void set_cursor(uint32_t pos); void set_cursor(uint32_t pos);
void set_max_length(size_t max_length);
void set_insert_mode(); void set_insert_mode();
void set_overwrite_mode(); void set_overwrite_mode();
@ -103,18 +101,22 @@ protected:
}; };
}; };
// 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( void text_prompt(
NavigationView& nav, NavigationView& nav,
std::string& str, std::string& str,
size_t max_length, size_t max_length,
const std::function<void(std::string&)> on_done = nullptr); std::function<void(std::string&)> on_done = nullptr);
void text_prompt( void text_prompt(
NavigationView& nav, NavigationView& nav,
std::string& str, std::string& str,
uint32_t cursor_pos, uint32_t cursor_pos,
size_t max_length, size_t max_length,
const std::function<void(std::string&)> on_done = nullptr); std::function<void(std::string&)> on_done = nullptr);
} /* namespace ui */ } /* namespace ui */

View File

@ -76,6 +76,15 @@ namespace ui
{ {
return reinterpret_cast<T *>(push_view(std::unique_ptr<View>(new T(*this, std::forward<Args>(args)...)))); 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> template <class T, class... Args>
T *replace(Args &&...args) T *replace(Args &&...args)
{ {