Add file menu service boundary
This commit is contained in:
@@ -45,6 +45,23 @@ struct FileMenuPlan {
|
||||
DocumentExportMenuKind export_kind = DocumentExportMenuKind::jpeg;
|
||||
};
|
||||
|
||||
class FileMenuServices {
|
||||
public:
|
||||
virtual ~FileMenuServices() = default;
|
||||
|
||||
virtual void show_new_document_dialog() = 0;
|
||||
virtual void pick_image_for_import() = 0;
|
||||
virtual void pick_project_file() = 0;
|
||||
virtual void show_cloud_browser_dialog() = 0;
|
||||
virtual void save_document(DocumentSaveIntent intent) = 0;
|
||||
virtual void show_export_jpeg_dialog(DocumentExportMenuKind kind) = 0;
|
||||
virtual void show_export_submenu() = 0;
|
||||
virtual void share_document() = 0;
|
||||
virtual void show_resize_dialog() = 0;
|
||||
virtual void upload_to_cloud() = 0;
|
||||
virtual void browse_cloud_documents() = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr FileMenuPlan plan_file_menu_command(FileMenuCommand command) noexcept
|
||||
{
|
||||
FileMenuPlan plan;
|
||||
@@ -146,4 +163,47 @@ struct FileMenuPlan {
|
||||
pp::foundation::Status::invalid_argument("unknown file menu command"));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_file_menu_plan(
|
||||
const FileMenuPlan& plan,
|
||||
FileMenuServices& services)
|
||||
{
|
||||
switch (plan.action) {
|
||||
case FileMenuAction::show_new_document_dialog:
|
||||
services.show_new_document_dialog();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::pick_image_for_import:
|
||||
services.pick_image_for_import();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::pick_project_file:
|
||||
services.pick_project_file();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::show_cloud_browser_dialog:
|
||||
services.show_cloud_browser_dialog();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::save_document:
|
||||
services.save_document(plan.save_intent);
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::show_export_jpeg_dialog:
|
||||
services.show_export_jpeg_dialog(plan.export_kind);
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::show_export_submenu:
|
||||
services.show_export_submenu();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::share_document:
|
||||
services.share_document();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::show_resize_dialog:
|
||||
services.show_resize_dialog();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::upload_to_cloud:
|
||||
services.upload_to_cloud();
|
||||
return pp::foundation::Status::success();
|
||||
case FileMenuAction::browse_cloud_documents:
|
||||
services.browse_cloud_documents();
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
return pp::foundation::Status::invalid_argument("unknown file menu action");
|
||||
}
|
||||
|
||||
} // namespace pp::app
|
||||
|
||||
@@ -127,18 +127,22 @@ 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)
|
||||
class LegacyFileMenuServices final : public pp::app::FileMenuServices {
|
||||
public:
|
||||
explicit LegacyFileMenuServices(App& app) noexcept
|
||||
: app_(app)
|
||||
{
|
||||
case pp::app::FileMenuAction::show_new_document_dialog:
|
||||
app.dialog_newdoc();
|
||||
break;
|
||||
case pp::app::FileMenuAction::pick_image_for_import:
|
||||
}
|
||||
|
||||
void show_new_document_dialog() override
|
||||
{
|
||||
auto* app_ptr = &app;
|
||||
app.pick_image([app_ptr](std::string path) {
|
||||
app_.dialog_newdoc();
|
||||
}
|
||||
|
||||
void pick_image_for_import() override
|
||||
{
|
||||
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);
|
||||
@@ -157,40 +161,66 @@ void apply_file_menu_plan(App& app, pp::app::FileMenuCommand command)
|
||||
Canvas::set_mode(kCanvasMode::Import);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case pp::app::FileMenuAction::pick_project_file:
|
||||
|
||||
void pick_project_file() override
|
||||
{
|
||||
auto* app_ptr = &app;
|
||||
app.pick_file({ "ppi" }, [app_ptr](std::string path) {
|
||||
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;
|
||||
|
||||
void show_cloud_browser_dialog() override
|
||||
{
|
||||
app_.dialog_browse();
|
||||
}
|
||||
|
||||
void save_document(pp::app::DocumentSaveIntent intent) override
|
||||
{
|
||||
app_.save_document(intent);
|
||||
}
|
||||
|
||||
void show_export_jpeg_dialog(pp::app::DocumentExportMenuKind kind) override
|
||||
{
|
||||
apply_document_export_menu_plan(app_, kind);
|
||||
}
|
||||
|
||||
void show_export_submenu() override
|
||||
{
|
||||
}
|
||||
|
||||
void share_document() override
|
||||
{
|
||||
app_.share_file(app_.doc_path);
|
||||
}
|
||||
|
||||
void show_resize_dialog() override
|
||||
{
|
||||
app_.dialog_resize();
|
||||
}
|
||||
|
||||
void upload_to_cloud() override
|
||||
{
|
||||
app_.cloud_upload();
|
||||
}
|
||||
|
||||
void browse_cloud_documents() override
|
||||
{
|
||||
app_.cloud_browse();
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
};
|
||||
|
||||
void apply_file_menu_plan(App& app, pp::app::FileMenuCommand command)
|
||||
{
|
||||
const auto plan = pp::app::plan_file_menu_command(command);
|
||||
LegacyFileMenuServices services(app);
|
||||
const auto status = pp::app::execute_file_menu_plan(plan, services);
|
||||
if (!status.ok())
|
||||
LOG("File menu action failed: %s", status.message);
|
||||
}
|
||||
|
||||
pp::app::DocumentLayerMenuPlan make_layer_menu_plan(
|
||||
|
||||
Reference in New Issue
Block a user