From 9df81e20d77bf98267302cfde50dd92f079f6a0d Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 13 Aug 2022 21:11:11 +0200 Subject: [PATCH] ThemeEditor: Use `FileSystemAccessServer` instead of unveiling files --- Userland/Applications/ThemeEditor/main.cpp | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Userland/Applications/ThemeEditor/main.cpp b/Userland/Applications/ThemeEditor/main.cpp index 82e13efc31..b617b843ef 100644 --- a/Userland/Applications/ThemeEditor/main.cpp +++ b/Userland/Applications/ThemeEditor/main.cpp @@ -17,9 +17,9 @@ #include #include #include +#include #include #include -#include ErrorOr serenity_main(Main::Arguments arguments) { @@ -27,7 +27,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto app = TRY(GUI::Application::try_create(arguments)); - char const* file_to_edit = nullptr; + StringView file_to_edit; Core::ArgsParser parser; parser.add_positional_argument(file_to_edit, "Theme file to edit", "file", Core::ArgsParser::Required::No); @@ -35,16 +35,8 @@ ErrorOr serenity_main(Main::Arguments arguments) Optional path = {}; - if (file_to_edit) { + if (!file_to_edit.is_empty()) path = Core::File::absolute_path(file_to_edit); - if (Core::File::exists(*path)) { - dbgln("unveil for: {}", *path); - if (unveil(path->characters(), "r") < 0) { - perror("unveil"); - return 1; - } - } - } TRY(Core::System::pledge("stdio recvfd sendfd thread rpath unix")); TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw")); @@ -55,8 +47,19 @@ ErrorOr serenity_main(Main::Arguments arguments) auto window = GUI::Window::construct(); auto main_widget = TRY(window->try_set_main_widget()); - if (path.has_value()) - main_widget->load_from_file(TRY(Core::File::open(*path, Core::OpenMode::ReadOnly))); + + if (path.has_value()) { + // Note: This is deferred to ensure that the window has already popped and thus proper window stealing can be performed. + app->event_loop().deferred_invoke( + [&window, &path, &main_widget]() { + auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, path.value()); + if (response.is_error()) + GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", path.value(), response.error())); + else + main_widget->load_from_file(response.release_value()); + }); + } + TRY(main_widget->initialize_menubar(window)); main_widget->update_title();