Plan picked path callbacks in app core

This commit is contained in:
2026-06-03 03:33:33 +02:00
parent 777723b68c
commit 712c28068d
10 changed files with 165 additions and 20 deletions

View File

@@ -0,0 +1,19 @@
#pragma once
#include <string_view>
namespace pp::app {
enum class PickedPathAction {
ignore_empty_path,
invoke_callback,
};
[[nodiscard]] constexpr PickedPathAction plan_picked_path(std::string_view path) noexcept
{
return path.empty()
? PickedPathAction::ignore_empty_path
: PickedPathAction::invoke_callback;
}
}

View File

@@ -1,5 +1,6 @@
#include "pch.h"
#include "app.h"
#include "app_core/document_platform_io.h"
#include "app_core/document_sharing.h"
#include "renderer_gl/opengl_capabilities.h"
@@ -10,6 +11,14 @@ namespace {
return static_cast<GLint>(pp::renderer::gl::rgba8_internal_format());
}
void invoke_picked_path_if_selected(
const std::string& path,
const std::function<void(std::string path)>& callback)
{
if (pp::app::plan_picked_path(path) == pp::app::PickedPathAction::invoke_callback)
callback(path);
}
}
#ifdef __ANDROID__
@@ -156,18 +165,16 @@ void App::pick_image(std::function<void(std::string path)> callback)
dispatch_async(dispatch_get_main_queue(), ^{
NSArray* fileTypes = [NSArray arrayWithObjects:@"png", @"PNG", @"jpg", @"JPG", @"jpeg", nil];
std::string path = [osx_view pick_file:fileTypes];
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
});
#elif __ANDROID__
android_pick_file(callback);
#elif _WIN32
std::string path = win32_open_file("Image Files (*.jpg, *.png)\0*.jpg;*.png");
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
#elif __LINUX__
if (auto p = tinyfd_openFileDialog("Open File", "", 0, nullptr, nullptr, false))
callback(p);
invoke_picked_path_if_selected(p, callback);
#elif __WEB__
webgl_pick_file(callback);
#endif
@@ -189,8 +196,7 @@ void App::pick_file(std::vector<std::string> types, std::function<void (std::str
for (const auto& t : types)
[fileTypes addObject:[NSString stringWithCString:t.c_str() encoding:NSUTF8StringEncoding]];
std::string path = [osx_view pick_file:fileTypes];
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
});
#elif __ANDROID__
android_pick_file(callback);
@@ -212,11 +218,10 @@ void App::pick_file(std::vector<std::string> types, std::function<void (std::str
}
filter.push_back(0);
std::string path = win32_open_file(filter.c_str());
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
#elif __LINUX__
if (auto p = tinyfd_openFileDialog("Open File", "", 0, nullptr, nullptr, false))
callback(p);
invoke_picked_path_if_selected(p, callback);
#elif __WEB__
webgl_pick_file(callback);
#endif
@@ -258,8 +263,7 @@ void App::pick_file_save(std::vector<std::string> types, std::function<void(std:
for (const auto& t : types)
[fileTypes addObject:[NSString stringWithCString:t.c_str() encoding:NSUTF8StringEncoding]];
std::string path = [osx_view pick_file_save:fileTypes];
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
});
#elif __ANDROID__
android_pick_file_save(callback);
@@ -281,8 +285,7 @@ void App::pick_file_save(std::vector<std::string> types, std::function<void(std:
}
filter.push_back(0);
std::string path = win32_save_file(filter.c_str());
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
#endif
}
#endif
@@ -295,16 +298,14 @@ void App::pick_dir(std::function<void(std::string path)> callback)
#elif __OSX__
dispatch_async(dispatch_get_main_queue(), ^{
std::string path = [osx_view pick_dir];
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
});
#elif __ANDROID__
// NOT IMPLEMENTED
#elif _WIN32
// TODO: to be implemented
std::string path = win32_open_dir();
if (!path.empty())
callback(path);
invoke_picked_path_if_selected(path, callback);
#endif
}