Extract file menu action planning

This commit is contained in:
2026-06-03 11:56:14 +02:00
parent 65b262207c
commit 6dac909869
8 changed files with 474 additions and 34 deletions

149
src/app_core/file_menu.h Normal file
View File

@@ -0,0 +1,149 @@
#pragma once
#include "app_core/document_export.h"
#include "app_core/document_session.h"
#include "foundation/result.h"
#include <string_view>
namespace pp::app {
enum class FileMenuCommand {
new_document,
import_image,
open_project,
browse_cloud,
save,
save_as,
save_version,
export_jpeg,
export_submenu,
share,
resize,
cloud_upload,
cloud_browse,
};
enum class FileMenuAction {
show_new_document_dialog,
pick_image_for_import,
pick_project_file,
show_cloud_browser_dialog,
save_document,
show_export_jpeg_dialog,
show_export_submenu,
share_document,
show_resize_dialog,
upload_to_cloud,
browse_cloud_documents,
};
struct FileMenuPlan {
FileMenuCommand command = FileMenuCommand::new_document;
FileMenuAction action = FileMenuAction::show_new_document_dialog;
DocumentSaveIntent save_intent = DocumentSaveIntent::save;
DocumentExportMenuKind export_kind = DocumentExportMenuKind::jpeg;
};
[[nodiscard]] constexpr FileMenuPlan plan_file_menu_command(FileMenuCommand command) noexcept
{
FileMenuPlan plan;
plan.command = command;
switch (command) {
case FileMenuCommand::new_document:
plan.action = FileMenuAction::show_new_document_dialog;
break;
case FileMenuCommand::import_image:
plan.action = FileMenuAction::pick_image_for_import;
break;
case FileMenuCommand::open_project:
plan.action = FileMenuAction::pick_project_file;
break;
case FileMenuCommand::browse_cloud:
plan.action = FileMenuAction::show_cloud_browser_dialog;
break;
case FileMenuCommand::save:
plan.action = FileMenuAction::save_document;
plan.save_intent = DocumentSaveIntent::save;
break;
case FileMenuCommand::save_as:
plan.action = FileMenuAction::save_document;
plan.save_intent = DocumentSaveIntent::save_as;
break;
case FileMenuCommand::save_version:
plan.action = FileMenuAction::save_document;
plan.save_intent = DocumentSaveIntent::save_version;
break;
case FileMenuCommand::export_jpeg:
plan.action = FileMenuAction::show_export_jpeg_dialog;
plan.export_kind = DocumentExportMenuKind::jpeg;
break;
case FileMenuCommand::export_submenu:
plan.action = FileMenuAction::show_export_submenu;
break;
case FileMenuCommand::share:
plan.action = FileMenuAction::share_document;
break;
case FileMenuCommand::resize:
plan.action = FileMenuAction::show_resize_dialog;
break;
case FileMenuCommand::cloud_upload:
plan.action = FileMenuAction::upload_to_cloud;
break;
case FileMenuCommand::cloud_browse:
plan.action = FileMenuAction::browse_cloud_documents;
break;
}
return plan;
}
[[nodiscard]] inline pp::foundation::Result<FileMenuCommand> parse_file_menu_command(
std::string_view command) noexcept
{
if (command == "new" || command == "new-document") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::new_document);
}
if (command == "import" || command == "import-image") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::import_image);
}
if (command == "open" || command == "open-project") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::open_project);
}
if (command == "browse") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::browse_cloud);
}
if (command == "save") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::save);
}
if (command == "save-as") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::save_as);
}
if (command == "save-version") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::save_version);
}
if (command == "export" || command == "export-jpeg") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::export_jpeg);
}
if (command == "export-submenu") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::export_submenu);
}
if (command == "share") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::share);
}
if (command == "resize") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::resize);
}
if (command == "cloud-upload") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::cloud_upload);
}
if (command == "cloud-browse") {
return pp::foundation::Result<FileMenuCommand>::success(FileMenuCommand::cloud_browse);
}
return pp::foundation::Result<FileMenuCommand>::failure(
pp::foundation::Status::invalid_argument("unknown file menu command"));
}
} // namespace pp::app

View File

