Extract document resize planning
This commit is contained in:
57
src/app_core/document_resize.h
Normal file
57
src/app_core/document_resize.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "app_core/app_status.h"
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace pp::app {
|
||||
|
||||
struct DocumentResizeDialogState {
|
||||
int current_resolution = 0;
|
||||
std::string current_resolution_text;
|
||||
int current_resolution_index = 0;
|
||||
};
|
||||
|
||||
struct DocumentResizePlan {
|
||||
int resolution = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
bool clears_history = false;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline DocumentResizeDialogState make_document_resize_dialog_state(
|
||||
int current_resolution)
|
||||
{
|
||||
const auto label = document_resolution_label(current_resolution);
|
||||
const auto index = document_resolution_to_index(current_resolution);
|
||||
std::string text = "Current: ";
|
||||
text.append(label ? std::string_view(label.value()) : std::string_view("unknown"));
|
||||
|
||||
return {
|
||||
current_resolution,
|
||||
text,
|
||||
index ? static_cast<int>(index.value()) : static_cast<int>(document_resolution_values.size()),
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentResizePlan> plan_document_resize(
|
||||
int selected_resolution_index)
|
||||
{
|
||||
const auto resolution = display_resolution_from_index(selected_resolution_index);
|
||||
if (!resolution) {
|
||||
return pp::foundation::Result<DocumentResizePlan>::failure(resolution.status());
|
||||
}
|
||||
|
||||
const auto value = resolution.value();
|
||||
return pp::foundation::Result<DocumentResizePlan>::success(
|
||||
DocumentResizePlan {
|
||||
value,
|
||||
value,
|
||||
value,
|
||||
true,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "pch.h"
|
||||
#include "app.h"
|
||||
#include "action.h"
|
||||
#include "app_core/document_resize.h"
|
||||
#include "app_core/document_export.h"
|
||||
#include "app_core/document_session.h"
|
||||
#include "settings.h"
|
||||
@@ -558,9 +559,15 @@ void App::dialog_resize()
|
||||
|
||||
dialog->btn_ok->on_click = [this,dialog](Node*)
|
||||
{
|
||||
int res = dialog->get_resolution();
|
||||
const auto plan = pp::app::plan_document_resize(
|
||||
dialog->combo ? dialog->combo->m_current_index : 0);
|
||||
if (!plan)
|
||||
{
|
||||
dialog->destroy();
|
||||
return;
|
||||
}
|
||||
if (canvas)
|
||||
canvas->m_canvas->resize(res, res);
|
||||
canvas->m_canvas->resize(plan.value().width, plan.value().height);
|
||||
App::I->title_update();
|
||||
ActionManager::clear();
|
||||
dialog->destroy();
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "pch.h"
|
||||
#include "log.h"
|
||||
#include "node_dialog_resize.h"
|
||||
#include "app_core/document_resize.h"
|
||||
#include "canvas.h"
|
||||
#include "node_image_texture.h"
|
||||
#include "app.h"
|
||||
#include <array>
|
||||
|
||||
Node* NodeDialogResize::clone_instantiate() const
|
||||
@@ -30,9 +30,12 @@ void NodeDialogResize::init_controls()
|
||||
combo = find<NodeComboBox>("resolution");
|
||||
text = find<NodeText>("current-res");
|
||||
resolution = Canvas::I->m_width;
|
||||
static char txt[128];
|
||||
sprintf(txt, "Current: %s", App::I->res_to_string(resolution).c_str());
|
||||
text->set_text(txt);
|
||||
const auto state = pp::app::make_document_resize_dialog_state(resolution);
|
||||
text->set_text(state.current_resolution_text.c_str());
|
||||
if (combo && state.current_resolution_index >= 0
|
||||
&& state.current_resolution_index < static_cast<int>(combo->m_items.size())) {
|
||||
combo->m_current_index = state.current_resolution_index;
|
||||
}
|
||||
btn_cancel->on_click = [this](Node*) {
|
||||
destroy();
|
||||
};
|
||||
@@ -47,5 +50,6 @@ void NodeDialogResize::loaded()
|
||||
|
||||
int NodeDialogResize::get_resolution()
|
||||
{
|
||||
return combo ? App::I->res_from_index(combo->m_current_index) : 512;
|
||||
const auto plan = pp::app::plan_document_resize(combo ? combo->m_current_index : 0);
|
||||
return plan ? plan.value().resolution : pp::app::document_resolution_values.front();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user