diff options
author | Caoimhe <caoimhebyrne06@gmail.com> | 2023-03-24 19:49:14 +0000 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2023-05-05 16:25:55 +0100 |
commit | 5e4d835cafa3e903f938c3415228db4e5b960dcf (patch) | |
tree | 2d145440c1a86bbca4e687a1dcbba931893c3c02 /Userland | |
parent | f663e2dbd1dbc75c8702a8e22dec40a1be804e9e (diff) | |
download | serenity-5e4d835cafa3e903f938c3415228db4e5b960dcf.zip |
FontEditor: Use `LibFileSystemAccessClient`
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/FontEditor/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Userland/Applications/FontEditor/MainWidget.cpp | 48 | ||||
-rw-r--r-- | Userland/Applications/FontEditor/MainWidget.h | 4 | ||||
-rw-r--r-- | Userland/Applications/FontEditor/main.cpp | 24 |
4 files changed, 52 insertions, 26 deletions
diff --git a/Userland/Applications/FontEditor/CMakeLists.txt b/Userland/Applications/FontEditor/CMakeLists.txt index 5a59b35fdd..3beeece431 100644 --- a/Userland/Applications/FontEditor/CMakeLists.txt +++ b/Userland/Applications/FontEditor/CMakeLists.txt @@ -25,4 +25,4 @@ set(GENERATED_SOURCES ) serenity_app(FontEditor ICON app-font-editor) -target_link_libraries(FontEditor PRIVATE LibConfig LibCore LibGUI LibDesktop LibGfx LibMain LibUnicode) +target_link_libraries(FontEditor PRIVATE LibConfig LibCore LibFileSystemAccessClient LibGUI LibDesktop LibGfx LibMain LibUnicode) diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 8c6a234225..a50d06e8fb 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -15,6 +15,7 @@ #include <Applications/FontEditor/FontPreviewWindowGML.h> #include <LibConfig/Client.h> #include <LibDesktop/Launcher.h> +#include <LibFileSystemAccessClient/Client.h> #include <LibGUI/Action.h> #include <LibGUI/Application.h> #include <LibGUI/Button.h> @@ -110,26 +111,36 @@ ErrorOr<void> MainWidget::create_actions() m_open_action = GUI::CommonActions::make_open_action([&](auto&) { if (!request_close()) return; - Optional<DeprecatedString> open_path = GUI::FilePicker::get_open_filepath(window(), {}, "/res/fonts/"sv); - if (!open_path.has_value()) + + auto result = FileSystemAccessClient::Client::the().open_file(window(), "Open font file", "/res/fonts"sv); + if (result.is_error()) return; - if (auto result = open_file(open_path.value()); result.is_error()) - show_error(result.release_error(), "Opening"sv, LexicalPath { open_path.value() }.basename()); + + auto file = result.release_value(); + if (auto result = open_file(file.filename(), file.release_stream()); result.is_error()) + show_error(result.release_error(), "Opening"sv, LexicalPath { file.filename().to_deprecated_string() }.basename()); }); m_save_action = GUI::CommonActions::make_save_action([&](auto&) { if (m_path.is_empty()) return m_save_as_action->activate(); - if (auto result = save_file(m_path); result.is_error()) + + auto response = FileSystemAccessClient::Client::the().request_file(window(), m_path, Core::File::OpenMode::Truncate | Core::File::OpenMode::Write); + if (response.is_error()) + return; + + if (auto result = save_file(m_path, response.value().release_stream()); result.is_error()) show_error(result.release_error(), "Saving"sv, LexicalPath { m_path }.basename()); }); m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { LexicalPath lexical_path(m_path.is_empty() ? "Untitled.font" : m_path); - Optional<DeprecatedString> save_path = GUI::FilePicker::get_save_filepath(window(), lexical_path.title(), lexical_path.extension()); - if (!save_path.has_value()) + + auto response = FileSystemAccessClient::Client::the().save_file(window(), lexical_path.title(), lexical_path.extension()); + if (response.is_error()) return; - if (auto result = save_file(save_path.value()); result.is_error()) + + if (auto result = save_file(lexical_path.string(), response.value().release_stream()); result.is_error()) show_error(result.release_error(), "Saving"sv, lexical_path.basename()); }); @@ -724,10 +735,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window) return {}; } -ErrorOr<void> MainWidget::save_file(DeprecatedString const& path) +ErrorOr<void> MainWidget::save_file(DeprecatedString const& path, NonnullOwnPtr<Core::File> file) { auto masked_font = TRY(m_edited_font->masked_character_set()); - TRY(masked_font->write_to_file(path)); + TRY(masked_font->write_to_file(move(file))); + m_path = path; m_undo_stack->set_current_unmodified(); window()->set_modified(false); @@ -781,10 +793,11 @@ void MainWidget::set_show_system_emoji(bool show) m_glyph_map_widget->set_show_system_emoji(show); } -ErrorOr<void> MainWidget::open_file(DeprecatedString const& path) +ErrorOr<void> MainWidget::open_file(StringView path, NonnullOwnPtr<Core::File> file) { - auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_file(path))->unmasked_character_set()); - TRY(initialize(path, move(unmasked_font))); + auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path)); + auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set()); + TRY(initialize(path.to_deprecated_string(), move(unmasked_font))); return {}; } @@ -976,8 +989,13 @@ void MainWidget::drop_event(GUI::DropEvent& event) return; auto file_path = urls.first().serialize_path(); - if (auto result = open_file(file_path); result.is_error()) - show_error(result.release_error(), "Opening"sv, LexicalPath { file_path }.basename()); + auto result = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), file_path); + if (result.is_error()) + return; + + auto file = result.release_value(); + if (auto result = open_file(file.filename(), file.release_stream()); result.is_error()) + show_error(result.release_error(), "Opening"sv, LexicalPath { file.filename().to_deprecated_string() }.basename()); } } diff --git a/Userland/Applications/FontEditor/MainWidget.h b/Userland/Applications/FontEditor/MainWidget.h index 59c322cd29..ebb18213d5 100644 --- a/Userland/Applications/FontEditor/MainWidget.h +++ b/Userland/Applications/FontEditor/MainWidget.h @@ -37,8 +37,8 @@ public: ErrorOr<void> initialize(DeprecatedString const& path, RefPtr<Gfx::BitmapFont>&&); ErrorOr<void> initialize_menubar(GUI::Window&); - ErrorOr<void> open_file(DeprecatedString const&); - ErrorOr<void> save_file(DeprecatedString const&); + ErrorOr<void> open_file(StringView, NonnullOwnPtr<Core::File>); + ErrorOr<void> save_file(DeprecatedString const&, NonnullOwnPtr<Core::File>); bool request_close(); void update_title(); diff --git a/Userland/Applications/FontEditor/main.cpp b/Userland/Applications/FontEditor/main.cpp index ee808b630b..e4326385b2 100644 --- a/Userland/Applications/FontEditor/main.cpp +++ b/Userland/Applications/FontEditor/main.cpp @@ -10,6 +10,7 @@ #include <LibCore/ArgsParser.h> #include <LibCore/System.h> #include <LibDesktop/Launcher.h> +#include <LibFileSystemAccessClient/Client.h> #include <LibGUI/Application.h> #include <LibGUI/Icon.h> #include <LibGUI/Window.h> @@ -26,7 +27,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) TRY(Desktop::Launcher::seal_allowlist()); Config::pledge_domain("FontEditor"); - TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath")); + + TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw")); + TRY(Core::System::unveil("/res", "r")); + TRY(Core::System::unveil(nullptr, nullptr)); StringView path; Core::ArgsParser args_parser; @@ -42,13 +46,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto font_editor = TRY(window->set_main_widget<FontEditor::MainWidget>()); TRY(font_editor->initialize_menubar(*window)); - if (!path.is_empty()) { - TRY(font_editor->open_file(path)); - } else { - auto mutable_font = TRY(TRY(Gfx::BitmapFont::try_load_from_file("/res/fonts/KaticaRegular10.font"))->unmasked_character_set()); - TRY(font_editor->initialize({}, move(mutable_font))); - } - window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision { if (font_editor->request_close()) return GUI::Window::CloseRequestDecision::Close; @@ -57,5 +54,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) window->show(); + auto path_to_load = path.is_empty() ? "/res/fonts/KaticaRegular10.font"sv : path; + auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path_to_load)); + + if (!path.is_empty()) { + TRY(font_editor->open_file(file.filename(), file.release_stream())); + } else { + auto mapped_file = TRY(Core::MappedFile::map_from_file(file.release_stream(), path_to_load)); + auto mutable_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set()); + TRY(font_editor->initialize({}, move(mutable_font))); + } + return app->exec(); } |