Don't display an error message when cancelling a file picking operation
This commit is contained in:
+21
-16
@@ -43,26 +43,31 @@ std::future<file_picker_result> file_picker::open()
|
|||||||
open_doc.start([promise](int result_code, intent && data) mutable {
|
open_doc.start([promise](int result_code, intent && data) mutable {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (result_code != -1 /* Activity.RESULT_OK */)
|
if (result_code == -1 /* Activity.RESULT_OK */)
|
||||||
return;
|
{
|
||||||
|
auto uri = data.get_uri();
|
||||||
|
auto uri_string = (std::string)uri.call<jni::string>("toString");
|
||||||
|
|
||||||
auto uri = data.get_uri();
|
jni::object<""> activity(application::native_app()->activity->clazz);
|
||||||
auto uri_string = (std::string)uri.call<jni::string>("toString");
|
auto content_resolver = activity.call<jni::object<"android/content/ContentResolver">>("getContentResolver");
|
||||||
|
|
||||||
jni::object<""> activity(application::native_app()->activity->clazz);
|
content_resolver.call<void>("takePersistableUriPermission", uri, jni::Int{1} /* Intent.FLAG_GRANT_READ_URI_PERMISSION */);
|
||||||
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 */);
|
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"});
|
// The file descriptor is owned by the ParcelFileDescriptor, dup it before
|
||||||
int fd = pfd.call<int>("getFd");
|
// passing it to mapped_file to ensure the original fd is not closed
|
||||||
|
promise->set_value({
|
||||||
// The file descriptor is owned by the ParcelFileDescriptor, dup it before
|
.accepted = true,
|
||||||
// passing it to mapped_file to ensure the original fd is not closed
|
.path = uri_string,
|
||||||
promise->set_value(file_picker_result{
|
.file = utils::mapped_file{fd},
|
||||||
.path = uri_string,
|
});
|
||||||
.file = utils::mapped_file{fd},
|
}
|
||||||
});
|
else
|
||||||
|
{
|
||||||
|
promise->set_value({.accepted = false});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,8 +23,14 @@
|
|||||||
|
|
||||||
struct file_picker_result
|
struct file_picker_result
|
||||||
{
|
{
|
||||||
|
bool accepted = false;
|
||||||
std::filesystem::path path;
|
std::filesystem::path path;
|
||||||
utils::mapped_file file;
|
utils::mapped_file file;
|
||||||
|
|
||||||
|
operator bool() const
|
||||||
|
{
|
||||||
|
return accepted;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class file_picker
|
class file_picker
|
||||||
|
|||||||
@@ -196,13 +196,18 @@ void scenes::lobby::update_file_picker()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
load_environment_status = "";
|
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>(
|
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 t = std::chrono::system_clock::now();
|
||||||
auto local_path = application::get_cache_path() / fmt::format("local-{:%F-%T}.glb", t);
|
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);
|
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{
|
environment_model model{
|
||||||
.name = _S("Locally loaded environment"),
|
.name = _S("Locally loaded environment"),
|
||||||
@@ -210,7 +215,7 @@ void scenes::lobby::update_file_picker()
|
|||||||
.description = "",
|
.description = "",
|
||||||
.screenshot_url = "",
|
.screenshot_url = "",
|
||||||
.gltf_url = fmt::format("local-{:%F-%T}", t), // Used as key
|
.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_screenshot_path = local_screenshot_path,
|
||||||
.local_gltf_path = local_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);
|
write_image(device, queue, queue_family_index, local_screenshot_path, output);
|
||||||
|
|
||||||
return std::make_pair(local_path, env);
|
return std::make_pair(local_path, env);
|
||||||
},
|
});
|
||||||
lobby_file_picker_future.get());
|
|
||||||
}
|
}
|
||||||
catch (std::exception & e)
|
catch (std::exception & e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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 = std::make_shared<typename future<Result, Progress>::state>();
|
||||||
|
|
||||||
fut.shared_state->thread = std::thread(
|
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
|
try
|
||||||
{
|
{
|
||||||
token.set_result(std::invoke(f, token, std::forward<Args>(args)...));
|
token.set_result(std::invoke(f, token, std::forward<Args>(args)...));
|
||||||
|
|||||||
Reference in New Issue
Block a user