Don't display an error message when cancelling a file picking operation

This commit is contained in:
Guillaume Meunier
2025-12-15 19:00:23 +01:00
parent 5329bfd28c
commit a32a4e4182
4 changed files with 37 additions and 22 deletions
+21 -16
View File
@@ -43,26 +43,31 @@ std::future<file_picker_result> file_picker::open()
open_doc.start([promise](int result_code, intent && data) mutable {
try
{
if (result_code != -1 /* Activity.RESULT_OK */)
return;
if (result_code == -1 /* Activity.RESULT_OK */)
{
auto uri = data.get_uri();
auto uri_string = (std::string)uri.call<jni::string>("toString");
auto uri = data.get_uri();
auto uri_string = (std::string)uri.call<jni::string>("toString");
jni::object<""> activity(application::native_app()->activity->clazz);
auto content_resolver = activity.call<jni::object<"android/content/ContentResolver">>("getContentResolver");
jni::object<""> activity(application::native_app()->activity->clazz);
auto content_resolver = activity.call<jni::object<"android/content/ContentResolver">>("getContentResolver");
content_resolver.call<void>("takePersistableUriPermission", uri, jni::Int{1} /* Intent.FLAG_GRANT_READ_URI_PERMISSION */);
content_resolver.call<void>("takePersistableUriPermission", uri, jni::Int{1} /* Intent.FLAG_GRANT_READ_URI_PERMISSION */);
auto pfd = content_resolver.call<jni::object<"android/os/ParcelFileDescriptor">>("openFileDescriptor", uri, jni::string{"r"});
int fd = pfd.call<int>("getFd");
auto pfd = content_resolver.call<jni::object<"android/os/ParcelFileDescriptor">>("openFileDescriptor", uri, jni::string{"r"});
int fd = pfd.call<int>("getFd");
// The file descriptor is owned by the ParcelFileDescriptor, dup it before
// passing it to mapped_file to ensure the original fd is not closed
promise->set_value(file_picker_result{
.path = uri_string,
.file = utils::mapped_file{fd},
});
// The file descriptor is owned by the ParcelFileDescriptor, dup it before
// passing it to mapped_file to ensure the original fd is not closed
promise->set_value({
.accepted = true,
.path = uri_string,
.file = utils::mapped_file{fd},
});
}
else
{
promise->set_value({.accepted = false});
}
}
catch (...)
{
+6
View File
@@ -23,8 +23,14 @@
struct file_picker_result
{
bool accepted = false;
std::filesystem::path path;
utils::mapped_file file;
operator bool() const
{
return accepted;
}
};
class file_picker
+9 -5
View File
@@ -196,13 +196,18 @@ void scenes::lobby::update_file_picker()
try
{
load_environment_status = "";
file_picker_result picked_file = lobby_file_picker_future.get();
if (not picked_file)
return;
future_environment = utils::async<std::pair<std::string, std::shared_ptr<entt::registry>>, float>(
[this](auto token, file_picker_result environment) {
[this, picked_file = std::move(picked_file)](auto token) {
auto t = std::chrono::system_clock::now();
auto local_path = application::get_cache_path() / fmt::format("local-{:%F-%T}.glb", t);
auto local_screenshot_path = application::get_cache_path() / fmt::format("local-{:%F-%T}.png", t);
utils::write_whole_file(local_path, (std::span<const std::byte>)environment.file);
utils::write_whole_file(local_path, (std::span<const std::byte>)picked_file.file);
environment_model model{
.name = _S("Locally loaded environment"),
@@ -210,7 +215,7 @@ void scenes::lobby::update_file_picker()
.description = "",
.screenshot_url = "",
.gltf_url = fmt::format("local-{:%F-%T}", t), // Used as key
.size = environment.file.size(),
.size = picked_file.file.size(),
.local_screenshot_path = local_screenshot_path,
.local_gltf_path = local_path,
};
@@ -287,8 +292,7 @@ void scenes::lobby::update_file_picker()
write_image(device, queue, queue_family_index, local_screenshot_path, output);
return std::make_pair(local_path, env);
},
lobby_file_picker_future.get());
});
}
catch (std::exception & e)
{
+1 -1
View File
@@ -217,7 +217,7 @@ future<Result, Progress> async(F && f, Args &&... args)
fut.shared_state = std::make_shared<typename future<Result, Progress>::state>();
fut.shared_state->thread = std::thread(
[f](async_token<Result, Progress> token, Args &&... args) {
[f = std::move(f)](async_token<Result, Progress> token, Args &&... args) {
try
{
token.set_result(std::invoke(f, token, std::forward<Args>(args)...));