Plan document file saves in app core

This commit is contained in:
2026-06-02 22:42:51 +02:00
parent c8d769c02c
commit 5841878df9
9 changed files with 199 additions and 25 deletions

View File

@@ -1,5 +1,11 @@
#pragma once
#include "foundation/result.h"
#include <string>
#include <string_view>
#include <utility>
namespace pp::app {
enum class ProjectOpenDecision {
@@ -33,6 +39,17 @@ enum class DocumentWorkflowDecision {
prompt_save_before_continue,
};
enum class DocumentFileWriteDecision {
save_now,
prompt_overwrite,
};
struct DocumentFileTarget {
std::string name;
std::string directory;
std::string path;
};
[[nodiscard]] constexpr ProjectOpenDecision plan_project_open(bool has_unsaved_changes) noexcept
{
return has_unsaved_changes
@@ -97,4 +114,32 @@ enum class DocumentWorkflowDecision {
: DocumentWorkflowDecision::continue_now;
}
[[nodiscard]] inline pp::foundation::Result<DocumentFileTarget> make_document_file_target(
std::string_view work_directory,
std::string_view document_name)
{
if (document_name.empty()) {
return pp::foundation::Result<DocumentFileTarget>::failure(
pp::foundation::Status::invalid_argument("document name must not be empty"));
}
DocumentFileTarget target;
target.name = std::string(document_name);
target.directory = std::string(work_directory);
target.path.reserve(target.directory.size() + target.name.size() + 5);
target.path += target.directory;
target.path += "/";
target.path += target.name;
target.path += ".ppi";
return pp::foundation::Result<DocumentFileTarget>::success(std::move(target));
}
[[nodiscard]] constexpr DocumentFileWriteDecision plan_document_file_write(
bool target_exists) noexcept
{
return target_exists
? DocumentFileWriteDecision::prompt_overwrite
: DocumentFileWriteDecision::save_now;
}
}

View File

@@ -157,21 +157,20 @@ void App::dialog_newdoc()
dialog->btn_ok->on_click = [this, dialog](Node*)
{
std::string name = dialog->input->m_text;
std::string path = work_path + "/" + name + ".ppi";
if (name.empty())
const auto target = pp::app::make_document_file_target(work_path, name);
if (!target)
{
message_box("Warning", "You need to specify a name to file.");
return;
}
auto action = [this, dialog, name, path] {
auto action = [this, dialog, target = target.value()] {
std::array<int, 6> resolutions{ 512, 1024, 1536, 2048, 4096, 8192 };
int res = resolutions[dialog->m_resolution->m_current_index];
doc_name = name;
doc_path = path;
doc_filename = name + ".ppi";
doc_dir = work_path;
doc_name = target.name;
doc_path = target.path;
doc_filename = target.name + ".ppi";
doc_dir = target.directory;
layers->clear();
canvas->m_canvas->m_layers.clear();
@@ -189,7 +188,8 @@ void App::dialog_newdoc()
App::I->hideKeyboard();
};
if (Asset::exist(path))
const auto write_decision = pp::app::plan_document_file_write(Asset::exist(target.value().path));
if (write_decision == pp::app::DocumentFileWriteDecision::prompt_overwrite)
{
// ask confirm is file already exist
auto msgbox = new NodeMessageBox();
@@ -363,32 +363,32 @@ void App::dialog_save()
dialog->btn_ok->on_click = [this, dialog](Node*)
{
std::string name = dialog->input->m_text;
std::string path = work_path + "/" + name + ".ppi";
if (name.empty())
const auto target = pp::app::make_document_file_target(work_path, name);
if (!target)
{
message_box("Warning", "You need to specify a name to file.");
return;
}
auto action = [this, dialog, name, path] {
canvas->m_canvas->project_save(path);
doc_name = name;
doc_path = path;
doc_dir = work_path;
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;
title_update();
dialog->destroy();
App::I->hideKeyboard();
};
if (Asset::exist(path))
const auto write_decision = pp::app::plan_document_file_write(Asset::exist(target.value().path));
if (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 " + name + "?").c_str());
msgbox->m_message->set_text(("Are you sure you want to overwrite " + target.value().name + "?").c_str());
msgbox->btn_ok->on_click = [this, msgbox, action](Node*) {
action();
msgbox->destroy();