From a32a4e41827b8949ac9a47bcdf988687a15977eb Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sun, 14 Dec 2025 15:27:12 +0100 Subject: [PATCH] Don't display an error message when cancelling a file picking operation --- client/file_picker.cpp | 37 +++++++++++++++------------ client/file_picker.h | 6 +++++ client/scenes/lobby_gui_customize.cpp | 14 ++++++---- client/utils/async.h | 2 +- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/client/file_picker.cpp b/client/file_picker.cpp index 3a335d0e..645f308b 100644 --- a/client/file_picker.cpp +++ b/client/file_picker.cpp @@ -43,26 +43,31 @@ std::future 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("toString"); - auto uri = data.get_uri(); - auto uri_string = (std::string)uri.call("toString"); + jni::object<""> activity(application::native_app()->activity->clazz); + auto content_resolver = activity.call>("getContentResolver"); - jni::object<""> activity(application::native_app()->activity->clazz); - auto content_resolver = activity.call>("getContentResolver"); + content_resolver.call("takePersistableUriPermission", uri, jni::Int{1} /* Intent.FLAG_GRANT_READ_URI_PERMISSION */); - content_resolver.call("takePersistableUriPermission", uri, jni::Int{1} /* Intent.FLAG_GRANT_READ_URI_PERMISSION */); + auto pfd = content_resolver.call>("openFileDescriptor", uri, jni::string{"r"}); + int fd = pfd.call("getFd"); - auto pfd = content_resolver.call>("openFileDescriptor", uri, jni::string{"r"}); - int fd = pfd.call("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 (...) { diff --git a/client/file_picker.h b/client/file_picker.h index 42ba8ca6..ad49884f 100644 --- a/client/file_picker.h +++ b/client/file_picker.h @@ -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 diff --git a/client/scenes/lobby_gui_customize.cpp b/client/scenes/lobby_gui_customize.cpp index f10bb0b4..188b8177 100644 --- a/client/scenes/lobby_gui_customize.cpp +++ b/client/scenes/lobby_gui_customize.cpp @@ -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>, 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)environment.file); + utils::write_whole_file(local_path, (std::span)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) { diff --git a/client/utils/async.h b/client/utils/async.h index 914ae17f..9880215d 100644 --- a/client/utils/async.h +++ b/client/utils/async.h @@ -217,7 +217,7 @@ future async(F && f, Args &&... args) fut.shared_state = std::make_shared::state>(); fut.shared_state->thread = std::thread( - [f](async_token token, Args &&... args) { + [f = std::move(f)](async_token token, Args &&... args) { try { token.set_result(std::invoke(f, token, std::forward(args)...));