Route layer merge through app core
This commit is contained in:
@@ -78,6 +78,12 @@ struct DocumentLayerMenuPlan {
|
||||
int to_index = 0;
|
||||
};
|
||||
|
||||
struct DocumentLayerMergePlan {
|
||||
int from_index = 0;
|
||||
int to_index = 0;
|
||||
bool create_history = true;
|
||||
};
|
||||
|
||||
class DocumentLayerMenuServices {
|
||||
public:
|
||||
virtual ~DocumentLayerMenuServices() = default;
|
||||
@@ -115,6 +121,13 @@ public:
|
||||
virtual void update_title() = 0;
|
||||
};
|
||||
|
||||
class DocumentLayerMergeServices {
|
||||
public:
|
||||
virtual ~DocumentLayerMergeServices() = default;
|
||||
|
||||
virtual void merge_layers(int from_index, int to_index, bool create_history) = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status validate_layer_index(
|
||||
int layer_count,
|
||||
int index) noexcept
|
||||
@@ -447,6 +460,45 @@ public:
|
||||
return pp::foundation::Result<DocumentLayerMenuPlan>::success(std::move(plan));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentLayerMergePlan> plan_document_layer_merge(
|
||||
int layer_count,
|
||||
int from_index,
|
||||
int to_index,
|
||||
int animation_duration,
|
||||
bool create_history = true)
|
||||
{
|
||||
auto index_status = validate_layer_index(layer_count, from_index);
|
||||
if (!index_status.ok()) {
|
||||
return pp::foundation::Result<DocumentLayerMergePlan>::failure(index_status);
|
||||
}
|
||||
|
||||
index_status = validate_layer_index(layer_count, to_index);
|
||||
if (!index_status.ok()) {
|
||||
return pp::foundation::Result<DocumentLayerMergePlan>::failure(index_status);
|
||||
}
|
||||
|
||||
if (animation_duration < 0) {
|
||||
return pp::foundation::Result<DocumentLayerMergePlan>::failure(
|
||||
pp::foundation::Status::out_of_range("animation duration must not be negative"));
|
||||
}
|
||||
|
||||
if (animation_duration > 1) {
|
||||
return pp::foundation::Result<DocumentLayerMergePlan>::failure(
|
||||
pp::foundation::Status::invalid_argument("animated layer merge is not supported"));
|
||||
}
|
||||
|
||||
if (from_index <= to_index) {
|
||||
return pp::foundation::Result<DocumentLayerMergePlan>::failure(
|
||||
pp::foundation::Status::invalid_argument("layer merge source must be above the destination"));
|
||||
}
|
||||
|
||||
DocumentLayerMergePlan plan;
|
||||
plan.from_index = from_index;
|
||||
plan.to_index = to_index;
|
||||
plan.create_history = create_history;
|
||||
return pp::foundation::Result<DocumentLayerMergePlan>::success(plan);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_layer_rename_plan(
|
||||
const DocumentLayerRenamePlan& plan,
|
||||
DocumentLayerRenameServices& services)
|
||||
@@ -545,6 +597,19 @@ inline void execute_document_layer_operation_side_effects(
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_layer_merge_plan(
|
||||
const DocumentLayerMergePlan& plan,
|
||||
DocumentLayerMergeServices& services)
|
||||
{
|
||||
if (plan.from_index <= plan.to_index) {
|
||||
return pp::foundation::Status::invalid_argument(
|
||||
"layer merge source must be above the destination");
|
||||
}
|
||||
|
||||
services.merge_layers(plan.from_index, plan.to_index, plan.create_history);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_layer_menu_plan(
|
||||
const DocumentLayerMenuPlan& plan,
|
||||
DocumentLayerMenuServices& services)
|
||||
|
||||
@@ -167,6 +167,8 @@ void execute_document_canvas_clear_plan(App& app, const pp::app::DocumentCanvasC
|
||||
LOG("Canvas clear failed: %s", status.message);
|
||||
}
|
||||
|
||||
void execute_document_layer_merge_plan(App& app, const pp::app::DocumentLayerMergePlan& plan);
|
||||
|
||||
bool apply_document_export_menu_plan(App& app, pp::app::DocumentExportMenuKind kind)
|
||||
{
|
||||
class LegacyDocumentExportMenuServices final : public pp::app::DocumentExportMenuServices {
|
||||
@@ -605,8 +607,21 @@ public:
|
||||
|
||||
void merge_with_lower_layer(int from_index, int to_index) override
|
||||
{
|
||||
if (app_.layers)
|
||||
app_.layers->merge(from_index, to_index, true);
|
||||
const int layer_count = app_.canvas && app_.canvas->m_canvas
|
||||
? static_cast<int>(app_.canvas->m_canvas->m_layers.size())
|
||||
: 0;
|
||||
const int animation_duration = Canvas::I
|
||||
? Canvas::I->anim_duration()
|
||||
: 0;
|
||||
const auto plan = pp::app::plan_document_layer_merge(
|
||||
layer_count,
|
||||
from_index,
|
||||
to_index,
|
||||
animation_duration);
|
||||
if (!plan)
|
||||
return;
|
||||
|
||||
execute_document_layer_merge_plan(app_, plan.value());
|
||||
}
|
||||
|
||||
void show_merge_animated_not_supported() override
|
||||
@@ -618,6 +633,23 @@ private:
|
||||
App& app_;
|
||||
};
|
||||
|
||||
class LegacyDocumentLayerMergeServices final : public pp::app::DocumentLayerMergeServices {
|
||||
public:
|
||||
explicit LegacyDocumentLayerMergeServices(App& app) noexcept
|
||||
: app_(app)
|
||||
{
|
||||
}
|
||||
|
||||
void merge_layers(int from_index, int to_index, bool create_history) override
|
||||
{
|
||||
if (app_.layers)
|
||||
app_.layers->merge(from_index, to_index, create_history);
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
};
|
||||
|
||||
class LegacyDocumentLayerOperationServices final : public pp::app::DocumentLayerOperationServices {
|
||||
public:
|
||||
LegacyDocumentLayerOperationServices(
|
||||
@@ -786,6 +818,14 @@ void execute_document_layer_menu_plan(App& app, const pp::app::DocumentLayerMenu
|
||||
LOG("Layer menu action failed: %s", status.message);
|
||||
}
|
||||
|
||||
void execute_document_layer_merge_plan(App& app, const pp::app::DocumentLayerMergePlan& plan)
|
||||
{
|
||||
LegacyDocumentLayerMergeServices services(app);
|
||||
const auto status = pp::app::execute_document_layer_merge_plan(plan, services);
|
||||
if (!status.ok())
|
||||
LOG("Layer merge failed: %s", status.message);
|
||||
}
|
||||
|
||||
void execute_document_layer_operation_plan(
|
||||
App& app,
|
||||
const pp::app::DocumentLayerOperationPlan& plan,
|
||||
|
||||
Reference in New Issue
Block a user