Route prepared file targets through platform services

This commit is contained in:
2026-06-04 16:58:05 +02:00
parent dc369c89b0
commit cabfa44729
9 changed files with 122 additions and 25 deletions

View File

@@ -181,12 +181,9 @@ public:
bool clipboard_set_text(const std::string& s);
void pick_image(std::function<void(std::string path)> callback);
void pick_file(std::vector<std::string> types, std::function<void(std::string path)> callback);
#if __IOS__ || __WEB__
void pick_file_save(const std::string& type, const std::string& default_name,
std::function<void(std::string path)> writer, std::function<void(const std::string& path, bool saved)> callback);
#else
void pick_file_save(std::vector<std::string> types, std::function<void(std::string path)> callback);
#endif
void pick_dir(std::function<void(std::string path)> callback);
void display_file(std::string path);
void share_file(std::string path);

View File

@@ -152,35 +152,34 @@ void App::pick_file(std::vector<std::string> types, std::function<void (std::str
active_platform_services().pick_file(std::move(types), std::move(callback));
}
#if __IOS__
void App::pick_file_save(const std::string& type, const std::string& default_name,
std::function<void(std::string)> writer, std::function<void(const std::string& path, bool saved)> callback)
{
redraw = true;
std::string ext = "." + type;
std::string path = tmp_path + "/" + default_name + ext;
std::thread([=]{
writer(path);
save_prepared_file(path, default_name + ext, callback);
}).detach();
const auto target = active_platform_services().prepare_writable_file(type, default_name, data_path, tmp_path);
if (target.path.empty())
{
callback({}, false);
return;
}
LOG("App::pick_file_save %s", target.path.c_str());
auto write_and_save = [=] {
writer(target.path);
save_prepared_file(target.path, target.suggested_name, callback);
};
if (target.write_on_background_thread)
std::thread(write_and_save).detach();
else
write_and_save();
}
#elif __WEB__
void App::pick_file_save(const std::string& type, const std::string& default_name,
std::function<void(std::string)> writer, std::function<void(const std::string& path, bool saved)> callback)
{
redraw = true;
auto path = data_path + "/" + default_name + "." + type;
LOG("App::pick_file_save %s", path.c_str());
writer(path);
save_prepared_file(path, default_name + "." + type, std::move(callback));
}
#else
void App::pick_file_save(std::vector<std::string> types, std::function<void(std::string)> callback)
{
redraw = true;
active_platform_services().pick_save_file(std::move(types), std::move(callback));
}
#endif
void App::pick_dir(std::function<void(std::string path)> callback)
{

View File

@@ -10,6 +10,12 @@ namespace pp::platform {
using PickedPathCallback = std::function<void(std::string path)>;
using PreparedFileCallback = std::function<void(std::string path, bool saved)>;
struct PreparedFileTarget {
std::string path;
std::string suggested_name;
bool write_on_background_thread = false;
};
struct PlatformStoragePaths {
std::string data_path;
std::string work_path;
@@ -57,6 +63,11 @@ public:
virtual void pick_file(std::vector<std::string> file_types, PickedPathCallback callback) = 0;
virtual void pick_save_file(std::vector<std::string> file_types, PickedPathCallback callback) = 0;
virtual void pick_directory(PickedPathCallback callback) = 0;
[[nodiscard]] virtual PreparedFileTarget prepare_writable_file(
std::string_view type,
std::string_view default_name,
std::string_view data_path,
std::string_view temporary_path) = 0;
virtual void save_prepared_file(
std::string_view path,
std::string_view suggested_name,

View File

@@ -431,6 +431,34 @@ public:
#endif
}
[[nodiscard]] pp::platform::PreparedFileTarget prepare_writable_file(
std::string_view type,
std::string_view default_name,
std::string_view data_path,
std::string_view temporary_path) override
{
const std::string name = std::string(default_name) + "." + std::string(type);
#ifdef __IOS__
(void)data_path;
return {
std::string(temporary_path) + "/" + name,
name,
true,
};
#elif __WEB__
(void)temporary_path;
return {
std::string(data_path) + "/" + name,
name,
false,
};
#else
(void)data_path;
(void)temporary_path;
return {};
#endif
}
void display_file(std::string_view path) override
{
const std::string value(path);

View File

@@ -448,6 +448,19 @@ public:
invoke_selected_path(path, callback);
}
[[nodiscard]] pp::platform::PreparedFileTarget prepare_writable_file(
std::string_view type,
std::string_view default_name,
std::string_view data_path,
std::string_view temporary_path) override
{
(void)type;
(void)default_name;
(void)data_path;
(void)temporary_path;
return {};
}
void save_prepared_file(
std::string_view path,
std::string_view suggested_name,