Plan save-as file writes in app core
This commit is contained in:
@@ -66,6 +66,11 @@ struct DocumentVersionTarget {
|
||||
std::string path;
|
||||
};
|
||||
|
||||
struct DocumentFileSavePlan {
|
||||
DocumentFileTarget target;
|
||||
DocumentFileWriteDecision write_decision = DocumentFileWriteDecision::save_now;
|
||||
};
|
||||
|
||||
struct NewDocumentPlan {
|
||||
DocumentFileTarget target;
|
||||
int resolution = 0;
|
||||
@@ -182,6 +187,23 @@ struct NewDocumentPlan {
|
||||
: DocumentFileWriteDecision::save_now;
|
||||
}
|
||||
|
||||
template <typename ExistsPredicate>
|
||||
[[nodiscard]] pp::foundation::Result<DocumentFileSavePlan> plan_document_file_save(
|
||||
std::string_view work_directory,
|
||||
std::string_view document_name,
|
||||
ExistsPredicate&& exists)
|
||||
{
|
||||
auto target = make_document_file_target(work_directory, document_name);
|
||||
if (!target) {
|
||||
return pp::foundation::Result<DocumentFileSavePlan>::failure(target.status());
|
||||
}
|
||||
|
||||
DocumentFileSavePlan plan;
|
||||
plan.target = std::move(target.value());
|
||||
plan.write_decision = plan_document_file_write(exists(plan.target.path));
|
||||
return pp::foundation::Result<DocumentFileSavePlan>::success(std::move(plan));
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr pp::foundation::Result<int> document_resolution_from_index(int index) noexcept
|
||||
{
|
||||
constexpr std::array<int, 6> resolutions{ 512, 1024, 1536, 2048, 4096, 8192 };
|
||||
@@ -205,15 +227,18 @@ template <typename ExistsPredicate>
|
||||
return pp::foundation::Result<NewDocumentPlan>::failure(resolution.status());
|
||||
}
|
||||
|
||||
auto target = make_document_file_target(work_directory, document_name);
|
||||
if (!target) {
|
||||
return pp::foundation::Result<NewDocumentPlan>::failure(target.status());
|
||||
auto save_plan = plan_document_file_save(
|
||||
work_directory,
|
||||
document_name,
|
||||
std::forward<ExistsPredicate>(exists));
|
||||
if (!save_plan) {
|
||||
return pp::foundation::Result<NewDocumentPlan>::failure(save_plan.status());
|
||||
}
|
||||
|
||||
NewDocumentPlan plan;
|
||||
plan.target = std::move(target.value());
|
||||
plan.target = std::move(save_plan.value().target);
|
||||
plan.resolution = resolution.value();
|
||||
plan.write_decision = plan_document_file_write(exists(plan.target.path));
|
||||
plan.write_decision = save_plan.value().write_decision;
|
||||
return pp::foundation::Result<NewDocumentPlan>::success(std::move(plan));
|
||||
}
|
||||
|
||||
|
||||
@@ -361,32 +361,36 @@ void App::dialog_save()
|
||||
dialog->btn_ok->on_click = [this, dialog](Node*)
|
||||
{
|
||||
std::string name = dialog->input->m_text;
|
||||
const auto target = pp::app::make_document_file_target(work_path, name);
|
||||
if (!target)
|
||||
const auto plan = pp::app::plan_document_file_save(
|
||||
work_path,
|
||||
name,
|
||||
[](const std::string& path) {
|
||||
return Asset::exist(path);
|
||||
});
|
||||
if (!plan)
|
||||
{
|
||||
message_box("Warning", "You need to specify a name to file.");
|
||||
return;
|
||||
}
|
||||
|
||||
auto action = [this, dialog, target = target.value()] {
|
||||
canvas->m_canvas->project_save(target.path);
|
||||
doc_name = target.name;
|
||||
doc_path = target.path;
|
||||
doc_dir = target.directory;
|
||||
auto action = [this, dialog, plan = plan.value()] {
|
||||
canvas->m_canvas->project_save(plan.target.path);
|
||||
doc_name = plan.target.name;
|
||||
doc_path = plan.target.path;
|
||||
doc_dir = plan.target.directory;
|
||||
title_update();
|
||||
dialog->destroy();
|
||||
App::I->hideKeyboard();
|
||||
};
|
||||
|
||||
const auto write_decision = pp::app::plan_document_file_write(Asset::exist(target.value().path));
|
||||
if (write_decision == pp::app::DocumentFileWriteDecision::prompt_overwrite)
|
||||
if (plan.value().write_decision == pp::app::DocumentFileWriteDecision::prompt_overwrite)
|
||||
{
|
||||
// ask confirm is file already exist
|
||||
auto msgbox = new NodeMessageBox();
|
||||
msgbox->set_manager(&layout);
|
||||
msgbox->init();
|
||||
msgbox->m_title->set_text("Warning");
|
||||
msgbox->m_message->set_text(("Are you sure you want to overwrite " + target.value().name + "?").c_str());
|
||||
msgbox->m_message->set_text(("Are you sure you want to overwrite " + plan.value().target.name + "?").c_str());
|
||||
msgbox->btn_ok->on_click = [this, msgbox, action](Node*) {
|
||||
action();
|
||||
msgbox->destroy();
|
||||
|
||||
Reference in New Issue
Block a user