Centralize legacy document open bridge
This commit is contained in:
64
src/app.cpp
64
src/app.cpp
@@ -10,7 +10,7 @@
|
||||
#include "app_core/document_recording.h"
|
||||
#include "app_core/document_route.h"
|
||||
#include "app_core/document_session.h"
|
||||
#include "legacy_history_services.h"
|
||||
#include "legacy_document_open_services.h"
|
||||
#include "legacy_recording_services.h"
|
||||
#include "platform_api/platform_services.h"
|
||||
#include "renderer_gl/opengl_capabilities.h"
|
||||
@@ -197,65 +197,9 @@ void App::open_document(std::string path)
|
||||
const bool has_unsaved_project =
|
||||
route.value().kind == pp::app::DocumentOpenKind::open_project && Canvas::I->m_unsaved;
|
||||
const auto open_plan = pp::app::plan_document_open(route.value().kind, has_unsaved_project);
|
||||
if (open_plan == pp::app::DocumentOpenPlanAction::prompt_import_abr)
|
||||
{
|
||||
auto mb = message_box("Import ABR", "Would you like to import the brushes?", true);
|
||||
mb->on_submit = [this, path] (Node* target) {
|
||||
std::thread(&NodePanelBrushPreset::import_abr, presets, path).detach();
|
||||
target->destroy();
|
||||
};
|
||||
}
|
||||
else if (open_plan == pp::app::DocumentOpenPlanAction::prompt_import_ppbr)
|
||||
{
|
||||
auto mb = message_box("Import PPBR", "Would you like to import the brushes?", true);
|
||||
mb->on_submit = [this, path] (Node* target) {
|
||||
std::thread(&NodePanelBrushPreset::import_ppbr, presets, path).detach();
|
||||
target->destroy();
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::string base = route.value().directory;
|
||||
const std::string name = route.value().name;
|
||||
auto open_action = [this, path, base, name] {
|
||||
doc_name = name;
|
||||
doc_dir = base;
|
||||
doc_path = path;
|
||||
canvas->reset_camera();
|
||||
layers->clear();
|
||||
canvas->m_canvas->project_open(path, [this](bool success){
|
||||
// on complete
|
||||
if (success)
|
||||
{
|
||||
title_update();
|
||||
for (int layer_index = 0; layer_index < canvas->m_canvas->m_layers.size(); layer_index++)
|
||||
{
|
||||
auto l = layers->add_layer(canvas->m_canvas->m_layers[layer_index]->m_name.c_str(), false);
|
||||
l->m_visibility->set_value(canvas->m_canvas->m_layers[layer_index]->m_visible);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
message_box("Open Document Error",
|
||||
"There was an error opening the document.\n"
|
||||
"It may be inaccessible or corrupted.");
|
||||
}
|
||||
});
|
||||
pp::panopainter::clear_legacy_history();
|
||||
};
|
||||
if (open_plan == pp::app::DocumentOpenPlanAction::open_project_now)
|
||||
{
|
||||
open_action();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto mb = message_box("Unsaved document", "Do you want to close the unsaved document before opening the file?", true);
|
||||
mb->on_submit = [this, open_action] (Node* target) {
|
||||
open_action();
|
||||
target->destroy();
|
||||
};
|
||||
}
|
||||
}
|
||||
const auto status = pp::panopainter::execute_legacy_document_open_plan(*this, open_plan, route.value());
|
||||
if (!status.ok())
|
||||
LOG("Document open action failed: %s", status.message);
|
||||
}
|
||||
|
||||
bool App::request_close()
|
||||
|
||||
@@ -55,6 +55,16 @@ enum class DocumentOpenPlanAction {
|
||||
prompt_import_ppbr,
|
||||
};
|
||||
|
||||
class DocumentOpenServices {
|
||||
public:
|
||||
virtual ~DocumentOpenServices() = default;
|
||||
|
||||
virtual void prompt_import_abr(const DocumentOpenRoute& route) = 0;
|
||||
virtual void prompt_import_ppbr(const DocumentOpenRoute& route) = 0;
|
||||
virtual void open_project_now(const DocumentOpenRoute& route) = 0;
|
||||
virtual void prompt_discard_unsaved_project(const DocumentOpenRoute& route) = 0;
|
||||
};
|
||||
|
||||
struct DocumentFileTarget {
|
||||
std::string name;
|
||||
std::string directory;
|
||||
@@ -102,6 +112,41 @@ struct NewDocumentPlan {
|
||||
return DocumentOpenPlanAction::open_project_now;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_open_plan(
|
||||
DocumentOpenPlanAction action,
|
||||
const DocumentOpenRoute& route,
|
||||
DocumentOpenServices& services)
|
||||
{
|
||||
switch (action) {
|
||||
case DocumentOpenPlanAction::open_project_now:
|
||||
if (route.kind != DocumentOpenKind::open_project) {
|
||||
return pp::foundation::Status::invalid_argument("open-project action requires a project route");
|
||||
}
|
||||
services.open_project_now(route);
|
||||
return pp::foundation::Status::success();
|
||||
case DocumentOpenPlanAction::prompt_discard_unsaved_project:
|
||||
if (route.kind != DocumentOpenKind::open_project) {
|
||||
return pp::foundation::Status::invalid_argument("discard prompt requires a project route");
|
||||
}
|
||||
services.prompt_discard_unsaved_project(route);
|
||||
return pp::foundation::Status::success();
|
||||
case DocumentOpenPlanAction::prompt_import_abr:
|
||||
if (route.kind != DocumentOpenKind::import_abr) {
|
||||
return pp::foundation::Status::invalid_argument("ABR import prompt requires an ABR route");
|
||||
}
|
||||
services.prompt_import_abr(route);
|
||||
return pp::foundation::Status::success();
|
||||
case DocumentOpenPlanAction::prompt_import_ppbr:
|
||||
if (route.kind != DocumentOpenKind::import_ppbr) {
|
||||
return pp::foundation::Status::invalid_argument("PPBR import prompt requires a PPBR route");
|
||||
}
|
||||
services.prompt_import_ppbr(route);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
return pp::foundation::Status::invalid_argument("unknown document open action");
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr CloseRequestDecision plan_close_request(
|
||||
bool has_unsaved_changes,
|
||||
bool close_prompt_already_open) noexcept
|
||||
|
||||
101
src/legacy_document_open_services.cpp
Normal file
101
src/legacy_document_open_services.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "pch.h"
|
||||
|
||||
#include "legacy_document_open_services.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "legacy_history_services.h"
|
||||
#include "node_panel_brush.h"
|
||||
#include "node_panel_layer.h"
|
||||
|
||||
namespace pp::panopainter {
|
||||
namespace {
|
||||
|
||||
void open_legacy_project(App& app, const pp::app::DocumentOpenRoute& route)
|
||||
{
|
||||
app.doc_name = route.name;
|
||||
app.doc_dir = route.directory;
|
||||
app.doc_path = route.path;
|
||||
app.canvas->reset_camera();
|
||||
app.layers->clear();
|
||||
app.canvas->m_canvas->project_open(route.path, [&app](bool success) {
|
||||
if (success)
|
||||
{
|
||||
app.title_update();
|
||||
for (std::size_t layer_index = 0; layer_index < app.canvas->m_canvas->m_layers.size(); ++layer_index)
|
||||
{
|
||||
auto layer = app.layers->add_layer(app.canvas->m_canvas->m_layers[layer_index]->m_name.c_str(), false);
|
||||
layer->m_visibility->set_value(app.canvas->m_canvas->m_layers[layer_index]->m_visible);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
app.message_box(
|
||||
"Open Document Error",
|
||||
"There was an error opening the document.\n"
|
||||
"It may be inaccessible or corrupted.");
|
||||
}
|
||||
});
|
||||
pp::panopainter::clear_legacy_history();
|
||||
}
|
||||
|
||||
class LegacyDocumentOpenServices final : public pp::app::DocumentOpenServices {
|
||||
public:
|
||||
explicit LegacyDocumentOpenServices(App& app) noexcept
|
||||
: app_(app)
|
||||
{
|
||||
}
|
||||
|
||||
void prompt_import_abr(const pp::app::DocumentOpenRoute& route) override
|
||||
{
|
||||
auto* app = &app_;
|
||||
auto mb = app_.message_box("Import ABR", "Would you like to import the brushes?", true);
|
||||
mb->on_submit = [app, path = route.path](Node* target) {
|
||||
std::thread(&NodePanelBrushPreset::import_abr, app->presets, path).detach();
|
||||
target->destroy();
|
||||
};
|
||||
}
|
||||
|
||||
void prompt_import_ppbr(const pp::app::DocumentOpenRoute& route) override
|
||||
{
|
||||
auto* app = &app_;
|
||||
auto mb = app_.message_box("Import PPBR", "Would you like to import the brushes?", true);
|
||||
mb->on_submit = [app, path = route.path](Node* target) {
|
||||
std::thread(&NodePanelBrushPreset::import_ppbr, app->presets, path).detach();
|
||||
target->destroy();
|
||||
};
|
||||
}
|
||||
|
||||
void open_project_now(const pp::app::DocumentOpenRoute& route) override
|
||||
{
|
||||
open_legacy_project(app_, route);
|
||||
}
|
||||
|
||||
void prompt_discard_unsaved_project(const pp::app::DocumentOpenRoute& route) override
|
||||
{
|
||||
auto* app = &app_;
|
||||
auto mb = app_.message_box(
|
||||
"Unsaved document",
|
||||
"Do you want to close the unsaved document before opening the file?",
|
||||
true);
|
||||
mb->on_submit = [app, route](Node* target) {
|
||||
open_legacy_project(*app, route);
|
||||
target->destroy();
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
pp::foundation::Status execute_legacy_document_open_plan(
|
||||
App& app,
|
||||
pp::app::DocumentOpenPlanAction action,
|
||||
const pp::app::DocumentOpenRoute& route)
|
||||
{
|
||||
LegacyDocumentOpenServices services(app);
|
||||
return pp::app::execute_document_open_plan(action, route, services);
|
||||
}
|
||||
|
||||
} // namespace pp::panopainter
|
||||
15
src/legacy_document_open_services.h
Normal file
15
src/legacy_document_open_services.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "app_core/document_session.h"
|
||||
#include "foundation/result.h"
|
||||
|
||||
class App;
|
||||
|
||||
namespace pp::panopainter {
|
||||
|
||||
[[nodiscard]] pp::foundation::Status execute_legacy_document_open_plan(
|
||||
App& app,
|
||||
pp::app::DocumentOpenPlanAction action,
|
||||
const pp::app::DocumentOpenRoute& route);
|
||||
|
||||
} // namespace pp::panopainter
|
||||
Reference in New Issue
Block a user