LibFSAC+Userland: Pass options for FSAC::open_file() using a struct

It was rather inconvenient having to specify all arguments if you wanted
to modify only the last one.
This commit is contained in:
Karol Kosek 2023-05-28 13:38:15 +02:00 committed by Sam Atkins
parent 5c07aeb78e
commit 27011cf55d
10 changed files with 34 additions and 16 deletions

View file

@ -92,7 +92,7 @@ ErrorOr<size_t> CertificateStoreModel::add(Vector<Certificate> const& certificat
ErrorOr<void> CertificateStoreWidget::import_pem()
{
auto fsac_result = FileSystemAccessClient::Client::the().open_file(window(), "Import");
auto fsac_result = FileSystemAccessClient::Client::the().open_file(window(), { .window_title = "Import"sv });
if (fsac_result.is_error())
return {};

View file

@ -101,7 +101,12 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame()
auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button");
button.on_click = [this](auto) {
auto response = FileSystemAccessClient::Client::the().open_file(window(), "Select Wallpaper"sv, "/res/wallpapers"sv, Core::File::OpenMode::Read, { { GUI::FileTypeFilter::image_files(), GUI::FileTypeFilter::all_files() } });
FileSystemAccessClient::OpenFileOptions options {
.window_title = "Select Wallpaper"sv,
.path = "/res/wallpapers"sv,
.allowed_file_types = { { GUI::FileTypeFilter::image_files(), GUI::FileTypeFilter::all_files() } }
};
auto response = FileSystemAccessClient::Client::the().open_file(window(), options);
if (response.is_error())
return;
m_wallpaper_view->selection().clear();

View file

@ -133,9 +133,12 @@ ErrorOr<void> MainWidget::create_actions()
m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
if (!request_close())
return;
auto response = FileSystemAccessClient::Client::the().open_file(window(), "Open", "/res/fonts"sv, Core::File::OpenMode::Read,
{ { GUI::FileTypeFilter { "Bitmap Font Files", { { "font" } } },
GUI::FileTypeFilter::all_files() } });
FileSystemAccessClient::OpenFileOptions options {
.window_title = "Open"sv,
.path = "/res/fonts"sv,
.allowed_file_types = { { GUI::FileTypeFilter { "Bitmap Font Files", { { "font" } } }, GUI::FileTypeFilter::all_files() } },
};
auto response = FileSystemAccessClient::Client::the().open_file(window(), options);
if (response.is_error())
return;
auto file = response.release_value();

View file

@ -121,7 +121,7 @@ HexEditorWidget::HexEditorWidget()
if (!request_close())
return;
auto response = FileSystemAccessClient::Client::the().open_file(window(), {}, Core::StandardPaths::home_directory(), Core::File::OpenMode::ReadWrite);
auto response = FileSystemAccessClient::Client::the().open_file(window(), { .requested_access = Core::File::OpenMode::ReadWrite });
if (response.is_error())
return;

View file

@ -121,7 +121,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// Actions
auto open_action = GUI::CommonActions::make_open_action(
[&](auto&) {
auto result = FileSystemAccessClient::Client::the().open_file(window, "Open Image");
auto result = FileSystemAccessClient::Client::the().open_file(window, { .window_title = "Open Image"sv });
if (result.is_error())
return;

View file

@ -455,7 +455,7 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
})));
TRY(m_edit_menu->try_add_action(GUI::Action::create(
"&Load Color Palette...", g_icon_bag.load_color_palette, [&](auto&) {
auto response = FileSystemAccessClient::Client::the().open_file(&window, "Load Color Palette");
auto response = FileSystemAccessClient::Client::the().open_file(&window, { .window_title = "Load Color Palette"sv });
if (response.is_error())
return;

View file

@ -242,7 +242,7 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
return;
auto response = FileSystemAccessClient::Client::the().open_file(&window, "Select Theme", "/res/themes"sv);
auto response = FileSystemAccessClient::Client::the().open_file(&window, { .window_title = "Select Theme"sv, .path = "/res/themes"sv });
if (response.is_error())
return;
auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());

View file

@ -161,11 +161,14 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
auto open_action = GUI::CommonActions::make_open_action([&](auto&) {
if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
return;
auto response = FileSystemAccessClient::Client::the().open_file(&window, {}, "/usr/src/serenity/Userland/Applications"sv, Core::File::OpenMode::Read,
Vector {
FileSystemAccessClient::OpenFileOptions options {
.path = "/usr/src/serenity/Userland/Applications"sv,
.allowed_file_types = Vector {
GUI::FileTypeFilter { "GML Files", { { "gml" } } },
GUI::FileTypeFilter::all_files(),
});
}
};
auto response = FileSystemAccessClient::Client::the().open_file(&window, options);
if (response.is_error())
return;

View file

@ -63,10 +63,10 @@ Result Client::request_file(GUI::Window* parent_window, DeprecatedString const&
return handle_promise(id);
}
Result Client::open_file(GUI::Window* parent_window, DeprecatedString const& window_title, StringView path, Core::File::OpenMode requested_access, Optional<Vector<GUI::FileTypeFilter>> const& allowed_file_types)
Result Client::open_file(GUI::Window* parent_window, OpenFileOptions const& options)
{
auto const id = get_new_id();
m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, requested_access });
m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, options.requested_access });
auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
auto child_window_server_client_id = expose_window_server_client_id();
@ -78,7 +78,7 @@ Result Client::open_file(GUI::Window* parent_window, DeprecatedString const& win
GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
});
async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access, allowed_file_types);
async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, options.window_title, options.path, options.requested_access, options.allowed_file_types);
return handle_promise(id);
}

View file

@ -48,6 +48,13 @@ private:
using Result = ErrorOr<File>;
struct OpenFileOptions {
StringView window_title = {};
DeprecatedString path = Core::StandardPaths::home_directory();
Core::File::OpenMode requested_access = Core::File::OpenMode::Read;
Optional<Vector<GUI::FileTypeFilter>> allowed_file_types = {};
};
class Client final
: public IPC::ConnectionToServer<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>
, public FileSystemAccessClientEndpoint {
@ -56,7 +63,7 @@ class Client final
public:
Result request_file_read_only_approved(GUI::Window* parent_window, DeprecatedString const& path);
Result request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::File::OpenMode requested_access);
Result open_file(GUI::Window* parent_window, DeprecatedString const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), Core::File::OpenMode requested_access = Core::File::OpenMode::Read, Optional<Vector<GUI::FileTypeFilter>> const& = {});
Result open_file(GUI::Window* parent_window, OpenFileOptions const& = {});
Result save_file(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::File::OpenMode requested_access = Core::File::OpenMode::Write | Core::File::OpenMode::Truncate);
void set_silence_errors(u32 flags) { m_silenced_errors = flags; }