Extract canvas clear command planning

This commit is contained in:
2026-06-03 11:35:20 +02:00
parent c56d301b29
commit 888e94a77c
8 changed files with 251 additions and 2 deletions

View File

@@ -0,0 +1,58 @@
#pragma once
#include "foundation/result.h"
#include <cmath>
namespace pp::app {
struct DocumentCanvasClearPlan {
float r = 0.0F;
float g = 0.0F;
float b = 0.0F;
float a = 0.0F;
bool clears_canvas = false;
bool records_undo = false;
bool marks_unsaved = false;
bool no_op = true;
};
[[nodiscard]] inline pp::foundation::Status validate_clear_color_channel(float value) noexcept
{
if (!std::isfinite(value)) {
return pp::foundation::Status::invalid_argument("clear color channel must be finite");
}
if (value < 0.0F || value > 1.0F) {
return pp::foundation::Status::out_of_range("clear color channel must be within 0..1");
}
return pp::foundation::Status::success();
}
[[nodiscard]] inline pp::foundation::Result<DocumentCanvasClearPlan> plan_document_canvas_clear(
bool has_canvas,
float r = 0.0F,
float g = 0.0F,
float b = 0.0F,
float a = 0.0F) noexcept
{
const float channels[] { r, g, b, a };
for (const float channel : channels) {
const auto status = validate_clear_color_channel(channel);
if (!status.ok()) {
return pp::foundation::Result<DocumentCanvasClearPlan>::failure(status);
}
}
DocumentCanvasClearPlan plan;
plan.r = r;
plan.g = g;
plan.b = b;
plan.a = a;
plan.clears_canvas = has_canvas;
plan.records_undo = has_canvas;
plan.marks_unsaved = has_canvas;
plan.no_op = !has_canvas;
return pp::foundation::Result<DocumentCanvasClearPlan>::success(plan);
}
} // namespace pp::app

View File

@@ -10,6 +10,7 @@
#include "app_core/brush_ui.h"
#include "app_core/canvas_tool_ui.h"
#include "app_core/document_layer.h"
#include "app_core/document_canvas.h"
#include "app_core/app_status.h"
#include "app_core/history_ui.h"
#include "settings.h"
@@ -149,8 +150,13 @@ void App::init_toolbar_main()
{
button->on_click = [this](Node*) {
//exit(0);
if (canvas)
canvas->m_canvas->clear({ 0, 0, 0, 0 });
const auto plan = pp::app::plan_document_canvas_clear(static_cast<bool>(canvas));
if (plan && plan.value().clears_canvas)
canvas->m_canvas->clear({
plan.value().r,
plan.value().g,
plan.value().b,
plan.value().a });
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-popup"))