@@ -13,6 +13,7 @@
#include "app_core/document_canvas.h"
#include "app_core/document_export.h"
#include "app_core/document_import.h"
#include "app_core/file_menu.h"
#include "app_core/app_status.h"
#include "app_core/history_ui.h"
#include "settings.h"
@@ -123,6 +124,72 @@ bool apply_document_export_menu_plan(App& app, pp::app::DocumentExportMenuKind k
return false;
}
void apply_file_menu_plan(App& app, pp::app::FileMenuCommand command)
{
const auto plan = pp::app::plan_file_menu_command(command);
switch (plan.action)
{
case pp::app::FileMenuAction::show_new_document_dialog:
app.dialog_newdoc();
break;
case pp::app::FileMenuAction::pick_image_for_import:
{
auto* app_ptr = &app;
app.pick_image([app_ptr](std::string path) {
Image img;
img.load_file(path);
const auto import_plan = pp::app::plan_document_image_import(img.width, img.height);
if (!import_plan)
return;
if (import_plan.value().imports_equirectangular)
{
Canvas::I->import_equirectangular(path);
}
else
{
auto m = static_cast<CanvasModeTransform*>(app_ptr->canvas->m_canvas->modes[(int)kCanvasMode::Import][0]);
m->m_action = CanvasModeTransform::ActionType::Import;
m->m_source_image = std::move(img);
Canvas::set_mode(kCanvasMode::Import);
}
});
break;
}
case pp::app::FileMenuAction::pick_project_file:
{
auto* app_ptr = &app;
app.pick_file({ "ppi" }, [app_ptr](std::string path) {
app_ptr->open_document(path);
});
break;
}
case pp::app::FileMenuAction::show_cloud_browser_dialog:
app.dialog_browse();
break;
case pp::app::FileMenuAction::save_document:
app.save_document(plan.save_intent);
break;
case pp::app::FileMenuAction::show_export_jpeg_dialog:
apply_document_export_menu_plan(app, plan.export_kind);
break;
case pp::app::FileMenuAction::show_export_submenu:
break;
case pp::app::FileMenuAction::share_document:
app.share_file(app.doc_path);
break;
case pp::app::FileMenuAction::show_resize_dialog:
app.dialog_resize();
break;
case pp::app::FileMenuAction::upload_to_cloud:
app.cloud_upload();
break;
case pp::app::FileMenuAction::browse_cloud_documents:
app.cloud_browse();
break;
}
}
} // namespace
void App::title_update()
@@ -783,70 +850,49 @@ void App::init_menu_file()
if (auto b = popup->find<NodeButtonCustom>("file-newdoc"))
b->on_click = [this, popup](Node*) {
dialog_newdoc();
apply_file_menu_plan(*this, pp::app::FileMenuCommand::new_document);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-import"))
b->on_click = [this, popup](Node*) {
pick_image([this](std::string path){
Image img;
img.load_file(path);
const auto import_plan = pp::app::plan_document_image_import(img.width, img.height);
if (!import_plan)
return;
if (import_plan.value().imports_equirectangular)
{
Canvas::I->import_equirectangular(path);
}
else
{
auto m = static_cast<CanvasModeTransform*>(canvas->m_canvas->modes[(int)kCanvasMode::Import][0]);
m->m_action = CanvasModeTransform::ActionType::Import;
m->m_source_image = std::move(img);
Canvas::set_mode(kCanvasMode::Import);
}
});
apply_file_menu_plan(*this, pp::app::FileMenuCommand::import_image);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-open"))
b->on_click = [this, popup](Node*) {
//dialog_open();
pick_file({"ppi"}, [this](std::string path){
open_document(path);
});
apply_file_menu_plan(*this, pp::app::FileMenuCommand::open_project);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-browse"))
b->on_click = [this, popup](Node*) {
dialog_browse();
apply_file_menu_plan(*this, pp::app::FileMenuCommand::browse_cloud);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-save"))
b->on_click = [this, popup](Node*) {
save_document(pp::app::DocumentSaveIntent::save);
apply_file_menu_plan(*this, pp::app::FileMenuCommand::save);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-save-as"))
b->on_click = [this, popup](Node*) {
save_document(pp::app::DocumentSaveIntent::save_as);
apply_file_menu_plan(*this, pp::app::FileMenuCommand::save_as);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-save-ver"))
b->on_click = [this, popup](Node*) {
save_document(pp::app::DocumentSaveIntent::save_version);
apply_file_menu_plan(*this, pp::app::FileMenuCommand::save_version);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-export"))
b->on_click = [this, popup](Node*) {
apply_document_export_menu_plan(*this, pp::app::DocumentExportMenuKind::jpeg);
apply_file_menu_plan(*this, pp::app::FileMenuCommand::export_jpeg);
popup->mouse_release();
popup->destroy();
};
@@ -912,25 +958,25 @@ void App::init_menu_file()
};
if (auto b = popup->find<NodeButtonCustom>("file-share"))
b->on_click = [this, popup](Node*) {
share_file(doc_path);
apply_file_menu_plan(*this, pp::app::FileMenuCommand::share);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-resize"))
b->on_click = [this, popup](Node*) {
dialog_resize();
apply_file_menu_plan(*this, pp::app::FileMenuCommand::resize);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-cloud-upload"))
b->on_click = [this, popup](Node*) {
cloud_upload();
apply_file_menu_plan(*this, pp::app::FileMenuCommand::cloud_upload);
popup->mouse_release();
popup->destroy();
};
if (auto b = popup->find<NodeButtonCustom>("file-cloud-browse"))
b->on_click = [this, popup](Node*) {
cloud_browse();
apply_file_menu_plan(*this, pp::app::FileMenuCommand::cloud_browse);
popup->mouse_release();
popup->destroy();
};