#include "platform_apple/apple_platform_services.h" #include "app_core/document_platform_io.h" #include "platform_api/platform_policy.h" #include #include namespace pp::platform::apple { namespace { [[nodiscard]] std::vector apple_image_file_types() { static constexpr std::array kFileTypes = { "png", "PNG", "jpg", "JPG", "jpeg", }; return { kFileTypes.begin(), kFileTypes.end() }; } void invoke_picked_path_if_selected( const std::string& path, const PickedPathCallback& callback) { if (pp::app::plan_picked_path(path) == pp::app::PickedPathAction::invoke_callback) callback(path); } } AppleDocumentPlatformServices::AppleDocumentPlatformServices( PlatformFamily family, AppleDocumentPickerBridge bridge) : family_(family) , bridge_(std::move(bridge)) { } std::vector AppleDocumentPlatformServices::document_browse_roots( std::string_view work_path, std::string_view data_path) const { return platform_document_browse_roots(family_, work_path, data_path); } void AppleDocumentPlatformServices::pick_image(PickedPathCallback callback) const { if (family_ == PlatformFamily::ios) { if (bridge_.pick_image) bridge_.pick_image(std::move(callback)); return; } if (family_ == PlatformFamily::macos && bridge_.pick_file) { bridge_.pick_file( apple_image_file_types(), [callback = std::move(callback)](std::string path) { invoke_picked_path_if_selected(path, callback); }); } } void AppleDocumentPlatformServices::pick_file( std::vector file_types, PickedPathCallback callback) const { if (!bridge_.pick_file) return; if (family_ == PlatformFamily::ios) { bridge_.pick_file(std::move(file_types), std::move(callback)); return; } if (family_ == PlatformFamily::macos) { bridge_.pick_file( std::move(file_types), [callback = std::move(callback)](std::string path) { invoke_picked_path_if_selected(path, callback); }); } } void AppleDocumentPlatformServices::pick_save_file( std::vector file_types, PickedPathCallback callback) const { if (family_ == PlatformFamily::macos && bridge_.pick_save_file) { bridge_.pick_save_file( std::move(file_types), [callback = std::move(callback)](std::string path) { invoke_picked_path_if_selected(path, callback); }); } } void AppleDocumentPlatformServices::pick_directory(PickedPathCallback callback) const { if (family_ == PlatformFamily::macos && bridge_.pick_directory) { bridge_.pick_directory([callback = std::move(callback)](std::string path) { invoke_picked_path_if_selected(path, callback); }); } } bool AppleDocumentPlatformServices::supports_working_directory_picker() const { return platform_supports_working_directory_picker(family_); } std::string AppleDocumentPlatformServices::format_working_directory_path(std::string_view path) const { if (family_ == PlatformFamily::macos && bridge_.format_working_directory_path) return bridge_.format_working_directory_path(path); return std::string(path); } }