Centralize legacy new document bridge
This commit is contained in:
@@ -112,6 +112,14 @@ struct NewDocumentPlan {
|
||||
DocumentFileWriteDecision write_decision = DocumentFileWriteDecision::save_now;
|
||||
};
|
||||
|
||||
class NewDocumentServices {
|
||||
public:
|
||||
virtual ~NewDocumentServices() = default;
|
||||
|
||||
virtual void create_new_document(const NewDocumentPlan& plan) = 0;
|
||||
virtual void prompt_overwrite_new_document(const NewDocumentPlan& plan) = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr ProjectOpenDecision plan_project_open(bool has_unsaved_changes) noexcept
|
||||
{
|
||||
return has_unsaved_changes
|
||||
@@ -369,6 +377,22 @@ template <typename ExistsPredicate>
|
||||
return pp::foundation::Result<NewDocumentPlan>::success(std::move(plan));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_new_document_plan(
|
||||
const NewDocumentPlan& plan,
|
||||
NewDocumentServices& services)
|
||||
{
|
||||
switch (plan.write_decision) {
|
||||
case DocumentFileWriteDecision::save_now:
|
||||
services.create_new_document(plan);
|
||||
return pp::foundation::Status::success();
|
||||
case DocumentFileWriteDecision::prompt_overwrite:
|
||||
services.prompt_overwrite_new_document(plan);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
return pp::foundation::Status::invalid_argument("unknown new document write decision");
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool has_legacy_two_character_version_suffix(std::string_view document_name) noexcept
|
||||
{
|
||||
const auto dot = document_name.rfind('.');
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "legacy_document_canvas_services.h"
|
||||
#include "legacy_document_layer_services.h"
|
||||
#include "legacy_document_session_services.h"
|
||||
#include "legacy_history_services.h"
|
||||
#include "settings.h"
|
||||
#include "node_dialog_open.h"
|
||||
#include "node_dialog_browse.h"
|
||||
@@ -183,46 +182,9 @@ void App::dialog_newdoc()
|
||||
return;
|
||||
}
|
||||
|
||||
auto action = [this, dialog, plan = plan.value()] {
|
||||
doc_name = plan.target.name;
|
||||
doc_path = plan.target.path;
|
||||
doc_filename = plan.target.name + ".ppi";
|
||||
doc_dir = plan.target.directory;
|
||||
|
||||
layers->clear();
|
||||
canvas->m_canvas->m_layers.clear();
|
||||
canvas->m_canvas->resize(plan.resolution, plan.resolution);
|
||||
canvas->reset_camera();
|
||||
pp::panopainter::clear_legacy_history();
|
||||
|
||||
layers->add_layer("Default", false, true);
|
||||
|
||||
canvas->m_canvas->m_unsaved = true;
|
||||
canvas->m_canvas->m_newdoc = false;
|
||||
title_update();
|
||||
|
||||
dialog->destroy();
|
||||
App::I->hideKeyboard();
|
||||
};
|
||||
|
||||
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("A document with this name already exists, continue?");
|
||||
msgbox->btn_ok->on_click = [this, msgbox, action](Node*) {
|
||||
action();
|
||||
msgbox->destroy();
|
||||
};
|
||||
layout[main_id]->add_child(msgbox);
|
||||
}
|
||||
else
|
||||
{
|
||||
action();
|
||||
}
|
||||
const auto status = pp::panopainter::execute_legacy_new_document_plan(*this, plan.value(), dialog);
|
||||
if (!status.ok())
|
||||
LOG("New document action failed: %s", status.message);
|
||||
|
||||
};
|
||||
dialog->btn_cancel->on_click = [this, dialog](Node*)
|
||||
|
||||
@@ -3,10 +3,74 @@
|
||||
#include "legacy_document_session_services.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "legacy_history_services.h"
|
||||
#include "node_dialog_open.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace pp::panopainter {
|
||||
namespace {
|
||||
|
||||
void create_legacy_new_document(
|
||||
App& app,
|
||||
const pp::app::NewDocumentPlan& plan,
|
||||
const std::shared_ptr<NodeDialogNewDoc>& dialog)
|
||||
{
|
||||
app.doc_name = plan.target.name;
|
||||
app.doc_path = plan.target.path;
|
||||
app.doc_filename = plan.target.name + ".ppi";
|
||||
app.doc_dir = plan.target.directory;
|
||||
|
||||
app.layers->clear();
|
||||
app.canvas->m_canvas->m_layers.clear();
|
||||
app.canvas->m_canvas->resize(plan.resolution, plan.resolution);
|
||||
app.canvas->reset_camera();
|
||||
pp::panopainter::clear_legacy_history();
|
||||
|
||||
app.layers->add_layer("Default", false, true);
|
||||
|
||||
app.canvas->m_canvas->m_unsaved = true;
|
||||
app.canvas->m_canvas->m_newdoc = false;
|
||||
app.title_update();
|
||||
|
||||
dialog->destroy();
|
||||
App::I->hideKeyboard();
|
||||
}
|
||||
|
||||
class LegacyNewDocumentServices final : public pp::app::NewDocumentServices {
|
||||
public:
|
||||
LegacyNewDocumentServices(App& app, std::shared_ptr<NodeDialogNewDoc> dialog) noexcept
|
||||
: app_(app)
|
||||
, dialog_(std::move(dialog))
|
||||
{
|
||||
}
|
||||
|
||||
void create_new_document(const pp::app::NewDocumentPlan& plan) override
|
||||
{
|
||||
create_legacy_new_document(app_, plan, dialog_);
|
||||
}
|
||||
|
||||
void prompt_overwrite_new_document(const pp::app::NewDocumentPlan& plan) override
|
||||
{
|
||||
auto msgbox = new NodeMessageBox();
|
||||
msgbox->set_manager(&app_.layout);
|
||||
msgbox->init();
|
||||
msgbox->m_title->set_text("Warning");
|
||||
msgbox->m_message->set_text("A document with this name already exists, continue?");
|
||||
auto* app = &app_;
|
||||
auto dialog = dialog_;
|
||||
msgbox->btn_ok->on_click = [app, msgbox, dialog, plan](Node*) {
|
||||
create_legacy_new_document(*app, plan, dialog);
|
||||
msgbox->destroy();
|
||||
};
|
||||
app_.layout[app_.main_id]->add_child(msgbox);
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
std::shared_ptr<NodeDialogNewDoc> dialog_;
|
||||
};
|
||||
|
||||
class LegacyCloseRequestServices final : public pp::app::CloseRequestServices {
|
||||
public:
|
||||
LegacyCloseRequestServices(App& app, bool& dialog_already_opened) noexcept
|
||||
@@ -140,4 +204,13 @@ pp::foundation::Status execute_legacy_document_workflow_decision(
|
||||
return pp::app::execute_document_workflow_decision(decision, services);
|
||||
}
|
||||
|
||||
pp::foundation::Status execute_legacy_new_document_plan(
|
||||
App& app,
|
||||
const pp::app::NewDocumentPlan& plan,
|
||||
std::shared_ptr<NodeDialogNewDoc> dialog)
|
||||
{
|
||||
LegacyNewDocumentServices services(app, std::move(dialog));
|
||||
return pp::app::execute_new_document_plan(plan, services);
|
||||
}
|
||||
|
||||
} // namespace pp::panopainter
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
class App;
|
||||
class NodeDialogNewDoc;
|
||||
|
||||
namespace pp::panopainter {
|
||||
|
||||
@@ -23,4 +25,9 @@ namespace pp::panopainter {
|
||||
pp::app::DocumentWorkflowDecision decision,
|
||||
std::function<void()> action);
|
||||
|
||||
[[nodiscard]] pp::foundation::Status execute_legacy_new_document_plan(
|
||||
App& app,
|
||||
const pp::app::NewDocumentPlan& plan,
|
||||
std::shared_ptr<NodeDialogNewDoc> dialog);
|
||||
|
||||
} // namespace pp::panopainter
|
||||
|
||||
Reference in New Issue
Block a user